Sail E0 Webinar

MCQs

Total Questions : 14 | Page 1 of 2 pages
Question 1. 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 runtime error because the array element x[0] is not defined.
  2.    The program has a compile error because the size of the array wasn't specified when declaring the array.
  3.    The program runs fine and displays x[0] is 0.
  4.    The program has a runtime error because the array elements are not initialized.
 Discuss Question
Answer: Option C. -> The program runs fine and displays x[0] is 0.
Question 2. 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.    4
  2.    3
  3.    0
  4.    1
  5.    2
 Discuss Question
Answer: Option D. -> 1
Question 3. In Java arrays are
  1.    None of these
  2.    objects
  3.    object references
  4.    primitive data type
 Discuss Question
Answer: Option B. -> objects
Question 4. 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.    None of these
  2.    0
  3.    Compilation error, arrays cannot be initialized to zero size.
  4.    Compilation error, it is a.length() not a.length
 Discuss Question
Answer: Option B. -> 0
Question 5. 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 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 016
  2.    120 200 16
  3.    120 200 14
  4.    016 is a compile error. It should be written as 16.
 Discuss Question
Answer: Option C. -> 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.    None of these
  3.    0 0
  4.    1 2
  5.    0 0 0 0
 Discuss Question
Answer: Option D. -> 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.    a[1] is 1
  3.    The program has a runtime error because a[1
  4.    a[1] is 0
 Discuss Question
Answer: Option D. -> 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 can compile and run fine. The second line assigns a new array to arr.
  3.    The code has compile errors because we cannot assign a different size array to arr.
  4.    The code has runtime errors because the variable arr cannot be changed once it is assigned.
 Discuss Question
Answer: Option B. -> 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.    The reference of the array.
  2.    A copy of the array.
  3.    A copy of the first element.
  4.    The length of the array.
 Discuss Question
Answer: Option A. -> The reference of the array.

Latest Videos

Latest Test Papers