PREV NEXT
Java Declaration and Access Control Questions:
- Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
- arr.length
- arr.length – 1
- arr.size
- arr.size – 1
- arr.length()
- arr.length() – 1
- Which of these statements are legal. Select the three correct answers.
- int arr[][] = new int[5][5];
- int []arr[] = new int[5][5];
- int[][] arr = new int[5][5];
- int[] arr = new int[5][];
- int[] arr = new int[][5];
- Write the expression to access the number of elements in a one dimensional array arr. The expression should not be assigned to any variable.
- Which of these array declarations and initializations are legal? Select the two correct answers.
- int arr[4] = new int[4];
- int[4] arr = new int[4];
- int arr[] = new int[4];
- int arr[] = new int[4][4];
- int[] arr = new int[4];
- What will the result of compiling and executing the following program. Select the one correct answer.
1234567891011class Test {public static void main(String args[]) {int arr[] = new int[2];System.out.println(arr[0]);}}- The program does not compile because arr[0] is being read before being initialized.
- The program generates a runtime exception because arr[0] is being read before being initialized.
- The program compiles and prints 0 when executed.
- The program compiles and prints 1 when executed.
- The program compiles and runs but the results are not predictable because of un-initialized memory being read.
- Which of the following are legal declaration and definition of a method. Select all correct answers.
- void method() {};
- void method(void) {};
- method() {};
- method(void) {};
- void method {};
- Which of the following are valid constructors within a class Test. Select the two correct answers.
- test() { }
- Test() { }
- void Test() { }
- private final Test() { }
- abstract Test() { }
- Test(Test t) { }
- Test(void) { }
- What is the result of compiling and running the following class. Select the one correct answer.
12345678910111213141516171819202122232425class 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);}}
- The program compiles and runs printing 5.
- The program compiles and runs printing 6.
- The program gives runtime exception because it does not find the method Test.methodA(int)
- The program give compilation error because methodA is defined twice in class Test.
Answers to questions on Declarations
- a
- a, b, c
- arr.length
- c, e. The size of the array should not be specified when declaring the array.
- c
- a
- 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.
- d
Post Your Thoughts