Java Programming — OOP in Java
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in OOP in Java
Q.21 Medium OOP in Java
What is the output of this code?
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(x++);
}
}
A 10
B 11
C Compilation error
D Runtime exception
Correct Answer:  A. 10
EXPLANATION

The post-increment operator (x++) returns the value before incrementing. So it prints 10, and then x becomes 11.

Take Test
Q.22 Medium OOP in Java
Consider a class hierarchy: Animal -> Dog -> Puppy. A Puppy reference can access which of the following?
A Only Puppy methods
B Puppy and Dog methods, but not Animal methods
C Puppy, Dog, and Animal methods
D Only methods declared as public
Correct Answer:  C. Puppy, Dog, and Animal methods
EXPLANATION

Through inheritance, a Puppy reference can access all non-private methods from Puppy, Dog, and Animal classes through the inheritance chain.

Take Test
Q.23 Medium OOP in Java
Which statement about Java interfaces is INCORRECT according to 2024 specifications?
A Interfaces can have static methods
B Interfaces can have default methods
C Interfaces can have private methods
D Interfaces can be instantiated directly
Correct Answer:  D. Interfaces can be instantiated directly
EXPLANATION

Interfaces cannot be instantiated directly. Since Java 8, interfaces can have static and default methods, and since Java 9, they can have private methods.

Take Test
Q.24 Medium OOP in Java
What is the correct way to call a parent class constructor from a child class?
A parent();
B super();
C this.parent();
D ParentClass();
Correct Answer:  B. super();
EXPLANATION

The super() keyword is used to call the parent class constructor. It must be the first statement in the child class constructor.

Take Test
Q.25 Medium OOP in Java
In Java, which of the following is true about abstract classes?
A An abstract class can have no abstract methods
B An abstract class must have all abstract methods
C An abstract class cannot have constructors
D An abstract class cannot have instance variables
Correct Answer:  A. An abstract class can have no abstract methods
EXPLANATION

An abstract class can have zero or more abstract methods. It can also have concrete methods, constructors, and instance variables.

Take Test
Advertisement
Q.26 Medium OOP in Java
What will happen if you try to override a final method in a subclass?
A It will compile and run successfully
B Compilation error will occur
C Runtime exception will be thrown
D It will override silently
Correct Answer:  B. Compilation error will occur
EXPLANATION

A final method cannot be overridden. Attempting to override it results in a compilation error: 'cannot override final method'.

Take Test
Q.27 Medium OOP in Java
Which of the following demonstrates proper method overloading in Java?
A public void display() { } and public int display() { }
B public void display(int a) { } and public void display(int b) { }
C public void display(int a) { } and public void display(String a) { }
D public void display() { } and public void display() { return 5; }
Correct Answer:  C. public void display(int a) { } and public void display(String a) { }
EXPLANATION

Method overloading requires different parameter types or number of parameters. Options A and D have different return types (not valid for overloading), and option B has the same signature with different parameter names (not overloading).

Take Test
Q.28 Easy OOP in Java
Can you instantiate an interface in Java?
A Yes, directly with new keyword
B No, interfaces cannot be instantiated
C Yes, only if it has default methods
D Yes, if it has only abstract methods
Correct Answer:  B. No, interfaces cannot be instantiated
EXPLANATION

Interfaces cannot be instantiated directly. You can only create objects of classes that implement interfaces.

Take Test
Q.29 Easy OOP in Java
Which keyword is used to prevent a class from being subclassed?
A abstract
B final
C sealed
D static
Correct Answer:  B. final
EXPLANATION

The 'final' keyword prevents a class from being extended. For example: final class MyClass {} cannot be subclassed.

Take Test
Q.30 Easy OOP in Java
What is the default access modifier for class members in Java if not explicitly specified?
A public
B private
C protected
D package-private (default)
Correct Answer:  D. package-private (default)
EXPLANATION

If no access modifier is specified for a class member, it has package-private (default) access, meaning it's accessible only within the same package.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips