Java Arrays Quiz - Multiple Choice Questions (MCQ)


Arrays are an essential data structure in Java, allowing you to store and manipulate multiple values of the same type. In this blog post, we present a Java Arrays Quiz comprising 15+ multiple-choice questions (MCQ). This quiz aims to assess your understanding of arrays in Java, including array declaration, initialization, accessing elements, and common array operations. Let's put your knowledge of Java arrays to the test! 

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

1. What is an Array in Java? 

a) A collection of elements with different types 

b) A collection of elements with the same type 

c) A resizable data structure 

d) A container for storing key-value pairs 

Answer:

b) A collection of elements with the same type 

Explanation:

An array in Java is a collection of elements of the same type, accessed by their index positions. 

2. How do you declare an array in Java? 

a) int[] arr; 

b) int arr[]; 

c) int arr; 

d) Array<int> arr; 

Answer:

Options (a) and (b) are correct

Explanation:

Both int[] arr; and int arr[]; are valid ways to declare an array of integers in Java. 

3. What is the index range for the elements of an array in Java? 

a) 0 to length - 1

b) 1 to length 

c) -1 to length - 1 

d) 0 to length 

Answer:

a) 0 to length - 1

Explanation:

The index range for the elements of an array in Java is 0 to length - 1, where length is the number of elements in the array. 

4. How do you access an element in an array in Java? 

a) By using the element's value 

b) By using the element's index 

c) By using the element's key 

d) By using the element's label

Answer:

b) By using the element's index 

Explanation:

In Java, you access an element in an array by using the element's index, which represents its position in the array. 

5. What happens if you try to access an array element with an index that is out of bounds? 

a) A runtime exception is thrown 

b) The program terminates abruptly 

c) The element value is set to null 

d) The element value is set to 0 

Answer:

a) A runtime exception is thrown 

Explanation:

If you try to access an array element with an index that is out of bounds, a runtime exception called ArrayIndexOutOfBoundsException is thrown. 

6. Which method is used to copy one array into another in Java? 

a) copy() 

b) clone() 

c) System.arraycopy() 

d) Arrays.copy() 

Answer:

a) A runtime exception is thrown 

Explanation:

The System.arraycopy() method is used to copy one array into another in Java, allowing you to efficiently copy elements between arrays. 

7. Can the length of an array be changed after its creation in Java? 

a) Yes, by using the resize() method 

b) Yes, by using the length property 

c) No, the length of an array is fixed after creation 

d) No, arrays in Java have a fixed length 

Answer:

c) No, the length of an array is fixed after creation 

Explanation:

No, the length of an array cannot be changed after its creation in Java. Arrays have a fixed length determined at the time of initialization. 

8. 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.

9. 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 correctly. The braces are not allowed to appear before the type making the lion declaration incorrect. Therefore, Option C is correct.

10. 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.

11. Which of the following creates 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.

12. 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. 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.

13. 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.

14. 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.

15. 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.

16. 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).

17. 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.

Conclusion

Congratulations on completing the Java Arrays Quiz! We hope it challenged your knowledge and provided valuable insights into arrays in Java. Arrays are a fundamental data structure in Java, enabling you to store and manipulate collections of elements.

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

Keep practicing, keep learning, and become proficient in working with Java arrays!

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