Java 50 MCQs with Answers & Explanations
31. Which of the following is the superclass of all classes in Java?
a) Object
b) Class
c) Runtime
d) System
■ a) Object
All classes in Java inherit from Object either directly or indirectly.
32. Which keyword is used to inherit an interface in Java?
a) extends
b) implements
c) import
d) interface
■ b) implements
Classes implement interfaces, classes extend other classes.
33. Which exception is a checked exception?
a) NullPointerException
b) IOException
c) ArithmeticException
d) ArrayIndexOutOfBoundsException
■ b) IOException
Checked = must be handled at compile time. Unchecked = RuntimeException & its subclasses.
34. Which operator is used for object type comparison in Java?
a) ==
b) equals
c) instanceof
d) compareTo
■ c) instanceof
Checks whether an object is an instance of a specific class or subclass.
35. Which class is used for reading input from the console in Java?
a) Scanner
b) Console
c) BufferedReader
d) All of the above
■ d) All of the above
Scanner, Console, and BufferedReader can all read input from console.
36. Which of the following is not a feature of Java?
a) Platform Independent
b) Pointers
c) Object-Oriented
d) Robust
■ b) Pointers
Java does not support explicit pointers like C/C++.
37. What will be the output of this code?
String s1 = "Hello";
String s2 = "Hello";
[Link](s1 == s2);
a) true
b) false
c) Compilation error
d) Runtime error
■ a) true
String literals are stored in the String pool, so both reference the same object.
38. Which keyword is used to define a constant variable in Java?
a) const
b) final
c) static
d) define
■ b) final
Final variables cannot be reassigned.
39. Which collection class allows duplicate elements?
a) Set
b) List
c) Map
d) None
■ b) List
Lists allow duplicates. Sets do not. Maps use keys which are unique.
40. Which thread state is not part of Java’s Thread life cycle?
a) Runnable
b) Waiting
c) Timed Waiting
d) Deadlock
■ d) Deadlock
Thread states: New, Runnable, Running, Waiting, Timed Waiting, Terminated.
41. Which method must be implemented when a class implements Runnable?
a) start()
b) run()
c) execute()
d) call()
■ b) run()
Runnable defines only run() method.
42. Which keyword is used to stop method overriding?
a) static
b) abstract
c) final
d) const
■ c) final
Final methods cannot be overridden.
43. Which loop is guaranteed to execute at least once?
a) for
b) while
c) do-while
d) foreach
■ c) do-while
do-while executes body first, then checks condition.
44. Which operator is used to allocate memory for an object in Java?
a) malloc
b) new
c) alloc
d) create
■ b) new
The new operator creates objects dynamically.
45. Which method is used to get the length of a string in Java?
a) size()
b) getSize()
c) length()
d) len()
■ c) length()
length() returns number of characters in a string.
46. Which keyword is used to refer to the parent class constructor?
a) super
b) this
c) parent
d) base
■ a) super
super() is used to call parent constructor.
47. Which of the following is true about Java arrays?
a) Arrays are primitive
b) Arrays are objects
c) Arrays are pointers
d) Arrays are dynamic
■ b) Arrays are objects
Arrays are fixed-size, indexed, and store elements of the same type.
48. Which keyword is used to handle exceptions?
a) throw
b) try-catch
c) finally
d) error
■ b) try-catch
Exceptions are handled using try-catch blocks.
49. Which collection class maintains natural ordering of elements?
a) HashSet
b) LinkedHashSet
c) TreeSet
d) HashMap
■ c) TreeSet
TreeSet sorts elements in ascending order by default.
50. Which method is used to convert a string into an integer in Java?
a) [Link]()
b) [Link]()
c) Both a and b
d) toInt()
■ c) Both a and b
[Link]("123") → int, [Link]("123") → Integer object.