1. Which of the following methods is used to start a thread in Java?
A) startThread()
B) run()
C) execute()
D) start()
Answer: D) start()
2. Which of the following is true about Java constructors?
A) A constructor can return a value.
B) A constructor is called when an object is created.
C) A constructor must have a return statement.
D) A constructor is only used to initialize static fields.
Answer: B) A constructor is called when an object is created.
3. What is the output of the following code?
String str = "Hello";
str.concat(" World");
System.out.println(str);
A) Hello World
B) World
C) Hello
D) Compilation error
Answer: C) Hello
Explanation: Strings in Java are immutable, meaning that the concat()
method does not modify the original string but returns a new string. Since
the result of concat() is not assigned to the str variable, the original string
"Hello" is printed.
4. Which of the following classes is used to read characters from a file in
Java?
A) FileReader
B) BufferedReader
C) FileInputStream
D) PrintWriter
Answer: A) FileReader
5. What does the finally block do in Java exception handling?
A) It executes only if no exception occurs.
B) It executes only if an exception occurs.
C) It executes regardless of whether an exception occurs or not.
D) It is used to catch multiple exceptions.
Answer: C) It executes regardless of whether an exception occurs or not.
6. Which of the following can be used to create an immutable class in Java?
A) Declaring all fields as final and providing no setter methods.
B) Declaring all fields as private and providing setter methods for them.
C) Declaring the class as abstract.
D) Declaring the class as static.
Answer: A) Declaring all fields as final and providing no setter methods.
7. What is the default value of a boolean variable in Java?
A) true
B) false
C) null
D) 0
Answer: B) false
8. Which of the following is a valid statement to declare a constant in Java?
A) const int MAX = 100;
B) final int MAX = 100;
C) constant int MAX = 100;
D) static final int MAX = 100;
Answer: B) final int MAX = 100;
9. What is the output of the following code?
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
A) 0
B) 1
C) ArrayIndexOutOfBoundsException
D) Compilation error
Answer: C) ArrayIndexOutOfBoundsException
Explanation: Arrays in Java are 0-indexed, so an array of length 3 (with
indexes 0, 1, and 2) will throw an ArrayIndexOutOfBoundsException when
trying to access index 3.
10. Which of the following collection classes in Java maintains the order of
elements?
A) HashSet
B) HashMap
C) TreeSet
D) LinkedList
Answer: D) LinkedList