- 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
Answer- A
- 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];
Answer-A,B,C
- Write the expression to access the number of elements in a one dimensional array arr. The expression should not be assigned to any variable.
Answer- arr.length
- 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];
Answer- C, E. The size of the
array should not be specified when declaring the array.
class 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.
Answer-C
06. Which
of the following are legal declaration and definition of a method. Select all
correct answers.
A. void
method() {};
B. void method(void) {};
C. method() {};
D. method(void) {};
F. void method {};
B. void method(void) {};
C. method() {};
D. method(void) {};
F. void method {};
Answer- A
07. Which
of the following are valid constructors within a class Test. Select the two
correct answers.
A.
test() { }
B.
Test() { }
C.
void Test() { }
D.
private final Test() { }
E.
abstract Test() { }
F.
Test(Test t) { }
G.
Test(void) { }
Answer- 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.
08. 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.
A.
The program compiles and runs
printing 5.
B.
The program compiles and runs
printing 6.
C.
The program gives runtime exception
because it does not find the method Test.methodA(int)
D.
The program give compilation error because
methodA is defined twice in class Test
Answer-. D
0 comments:
Post a Comment