Sail E0 Webinar

MCQs

Total Questions : 105 | Page 1 of 11 pages
Question 1. In Java arrays are
  1.    objects
  2.    object references
  3.    primitive data type
  4.    None of the above
 Discuss Question
Answer: Option A. -> objects
Question 2. Which one of the following is a valid statement?
  1.    char[] c = new char();
  2.    char[] c = new char[5];
  3.    char[] c = new char(4);
  4.    char[] c = new char[];
 Discuss Question
Answer: Option B. -> char[] c = new char[5];
Question 3. What is the result of compiling and running the following code?
public class Test{
public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}
  1.    0
  2.    Compilation error, arrays cannot be initialized to zero size.
  3.    Compilation error, it is a.length() not a.length
  4.    None of the above
 Discuss Question
Answer: Option A. -> 0
Question 4. What will be the output?
public class Test{
public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}
  1.    The program has a compile error because the size of the array wasn't specified when declaring the array.
  2.    The program has a runtime error because the array elements are not initialized.
  3.    The program runs fine and displays x[0] is 0.
  4.    The program has a runtime error because the array element x[0] is not defined.
 Discuss Question
Answer: Option C. -> The program runs fine and displays x[0] is 0.
Question 5. What is the output of the following code?
public class Test{
public static void main(String args[]){
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for(int i = 1; i < myList.length; i++){
if(myList[i] > max){
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
}
}
  1.    0
  2.    1
  3.    2
  4.    3
  5.    4
 Discuss Question
Answer: Option B. -> 1
Question 6. What is output of the following code:
public class Test{
public static void main(String[] args){
int[] x = {120, 200, 016 };
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}
  1.    120 200 16
  2.    120 200 14
  3.    120 200 016
  4.    016 is a compile error. It should be written as 16.
 Discuss Question
Answer: Option B. -> 120 200 14
Question 7. Determine output:
public class Test{
public static void main(String[] args){
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for(int i = 0; i < x.length; i++)
System.out.print(y[i] + " ");
}
}
  1.    1 2 3 4
  2.    0 0 0 0
  3.    1 2
  4.    0 0
  5.    None of these
 Discuss Question
Answer: Option C. -> 1 2
Question 8. What will be the output?
public class Test{
public static void main(String[] args){
int[] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}
  1.    The program has a compile error because new int[2
  2.    The program has a runtime error because a[1
  3.    a[1] is 0
  4.    a[1] is 1
 Discuss Question
Answer: Option C. -> a[1] is 0
Question 9. Analyze the following code and choose the correct answer.
int[] arr = new int[5];
arr = new int[6];
  1.    The code has compile errors because the variable arr cannot be changed once it is assigned.
  2.    The code has runtime errors because the variable arr cannot be changed once it is assigned.
  3.    The code can compile and run fine. The second line assigns a new array to arr.
  4.    The code has compile errors because we cannot assign a different size array to arr.
 Discuss Question
Answer: Option C. -> The code can compile and run fine. The second line assigns a new array to arr.
Question 10. When you pass an array to a method, the method receives ________ .
  1.    A copy of the array.
  2.    A copy of the first element.
  3.    The reference of the array.
  4.    The length of the array.
 Discuss Question
Answer: Option C. -> The reference of the array.

Latest Videos

Latest Test Papers