Java OCA OCP Practice Question 999

Question

What will the following code snippet print?

Object t = new Integer (107); 
int k =  (Integer) t.intValue ()/9; 
System.out.println (k); 

Select 1 option

  • A. 11
  • B. 12
  • C. It will not compile.
  • D. It will throw an exception at runtime.


Correct Option is  : C

Note

Compiler will complain that the method intValue() is not available in Object.

This is because the . operator has more precedence than the cast operator.

So you have to write it like this:

int k =  ((Integer) t).intValue ()/9; 

Since both the operands of / are ints, it is an integer division.

The resulting value is truncated (and not rounded).

The above statement will print 11 and not 12.




PreviousNext

Related