Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 821–830 of 958
Topics in Java Programming
Q.821 Easy OOP in Java
What will be the output of the following code?
class Parent { void display() { System.out.println("Parent"); } }
class Child extends Parent { void display() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.display(); } }
A Child
B Parent
C Compilation Error
D Runtime Error
Correct Answer:  A. Child
EXPLANATION

This demonstrates runtime polymorphism. Even though p is a Parent reference, it points to a Child object, so the overridden display() method in Child class is executed.

Take Test
Q.822 Medium OOP in Java
Which statement about the 'protected' access modifier is correct?
A It allows access only within the same package
B It allows access within the same package and by subclasses in different packages
C It is equivalent to 'private'
D It allows access globally like 'public'
Correct Answer:  B. It allows access within the same package and by subclasses in different packages
EXPLANATION

'protected' members are accessible within the same package and also to subclasses in different packages. It provides more access than default but less than public.

Take Test
Q.823 Hard OOP in Java
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
A Create an abstract parent class with abstract methods
B Create an interface with abstract method declarations
C Use composition instead of inheritance
D Create static utility methods in a common class
Correct Answer:  B. Create an interface with abstract method declarations
EXPLANATION

When unrelated classes need to implement a common contract, an interface is the best choice. Interfaces are specifically designed for this purpose, allowing multiple implementation without forcing an inheritance hierarchy.

Take Test
Q.824 Hard OOP in Java
When a static method is called on an instance of a class, what happens?
A The method is called on the instance object
B The method is called on the class, not the instance (though syntactically allowed)
C A compilation error occurs
D The instance data is used in the static method
Correct Answer:  B. The method is called on the class, not the instance (though syntactically allowed)
EXPLANATION

Static methods belong to the class, not instances. When called on an instance, Java internally calls the method on the class. Static methods cannot access instance variables or use 'this' keyword.

Take Test
Q.825 Easy OOP in Java
Which of the following correctly describes the relationship between a class and an interface?
A A class extends an interface
B An interface implements a class
C A class implements an interface
D Both are interchangeable terms
Correct Answer:  C. A class implements an interface
EXPLANATION

In Java, a class implements an interface. A class extends another class. An interface can extend another interface. This is the correct terminology and relationship structure.

Take Test
Q.826 Hard OOP in Java
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
A abstract and final
B final and ensure all fields are final
C private and synchronized
D static and volatile
Correct Answer:  B. final and ensure all fields are final
EXPLANATION

To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.

Take Test
Q.827 Medium OOP in Java
Which statement about interface implementation in Java is true?
A An interface can contain only abstract methods and no variables
B From Java 8 onwards, interfaces can have default methods and static methods
C A class implementing an interface must implement all its methods
D Both B and C are correct
Correct Answer:  D. Both B and C are correct
EXPLANATION

Java 8 introduced default and static methods in interfaces. Any class implementing an interface must provide implementations for all its abstract methods (unless the implementing class is abstract).

Take Test
Q.828 Hard OOP in Java
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
A A compilation error occurs
B The parent class constructor is automatically called (default no-arg constructor)
C The parent class constructor is never called
D The child class constructor must be declared as abstract
Correct Answer:  B. The parent class constructor is automatically called (default no-arg constructor)
EXPLANATION

Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.

Take Test
Q.829 Medium OOP in Java
Which of the following correctly implements method overloading rules in Java?
A Methods must have the same name and same parameter types
B Methods must have the same name but different parameter types, number, or order
C Methods must have different names and different return types
D Methods can have same name with different return types only
Correct Answer:  B. Methods must have the same name but different parameter types, number, or order
EXPLANATION

Method overloading requires methods to have the same name but different parameter lists (different types, number, or order of parameters). Return type alone cannot distinguish overloaded methods.

Take Test
Q.830 Medium OOP in Java
An abstract class in Java cannot be instantiated, but it can have concrete methods. Which statement explains why?
A Abstract classes are incomplete and cannot have objects
B Abstract classes serve as templates and concrete methods provide default implementations that subclasses can use
C Abstract methods do not allow concrete methods in the same class
D Concrete methods in abstract classes are automatically inherited as abstract
Correct Answer:  B. Abstract classes serve as templates and concrete methods provide default implementations that subclasses can use
EXPLANATION

Abstract classes cannot be instantiated but can contain both abstract methods (without implementation) and concrete methods (with implementation). This allows providing default behavior while enforcing certain methods to be implemented by subclasses.

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