NEW
Font size
WorksheetsMastering Java Concepts
Total questions: 15
Worksheet time: 8mins
What is a Java Collection?
A Java Collection is a framework for storing and manipulating groups of objects.
A Java Collection is a type of database.
A Java Collection is a programming language.
A Java Collection is a method for writing Java code.
Name three types of Java Collections.
List, Set, Map
Array, Queue, Stack
Tree, Graph, Tuple
String, Character, Byte
What is the syntax for an if statement in Java?
if { condition } then { code }
if (condition) code;
if (condition) { // code }
if condition { code }
How do you write an if-else statement in Java?
if (condition) // code else { // code }
if condition { // code } else // code
if (condition) { // code } else if { // code }
if (condition) { // code } else { // code }
What are the different looping constructs available in Java?
foreach
repeat-until
loop
for, while, do-while
Explain the difference between a for loop and a while loop.
A for loop runs indefinitely, while a while loop stops after a set number of iterations.
A for loop iterates a known number of times, while a while loop continues until a condition is false.
A for loop can only be used with arrays, while a while loop can be used with any data type.
A for loop is used for conditional statements, while a while loop is used for iteration.
How do you declare an array in Java?
You declare an array in Java using the syntax: 'type[] arrayName;'. For example, 'int[] numbers;'. You can also initialize it with 'new type[size];'. For example, 'int[] numbers = new int[10];'.
array type arrayName;
int numbers[] = new int[10];
arrayName[] type;
What is the syntax to access an element in an array?
arrayName{index}
arrayName:index
arrayName[index]
array[index]
How can you read a file in Java?
Use PrintWriter to write to the file instead of reading.
Use Scanner to read the file line by line.
Read the file using a FileInputStream only.
Use BufferedReader with FileReader or Files.readAllLines method from java.nio.file.
What classes are commonly used for file handling in Java?
FileWriter
File, FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, PrintWriter
FileReader
RandomAccessFile
What is the purpose of the Arrays.sort() method?
To merge two arrays into one.
To find the maximum element in an array.
To reverse the elements of an array.
To sort the elements of an array in ascending order.
Explain the bubble sort algorithm.
Bubble sort algorithm sorts a list by repeatedly swapping adjacent elements if they are in the wrong order.
Bubble sort algorithm sorts a list by dividing it into two halves.
Bubble sort algorithm sorts a list by selecting the smallest element and moving it to the front.
Bubble sort algorithm sorts a list by inserting elements in their correct position.
What is the time complexity of quicksort?
O(n) average, O(n log n) worst case
O(n^2) average, O(n log n) worst case
O(n log n) average, O(n^2) worst case
O(log n) average, O(n) worst case
How do you handle exceptions during file operations in Java?
Use print statements to debug file operations.
Ignore exceptions and continue execution.
Always close files without handling exceptions.
Use try-catch blocks to handle exceptions during file operations.
What is the difference between List and Set in Java?
List allows duplicates and maintains order; Set does not allow duplicates and is unordered.
List is a collection of unique elements; Set can contain duplicates and is ordered.
List does not allow duplicates and is unordered; Set allows duplicates and maintains order.
List is faster for searching elements; Set is slower but allows duplicates.
