1. Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
    1. arr.length
    2. arr.length – 1
    3. arr.size
    4. arr.size – 1
    5. arr.length()
    6. arr.length() – 1
  2. Which of these statements are legal. Select the three correct answers.
    1. int arr[][] = new int[5][5];
    2. int []arr[] = new int[5][5];
    3. int[][] arr = new int[5][5];
    4. int[] arr = new int[5][];
    5. int[] arr = new int[][5];
  3. Write the expression to access the number of elements in a one dimensional array arr. The expression should not be assigned to any variable.
  4. Which of these array declarations and initializations are legal? Select the two correct answers.
    1. int arr[4] = new int[4];
    2. int[4] arr = new int[4];
    3. int arr[] = new int[4];
    4. int arr[] = new int[4][4];
    5. int[] arr = new int[4];
  5. What will the result of compiling and executing the following program. Select the one correct answer.
    class Test {
    
        public static void main(String args[]) {
    
    	int arr[] = new int[2];
    
    	System.out.println(arr[0]);
    
        }
    
    }
    
    
    1. The program does not compile because arr[0] is being read before being initialized.
    2. The program generates a runtime exception because arr[0] is being read before being initialized.
    3. The program compiles and prints 0 when executed.
    4. The program compiles and prints 1 when executed.
    5. The program compiles and runs but the results are not predictable because of un-initialized memory being read.
  6. Which of the following are legal declaration and definition of a method. Select all correct answers.
    1. void method() {};
    2. void method(void) {};
    3. method() {};
    4. method(void) {};
    5. void method {};
  7. Which of the following are valid constructors within a class Test. Select the two correct answers.
    1. test() { }
    2. Test() { }
    3. void Test() { }
    4. private final Test() { }
    5. abstract Test() { }
    6. Test(Test t) { }
    7. Test(void) { }
  8. What is the result of compiling and running the following class. Select the one correct answer.
    class Test {
    
       public void methodA(int i) {
    
          System.out.println(i);
    
       }
    
       public int methodA(int i) {
    
          System.out.println(i+1);
    
          return i+1;
    
       }
    
       public static void main(String args[]) {
    
          Test X = new Test();
    
          X.methodA(5);
    
       }
    
    }
    

    Select the one correct answer.

    1. The program compiles and runs printing 5.
    2. The program compiles and runs printing 6.
    3. The program gives runtime exception because it does not find the method Test.methodA(int)
    4. The program give compilation error because methodA is defined twice in class Test.

Answers to questions on Declarations

  1. a
  2. a, b, c
  3. arr.length
  4. c, e. The size of the array should not be specified when declaring the array.
  5. c
  6. a
  7. b, f. A constructor must have the same name as the class, hence a is not a constructor. It must not return any value, hence c is not correct. A constructor cannot be declared abstract or final.
  8. d