What will happen when you try to compile the following code? : Questions « Operators « SCJP






public class MainClass {
  public static void main(String[] argv) {
    printArray(new int[]{1,2,3});
  }

  public static void printArray(Object x) {
    if (x instanceof int[]) {
      int[] n = (int[]) x;
      for (int i = 0; i < n.length; i++) {
        System.out.println("integers = " + n[i]);
      }
    }
    if (x instanceof String[]) {
      System.out.println("Array of Strings");
    }
  }

}








2.12.Questions
2.12.1.May an interface type appear on the left-hand side of an instanceof operator?
2.12.2.Answer: interface type and instanceof operator
2.12.3.May a variable of primitive type appear on the left-hand side of an instanceof operator?
2.12.4.Answer: primitive type and instanceof operator
2.12.5.May a variable of object type appear on the right-hand side of an instanceof operator?
2.12.6.Answer: Reference type and instanceof operator
2.12.7.May a class appear on the right-hand side of an instanceof operator?
2.12.8.Answer: Class and instanceof operator
2.12.9.May an interface appear on the right-hand side of an instanceof operator?
2.12.10.Answer: interface and right side of instanceof operator
2.12.11.May A variable of primitive type appear on the right-hand side of an instanceof operator?
2.12.12.Answer: primitive type and the right-hand side of an instanceof operator
2.12.13.May a name of a primitive type appear on the right-hand side of an instanceof operator?
2.12.14.Answer: name of a primitive type and right-hand side of an instanceof operator
2.12.15.What is -50 >> 1?
2.12.16.Answer: result of -50 >> 1
2.12.17.What will happen when you try to compile the following code?
2.12.18.Answer: instanceof arraytype
2.12.19.How to check if the object reference x has the null value?
2.12.20.Answer: instanceof null