WorksheetsJava Quiz
Total questions: 20
Worksheet time: 12mins
Which Java collection does not allow duplicate elements?
HashSet
ArrayList
LinkedList
TreeMap
Which Java collection allows access to elements using index-based methods?
Set
Queue
Map
List
Which Java collection ensures that elements are stored in ascending order based on their natural ordering or a custom comparator?
HashMap
TreeSet
LinkedHashMap
HashSet
What interface in Java Collection Framework allows you to store key-value pairs?
Set
List
Map
Collection
Which is the root interface used for storing elements and provides methods to manipulate them?
List
Map
Collection
Set
Which Java collection guarantees insertion order?
HashSet
TreeMap
LinkedHashMap
HashMap
Which Java collection class provides a resizable array implementation?
HashSet
ArrayList
HashMap
LinkedList
Which of the following is NOT a primitive data type in Java?
int
float
double
String
What happens when you concatenate a String with a primitive data type in Java?
The primitive data type is automatically converted to a String before concatenation.
The primitive data type remains unchanged.
An error occurs because primitive data types cannot be concatenated with Strings.
The primitive data type is converted to its corresponding object wrapper class before concatenation.
What does the == operator compare when used with reference types in Java?
It compares the contents of the objects.
It compares the memory addresses of the objects.
it compares the sizes of the objects.
It compares the types of the objects.
When should you use the equals method instead of the == operator to compare objects in Java?
When comparing primitive data types.
When comparing the contents of objects.
When comparing memory addresses of objects.
When comparing the types of objects.
What is the purpose of the pom.xml file in Maven projects?
It contains the source code of the project.
It defines project metadata, dependencies, and build settings.
It specifies the main class of the project.
It is used for unit testing configurations.
Which method in the String class is used to concatenate two strings?
concat()
append()
concatenate()
add()
What does the trim() method do in the String class?
It removes leading and trailing whitespace from the string.
It splits the string into an array of substrings based on a delimiter.
It converts the string to uppercase.
It replaces all occurrences of a specified character with another character.
Which method in the String class is used to obtain the length of a string?
size()
length()
getSize()
count()
Which statement in Java is used to declare an array?
int array;
array<int>;
int[] array;
array = new int[];
Which loop structure in Java is best suited for iterating through an array?
for loop
while loop
do-while loop
enhanced for loop (for-each loop)
What is the correct way to access the third element of an array named arr in Java?
arr.get(2)
arr[2]
arr.get(3)
arr[3]
What does the following for loop do?
for (int i = 0; i < 5; i++) { }
It iterates 5 times.
It iterates 4 times.
It iterates indefinitely.
It iterates once.
Which loop structure in Java is best suited for situations where the loop body needs to be executed at least once?
for loop
while loop
do-while loop
enhanced for loop (for-each loop)
