单项选择题

1. public class A { 
2. 
3. private int counter = 0; 
4. 
5. public static int getInstanceCount() { 
6. return counter; 
7. } 
8. 
9. public A() { 
10. counter++; 
11. } 
12. 
13. } 
Given this code from Class B: 
25.A a1 =new A(); 
26. A a2 =new A(); 
27. A a3 =new A(); 
28. System.out.printIn(A.getInstanceCount() );
What is the result?() 

A. Compilation of class A fails.
B. Line 28 prints the value 3 to System.out.
C. Line 28 prints the value 1 to System.out.
D. A runtime error occurs when line 25 executes.
E. Compilation fails because of an error on line 28.


您可能感兴趣的试卷

你可能感兴趣的试题

3.多项选择题

10. class Foo { 
11. static void alpha() { /* more code here */ } 
12. void beta() { /* more code here */ } 
13. } 
Which two are true?()

A. Foo.beta() is a valid invocation of beta().
B. Foo.alpha() is a valid invocation of alpha().
C. Method beta() can directly call method alpha().
D. Method alpha() can directly call method beta().

4.单项选择题

11. public static void main(String[] args) { 
12. Object obj =new int[] { 1,2,3 }; 
13. int[] someArray = (int[])obj; 
14. for (int i: someArray) System.out.print(i +“ “) 
15. } 
What is the result? ()

A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.

6.单项选择题

public class Ball { 
public enum Color { RED, GREEN, BLUE }; 
public void foo() { 
// insert code here 
{ System.out.println(c); } 
} 
} 
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?() 

A. for( Color c : Color.values())
B. for( Color c = RED; c <= BLUE; c++)
C. for( Color c; c.hasNext() ; c.next())
D. for( Color c = Color[0]; c <= Color[2]; c++)
E. for( Color c = Color.RED; c <= Color.BLUE; c++)

7.多项选择题Which two code fragments correctly create and initialize a static array of int elements?() 

A. static final int[] a = { 100,200 };
B. static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2] { 100,200 };
D. static final int[] a; static void init() { a = new int[3]; a[0]=100; a[1]=200; }

8.多项选择题

35. String #name = “Jane Doe”; 
36. int$age=24; 
37. Double_height = 123.5; 
38. double~temp = 37.5; 
Which two are true?()

A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.

9.单项选择题

55. int []x= {1, 2,3,4, 5}; 
56. int y[] =x; 
57. System.out.println(y[2]); 
Which is true?() 

A. Line 57 will print the value 2.
B. Line 57 will print the value 3.
C. Compilation will fail because of an error in line 55.
D. Compilation will fail because of an error in line 56.