0% found this document useful (0 votes)
3 views28 pages

Java OOP Practice Questions

The document is a comprehensive guide to Java Object-Oriented Programming (OOP) that includes practice questions organized by chapter, covering topics such as classes, objects, encapsulation, inheritance, and polymorphism. It features multiple-choice questions, coding exercises, and tricky questions to test knowledge and understanding of OOP concepts. The material is designed to help learners build confidence and prepare for exams in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Java OOP Practice Questions

The document is a comprehensive guide to Java Object-Oriented Programming (OOP) that includes practice questions organized by chapter, covering topics such as classes, objects, encapsulation, inheritance, and polymorphism. It features multiple-choice questions, coding exercises, and tricky questions to test knowledge and understanding of OOP concepts. The material is designed to help learners build confidence and prepare for exams in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

?

Java OOP
Practice Questions
Chapter-wise MCQ • Theory • Coding • Tricky

15 90+ 4
Chapters Questions Question Types

Companion to: Java OOP Complete Guide

Test Your Knowledge • Build Confidence • Exam Ready

Java Programming Series • 2025 Edition

Java OOP Practice Questions • Page 1


TABLE OF CONTENTS

1 Introduction to OOP 4 MCQ + 2 Theory

2 Classes and Objects 3 MCQ + 2 Coding + 1 Tricky

3 Constructors 3 MCQ + 1 Coding + 1 Tricky

4 The this Keyword 3 MCQ + 1 Theory + 1 Tricky

5 Encapsulation 3 MCQ + 2 Coding + 1 Theory

6 Access Modifiers 4 MCQ + 1 Tricky

7 Inheritance 4 MCQ + 1 Coding + 1 Theory

8 The super Keyword 3 MCQ + 1 Coding + 1 Tricky

9 Polymorphism 4 MCQ + 1 Coding + 1 Tricky

10 Abstraction 3 MCQ + 1 Theory + 1 Tricky

11 Interfaces 4 MCQ + 1 Theory

12 Static Keyword 3 MCQ + 1 Coding

13 Final Keyword 3 MCQ + 1 Tricky

14 Upcasting & Downcasting 3 MCQ + 1 Tricky

15 Grand Mixed Test 3 MCQ + 2 Theory + 2 Coding + 2 Tricky

Java OOP Practice Questions • Page 2


Introduction to OOP
1 What is OOP? The Four Pillars

■ Multiple Choice

Q1. What does OOP stand for?


A) Object Oriented Procedure
B) Object Oriented Programming
C) Ordered Object Protocol
D) Optional Object Processing
✔ Answer: B) Object Oriented Programming

Q2. Which of the following is NOT one of the four pillars of OOP?
A) Encapsulation
B) Inheritance
C) Compilation
D) Abstraction
✔ Answer: C) Compilation
■ The four pillars are Encapsulation, Inheritance, Polymorphism, Abstraction.

Q3. In OOP, which term refers to bundling data and methods together?
A) Abstraction
B) Polymorphism
C) Encapsulation
D) Inheritance
✔ Answer: C) Encapsulation

Q4. Which pillar of OOP allows one class to acquire properties of another?
A) Abstraction
B) Encapsulation
C) Polymorphism
D) Inheritance
✔ Answer: D) Inheritance

■ Theory

Q5. What is the difference between procedural programming and OOP?


■ Procedural: code is a sequence of instructions executed top to bottom.
■ OOP: code is organized around objects with data and behavior.
■ OOP is more modular, reusable, and closer to real-world modeling.

Q6. Name and briefly explain the four pillars of OOP.


■ Encapsulation — hides data using private fields + public methods.

Java OOP Practice Questions • Page 3


■ Inheritance — child class reuses parent class code via extends.
■ Polymorphism — same method name behaves differently (overload/override).
■ Abstraction — hides implementation, shows only what is needed.

Java OOP Practice Questions • Page 4


Classes and Objects
2 Blueprints, Instances, and Memory

■ Multiple Choice

Q7. A class in Java is best described as:


A) A running program
B) A blueprint for creating objects
C) A method that returns data
D) A variable type
✔ Answer: B) A blueprint for creating objects

Q8. Which keyword is used to create an object in Java?


A) create
B) object
C) new
D) make
✔ Answer: C) new

Q9. What does the reference variable hold?


A) The actual object data
B) The class name
C) The memory address of the object
D) The method names
✔ Answer: C) The memory address of the object

■ Coding

Q10. What is the output of the following code?


class Car {
String brand;
void show() {
[Link](brand);
}
}
Car c = new Car();
[Link] = "Toyota";
[Link]();

✔ Expected Output: Toyota

Q11. Write a class 'Book' with fields title and price, and a method display() that prints both.
Create an object and call display().
// Write your answer here

Java OOP Practice Questions • Page 5


■ Declare the class, add a constructor or set fields directly, then call display().

■ Tricky

Q12. ■ TRICKY — How many objects are created by the following code?
Student s1, s2;
s1 = new Student();
s2 = s1;
A) 3 B) 2 C) 1 D) 0
✔ Answer: C) 1
■ Only ONE object is created with 'new Student()'. s2 = s1 just copies the reference — both s1 and s2 point
to the SAME object.

Java OOP Practice Questions • Page 6


Constructors
3 Object Initialization Deep Dive

■ Multiple Choice

Q13. Which statement about constructors is TRUE?


A) A constructor must have a return type
B) A constructor name can differ from class name
C) A constructor is called automatically when an object is created
D) A constructor cannot be overloaded
✔ Answer: C) A constructor is called automatically when an object is created

Q14. What type of constructor takes no arguments?


A) Parameterized Constructor
B) Static Constructor
C) Default Constructor
D) Copy Constructor
✔ Answer: C) Default Constructor

Q15. What will happen if you write: void Student() { } inside a class?
A) It becomes the default constructor
B) It is treated as a regular method, not a constructor
C) It causes a compile error
D) It overrides the constructor
✔ Answer: B) It is treated as a regular method, not a constructor
■ Constructors have NO return type, not even void.

■ Coding

Q16. What is the output?


class Box {
int size;
Box() { size = 10; }
Box(int s) { size = s; }
}
Box b1 = new Box();
Box b2 = new Box(25);
[Link]([Link] + " " + [Link]);

✔ Expected Output: 10 25

■ Tricky

Java OOP Practice Questions • Page 7


Q17. ■ TRICKY — If a class has a parameterized constructor but NO default constructor,
what happens when you call 'new Student()'?
A) Java auto-generates a default constructor
B) Compile error — no matching constructor found
C) Runtime error
D) Object is created with null values
✔ Answer: B) Compile error
■ Java only auto-generates a default constructor if you write NO constructors at all. Once you define any
constructor, Java stops providing the default.

Java OOP Practice Questions • Page 8


The this Keyword
4 Self-Reference and Disambiguation

■ Multiple Choice

Q18. What does the 'this' keyword refer to in Java?


A) The parent class
B) The current object
C) The static method
D) The interface
✔ Answer: B) The current object

Q19. When is 'this' keyword most commonly used?


A) To call a static method
B) To distinguish between instance variable and parameter with the same name
C) To inherit from parent class
D) To create a new object
✔ Answer: B) To distinguish between instance variable and parameter with the same name

Q20. What is the rule for using this() to call another constructor?
A) It can be anywhere in the constructor
B) It must be the last statement
C) It must be the first statement
D) It can only be used in static methods
✔ Answer: C) It must be the first statement

■ Theory

Q21. Explain with an example why 'this' is needed in a constructor.


■ When parameter name == field name, Java cannot distinguish them.
■ [Link] refers to the field; name refers to the parameter.
■ Without this, the field would never get assigned the parameter value.

■ Tricky

Q22. ■ TRICKY — What is the output of the following?


class A {
int x;
A(int x) { this.x = x + 1; }
}
A obj = new A(5);
[Link](obj.x);
A) 5 B) 6 C) 0 D) Compile Error

Java OOP Practice Questions • Page 9


✔ Answer: B) 6
■ this.x = x + 1 means field x = parameter x + 1 = 5 + 1 = 6.

Java OOP Practice Questions • Page 10


Encapsulation
5 Data Hiding and Access Control

■ Multiple Choice

Q23. Which access modifier is used to implement encapsulation?


A) public
B) protected
C) private
D) static
✔ Answer: C) private

Q24. What are getter and setter methods used for?


A) To create objects
B) To provide controlled access to private fields
C) To inherit from parent class
D) To override methods
✔ Answer: B) To provide controlled access to private fields

Q25. Which real-world object is the best analogy for encapsulation?


A) A window
B) An ATM machine
C) A road
D) A tree
✔ Answer: B) An ATM machine
■ ATM hides internal banking logic; you only interact via deposit/withdraw buttons.

■ Coding

Q26. What error occurs and why?


class Person {
private int age;
}
Person p = new Person();
[Link] = 25; // What happens here?

✔ Expected Output: Compile Error: age has private access in Person


■ You must use a public setter method to set the age.

Q27. Fix the above code by adding a proper setter and getter for age.
// Write setAge(int age) and getAge() methods here

■ setAge should validate that age > 0 before assigning.

Java OOP Practice Questions • Page 11


■ Theory

Q28. List THREE advantages of Encapsulation.


■ 1. Data Security — private fields cannot be accessed or corrupted directly.
■ 2. Validation — setters can check values before assigning.
■ 3. Flexibility — internal implementation can change without breaking external code.

Java OOP Practice Questions • Page 12


Access Modifiers
6 Visibility and Scope

■ Multiple Choice

Q29. Which modifier allows access from anywhere?


A) private
B) protected
C) default
D) public
✔ Answer: D) public

Q30. Which modifier allows access only within the same class?
A) public
B) protected
C) private
D) default
✔ Answer: C) private

Q31. A method with 'protected' access can be accessed from:


A) Only same class
B) Same class and same package only
C) Same class, same package, and subclasses
D) Everywhere
✔ Answer: C) Same class, same package, and subclasses

Q32. What is the default access modifier if none is specified?


A) private
B) public
C) package-private (default)
D) protected
✔ Answer: C) package-private (default)
■ No keyword = default access = accessible only within the same package.

■ Tricky

Q33. ■ TRICKY — Which will cause a compile error if accessed from a completely different
package (not a subclass)?
A) public field
B) protected field
C) default field
D) Both B and C
✔ Answer: D) Both B and C

Java OOP Practice Questions • Page 13


■ protected is accessible in subclasses even across packages, but NOT in unrelated classes in other
packages. default is only within same package.

Java OOP Practice Questions • Page 14


Inheritance
7 Parent and Child Classes

■ Multiple Choice

Q34. Which keyword is used to inherit a class in Java?


A) implements
B) inherits
C) extends
D) super
✔ Answer: C) extends

Q35. What type of inheritance does Java NOT support through classes?
A) Single
B) Multilevel
C) Multiple
D) Hierarchical
✔ Answer: C) Multiple
■ Java avoids the Diamond Problem by not allowing a class to extend two classes.

Q36. In 'class Dog extends Animal', which is the parent class?


A) Dog
B) Animal
C) Both
D) Neither
✔ Answer: B) Animal

Q37. Which members are NOT inherited by a child class?


A) public methods
B) protected fields
C) private fields
D) public fields
✔ Answer: C) private fields

■ Coding

Q38. What is the output?


class Vehicle {
void start() { [Link]("Vehicle started"); }
}
class Bike extends Vehicle {
void ride() { [Link]("Bike riding"); }
}

Java OOP Practice Questions • Page 15


Bike b = new Bike();
[Link]();
[Link]();

✔ Expected Output: Vehicle started Bike riding

■ Theory

Q39. Explain the IS-A relationship with two examples.


■ IS-A = the child class IS a type of the parent class.
■ Example 1: Dog IS-A Animal (Dog extends Animal).
■ Example 2: Manager IS-A Employee (Manager extends Employee).
■ This relationship justifies using the parent type for child object references.

Java OOP Practice Questions • Page 16


The super Keyword
8 Accessing Parent Members

■ Multiple Choice

Q40. What does super() do inside a child constructor?


A) Creates a new parent object
B) Calls the parent class constructor
C) Overrides the parent method
D) Declares a static variable
✔ Answer: B) Calls the parent class constructor

Q41. Where must super() appear in a constructor?


A) Last statement
B) Anywhere
C) First statement
D) Inside an if block
✔ Answer: C) First statement

Q42. [Link]() is used to:


A) Call a static method
B) Call the parent class version of display()
C) Create a parent object
D) Override display()
✔ Answer: B) Call the parent class version of display()

■ Coding

Q43. What is the output?


class A {
A() { [Link]("A created"); }
}
class B extends A {
B() { super(); [Link]("B created"); }
}
new B();

✔ Expected Output: A created B created

■ Tricky

Q44. ■ TRICKY — If you do NOT write super() in the child constructor, what happens?
A) Compile error always
B) Java automatically inserts super() as the first statement

Java OOP Practice Questions • Page 17


C) The parent constructor is never called
D) Runtime NullPointerException
✔ Answer: B) Java automatically inserts super() as the first statement
■ Java implicitly calls the no-arg super() if you don't write it. But if the parent has NO default constructor,
you MUST explicitly call super(args) — or you get a compile error.

Java OOP Practice Questions • Page 18


Polymorphism
9 Overloading and Overriding

■ Multiple Choice

Q45. Method overloading is resolved at:


A) Runtime
B) Compile time
C) Link time
D) Load time
✔ Answer: B) Compile time
■ That is why it is also called Compile-Time Polymorphism.

Q46. Method overriding is resolved at:


A) Compile time
B) Runtime
C) Declaration time
D) Object creation time
✔ Answer: B) Runtime

Q47. Which annotation is used when overriding a method?


A) @Static
B) @Super
C) @Override
D) @Final
✔ Answer: C) @Override

Q48. Which is an example of method overloading?


A) Child class redefines parent method with same signature
B) Same method name with different number of parameters
C) Using super to call parent method
D) Abstract method in child class
✔ Answer: B) Same method name with different number of parameters

■ Coding

Q49. What is the output?


class Animal { void sound() { [Link]("..."); } }
class Dog extends Animal {
@Override
void sound() { [Link]("Woof"); }
}
Animal a = new Dog();

Java OOP Practice Questions • Page 19


[Link]();

✔ Expected Output: Woof


■ Runtime polymorphism — JVM calls Dog's sound() because the actual object is Dog.

■ Tricky

Q50. ■ TRICKY — Can you override a static method in Java?


A) Yes, using @Override
B) No, static methods cannot be overridden
C) Yes, but only in subclasses
D) No, static methods cannot be inherited at all
✔ Answer: B) No, static methods cannot be overridden
■ Static methods belong to the class, not the object. They are hidden (method hiding) in subclasses, not
overridden. @Override on a static method causes a compile error.

Java OOP Practice Questions • Page 20


Abstraction
10 Abstract Classes and Methods

■ Multiple Choice

Q51. Which keyword declares an abstract class?


A) interface
B) abstract
C) virtual
D) override
✔ Answer: B) abstract

Q52. Can you create an object of an abstract class?


A) Yes
B) No
C) Only with new keyword
D) Only in same package
✔ Answer: B) No
■ 'new AbstractClass()' causes a compile error.

Q53. An abstract method in Java:


A) Has a complete body
B) Can only be static
C) Has no body and must be implemented by subclass
D) Is automatically called on object creation
✔ Answer: C) Has no body and must be implemented by subclass

■ Theory

Q54. What is the difference between an abstract class and a concrete class?
■ Abstract class: declared with abstract, may have abstract methods, cannot be instantiated.
■ Concrete class: all methods have bodies, can be instantiated with new.
■ Abstract class is a partial blueprint; concrete class is a complete one.

■ Tricky

Q55. ■ TRICKY — If a child class does NOT implement all abstract methods of the parent,
what happens?
A) Runtime error
B) Compile error
C) Methods are auto-implemented with empty body
D) No error
✔ Answer: B) Compile error

Java OOP Practice Questions • Page 21


■ The child class must either implement all abstract methods OR itself be declared abstract.

Java OOP Practice Questions • Page 22


Interfaces
11 Contracts and Multiple Inheritance

■ Multiple Choice

Q56. Which keyword is used to implement an interface?


A) extends
B) inherits
C) implements
D) uses
✔ Answer: C) implements

Q57. Can a class implement multiple interfaces?


A) No
B) Yes
C) Only two
D) Only if they are in same package
✔ Answer: B) Yes
■ This is how Java achieves multiple inheritance safely.

Q58. By default, methods in an interface are:


A) private and static
B) public and abstract
C) protected and final
D) private and abstract
✔ Answer: B) public and abstract

Q59. Variables declared in an interface are:


A) private instance variables
B) mutable class variables
C) public static final constants
D) protected variables
✔ Answer: C) public static final constants

■ Theory

Q60. What is the difference between an interface and an abstract class?


■ Interface: all methods abstract (Java 8+ allows default), no constructor, supports multiple.
■ Abstract class: can have concrete methods, has constructor, single inheritance.
■ Use interface for contract/capability; use abstract class for partial implementation.

Java OOP Practice Questions • Page 23


Static Keyword
12 Class-Level Members

■ Multiple Choice

Q61. A static variable is shared by:


A) Only one specific object
B) The main method only
C) All objects of the class
D) Only child classes
✔ Answer: C) All objects of the class

Q62. How do you call a static method?


A) Create an object first, then call
B) [Link]()
C) Using super keyword
D) Using this keyword
✔ Answer: B) [Link]()

Q63. Can a static method access a non-static instance variable?


A) Yes, always
B) No, not directly
C) Only with the super keyword
D) Only in the same class
✔ Answer: B) No, not directly
■ Static methods have no 'this' context — they don't belong to any specific object.

■ Coding

Q64. What is the output?


class Counter {
static int count = 0;
Counter() { count++; }
}
new Counter(); new Counter(); new Counter();
[Link]([Link]);

✔ Expected Output: 3

Java OOP Practice Questions • Page 24


Final Keyword
13 Immutability and Locking

■ Multiple Choice

Q65. A final variable in Java:


A) Can be changed once
B) Cannot be reassigned after initialization
C) Is automatically static
D) Can only hold integers
✔ Answer: B) Cannot be reassigned after initialization

Q66. A final method:


A) Can be overridden in child class
B) Cannot be called from outside
C) Cannot be overridden
D) Must be static
✔ Answer: C) Cannot be overridden

Q67. A final class:


A) Can be extended
B) Cannot be instantiated
C) Cannot be extended/inherited
D) Must implement an interface
✔ Answer: C) Cannot be extended/inherited
■ Example: String class in Java is final.

■ Tricky

Q68. ■ TRICKY — What is the output?


final int x = 10;
x = 20;
[Link](x);
A) 10 B) 20 C) Compile Error D) Runtime Error
✔ Answer: C) Compile Error
■ You cannot reassign a final variable. Once assigned, x = 10 forever.

Java OOP Practice Questions • Page 25


Upcasting & Downcasting
14 Type Conversion in Inheritance

■ Multiple Choice

Q69. Which statement correctly describes upcasting?


A) Explicit conversion from child to parent
B) Automatic conversion from child to parent
C) Explicit conversion from parent to child
D) Automatic conversion from parent to child
✔ Answer: B) Automatic conversion from child to parent

Q70. Which operator checks if an object is an instance of a class?


A) typeof
B) instanceof
C) isType
D) classOf
✔ Answer: B) instanceof

Q71. Downcasting requires:


A) No syntax change
B) Using super keyword
C) Explicit cast syntax: (ChildType) reference
D) Using new keyword
✔ Answer: C) Explicit cast syntax: (ChildType) reference

■ Tricky

Q72. ■ TRICKY — What exception can occur with incorrect downcasting?


A) NullPointerException
B) ClassCastException
C) ArrayIndexOutOfBoundsException
D) StackOverflowError
✔ Answer: B) ClassCastException
■ If you cast an object to a type it is not actually an instance of, Java throws ClassCastException at
runtime. Always use instanceof before downcasting.

Java OOP Practice Questions • Page 26


Grand Mixed Test
15 All Chapters — Final Review

■ Multiple Choice

Q73. Which of the following is a valid constructor declaration?


A) void Student() { }
B) int Student() { }
C) Student() { }
D) static Student() { }
✔ Answer: C) Student() { }
■ Constructors have the same name as the class and NO return type.

Q74. Which OOP concept is demonstrated when Animal a = new Dog() is written?
A) Encapsulation
B) Abstraction
C) Runtime Polymorphism
D) Method Overloading
✔ Answer: C) Runtime Polymorphism

Q75. Which of these is TRUE about interfaces?


A) They can have constructors
B) A class can implement only one interface
C) Interface variables are public static final
D) Interface methods are private by default
✔ Answer: C) Interface variables are public static final

■ Theory

Q76. Explain the difference between method overloading and method overriding in 3 points.
■ Overloading: same class, same name, different parameters, compile-time.
■ Overriding: parent and child class, same name and parameters, runtime.
■ Overloading = static polymorphism; Overriding = dynamic polymorphism.

Q77. When should you use an abstract class vs an interface?


■ Use abstract class: when classes share code (partial implementation needed).
■ Use interface: when unrelated classes need to follow the same contract.
■ Rule of thumb: IS-A with shared code → abstract class; CAN-DO contract → interface.

■ Coding

Q78. Write a complete program with: abstract class Shape with abstract method area(), class
Circle that extends Shape, constructor accepting radius, implementing area(). Create object

Java OOP Practice Questions • Page 27


and print area.
// Write your full program here

■ area() = [Link] * radius * radius. Use [Link] for pi value.

Q79. What is the output of this program?


class Parent {
static void show() { [Link]("Parent"); }
}
class Child extends Parent {
static void show() { [Link]("Child"); }
}
Parent obj = new Child();
[Link]();

✔ Expected Output: Parent


■ Static methods are NOT polymorphic. They use the reference type (Parent), not the actual object type
(Child). This is method HIDING, not overriding.

■ Tricky

Q80. ■ TRICKY — How many times is the Animal constructor called: new Dog() where Dog
extends Animal?
A) 0 B) 1 (Dog's only) C) 1 (Animal's only) D) 2 (both Animal and Dog)
✔ Answer: D) 2
■ When Dog() runs, Java calls super() which triggers Animal(). So BOTH constructors run — Animal first,
then Dog. This is constructor chaining.

Q81. ■ TRICKY — Can an abstract class have a constructor?


A) No — abstract classes cannot have constructors
B) Yes — but it can only be called via super() from a child
C) Yes — and it can be called with new AbstractClass()
D) Only if the constructor is static
✔ Answer: B) Yes — but it can only be called via super() from a child
■ Abstract classes CAN have constructors. They cannot be called with new directly, but child constructors
call them via super().

Java OOP Practice Questions • Page 28

You might also like