Java Arrays Quiz - Multiple Choice Questions (MCQ)

In this post, we have provided Java Arrays multiple-choice questions to test your knowledge about Arrays in Java.

Learn Java Arrays at https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

We would suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of each question). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.

Q1: What symbol is used for a varargs method parameter?

A.  ..

B.  ...

C.  --

D.  ---

Answer:

B.  ...

Explanation:

Three dots (...) are the syntax for a method parameter of type varargs. It is treated as an array.

Q2: How many of the following are legal declarations?

[]double lion;
double[] tiger;
double bear[];
A. None

B. One

C. Two

D. Three

Answer:

C. Two

Explanation:

The array braces are allowed to appear before or after the variable name, making the tiger and bear declarations correct. The braces are not allowed to appear before the type making the lion declaration incorrect. Therefore, Option C is correct.

Q3: How do you determine the number of elements in an array?

int buses[] = new int[5];

A. buses.length

B. buses.length()

C. buses.size

D. buses.size()

Answer:

A. buses.length

Explanation:

Arrays use the length variable to determine the number of elements, making Option A correct. 

For an ArrayList, Option D would have been the answer.

Q4: Which of the following create an empty two-dimensional array with dimensions 2×2?

A. int[][] blue = new int[2, 2];

B. int[][] blue = new int[2], [2];

C. int[][] blue = new int[2][2];

D. int[][] blue = new int[2 x 2];

Answer:

C. int[][] blue = new int[2][2];

Explanation:

A two-dimensional array is declared by listing both sizes in separate pairs of braces.
Option C correctly shows this syntax.

Q5: What does this code output?

String[] nums = new String[] { "1", "9", "10" };
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
A. [1, 9, 10]

B. [1, 10, 9]

C. [10, 1, 9]

D. None of the above

Answer:

B. [1, 10, 9]

Explanation:

The elements of the array are of type String rather than int. Therefore, we use alphabetical order when sorting. The character 1 sorts before character 9, alphabetically making Option A incorrect. Shorter strings sort before longer strings when all the other characters are the same, making Option B the answer.

Q6: How many of the following are legal declarations?

String lion [] = new String[] {"lion"};
String tiger [] = new String[1] {"tiger"};
String bear [] = new String[] {};
String cat [] = new String[0] {};
A. None

B. One

C. Two

D. Three

Answer:

C. Two

Explanation:

When using an array initializer, you are not allowed to specify the size separately. The size is inferred from the number of elements listed. Therefore, tiger and cat are incorrect. When you’re not using an array initializer, the size is required. An empty array initializer is allowed. Option C is correct because lion and bear are legal.

Q7: How many of the following are legal declarations?

float[] lion = new float[];
float[] tiger = new float[1];
float[] bear = new[] float;
float[] cat = new[1] float;
A. None

B. One

C. Two

D. Three

Answer:

B. One

Explanation:

Since no elements are being provided when creating the arrays, size is required. Therefore, lion and bear are incorrect. The braces containing the size are required to be after the type, making the cat incorrect. The only one that is correct is tiger, making the correct
answer Option B.

Q8: Which is not a true statement about an array?

A. An array expands automatically when it is full.

B. An array is allowed to contain duplicate values.

C. An array understands the concept of ordered elements.

D. An array uses a zero index to reference the first element.

Answer:

A. An array expands automatically when it is full.

Explanation:

An ArrayList expands automatically when it is full. An array does not, making Option A the answer. The other three statements are true of both an array and an ArrayList.

Q9: What is a possible output of the following code?

String[] strings = new String[2];
System.out.println(strings);
A. [null, null]

B. [,]

C. [Ljava.lang.String;@74a14482

D. None of the above

Answer:

C. [Ljava.lang.String;@74a14482

Explanation:

Calling toString() on an array doesn’t output the contents of the array, making Option C correct. If you wanted Option A to be the answer, you’d have to call Arrays.toString(strings)

Q10: What does the following output?

String[] os = new String[] { "Mac", "Linux", "Windows" };
Arrays.sort(os);
System.out.println(Arrays.binarySearch(os, "Mac"));
A. 0

B. 1

C. 2

D. The output is not defined.

Answer:

B. 1

Explanation:

The code sorts before calling binarySearch(), so it meets the precondition for that method. The target string of "Mac" is the second element in the sorted array. Since array indices begin with zero, the second position is index 1, and Option B is correct.

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments