目录
4. (单选题)Which is the advantage of encapsulation?
7. (单选题) is invoked to create an object.
11. (多选题)Which of the following statements are true about an immutable object?
12. (多选题)Which of the following statements are correct?
13. (多选题)To obtain the distance between the points (40, 50) and (5.5, 4.4), use .
15. (多选题)Which of the following statements are true?
一. 单选题(共10题,66分)
1. (单选题)The default value for data field of a boolean type, numeric type, object type is , respectively.
- A. false, 1, null
- B. false, 0, null
- C. true, 1, Null
- D. true, 1, null
- E. true, 0, null
我的答案: B
答案解析:b See the second and third paragraph.
2. (单选题)Analyze the following code.
public class Test { int x;
public Test(String t) { System.out.println("Test");
}
public static void main(String[] args) { Test test = null; System.out.println(test.x);
}
}
- A. The program has a compile error because test is not initialized.
- B. The program has a compile error because you cannot create an object from the class that defines the object.
- C. The program has a compile error because x has not been initialized.
- D. The program has a runtime NullPointerException because test is null while executing test.x.
- E. The program has a compile error because Test does not have a default constructor.
我的答案: D
答案解析:e test.x will raise NullPointerException, because test is null.
3. (单选题)Analyze the following code:
public class Test {
public static void main(String args[]) { NClass nc = new NClass();
nc.t = nc.t++;
}
}
class NClass { int t;
private NClass() {
}
}
- A. The program has a compile error because the NClass class has a private constructor.
- B. The program compiles and runs fine.
- C. The program does not compile because the parameter list of the main method is wrong.
- D. The program compiles, but has a runtime error because t has no initial value.
我的答案: A
答案解析:a You cannot use the private constructor to create an object.
4. (单选题)Which is the advantage of encapsulation?
- A. It enables changes to the implementation without changing a class's contract and causes no consequential changes to other code.
- B. It enables changes to a class's contract without changing the implementation and causes no consequential changes to other code.
- C. Making the class final causes no consequential changes to other code.
- D. Only public methods are needed.
我的答案: A
答案解析:c See the bullet items at the beginning of this section.
5. (单选题)Analyze the following code.
class TempClass { int i;
public void TempClass(int j) { int i = j;
}
}
public class C {
public static void main(String[] args) { TempClass temp = new TempClass(2);
}
}
- A. The program compiles and runs fine.
- B. The program has a compile error because TempClass does not have a constructor with an int argument.
- C. The program compiles fine, but it does not run because class C is not public.
- D. The program has a compile error because TempClass does not have a default constructor.
我的答案: B
答案解析:b The program would be fine if the void keyword is removed from public void TempClass(int j).