Java OCA OCP Practice Question 3241

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 = 4;//  ww  w. j  a va 2s.c  om
    float f = 4.3;
    double d = 1.8;
    int c = 0;
    if (i == f) c++;
    if (((int) (f + d)) == ((int) f + (int) d)) c += 2;
    System.out.println(c);
  }
}

Select the one correct answer.

  • (a) The code will fail to compile.
  • (b) The value 0 will be written to the standard output.
  • (c) The value 1 will be written to the standard output.
  • (d) The value 2 will be written to the standard output.
  • (e) The value 3 will be written to the standard output.


(a)

Note

The code will fail to compile because the literal 4.3 has the type double.

Assignment of a double value to a float variable without an explicit cast is not allowed.

The code would compile and write 0 to standard output when run, if the literal 4.3 was replaced with 4.3F.




PreviousNext

Related