Top 20 Basic Java Quiz Questions with Answers

Java is a _____ language.
A. Platform-dependent
B. Platform-independent
C. Machine-dependent
D. Assembly
Platform-independent
Java uses bytecode executed by the JVM, which makes it platform-independent.
Which feature makes Java portable?
A. OOP
B. JVM
C. Bytecode
D. Garbage Collection
JVM
Java compiles source code into bytecode, which runs on any system with a compatible JVM installed.
Which of the following is not a feature of Java programming?
A. Object-Oriented
B. Secure
C. Pointer-based
D. Robust
Pointer-based
Java does not support pointers directly.
Which of the following feature of Java handles memory automatically?
A. Inheritance
B. Encapsulation
C. Garbage Collection
D. Polymorphism
Garbage Collection
JVM automatically deletes unused objects using garbage collection.
Java is _____ typed programming language.
A. Weakly
B. Strongly
C. Loosely
D. Dynamically
Strongly
Every variable must have a data type defined in Java. That’s why Java is a strongly type programming language.
Which of the following is a valid variable name in Java?
A. 1num
B. num_1
C. num-1
D. @num
num_1
num_1 is a valid variable name in Java.
Which keyword is used to define a constant in Java?
A. const
B. static
C. fixed
D. final
final
The final keyword makes a variable constant.
Which among the following features supports code reusability in Java?
A. Encapsulation
B. Inheritance
C. Abstraction
D. Polymorphism
Inheritance
Inheritance is a fundamental object-oriented programming (OOP) concept in Java that directly supports and promotes code reusability.
What is the size of int data type in Java?
A. 2 bytes
B. 4 bytes
C. 8 bytes
D. Depends on system
4 bytes
The data type int is always 4 bytes (32-bit) in Java.
Which of the following keywords is used to inherit a class in Java?
A. extends
B. implements
C. inherits
D. super
extends
The keyword extends is used for class inheritance.
What will be the output of the program?
public class Main {
public static void main(String[] args) {
System.out.println(10 + 20 + "Java");
}
}
A. Java1020
B. 1020Java
C. 30Java
D. Error
30Java
10 + 20 = 30
Then “30” + “Java” → 30Java
Which of the following is not a primitive data type in Java?
A. char
B. int
C. float
D. String
String
A string is a class in Java, not a primitive data type.
What will be the expected output of the Java program?
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x++ + ++x);
}
}
A. 10
B. 11
C. 12
D. 13
12
x++ → 5 (x becomes 6)
++x → 7
Total = 5 + 7 = 12
Which of the following loops executes at least once?
A. for
B. while
C. do-while
D. None of the above
do-while
The do-while loop runs at least once before checking the condition.
What will be the output of the Java program?
public class Main {
public static void main(String[] args) {
System.out.println(5/2);
}
}
A. 2
B. 2.5
C. 3
D. Error
2
Integer division → result = 2
Which of the following keywords is used to handle exceptions in Java?
A. try
B. catch
C. throw
D. All of the above
All of the above
All keywords such as try, catch, and throw are used in exception handling.
What will be the expected output of the Java program?
public class Main {
public static void main(String[] args) {
System.out.println('A' + 1);
}
}
A. A1
B. A + 1
C. B
D. 66
66
‘A’ = 65 → 65 + 1 = 66
Which of the following output is correct for the below Java program
public class Main {
public static void main(String[] args) {
int x = 0;
System.out.println(x++);
System.out.println(++x);
}
}
A. 0 2
B. 0 1
C. 1 2
D. 1 1
0 2
x++ → prints 0, x=1
++x → prints 2
What is the expected output of the Java program below?
public class Main {
public static void main(String[] args) {
String s = "Java";
s.concat(" World");
System.out.println(s);
}
}
A. Java World
B. Java
C. World
D. null
Java
String is immutable class in Java.
Which of the following exceptions is unchecked in Java?
A. IOException
B. SQLException
C. FileNotFoundException
D. NullPointerException
NullPointerException
NullPointerException is a runtime exception, which are unchecked exception.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *