Array(Java Program ) Questions and Answers
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.
Explanation:-
Answer: Option B. -> 120 200 14016 is an octal number. The prefix 0 indicates that a number is in octal.
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);
}
}
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);
}
}
Explanation:-
Answer: Option B. -> 1In 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.
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.
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.