PART A: Multiple Choice Questions
1. A method that returns the value of a private field is called a: == getters
(a) Mutator (b) Constructor (c) Accessor (d) Modifier
Answer: (c) Accessor
2. A method that changes the value of a private field is called a: == setters
(a) Getter (b) Mutator (c) Constructor (d) Accessor
Answer: (b) Mutator
3. Constructor chaining in Java refers to:
(a) Calling a parent class method from a child class (b) Calling one constructor from
within another constructor (c) Chaining multiple method calls (d) Overriding constructors
in a subclass
Answer: (b) Calling one constructor from within another constructor
4. Which of the following is true about static methods?
(a) They can access instance fields directly (b) They require an object to be called (c)
They can only access static fields and methods (d) They must return void
Answer: (c) They can only access static fields and methods
5. Which Java class is an example of a utility class that uses static methods?
(a) Scanner (b) String (c) Math (d) ArrayList
Answer: (c) Math
6. When a subclass is instantiated, which constructor runs first?
(a) The subclass constructor (b) The static initializer (c) The superclass constructor (d)
The interface default method
Answer: (c) The superclass constructor
7. What is polymorphism in Java?
(a) Having multiple constructors in a class (b) The ability of a variable to hold objects of
different types (c) Overloading methods in a class (d) Using interfaces only
Answer: (b) The ability of a variable to hold objects of different types
8. A superclass reference variable can hold a reference to:
(a) Only superclass objects (b) Subclass objects (c) Interface objects directly (d) Static
objects only
Answer: (b) Subclass objects
9. Constructors are ________ by subclasses.
(a) Always inherited (b) Not inherited (c) Overridden (d) Overloaded automatically
Answer: (b) Not inherited
10. What happens if a subclass fails to override an abstract method?
(a) The method runs with default behavior (b) A runtime error occurs (c) A compiler
error occurs (d) The method is ignored
Answer: (c) A compiler error occurs.
11. Fields declared in an interface in JAVA, are automatically:
(a) private and non-static (b) public, final, and static (c) protected and mutable (d)
instance fields
Answer: (b) public, final, and static
12. Which of the following is a valid interface reference usage?
(a) RetailItem item = new RetailItem(); (b) RetailItem item = new CD(...); (c) abstract
RetailItem item; (d) final RetailItem item;
Answer: (b) RetailItem item = new CD(...);
13. Which of the following cannot be done with an interface reference variable?
(a) Reference an object of a class implementing the interface (b) Call methods declared
in the interface (c) Directly instantiate an interface object (d) Be passed as a
parameter
Answer: (c) Directly instantiate an interface object
14. Which keyword is used to define an enumerated type in Java?
(a) enum (b) Enum (c) enumerate (d) const
Answer: (a) enum
15. What does the ordinal() method of an enum return?
(a) The name of the constant (b) The zero-based position of the constant in the enum
(c) The hash code of the constant (d) The string value of the constant
Answer: (b) The zero-based position of the constant in the enum
16. Given: enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY}, what is the ordinal
of TUESDAY?
(a) 1 (b) 2 (c) 3 (d) 0
Answer: (b) 2
17. An enum in Java is a:
(a) Simple primitive type (b) Specialized class (c) Special type of array (d) Type of
interface
Answer: (b) Specialized class
18. Which enum method compares two enum constants by their ordinal values?
(a) equals() (b) toString() (c) compareTo() (d) ordinal()
Answer: (c) compareTo()
19. Which method is used to retrieve an element at a specific index in an ArrayList?
(a) fetch() (b) retrieve() (c) get() (d) element()
Answer: (c) get()
20. Which ArrayList method replaces an existing element at a given index?
(a) add() (b) replace() (c) update() (d) set()
Answer: (d) set()
21. After: ArrayList<String> list = new ArrayList<>(); [Link]("A"); [Link]("B"); [Link](0);
what does [Link](0) return?
(a) "A" (b) null (c) "B" (d) IndexOutOfBoundsException
Answer: (c) "B"
22. Which method returns the number of elements currently in an ArrayList?
(a) length() (b) count() (c) capacity() (d) size()
Answer: (d) size()
23. The ArrayList class differs from arrays because it:
(a) Can only store String types (b) Automatically resizes as elements are added or
removed (c) Is faster than an array (d) Uses a fixed size set at declaration
Answer: (b) Automatically resizes as elements are added or removed
24. To insert an element at index 2 in an ArrayList called 'list', you write:
(a) [Link](2, "X") (b) [Link](2, "X") (c) [Link](2, "X") (d) [Link](2, "X")
Answer: (c) [Link](2, "X")
25. Which of the following creates an ArrayList of BankAccount objects?
(a) ArrayList<BankAccount> list = new ArrayList(); (b) ArrayList<BankAccount> list =
new ArrayList<BankAccount>(); (c) BankAccount[] list = new ArrayList<BankAccount>();
(d) ArrayList list = new BankAccount();
Answer: (b) ArrayList<BankAccount> list = new ArrayList<BankAccount>();
26. The ArrayList's toString() method returns:
(a) The memory address of the list (b) Only the first element (c) A string representation
of all elements in the list (d) The size of the list
Answer: (c) A string representation of all elements in the list
PART B: True / False Questions
27. Method overriding can occur within the same class without any inheritance.
Answer: FALSE — Overriding requires an inheritance relationship between
classes.
28. Static methods can access instance (non-static) variables directly.
Answer: FALSE — Static methods can only directly access static members.
29. A subclass inherits constructors from its superclass.
Answer: FALSE — Constructors are not inherited.
30. The ordinal of the first constant in an enum is 1.
Answer: FALSE — The ordinal starts at 0.
31. Default methods in interfaces (Java 8+) have a method body.
Answer: TRUE
PART C: Essay & Tracing Questions
Q1: Explain the four pillars of Object-Oriented Programming (OOP) and relate each to
a Java concept covered in this course.
Model Answer:
1. Encapsulation: Hiding internal state using private fields and exposing them through
public getters/setters (accessors/mutators). Example: Rectangle class with private width
and public getWidth().
2. Inheritance: A subclass (derived class) inherits fields and methods from a superclass
(base class) using the 'extends' keyword. Example: Employee extends Person.
3. Polymorphism: A superclass reference can point to subclass objects; overriding
methods provide different behaviors. Example: different get_salary() implementations in
SalariedEmployee and HourlyEmployee.
4. Abstraction: Hiding implementation details using abstract classes and interfaces.
Example: abstract class with abstract method GetSalary(), or interface RetailItem.
Q2: What is the difference between an abstract class and an interface? When would
you use each?
Model Answer:
Abstract Class: Can have both abstract and concrete methods, instance variables, and
constructors. A class can extend only ONE abstract class.
Interface: All methods are abstract by default (before Java 8). Fields are public, static, and
final. A class can implement MULTIPLE interfaces.
Use abstract class when: classes share common code/state that should be inherited (e.g.,
all Employee types share a name field).
Use interface when: you want to define a contract that unrelated classes must fulfill (e.g.,
RetailItem implemented by both CD and Book).
Key rule: Java supports single inheritance for classes but allows multiple interface
implementation.
Q3: Trace the following code and write the exact output:
Model Answer:
public class Counter {
static int x = 0;
Counter() { x++; }
void display() { [Link](x); }
public static void main(String[] args) {
Counter c1 = new Counter(); [Link]();
Counter c2 = new Counter(); [Link]();
Counter c3 = new Counter(); [Link]();
[Link]();
[Link]();
} }
Output:
1 ← After c1 created, x becomes 1
2 ← After c2 created, x becomes 2
3 ← After c3 created, x becomes 3
3 ← After c1 created, x becomes 1
3 ← After c2 created, x becomes 2
Explanation: x is a static variable shared by all objects. Each constructor call increments
x once.
Q4: Write a complete Java program that defines an interface 'Shape' with abstract method
getArea(), then implement it in a class 'Circle' with radius field.
Model Answer:
interface Shape {
double getArea();
}
class Circle implements Shape {
private double radius;
public Circle(double r) { [Link] = r; }
@Override
public double getArea() { return [Link] * radius * radius; }
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(5.0);
[Link]("Area: " + [Link]()); } }
Output: Area: 78.53981633974483
Note: A Shape reference variable holds a Circle object — demonstrating interface
polymorphism.
Q5: Trace the following ArrayList code and write the contents of the list after each step:
Model Answer:
ArrayList<String> list = new ArrayList<>();
[Link]("Alice"); // Step 1
[Link]("Bob"); // Step 2
[Link]("Charlie"); // Step 3
[Link](1); // Step 4
[Link](1, "David"); // Step 5
[Link](0, "Eve"); // Step 6
[Link](list); // Step 7
Step 1: [Alice]
Step 2: [Alice, Bob]
Step 3: [Alice, Bob, Charlie]
Step 4: [Alice, Charlie] ← index 1 (Bob) removed
Step 5: [Alice, David, Charlie] ← David inserted at index 1
Step 6: [Eve, David, Charlie] ← Alice replaced by Eve at index 0
Step 7 Output: [Eve, David, Charlie]