wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering Java Concepts

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What is a Java Collection?

a)

A Java Collection is a framework for storing and manipulating groups of objects.

b)

A Java Collection is a type of database.

c)

A Java Collection is a programming language.

d)

A Java Collection is a method for writing Java code.

2.

Name three types of Java Collections.

a)

List, Set, Map

b)

Array, Queue, Stack

c)

Tree, Graph, Tuple

d)

String, Character, Byte

3.

What is the syntax for an if statement in Java?

a)

if { condition } then { code }

b)

if (condition) code;

c)

if (condition) { // code }

d)

if condition { code }

4.

How do you write an if-else statement in Java?

a)

if (condition) // code else { // code }

b)

if condition { // code } else // code

c)

if (condition) { // code } else if { // code }

d)

if (condition) { // code } else { // code }

5.

What are the different looping constructs available in Java?

a)

foreach

b)

repeat-until

c)

loop

d)

for, while, do-while

6.

Explain the difference between a for loop and a while loop.

a)

A for loop runs indefinitely, while a while loop stops after a set number of iterations.

b)

A for loop iterates a known number of times, while a while loop continues until a condition is false.

c)

A for loop can only be used with arrays, while a while loop can be used with any data type.

d)

A for loop is used for conditional statements, while a while loop is used for iteration.

7.

How do you declare an array in Java?

a)

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];'.

b)

array type arrayName;

c)

int numbers[] = new int[10];

d)

arrayName[] type;

8.

What is the syntax to access an element in an array?

a)

arrayName{index}

b)

arrayName:index

c)

arrayName[index]

d)

array[index]

9.

How can you read a file in Java?

a)

Use PrintWriter to write to the file instead of reading.

b)

Use Scanner to read the file line by line.

c)

Read the file using a FileInputStream only.

d)

Use BufferedReader with FileReader or Files.readAllLines method from java.nio.file.

10.

What classes are commonly used for file handling in Java?

a)

FileWriter

b)

File, FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, PrintWriter

c)

FileReader

d)

RandomAccessFile

11.

What is the purpose of the Arrays.sort() method?

a)

To merge two arrays into one.

b)

To find the maximum element in an array.

c)

To reverse the elements of an array.

d)

To sort the elements of an array in ascending order.

12.

Explain the bubble sort algorithm.

a)

Bubble sort algorithm sorts a list by repeatedly swapping adjacent elements if they are in the wrong order.

b)

Bubble sort algorithm sorts a list by dividing it into two halves.

c)

Bubble sort algorithm sorts a list by selecting the smallest element and moving it to the front.

d)

Bubble sort algorithm sorts a list by inserting elements in their correct position.

13.

What is the time complexity of quicksort?

a)

O(n) average, O(n log n) worst case

b)

O(n^2) average, O(n log n) worst case

c)

O(n log n) average, O(n^2) worst case

d)

O(log n) average, O(n) worst case

14.

How do you handle exceptions during file operations in Java?

a)

Use print statements to debug file operations.

b)

Ignore exceptions and continue execution.

c)

Always close files without handling exceptions.

d)

Use try-catch blocks to handle exceptions during file operations.

15.

What is the difference between List and Set in Java?

a)

List allows duplicates and maintains order; Set does not allow duplicates and is unordered.

b)

List is a collection of unique elements; Set can contain duplicates and is ordered.

c)

List does not allow duplicates and is unordered; Set allows duplicates and maintains order.

d)

List is faster for searching elements; Set is slower but allows duplicates.