Introduction

Java is one of the most widely used object-oriented programming languages in competitive tests, job interviews, and academic examinations across Pakistan. Whether you are preparing for PPSC Lecturer Computer Science, FPSC, NTS, or general software engineering exams, mastering Java syntax, OOP pillars, and output-based programming logic is essential. This high-yield collection brings you 50 top-rated Java MCQs with correct answers and detailed explanations.

Section 1: Java Fundamentals & Syntax

Q1. Who is known as the father of Java programming language?

A) Dennis Ritchie

B) James Gosling

C) Bjarne Stroustrup

D) Guido van Rossum

Correct Answer: B) James Gosling

Explanation: James Gosling designed and developed Java at Sun Microsystems in the early 1990s.

Q2. Which of the following is not a valid Java keyword?

A) static

B) Boolean

C) void

D) private

Correct Answer: B) Boolean

Explanation: Keywords in Java are case-sensitive. boolean is a primitive keyword, while Boolean with a capital ‘B’ is a wrapper class.

Q3. What is the extension of compiled Java bytecode files?

A) .java

B) .js

C) .class

D) .exe

Correct Answer: C) .class

Explanation: The Java compiler (javac) compiles .java source code into platform-independent .class bytecode files.

Q4. Which component is directly responsible for executing Java bytecode?

A) JDK

B) JRE

C) JVM

D) JIT

Correct Answer: C) JVM

Explanation: The Java Virtual Machine (JVM) interprets and executes Java bytecode on specific host operating systems.

Q5. Which data type is used to store boolean truth values in Java?

A) int

B) float

C) boolean

D) char

Correct Answer: C) boolean

Explanation: The boolean data type stores either true or false.

Q6. What will be the output of System.out.println(10 + 20 + "Java");?

A) 1020Java

B) 30Java

C) Java30

D) Compilation Error

Correct Answer: B) 30Java

Explanation: Operations run left-to-right. 10 + 20 evaluates to 30, which is then concatenated with "Java".

Q7. What will be the output of System.out.println("Java" + 10 + 20);?

A) Java30

B) Java1020

C) 30Java

D) Error

Correct Answer: B) Java1020

Explanation: Since string concatenation starts first, subsequent numbers are converted to strings: "Java10" + 20 = "Java1020".

Q8. Which loop is guaranteed to execute its code block at least once?

A) for loop

B) while loop

C) do-while loop

D) foreach loop

Correct Answer: C) do-while loop

Explanation: A do-while loop checks its condition at the end of the block, ensuring at least one execution.

Q9. Which statement is used to skip the remaining code in the current loop iteration and move to the next?

A) break

B) continue

C) return

D) exit

Correct Answer: B) continue

Explanation: continue skips the current loop pass and jumps directly to the next iteration evaluation.

Q10. What is the default value of an uninitialized local variable in Java?

A) 0

B) null

C) false

D) No default value (causes compilation error)

Correct Answer: D) No default value (causes compilation error)

Explanation: Local variables inside methods must be explicitly initialized before usage.

Section 2: Object-Oriented Programming (OOP) in Java

Q11. Which principle of OOP combines data variables and methods into a single unit?

A) Inheritance

B) Polymorphism

C) Encapsulation

D) Abstraction

Correct Answer: C) Encapsulation

Explanation: Encapsulation hides state variables behind private access and exposes them through public getter/setter methods.

Q12. Which keyword is used by a child class to inherit a parent class in Java?

A) implements

B) extends

C) super

D) inherits

Correct Answer: B) extends

Explanation: extends creates a class hierarchy inheritance relationship in Java.

Q13. Does Java support multiple class inheritance directly (class C extends A, B)?

A) Yes

B) No

C) Only for abstract classes

D) Only in main method

Correct Answer: B) No

Explanation: Java avoids multiple class inheritance to eliminate diamond ambiguity issues, supporting it via interfaces instead.

Q14. What is method overloading?

A) Methods with the same name but different parameters in the same class

B) Methods with the same name and parameters in parent and child classes

C) Creating multiple constructors in an interface

D) Overwriting private methods

Correct Answer: A) Methods with the same name but different parameters in the same class

Explanation: Method overloading represents compile-time (static) polymorphism.

Q15. Which keyword refers to the immediate parent class object?

A) this

B) super

C) base

D) parent

Correct Answer: B) super

Explanation: super is used to invoke parent methods, constructors (super()), or access parent attributes.

Q16. What is method overriding?

A) Declaring two methods with different names

B) Redefining a parent class method in a child class with the exact same signature

C) Calling static methods inside loops

D) Passing variables into constructors

Correct Answer: B) Redefining a parent class method in a child class with the exact same signature

Explanation: Method overriding implements runtime (dynamic) polymorphism.

Q17. Can a constructor be overridden in Java?

A) Yes

B) No

C) Only if public

D) Only in abstract classes

Correct Answer: B) No

Explanation: Constructors belong specifically to their declaring class and cannot be inherited or overridden.

Q18. What is the return type of a constructor in Java?

A) void

B) int

C) Object

D) No return type (not even void)

Correct Answer: D) No return type (not even void)

Explanation: Java constructors do not declare any return type.

Q19. Which keyword makes a variable constant so its value cannot be reassigned?

A) static

B) final

C) abstract

D) volatile

Correct Answer: B) final

Explanation: A final primitive variable cannot be reassigned once initialized.

Q20. What is a static variable in Java?

A) A variable local to a loop

B) A variable shared across all object instances of a class

C) A temporary stack variable

D) A constant array index

Correct Answer: B) A variable shared across all object instances of a class

Explanation: Static attributes belong to class memory rather than individual instances.

Section 3: Strings, Arrays & Code Logic

Q21. Which property/attribute is used to check the number of elements in a Java array?

A) length()

B) size()

C) length

D) getSize()

Correct Answer: C) length

Explanation: Arrays use the .length field, while Collections use .size() and Strings use .length().

Q22. Which class creates mutable (modifiable) string objects?

A) String

B) StringBuilder

C) StringLiteral

D) ConstantString

Correct Answer: B) StringBuilder

Explanation: String objects are immutable in Java, whereas StringBuilder and StringBuffer permit modification.

Q23. What will String s = "Java"; s.concat(" World"); System.out.println(s); output?

A) Java World

B) Java

C) World

D) Compilation Error

Correct Answer: B) Java

Explanation: Because String is immutable, s.concat() creates a new string but does not update s unless reassigned (s = s.concat(...)).

Q24. What is the index of the first element in a Java array?

A) -1

B) 0

C) 1

D) Variable

Correct Answer: B) 0

Explanation: Array indexing in Java is zero-based.

Q25. Which operator is used to compare whether two object references point to the exact same memory location?

A) .equals()

B) ==

C) ===

D) equalsIgnoreCase()

Correct Answer: B) ==

Explanation: == compares reference memory addresses, while .equals() checks content equality.

Q26. What is the output of System.out.println(5 / 2); in Java?

A) 2.5

B) 2

C) 2.0

D) Error

Correct Answer: B) 2

Explanation: Integer division truncates decimal remainders, resulting in 2.

Q27. What is the output of System.out.println(5.0 / 2);?

A) 2

B) 2.5

C) 2.0

D) Error

Correct Answer: B) 2.5

Explanation: Floating-point division preserves decimal precision when at least one operand is double/float.

Q28. What does int x = 5; System.out.println(x++); print?

A) 5

B) 6

C) 0

D) Error

Correct Answer: A) 5

Explanation: Post-increment (x++) evaluates the current value (5) first before incrementing x to 6.

Q29. What does int x = 5; System.out.println(++x); print?

A) 5

B) 6

C) 0

D) Error

Correct Answer: B) 6

Explanation: Pre-increment (++x) increases the value first before evaluation.

Q30. Which keyword is used to explicitly throw an exception object inside a method?

A) throws

B) throw

C) catch

D) try

Correct Answer: B) throw

Explanation: throw raises an exception instance, whereas throws declares potential exceptions in method signatures.

Section 4: Advanced Java & Abstract Classes

Q31. Which block in exception handling always executes regardless of whether an exception occurs?

A) try

B) catch

C) finally

D) throw

Correct Answer: C) finally

Explanation: The finally block is executed for cleanup operations whether an exception is caught or not.

Q32. What is the ultimate parent root class of all classes in Java?

A) java.lang.Class

B) java.lang.Object

C) java.lang.System

D) java.lang.Root

Correct Answer: B) java.lang.Object

Explanation: Every class implicitly extends java.lang.Object.

Q33. Which class cannot be instantiated using the new operator directly?

A) Public class

B) Abstract class

C) Final class

D) Concrete class

Correct Answer: B) Abstract class

Explanation: Abstract classes are incomplete blueprints and cannot be directly instantiated.

Q34. Which keyword stops a class from being inherited by any subclass?

A) static

B) final

C) abstract

D) private

Correct Answer: B) final

Explanation: Marking a class final prevents inheritance.

Q35. How many interfaces can a single Java class implement?

A) Only 1

B) Only 2

C) Unlimited (Multiple)

D) None

Correct Answer: C) Unlimited (Multiple)

Explanation: Java supports multiple interface implementation using commas (implements A, B, C).

Q36. Which package is automatically imported into every Java source file?

A) java.util

B) java.io

C) java.lang

D) java.net

Correct Answer: C) java.lang

Explanation: java.lang contains essential classes like Object, String, Math, and System.

Q37. Which memory region holds dynamically allocated objects in Java?

A) Stack

B) Heap

C) Code segment

D) Register

Correct Answer: B) Heap

Explanation: All objects instantiated with new reside in Heap memory.

Q38. What does local variable reference data reside on during method execution?

A) Heap

B) Stack

C) Disk

D) Metaspace

Correct Answer: B) Stack

Explanation: Method call frames and local primitive/reference variables are stored on the Stack.

Q39. Which interface must be implemented to define a task thread in Java?

A) Threadable

B) Runnable

C) Executable

D) Process

Correct Answer: B) Runnable

Explanation: Implementing Runnable requires providing the run() method logic.

Q40. Which method starts thread execution in Java?

A) run()

B) start()

C) execute()

D) init()

Correct Answer: B) start()

Explanation: Calling .start() creates a new call stack and triggers the thread’s .run() method.

Section 5: Collections, Modifiers & Logic (41-50)

Q41. Which collection class maintains insertion order and allows duplicate elements?

A) HashSet

B) TreeSet

C) ArrayList

D) HashMap

Correct Answer: C) ArrayList

Explanation: ArrayList stores ordered sequences and permits duplicate items.

Q42. Which collection does not allow duplicate elements?

A) ArrayList

B) LinkedList

C) HashSet

D) Vector

Correct Answer: C) HashSet

Explanation: Sets enforce unique elements.

Q43. Which access modifier restricts access strictly to the declaring class?

A) public

B) protected

C) private

D) default

Correct Answer: C) private

Explanation: private limits access strictly within the outer enclosing class body.

Q44. What is the default access level if no modifier is specified?

A) Public

B) Private

C) Package-Private (Default)

D) Protected

Correct Answer: C) Package-Private (Default)

Explanation: Unmarked members are accessible only within the same package.

Q45. Which class is commonly used to read keyboard input from users?

A) Printer

B) Scanner

C) Reader

D) Input

Correct Answer: B) Scanner

Explanation: java.util.Scanner parses primitive types and strings from input streams.

Q46. What is the default value of a boolean instance variable in a class?

A) true

B) false

C) null

D) 0

Correct Answer: B) false

Explanation: Instance boolean fields default to false.

Q47. What does the toString() method in Object class return by default?

A) Empty string

B) ClassName@HashCodeHex

C) Variable names

D) Null

Correct Answer: B) ClassName@HashCodeHex

Explanation: The default implementation prints class name followed by the hash code hex string.

Q48. Which exception is thrown when attempting to divide an integer by zero in Java?

A) NullPointerException

B) ArithmeticException

C) NumberFormatException

D) IllegalArgumentException

Correct Answer: B) ArithmeticException

Explanation: Integer division by zero triggers runtime ArithmeticException.

Q49. Which exception occurs when accessing an array with an invalid index?

A) NullPointerException

B) ArrayIndexOutOfBoundsException

C) ClassCastException

D) IndexNotFoundException

Correct Answer: B) ArrayIndexOutOfBoundsException

Explanation: Accessing negative or out-of-range array indices causes this runtime exception.

Q50. Which utility class provides static methods for sorting and searching arrays?

A) java.util.Collections

B) java.util.Arrays

C) java.lang.Math

D) java.util.List

Correct Answer: B) java.util.Arrays

Explanation: java.util.Arrays offers utility methods like Arrays.sort() and Arrays.binarySearch().

Conclusion

Mastering these 50 Java MCQs gives you a solid foundation in core programming logic, syntax, OOP principles, and exception handling for competitive exams like PPSC, FPSC, and NTS. Regular revision of coding mechanics and theory will ensure maximum scoring potential.

Related Subject-wise MCQs:

Leave a Comment

Your email address will not be published.

Scroll to Top