Java OCA OCP Practice Question 2972

Question

Given:

3. public class Main {  
4.   public static void main(String[] args) {  
5.     String s = "";  
6.     if(Integer.parseInt("011") == Integer.parseInt("9")) s += 1;  
7.     if(021 == Integer.valueOf("17")) s += 2;  
8.     if(1024 == new Integer(1024)) s += 3;  
9.     System.out.println(s);  
10. } } 

What is the result?

  • A. 2
  • B. 3
  • C. 13
  • D. 23
  • E. 123
  • F. Compilation fails.
  • G. An exception is thrown at runtime.


D is correct.

Note

On line 6, the parseInt() method assumes a radix of 10 if a radix is not specified.

Line 7 is comparing an octal int to an int.

Line 8 is comparing an int to an Integer, and autoboxing converts the Integer to an int to do the comparison.




PreviousNext

Related