更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
题目: 请找出以下代码中的10处错误,写出行号并说明错误原因。

1. public class ErrorTest2 {

2. public static void main(String[] args) {

3. // 实例化错误4. Animal animal = new Animal();

5. Dog dog = new Animal();

6.

7. // 静态使用错误

8. Counter c1 = new Counter();

9. Counter c2 = new Counter();

10. System.out.println(c1.count + " " + c2.count);

11.

12. // 接口实现错误

13. MyInterface obj = new MyInterface() {

14. // 缺少method2的实现

15. };

16.

17. // 访问权限错误

18. Student student = new Student();

19. student.name = "John";

20. System.out.println(student.getPassword());

21. }

22. }

23.

24. // 抽象类定义

25. abstract class Animal {

26. public abstract void sound();

27.

28. public void eat() {

29. System.out.println("动物吃东西");

30. }

31. }

32.

33. class Dog extends Animal {

34. public void sound() {

35. System.out.println("汪汪");

36. }

37.

38. public void bark() {

39. System.out.println("大声叫");

40. }

41. }

42.

43. // 静态变量使用

44. class Counter {

45. public static int count = 0; // 应为static

46.

47. public Counter() {

48. count++;49. }

50. }

51.

52. // 接口定义

53. interface MyInterface {

54. void method1();

55. void method2();

56. }

57.

58. // 封装性错误

59. class Student {

60. private String name;

61. private String password;

62.

63. public String getName() {

64. return name;

65. }

66.

67. private String getPassword() {

68. return password;

69. }

70. }

71.

72. // final使用错误

73. class Base {

74. public final void display() {

75. System.out.println("Base display");

76. }

77. }

78.

79. class Derived extends Base {

80. public void display() {

81. System.out.println("Derived display");

82. }

83. }

84.

85. // 构造方法错误

86. class TestClass {

87. private int value;

88.

89. public TestClass(int value) {

90. this.value = value;

91. }

92.

93. public void show() {94. System.out.println(value);

95. }

96.

97. // 缺少无参构造方法,但main中尝试使用 // 错误9

98. }

99.

100. // 数组协变错误

101. class Main {

102. public static void main(String[] args) {

103. Object[] objArray = new String[5];

104. objArray[0] = new Integer(10);

105. }

106. }

请找出以下代码中的10处错误,写出行号并说明错误原因。

1. import java.io.*;

2. import java.util.*;

3.

4. public class ErrorTest3 {

5. public static void main(String[] args) {

6. // 文件读取错误

7. readFile("nonexistent.txt");

8.

9. // 空指针异常

10. String text = null;

11. System.out.println(text.length());

12.

13. // 数字格式异常14. String numStr = "abc";

15. int number = Integer.parseInt(numStr);

16.

17. // 资源管理错误

18. FileInputStream fis = new FileInputStream("test.txt");

19. int data = fis.read();

20. // 忘记关闭流

21.

22. // 集合并发修改

23. List<String> items = new ArrayList<>();

24. items.add("A");

25. items.add("B");

26. items.add("C");

27. for (String item : items) {

28. if (item.equals("B")) {

29. items.remove(item);

31. }

32.

33. // 类型转换异常

34. Object obj = "hello";

35. Integer num = (Integer) obj;

36.

37. // 数组存储异常

38. String[] strArray = new String[5];

39. Object[] objArray = strArray;

40. objArray[0] = Integer.valueOf(1);

41.

42. // 方法递归错误

43. infiniteRecursion();

44.

45. // 日期时间使用错误

46. Date date = new Date(2024, 1, 1);

47. System.out.println(date);

48.

49. // 数学计算错误

50. int result = 100 / 0;

51. System.out.println(result);

52. }

53.

54. public static void readFile(String filename) {

55. // 方法声明没有throws,但内部有受检异常 // 错误补充

56. File file = new File(filename);

57. FileInputStream fis = new FileInputStream(file);

58. fis.close();59. }

60.

61. public static void infiniteRecursion() {

62. infiniteRecursion();

63. }

64.

65. // 克隆方法错误

66. class Person implements Cloneable {

67. private String name;

68.

69. public Object clone() {

70. return this; // 错误:应该深拷贝

71. }

72. }

73. }

1