Mcq Based on Arrays
Mcq Based on Arrays
A The program has a compile error because the size of the array wasn't specified when declaring the
array.
B The program has a runtime error because the array elements are not initialized.
C The program runs fine and displays x[0] is 0.
D The program has a runtime error because the array element x[0] is not defined.
Q 6: 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] + " ");
}
}
A The code has compile errors because the variable arr cannot be changed once it is assigned.
B The code has runtime errors because the variable arr cannot be changed once it is assigned.
C The code can compile and run fine. The second line assigns a new array to arr.
D The code has compile errors because we cannot assign a different size array to arr.
Q 10: What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
A0 B1 C2 D3
Q 12: What would be the result of attempting to compile and run the following code?
public class HelloWorld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}
A The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be
replaced by {1, 2, 3}.
B The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be
replaced by new double[3]{1, 2, 3};
C The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be
replaced by new double[]{1.0, 2.0, 3.0};
D The program compiles and runs fine and the output
Q 13: Which of these operators is used to allocate memory to array variable in Java?
A. int [] arr = new int[5]. B. int arr[] = new int[5]. C. int arr[] = new int[5]. D. int arr[] = int [5] new
class Main
{
public static void main(String args[])
{
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
A. 10 20 30 40 50 B. Compiler Error C. 10 20 30 40 D. None of the above
class Main
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
A. 0 2 4 6 8 B. 1 3 5 7 9 C. 0 1 2 3 4 5 6 7 8 9 D. 1 2 3 4 5 6 7 8 9 10
class Main
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.print(n);
}
}
A. 3 B. 0 C. 6 D. 1
26. Predict the output of following Java Program?
class Main
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + " ");
}
}
}
A. 1 2 3 4 5 6 7 8 9 10 B. 0 1 2 3 4 5 6 7 8 9 10 C. i j k l m n o p q r D. i i i i i i i i i i
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0 0 B. garbage value garbage value C. Compiler Error D. Exception
class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A. 0 0 B. garbage value garbage value C. Compiler Error D. Exception
class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5);
}
}
A. 8 B. 9 C. 10 D. 11
class Main
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
A. Same B. Not Same C. Compiler error D. None of the above
33. What is the type of variable "b" and "d" in the below snippet?
int a[], b;
int []c, d;
A. "b" and "d" are int B. "b" and "d" are arrays of type int
C. "d" is int variable; and "b" is int array D. "b" is int variable; and "d" is int array
34) An Array in Java is a collection of elements of ___ data type.
A) Same B) Different
35) The Java Virtual Machine (JVM) implements arrays as ___ type.
A) Primitive B) Object C)Reference D)None
36) An array declaration in Java without initialization ___ memory.
A) Does not allocate B) Allocates memory
37) In Java language, an array index starts with ___.
A) -1 B) 0 C) 1 D) Any integer
38) Which are the special symbols used to declare an array in Java?
A) Braces { } B) Parentheses () C) Square Brackets [ ] D) Angled Brackets < >
39) Which are the special symbols used to initialize an array at the time of the declaration itself?
A) Parentheses ( ) B) Square Brackets [ ] C) Braces { } D) Angled Brackets < >
40) It is possible to skip initializing some elements of the array during Shorthand Initialization. (TRUE / FALSE)
A) FALSE B) TRUE
41) In Java, an array can be declared without initialization without mentioning the size. (TRUE / FALSE)
A) TRUE B) FALSE
42) What is the output of the below Java code snippet with arrays?
static int[] nums;
public static void main(String args[])
{
System.out.println(nums.length);
}
A) 0 B) null C) Compiler error D) Runtime Exception -Null Pointer Exception
43) 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
44) 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
45) 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
46) What is the output of the below Java program with arrays?
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
47) 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
48) We should not specify the array size if declaration and initialization are done at the same time. (TRUE / FALSE)
A) FALSE B) TRUE
49) 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
50) An array in Java can be declared only of some predefined types. (TRUE/FALSE)
A) FALSE B) TRUE
51) The name of an array variable or identifier can start with ___.
A) A letter B) Underscore ( _ ) C) Dollar Symbol ($) D) All
52) What is the default value of an element of Object type array?
A) 0 B) null C) -1 D) Garbage value
53) What is the default value of byte, short, int or long data type elements of an array in Java?
A) -1 B) 1 C) 0 D) Garbage value
54) What is the default value of float or double data type elements of an array in Java?
A) 0 B) 0.0 C) 1 D) 1.0
55) What is the default value of a char data type elements of an array in Java?
A) 'A' B) '\0' C) null D) '\0' or null
56) What is the default value of boolean data type elements of an array in Java?
A) true B) false
57) What is the output of the below Java program?
int balls[], rounds=3;
balls = new int[rounds];
for(int i=0; i<balls.length; i++)
balls[i] = (i+1)*2;
for(int j=0; j<balls.length; j++)
System.out.print(balls[j] + ",");
A) 0,2,4, B) 1,2,3, C) 2,4,6, D) Compiler error
58) What is the output of the below Java program with arrays?
String[] ary = {"KITE", "AIR"};
String str = "PLANE";
ary[1] = str;
str = "FLY";
System.out.println(ary[1]);
A) AIR B) PLANE C) FLY D) Compiler error