Array(Java Program ) Questions and Answers

Question 1. 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 B.
  2.    a[1] is 0
  3.    a[1] is 1
Explanation:-
Answer: Option A. -> The program has a compile error because new int[2 B.
After executing the statement a = new int[2], a refers to int[2]. The default value for a[0] and a[1] is 0.

Question 2. 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.
Explanation:-
Answer: Option B. -> 120 200 14
016 is an octal number. The prefix 0 indicates that a number is in octal.

Question 3. 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.
Explanation:-
Answer: Option C. -> The code can compile and run fine. The second line assigns a new array to arr.

Question 4. 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
Explanation:-
Answer: Option C. -> 1 2

Question 5. 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.
Explanation:-
Answer: Option C. -> The reference of the array.

Question 6. In Java arrays are
  1.    objects
  2.    object references
  3.    primitive data type
  4.    None of the above
Explanation:-
Answer: Option A. -> objects
In java an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Question 7. 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
Explanation:-
Answer: Option B. -> 1
In the given program.
Line 7 : if(myList[i] > max) execute only on time when i =1;
when i = 1 then myList[i] = 5 and max = 1(so the statement is true and if block will be executed).
Then, max = myList[i] = 5 and indexOfMax = i = 1.
After that if statement always false. so indexOfMax value remain 1.
Therefore the value of indexOfMax is 1 at end of the for loop.

Question 8. 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[];
Explanation:-
Answer: Option B. -> char[] c = new char[5];
The syntax for declaring and creating an array variable in java is:
dataType[] arrayRefVar = new dataType[arraySize];
Thus, option (A) and option (C) is syntactically wrong as parentheses( ( ) ) is used instead of square brackets( [ ] ).
Option (D) is incorrect as the size of the array is missing.

Question 9. 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
Explanation:-
Answer: Option A. -> 0

Question 10. 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.
Explanation:-
Answer: Option C. -> The program runs fine and displays x[0] is 0.
Program is syntactically correct, so no error.
In java, if the array is not initialized at the time of declaration and creation then all the elements of the array are initialized to 0 by default.