Java OCA OCP Practice Question 1169

Question

What will be the result of attempting to compile and run the following code?

public class Main {
   public static void main(String args[]) {
      int i = 5;//from  w  w w.  java  2s .c om
      float f = 5.5f;
      double d = 3.8;
      char c = 'a';
      if (i == f)
         c++;
      if (((int) (f + d)) == ((int) f + (int) d))
         c += 2;
      System.out.println(c);
   }
}

Select 1 option

  • A. The code will fail to compile.
  • B. It will print d.
  • C. It will print c.
  • D. It will print b
  • E. It will print a.


Correct Option is  : E

Note

In the case of i == f, value of i will be promoted to a float i.e. 5.0, and so it returns false.

(int)f+(int)d =  (int)5.5 + (int) 3.8 => 5 + 3 = 8 

(int)(f + d) => (int) (5.5 + 3.8) => (int)(9.3) => 9, 

so this also return false.

c is not incremented at all. Hence c remains 'a'.




PreviousNext

Related