Table of Contents
Java is a powerful, versatile programming language used across various domains like web development, mobile apps, enterprise software, and more. It is important to have a solid grasp of core concepts for exams and interviews because, it continues to dominate the software development world. Whether you are a beginner or an experienced developer, understanding the fundamentals is important for both job opportunities and academic success.
This post contains 40 important Java MCQs which cover most of the important topics from data types, control structures, object-oriented programming, collections, exception handling, and multi-threading. You will be able to strengthen your knowledge, prepare for your exams, and crack those interviews by practicing these questions.
MCQs with Answers and Explanations
Q1. Which of the following is the default value of a boolean variable in Java?
a) true
b) false
c) 0
d) null
Answer: b) false
In Java, the default value of a boolean variable is false
. It is automatically initialized to false
for instance variables.
Q2. Which of the following is not a primitive data type in Java?
a) int
b) float
c) String
d) boolean
Answer: c) String
String
is a reference type, not a primitive data type. Primitives include int
, float
, boolean
, etc.
Q3. What is the size of an int variable in Java?
a) 16 bits
b) 32 bits
c) 64 bits
d) 8 bitsAnswer: b) 32 bits
Explanation: The int
data type in Java takes 32 bits (4 bytes) to store integer values.
Q4. What will be the output of the following code?javaCopy codeSystem.out.println(5 + 10 + "Hello");
a) 15Hello
b) Hello15
c) 5Hello
d) 510Hello
Answer: a) 15Hello
Java evaluates 5 + 10
first, resulting in 15
, then concatenates it with the string "Hello"
, producing 15Hello
.
Q5. Which of the following is used to declare an array in Java?
a) []
b) {}
c) <>
d) ()
Answer: a) []
In Java, arrays are declared using square brackets, like int[] arr = new int[10];
.
Q6. What is the superclass of every class in Java?
a) Object
b) Class
c) Parent
d) Super
Answer: a) Object
Explanation: All classes in Java, except for Object
itself, inherit from the Object
class, which is the root of the class hierarchy.
Q7. Which of the following statements is true about constructors in Java?
a) A constructor can return a value.
b) A constructor can be private.
c) A constructor must be declared public.
d) A constructor must have a return type.
Answer: b) A constructor can be private.
Constructors can be private, which is often used in the singleton pattern or to prevent instantiation from outside the class.
Q8. Which keyword is used to prevent method overriding in Java?
a) final
b) static
c) private
d) protected
Answer: a) final
The final
keyword prevents method overriding by making a method non-overridable.
Q9. Which of the following is a method of the Math
class in Java?
a) sqrt()
b) absolute()
c) pow()
d) All of the above
Answer: d) All of the above
Explanation: The Math
class contains methods like sqrt()
, absolute()
, and pow()
for mathematical operations.
Q10. Which of these interfaces does not belong to the Java Collection Framework?
a) List
b) Set
c) Queue
d) Map
Answer: d) MapMap
is not part of the Collection
framework directly. It is a separate interface for key-value pairs.
Q11. What is the size of a char in Java?
a) 8 bits
b) 16 bits
c) 32 bits
d) 64 bits
Answer: b) 16 bits
In Java, the char
data type takes 16 bits (2 bytes) and represents Unicode characters.
Q12. Which statement is used to exit a loop in Java?
a) stop
b) exit
c) break
d) continueAnswer: c) break
The break
statement is used to exit a loop prematurely.
Q13. Which of the following access modifiers allows visibility across the entire project in Java?
a) public
b) private
c) protected
d) default
Answer: a) publicpublic
allows the class, method, or variable to be accessible anywhere within the project.
Q14. What is the purpose of the super
keyword in Java?
a) To refer to the current class
b) To refer to the parent class
c) To invoke the default constructor
d) To stop method overridingAnswer: b) To refer to the parent class
Explanation: The super
keyword refers to the immediate parent class and can be used to access methods or variables of the parent class.
Q15. Which of the following is not an exception class in Java?
a) IOException
b) ArithmeticException
c) NullPointerException
d) InputMismatchException
Answer: d) InputMismatchExceptionInputMismatchException
is a runtime exception, but it’s not one of the common exception classes like IOException
or ArithmeticException
.
Q16. Which of the following is a valid declaration of a long variable in Java?
a) long x = 100L;
b) long x = 100;
c) long x = 100.0;
d) long x = “100”;
Answer: a) long x = 100L;
When declaring a long
variable, you must append L
or l
to the value to indicate it’s a long literal.
Q17. Which method must be implemented by all threads in Java?
a) start()
b) run()
c) execute()
d) stop()
Answer: b) run()
The run()
method contains the code that will be executed by a thread. start()
is used to begin thread execution.
Q18. What will be the output of the following code?javaCopy codeint a = 10; int b = 5; System.out.println(a / b);
a) 1
b) 2
c) 5
d) 10
Answer: b) 2
The division of 10 / 5
results in 2
since both a
and b
are integers.
Q19. Which of the following is a valid way to declare a constant in Java?
a) const int x = 10;
b) final int x = 10;
c) int const x = 10;
d) static int x = 10;
Answer: b) final int x = 10;
The final
keyword is used to declare a constant, which means the value cannot be modified after initialization.
Q20. Which of the following statements is true regarding the final
keyword in Java?
a) The final
keyword can be applied to variables, methods, and classes.
b) The final
keyword can only be used for methods.
c) The final
keyword only works with classes.
d) The final
keyword is used to restrict access modifiers.
Answer: a) The final
keyword can be applied to variables, methods, and classes.
Final
can be applied to variables (making them constants), methods (preventing overriding), and classes (preventing inheritance).
Q21. Which of these collections stores unique elements in Java?
a) List
b) Set
c) Queue
d) Map
Answer: b)
Set A Set
in Java is a collection that ensures all its elements are unique. Unlike List
, it does not allow duplicates.
Q22. What does the Thread.sleep()
method do in Java?
a) Pauses the current thread for a specified time
b) Stops the thread permanently
c) Creates a new thread
d) Terminates the program
Answer: a) Pauses the current thread for a specified time
The Thread.sleep()
method pauses the execution of the current thread for the specified amount of time (in milliseconds).
Q23. Which of the following is used to handle exceptions in Java?
a) try-catch block
b) if-else block
c) switch-case block
d) for-each loop
Answer: a) try-catch block
Java uses the try-catch
block to handle exceptions. Code that might throw exceptions is placed inside the try
block.
Q24. Which method is used to start a thread in Java?
a) start()
b) run()
c) execute()
d) launch()
Answer: a) start()
The start()
method is used to begin the execution of a thread. It internally calls the run()
method.
Q25. What is the result of dividing an integer by zero in Java?
a) Throws ArithmeticException
b) Returns 0
c) Returns infinity
d) No result
Answer: a) Throws ArithmeticException
Dividing by zero results in an ArithmeticException
in Java.
Q26. What is the default value of an instance variable of type int in Java?
a) 0
b) null
c) -1
d) undefined
Answer: a) 0
The default value of an instance variable of type int
is 0
.
Q27. What is the purpose of the instance of
keyword in Java?
a) To check if an object is an instance of a specific class
b) To create an instance of a class
c) To clone an object
d) To check for null references
Answer: a) To check if an object is an instance of a specific class
The instance of
keyword is used to check whether an object is an instance of a particular class or subclass.
Q28. Which method is used to compare two strings in Java?
a) compare()
b) equals()
c) equalsIgnoreCase()
d) compareTo()
Answer: b) equals()
The equals()
method is used to compare the content of two strings in Java.
Q29. Which of the following is not an access modifier in Java?
a) public
b) private
c) protected
d) static
Answer: d) staticStatic
is not an access modifier. It defines the scope of a variable or method but doesn’t control visibility.
Q30. What is the result of using the ==
operator to compare two strings in Java?
a) Compares the content of strings
b) Compares the memory addresses of the strings
c) Compares the length of strings
d) Throws a compile-time error
Answer: b) Compares the memory addresses of the strings
The ==
operator compares memory addresses (references) of objects, not their content. Use equals()
to compare the content of strings.
Q31. Which of the following is a feature of Java that supports multiple threads?
a) Multithreading
b) Polymorphism
c) Inheritance
d) Encapsulation
Answer: a) Multithreading
Java supports multithreading, which allows multiple threads to run concurrently, enabling better utilization of CPU resources.
Q32. Which of these is the default value of a reference variable in Java?
a) 0
b) null
c) undefined
d) “” (empty string)
Answer: b) null
Reference variables in Java are initialized to null
by default if no value is explicitly assigned.
Q33. Which collection in Java stores elements in key-value pairs?
a) List
b) Set
c) Map
d) Queue
Answer: c) Map
The Map
interface stores data in key-value pairs. HashMap
and TreeMap
are popular implementations of this interface.
Q34. What is the output of the following code?javaCopy codeint a = 5; System.out.println(++a);
a) 5
b) 6
c) 7
d) 4
Answer: b) 6
The ++a
is a pre-increment operator, so the value of a
is incremented before it is printed.
Q35. What is the return type of the main
method in this language?
a) void
b) int
c) String
d) null
Answer: a) void
The main
method has a return type of void
because it doesn’t return anything.
Q36. Which of the following methods is used to sort an array in this language?
a) sort()
b) order()
c) Arrays.sort()
d) Collections.sort()
Answer: c) Arrays.sort()
The Arrays.sort()
method is used to sort arrays in Java.
Q37. Which exception is thrown when you try to access an element outside the bounds of an array?
a) Array Index Out Of Bounds Exception
b) Index Out Of Bounds Exception
c) Null Pointer Exception
d) Class Cast Exception
Answer: a) ArrayIndexOutOfBoundsException
The ArrayIndexOutOfBoundsException
is thrown when trying to access an index that is out of the array’s bounds.
Q38. Which keyword is used to create a subclass in Java?
a) extends
b) super
c) implements
d) inheritAnswer: a) extends
The extends
keyword is used to create a subclass by inheriting properties from a superclass.
Q39. What will be the output of the following code?javaCopy codeString str = "Hello"; str = str.concat("World"); System.out.println(str);
a) HelloWorld
b) Hello World
c) WorldHello
d) Error
Answer: a) HelloWorld
The concat()
method is used to concatenate strings, so the result is "HelloWorld"
.
Q40. Which method in the Thread
class is used to pause the thread for a specific time?
a) wait()
b) sleep()
c) pause()
d) stop()
Answer: b) sleep()
The sleep()
method is used to pause the thread for the specified time (in milliseconds).
Also Read: Means Of Transport Of Jammu and Kashmir for JKSSB,JKAS and other UT Exams
You may also like to read our another post: Awards & Honors 2024 MCQs for SSC, UPSC and other Exams
In The End
This collection of 40 Java MCQs includes all the essential topics that are crucial for both exams and interviews. Each question is supported by a detailed explanation to help understand the reasoning behind the answers. If you practice regularly with questions like these, you would not only improve your knowledge about this language but also develop your ability to handle many different challenges in both assessments and job interviews. Just keep revising and good luck!
Arun Kumar is a professional content writer with over 5 years of experience in the education niche. With a speciality for content creation that’s thought-provoking and engaging, Arun specializes in making study guides and resources for exams and curriculum-based content.
With a zeal for learning, Arun comes across as a vibrant educator who simplifies the most difficult concepts into information that is easily digested, enabling learners to achieve success academically. His commitment to accuracy and creativity ensures that every piece of content not only teaches but also inspires.
Arun is committed to providing high-quality impactful content that works well for different student and faculty requirements.