目录
1. (单选题)Which of the following are correct? (Choose all that apply.)
3. (单选题)What is the output of executing the following class?
4. (单选题)Which statements about the following class are correct? (Choose all that apply.)
6. (多选题)Which statements about the following class are true? (Choose all that apply.)
9. (多选题)What lines are printed by the following program? (Choose all that apply.)
14. (多选题)Which are true about this code? (Choose all that apply.)
16. (多选题)Which statements about the following class are correct? (Choose all that apply.)
17. (多选题)Which of the following statements about var are true? (Choose all that apply.)
19. (多选题)Which of the following are valid Java identifiers? (Choose all that apply.)
21. (多选题)Which statements about the following program are correct? (Choose all that apply.)
23. (多选题)Which of the following statements about the code snippet are true? (Choose all that apply.)
一. 单选题
1. (单选题)Which of the following are correct? (Choose all that apply.)
- A. An instance variable of type float defaults to 0.
- B. An instance variable of type char defaults to null.
- C. A local variable of type double defaults to 0.0.
- D. A local variable of type int defaults to null.
- E. A class variable of type String defaults to null.
- F. A class variable of type String defaults to the empty string "".
- G. None of the above.
我的答案: E
答案解析:E. Options C and D are incorrect because local variables don’t have default values. Option A
is incorrect because float should have a decimal point. Option B is incorrect because primitives do not default to null. Option E is correct and option F incorrect because reference
types in class variables default to null.
2. (单选题)Given the following two class files, what is the maximum number of imports that can be
removed and have the code still compile?
// Water.java
package aquarium;
public class Water { }
// Tank.java
package aquarium;
import java.lang.*;
import java.lang.System;
import aquarium.Water;
import aquarium.*;
public class Tank {
public void print(Water water) {
System.out.println(water); } }
- A. 0
- B. 1
- C. 2
- D. 3
- E. 4
- F. Does not compile
我的答案: E
答案解析:E. The first two imports can be removed because java.lang is automatically imported. The
following two imports can be removed because Tank and Water are in the same package,
making the correct option E. If Tank and Water were in different packages, exactly one of
these two imports could be removed. In that case, the answer would be option D.
3. (单选题)What is the output of executing the following class?
- A. 7-0-2-1-
- B. 7-0-1-
- C. 0-7-2-1-
- D. 7-0-2-4-
- E. 0-7-1-
- F. The class does not compile because of line 3.
- G. The class does not compile because of line 4.
- H. None of the above.
我的答案: D
答案解析:D. We start with the main() method, which prints 7- on line 10. Next, a new Salmon
instance is created on line 11. This calls the two instance initializers on lines 3 and 4 to be
executed in order. The default value of an instance variable of type int is 0, so 0- is printed
next and count is assigned a value of 1. Next, the constructor is called. This assigns a value
of 4 to count and prints 2-. Finally, line 12 prints 4-, since that is the value of count.
Putting it all together, we have 7-0-2-4-, making option D the correct answer.
4. (单选题)Which statements about the following class are correct? (Choose all that apply.)
- A. It prints Q1=blue.
- B. It prints Q2=1200.
- C. It prints P1=null.
- D. It prints P2=1400.
- E. Line 4 does not compile.
- F. Line 12 does not compile.
- G. Line 13 does not compile.
- H. None of the above.
我的答案: C
答案解析:C. The key thing to notice is that line 4 does not define a constructor, but instead a method
named PoliceBox(), since it has a return type of void. This method is never executed
during the program run, and color and age are assigned the default values null and 0L,
respectively. Lines 11 and 12 change the values for an object associated with p, but then, on
line 13, the p variable is changed to point to the object associated with q, which still has the
default values. For this reason, the program prints Q1=null, Q2=0, P1=null, and P2=0,
making option C the only correct answer.
5. (单选题)Assuming the following class compiles, how many variables defined in the class or method
are in scope on the line marked on line 14?
- A. 2
- B. 3
- C. 4
- D. 5
- E. 6
- F. 7
- G. None of the above
我的答案: F
答案解析:F. To solve this problem, you need to trace the braces {} and see when variables go in and out of scope. The variables on lines 2 and 7 are only in scope for a single line block. The variable on line 12 is only in scope for the for loop. None of these are in scope on line 14. By
contrast, the three instance variables on lines 3 and 4 are available in all instance methods.
Additionally, the variables on lines 6, 9, and 10 are available since the method and while
loop are still in scope. This is a total of 7 variables, which is option F.
二. 多选题
6. (多选题)Which statements about the following class are true? (Choose all that apply.)
- A. Line 3 generates a compiler error.
- B. Line 6 generates a compiler error.
- C. Line 7 generates a compiler error.
- D. Line 10 generates a compiler error.
- E. The program prints 3 on line 10.
- F. The program prints 4 on line 10.
- G. The program prints 50.0 on line 11.
- H. The program prints 49.0 on line 11.
我的答案: AD
答案解析:A, D. The first compiler error is on line 3. The variable temp is declared as a float, but the
assigned value is 50.0, which is a double without the F/f postfix. Since a double doesn’t
fit inside a float, line 3 does not compile. Next, depth is declared inside the for loop and
only has scope inside this loop. Therefore, reading the value on line 10 triggers a compiler
error. For these reasons, options A and D are the correct answers.
7. (多选题)Which of the following are true? (Choose all that apply.)
public class Bunny {
public static void main(String[] x) { Bunny bun = new Bunny();
} }
- A. Bunny is a class.
- B. bun is a class.
- C. main is a class.
- D. Bunny is a reference to an object.
- E. bun is a reference to an object.
- F. main is a reference to an object.
- G. The main() method doesn’t run because the parameter name is incorrect.
我的答案: AE
答案解析:A, E. Bunny is a class, which can be seen from the declaration: public class Bunny.
The variable bun is a reference to an object. The method main() is the standard entry
p