Test Series - java

Test Number 42/64

Q: What is the output of the below Java program?
int[] marks = {35,65,95}; 
System.out.print(marks.length + "," + marks[1]);
A. 2,65
B. 3,95
C. 3,65
D. Compiler error
Solution: Array index starts with Zero (0). So marks[1] represents the second element.
Q: What is the output of the below Java code snippet?
int[] balls = {};
System.out.print(balls.length);
A. 0
B. -1
C. 1
D. Compiler error
Solution: no solution
Q: Which is the correct way of knowing Array Size in Java?
A. //int[] ary; ary.length()
B. //int[] ary; ary.length
C. //int[] ary; ary->length()
D. //int[] ary; ary->length
Solution: "length" is a field, not a method. So, parentheses are not required.
Q: What is the output of the below Java program with arrays?
String[] colors = {"RED";"YELLOW";"WHITE"};
System.out.print(colors[2]);
A. RED
B. YELLOW
C. WHITE
D. Compiler error
Solution: Array elements must be separated with Comma(,)s.
Q: What is the output of the below Java program with arrays?
public class Polo {
  public static void main(String args[])
  {
    String[] computer = {"RAM","HDD","MOUSE"};
    String[] parts = {computer[0],computer[2]};
    System.out.print(parts[1]);
  }
}
A. RAM
B. HDD
C. MOUSE
D. Compiler error
Solution: no solution
Q: What is the output of the below Java program?
int ages[3] = {25, 27, 30};
System.out.println(ages[1]);
A.  25
B. 27
C. 30
D. Compile error
Solution: We should not mention the array size at the time of Shorthand Initialization.
Error: Unresolved compilation problem: 
   Syntax error on token "3", delete this token
So make it like
int ages[] = {25, 27, 30};
Q: We should not specify the array size if declaration and initialization are done at the same time. (TRUE / FALSE)
A. FALSE
B. TRUE
C. none
D. Compile error
Solution: WRONG:
int ary[2]={34,45};

RIGHT:
int ary[] ={34,45};
Q: If an index of an element is N, what is its actual position in the array?
A. N-1
B. N
C. N+1
D. N+2
Solution: Array index starts with 0. So, an index of 6 means 7th position. An index of N means, N+1 position.
Q: An array in Java can be declared only of some predefined types. (TRUE/FALSE)
A. FALSE
B. TRUE
C. none
D. Compile error
Solution: An array can be of any data type, primitive or Object type.
Q: The name of an array variable or identifier can start with ___.
A. A letter
B. Underscore ( _ )
C. Dollar Symbol ($)
D. All
Solution: no solution

You Have Score    /10