Java Beginner Quiz
30 questions on Java Basics.
Question 1: Which method is the entry point of a Java program?
- A. start()
- B. run()
- C. main() β (correct answer)
- D. init()
Explanation: The public static void main(String[] args) method is the starting point of every Java application. The JVM calls this method first.
Question 2: What does JVM stand for?
- A. Java Virtual Machine β (correct answer)
- B. Java Variable Manager
- C. Java Version Module
- D. Java Visual Monitor
Explanation: The JVM (Java Virtual Machine) is the runtime engine that executes Java bytecode, enabling platform independence ("Write once, run anywhere").
Question 3: Which data type is used to store a single character in Java?
- A. String
- B. char β (correct answer)
- C. character
- D. letter
Explanation: The char data type stores a single 16-bit Unicode character. It is declared with single quotes: char letter = 'A';.
Question 4: What is the correct way to declare an integer variable in Java?
- A.
integer x = 10;
- B.
int x = 10; β (correct answer)
- C.
x = 10;
- D.
num x = 10;
Explanation: Java is a statically-typed language. You must declare the type (int) before the variable name. int x = 10; is the correct syntax.
Question 5: What is the output of the following code?
``java
System.out.println(10 / 3);
``
- A. 3.33
- B. 3 β (correct answer)
- C. 3.0
- D. Error
Explanation: When dividing two integers in Java, the result is an integer (floor division). 10 / 3 = 3, not 3.33. Use 10.0 / 3 for a float result.
Question 6: Which keyword is used to create an object in Java?
- A. create
- B. object
- C. new β (correct answer)
- D. init
Explanation: The new keyword allocates memory for a new object and calls its constructor. Example: Dog myDog = new Dog();.
Question 7: What is the default value of an int variable in Java (when declared as a class field)?
- A. null
- B. 0 β (correct answer)
- C. -1
- D. undefined
Explanation: Java initializes numeric instance variables to 0 by default. boolean defaults to false, and object references default to null.
Question 8: Which statement is used to exit a loop early in Java?
- A. exit
- B. stop
- C. return
- D. break β (correct answer)
Explanation: The break statement immediately exits the nearest enclosing loop (for, while, or do-while).
Question 9: What is the size of an int data type in Java?
- A. 8 bits
- B. 16 bits
- C. 32 bits β (correct answer)
- D. 64 bits
Explanation: int is a 32-bit signed integer, capable of storing values from -2,147,483,648 to 2,147,483,647.
Question 10: Which of the following is NOT a valid access modifier in Java?
- A. public
- B. private
- C. protected
- D. global β (correct answer)
Explanation: Java has four access levels: public, private, protected, and default (no modifier). There is no global keyword in Java.
Question 11: What is the output?
``java
String s = "Hello";
System.out.println(s.length());
``
- A. 4
- B. 5 β (correct answer)
- C. 6
- D. Error
Explanation: The length() method returns the number of characters in a string. "Hello" has 5 characters.
Question 12: How do you declare an array of integers in Java?
- A.
int arr[] = new int[5]; β (correct answer)
- B.
array int arr = new int[5];
- C.
int arr = [5];
- D.
int[5] arr;
Explanation: You can also write int[] arr = new int[5];. Both syntaxes create an integer array of size 5.
Question 13: Which keyword prevents a variable from being changed after initialization?
- A. constant
- B. static
- C. final β (correct answer)
- D. fixed
Explanation: The final keyword makes a variable a constant. Once assigned, it cannot be reassigned: final int MAX = 100;.
Question 14: What does ++i do?
- A. Increments
i after using its value
- B. Increments
i before using its value β (correct answer)
- C. Doubles
i
- D. Resets
i to 1
Explanation: ++i is the pre-increment operator β it increments i first, then uses the new value. i++ (post-increment) uses the current value first, then increments.
Question 15: Which loop guarantees at least one execution?
- A. for
- B. while
- C. do-while β (correct answer)
- D. foreach
Explanation: The do-while loop executes the body first, then checks the condition. This guarantees the body runs at least once even if the condition is false.
Question 16: What is the output?
``java
String a = "Hello";
String b = "Hello";
System.out.println(a == b);
``
- A. true β (correct answer)
- B. false
- C. Error
- D. null
Explanation: String literals in Java are stored in a **String Pool**. Both a and b point to the same object. However, using new String("Hello") would make == return false. Use .equals() for value comparison.
Question 17: What is method overloading?
- A. Creating a method with the same name but different parameters β (correct answer)
- B. Creating a method in a child class with the same name as the parent
- C. Calling a method multiple times
- D. Making a method static
Explanation: Method overloading (compile-time polymorphism) allows multiple methods with the same name but different parameter lists within the same class.
Question 18: What is the output?
``java
int[] arr = {10, 20, 30};
System.out.println(arr.length);
``
- A. 2
- B. 3 β (correct answer)
- C. 30
- D. Error
Explanation: arr.length is a property (not a method) that returns the number of elements in the array. Note: arrays use .length (no parentheses), strings use .length().
Question 19: Which OOP concept hides internal details and exposes only functionality?
- A. Inheritance
- B. Polymorphism
- C. Encapsulation β (correct answer)
- D. Abstraction
Explanation: Encapsulation bundles data and methods together and hides internal state using access modifiers. private fields with public getters/setters are a classic example.
Question 20: What will this code print?
``java
int x = 5;
System.out.println(x > 3 ? "Yes" : "No");
``
- A. Yes β (correct answer)
- B. No
- C. 5
- D. Error
Explanation: This is the ternary operator: condition ? valueIfTrue : valueIfFalse. Since 5 > 3 is true, it prints "Yes".
Question 21: What keyword is used to inherit a class in Java?
- A. inherits
- B. implements
- C. extends β (correct answer)
- D. super
Explanation: The extends keyword creates an inheritance relationship. implements is used for interfaces. super calls the parent constructor.
Question 22: What is the output?
``java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error caught!");
}
``
- A. 0
- B. Error caught! β (correct answer)
- C. Program crashes
- D. null
Explanation: Division by zero throws an ArithmeticException, which is caught by the catch block. The program continues normally after the catch.
Question 23: What is the correct way to create a constructor?
- A. A method with the same name as the class β (correct answer)
- B. A method named
constructor()
- C. A method named
init()
- D. A method named
create()
Explanation: Constructors have the same name as the class and no return type. They're called automatically when an object is created with new.
Question 24: What is the difference between == and .equals() for Strings?
- A. They are the same
- B.
== compares references; .equals() compares values β (correct answer)
- C.
== compares values; .equals() compares references
- D.
.equals() is faster
Explanation: For objects, == checks if they point to the same memory location. .equals() checks if the content is the same. Always use .equals() for String comparison.
Question 25: Which collection allows duplicate elements?
- A. Set
- B. HashSet
- C. ArrayList β (correct answer)
- D. TreeSet
Explanation: ArrayList (and other List implementations) allow duplicates. Set implementations (HashSet, TreeSet) do not allow duplicates.
Question 26: What is the output?
``java
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}
``
- A. 0 1 2 3 4
- B. 0 1 2 4 β (correct answer)
- C. 0 1 2
- D. 3
Explanation: continue skips the current iteration. When i == 3, the print is skipped, so 3 is not printed.
Question 27: What is an abstract class?
- A. A class that cannot be instantiated and may contain abstract methods β (correct answer)
- B. A class with only static methods
- C. A class that has no constructor
- D. A class with private methods only
Explanation: Abstract classes are declared with the abstract keyword. They can contain both abstract (unimplemented) and concrete methods. They must be subclassed.
Question 28: What is the difference between ArrayList and LinkedList?
- A. ArrayList uses linked nodes; LinkedList uses arrays
- B. ArrayList is faster for random access; LinkedList is faster for insertions/deletions β (correct answer)
- C. They are the same
- D. LinkedList is deprecated
Explanation: ArrayList uses a dynamic array (O(1) random access). LinkedList uses doubly-linked nodes (O(1) insertions at ends). Choose based on your use case.
Question 29: What is the super keyword used for?
- A. To call the parent class constructor or methods β (correct answer)
- B. To create a superclass
- C. To declare a variable as global
- D. To make a method static
Explanation: super() calls the parent constructor. super.methodName() calls a parent method. It's commonly used in constructors and when overriding methods.
Question 30: What is the output?
``java
String str = null;
try {
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Null error!");
}
``
- A. 0
- B. null
- C. Null error! β (correct answer)
- D. Program crashes
Explanation: Calling .length() on a null reference throws a NullPointerException. The catch block handles it and prints "Null error!" instead of crashing.