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
Answer:
Explanation:
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:
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:
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:
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:
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:
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;@74a14482Explanation:
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) 1Explanation:
Conclusion
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
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Comments
Post a Comment
Leave Comment