0% found this document useful (0 votes)
64 views

Mcq Based on Arrays

The document contains a series of multiple-choice questions (MCQs) focused on Java arrays, covering topics such as array declaration, initialization, memory allocation, and output predictions for various code snippets. Each question provides four answer options, testing knowledge of Java array concepts and syntax. The questions range from basic definitions to specific coding scenarios, making it a comprehensive assessment tool for understanding Java arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Mcq Based on Arrays

The document contains a series of multiple-choice questions (MCQs) focused on Java arrays, covering topics such as array declaration, initialization, memory allocation, and output predictions for various code snippets. Each question provides four answer options, testing knowledge of Java array concepts and syntax. The questions range from basic definitions to specific coding scenarios, making it a comprehensive assessment tool for understanding Java arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MCQ based on Arrays:-

Q 1: In Java arrays are

 A objects B object references C primitive data type D None of the above

Q 2: Which one of the following is a valid statement?

 A char[] c = new char(); B char[] c = new char[5];


 C char[] c = new char(4); D char[] c = new char[];

Q 3: 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);
}
}
A0

 B Compilation error, arrays cannot be initialized to zero size.


 C Compilation error, it is a.length() not a.length
 D None of the above

Q 4: 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]);
}
}

 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 5: 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] + " ");
}
}

A 120 200 16 B 120 200 14 C 120 200 016 D compile error

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] + " ");
}
}

A1234 B0000 C12 D00

Q 7: Analyze the following code and choose the correct answer.


int[] arr = new int[5];
arr = new int[6];

 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 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]);
}
}

A The program has a compile error because new int[2]

 B The program has a runtime error because a[1]


 C a[1] is 0
 D a[1] is 1

Q 9: When you pass an array to a method, the method receives ________ .

 A A copy of the array. B A copy of the first element.


 C The reference of the array. D The length of the array.

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 11: Which will legally declare, construct, and initialize an array?

 A int [] myList = {};


 B int [] myList = (5, 8, 2);
 C int myList [] [] = {4,9,7,0};
 D int myList [] = {4, 3, 7};

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 malloc B alloc C new D new malloc

14. Java array is a collection of ________.


A. similar type of elements B. different type of element
C. heterogeneous data D. Both A and C
15. Array data access using _____.
A. Operator B. Variable C. index D. Pointer
16. At time of array initialization which is necessary to specify?
A. Row B. Column C. Row and Column D. None of the above
17. Java Array can allocate __________.
A. Dynamic Memory B. Static Memory C. Both A and B D. None of the above
18. Which of the following is an incorrect array declaration?

A. int [] arr = new int[5]. B. int arr[] = new int[5]. C. int arr[] = new int[5]. D. int arr[] = int [5] new

19. Index in array start with ______.


A. -1 B. 0 C. 1 D. infinite
20. Which of the following is used to declare,construct, and initlaize an array?
A. int arr [] [] = {1, 2, 3, 4}; B. int [] arr = (1, 2, 3);
C. int [] arr = {}; D. int arr [] = {1, 2, 3};
21. We can calculate the length of an array using ________.
A. sizeof(array) B. array.len C. array.length D. array.sizeof()
22. In java, array elements are stored in ________ memory locations.
A. Random B. Sequential C. Sequential & Random D. Binary search
23. What will be the output of the program?

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

23. What will be the output of the program?

int arr[] = new int [5];


System.out.print(arr);
A. 0 B. value stored in arr[0]. C. 0 D. Garbage Value

24. What will be the output of the program?

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

25. What will be output for the following code?

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

27. What will be output for the following code?

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

28. What will be output for the following code?

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

29. What will be output for the following code?

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

30. What will be the output of the program?


class Main
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}
A. Same B. Not Same C. Compiler error D. None of the above

31. What is the output of this program?

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

32. Which of these is an incorrect Statement?

A. It is necessary to use new operator to initialize an array


B. Array can be initialized using comma separated expressions surrounded by curly braces
C. Array can be initialized when they are declared
D. None of the mentioned

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

You might also like