Learn Mode

Java Programming Concepts and Logic Quiz

#1

Which of the following is a primitive data type in Java?

Integer
Explanation

Integer is a primitive data type in Java.

#2

What is the result of 9 % 4 in Java?

1
Explanation

The modulo operator (%) returns the remainder after division.

#3

What is the default value of a boolean variable in Java?

false
Explanation

Boolean variables default to 'false' in Java.

#4

What is the purpose of the 'super' keyword in Java?

To call the superclass constructor
Explanation

'super' keyword in Java refers to the superclass.

#5

What is the output of the following code?

public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
System.out.print(i + " ");
i++;
}
}
}

0 1 2
Explanation

The loop iterates from 0 to 2, printing each value.

#6

What does the 'static' keyword mean in Java?

It indicates a method or variable that belongs to the class itself, rather than instances of the class
Explanation

Static keyword in Java indicates class-level entities.

#7

What is the output of the following code?

public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x++);
}
}

5
Explanation

The post-increment operator prints the current value of x, then increments it.

#8

What is the purpose of the 'break' statement in Java?

To terminate a loop or switch statement
Explanation

The 'break' statement exits the loop or switch statement.

#9

What does the 'this' keyword refer to in Java?

The current class instance
Explanation

'this' keyword in Java refers to the current instance of the class.

#10

What is the output of the following code?

public class Main {
public static void main(String[] args) {
String str = null;
System.out.println(str.length());
}
}

NullPointerException
Explanation

Attempting to access a method on a null object results in a NullPointerException.

#11

What is the output of the following code?

public class Main {
public static void main(String[] args) {
int x = 6;
if (x > 5) {
System.out.println("Hello");
} else {
System.out.println("World");
}
}
}

Hello
Explanation

The condition 'x > 5' is true, so 'Hello' is printed.

#12

Which of the following is true about Java packages?

A package can contain classes, interfaces, enumerations, and annotations
Explanation

Java packages provide a way to organize and manage classes.

#13

What is method overloading in Java?

Defining multiple methods with the same name but different parameters
Explanation

Method overloading allows multiple methods with the same name but different parameter lists.

#14

What does the 'transient' keyword do in Java?

It specifies that a variable should not be serialized
Explanation

'transient' keyword in Java prevents serialization of variables.

#15

What is the output of the following code?

public class Main {
public static void main(String[] args) {
int i = 10;
do {
System.out.print(i + " ");
i--;
} while (i > 0);
}
}

10 9 8 7 6 5 4 3 2 1
Explanation

The loop iterates from 10 to 1, printing each value.

#16

What is a Java interface?

A collection of abstract methods
Explanation

Java interface defines a contract for implementing classes.

#17

What is the purpose of the 'finally' block in Java exception handling?

To execute code after try-catch blocks, regardless of whether an exception occurs or not
Explanation

'finally' block is always executed, providing a mechanism for cleanup.

#18

What is the difference between '==', 'equals()', and 'compareTo()' methods in Java?

'==' compares object references, 'equals()' compares object contents, 'compareTo()' compares values of comparable objects
Explanation

'==' checks object identity, 'equals()' checks object equality, 'compareTo()' compares objects for sorting.

#19

What is the purpose of the 'volatile' keyword in Java?

To indicate that a variable's value may be changed by multiple threads
Explanation

'volatile' keyword in Java ensures visibility of shared variables among threads.

Test Your Knowledge

Craft your ideal quiz experience by specifying the number of questions and the difficulty level you desire. Dive in and test your knowledge - we have the perfect quiz waiting for you!