Java OCA OCP Practice Question 1664

Question

What would be the result of compiling and running the following program?.

public class Main {
  int[] ia = new int[1];
  boolean b;/*ww w  .  j  a v a  2 s .  c  o  m*/
  int i;
  Object o;

  public static void main(String[] args) {
    Main instance = new Main();
    instance.print();
  }

  public void print() {
    System.out.println(ia[0] + " " + b + " " + i + " " + o);
  }
}

Select the one correct answer.

  • (a) The program will fail to compile because of uninitialized variables.
  • (b) The program will throw a java.lang.NullPointerException when run.
  • (c) The program will print: 0 false NaN null.
  • (d) The program will print: 0 false 0 null.
  • (e) The program will print: null 0 0 null.
  • (f) The program will print: null false 0 null.


(d)

Note

The program will print "0 false 0 null" when run.

All the instance variables, including the array element, will be initialized to their default values.

When concatenated with a string, the values are converted to their string representation.

Notice that the null pointer is converted to the string "null", rather than throwing a NullPointerException.




PreviousNext

Related