What is the primary purpose of using the 'super' keyword in Java?
Answer: B
'super' is used to refer to parent class methods, constructors, and variables. It helps in accessing hidden or overridden members of the parent class.
Q.122Medium
Which of the following correctly describes method overriding in Java?
Answer: B
Method overriding occurs when a child class provides a specific implementation of a method already defined in the parent class with the same signature. The @Override annotation is optional but recommended.
Q.123Easy
In Java OOP, which principle ensures that internal details of a class are hidden from the outside world?
Answer: C
Encapsulation is the bundling of data and methods into a single unit (class) while hiding internal implementation details using access modifiers.
Q.124Medium
Which of the following statements about 'instanceof' operator is true?
Answer: B
The 'instanceof' operator is used to test if an object is an instance of a specified class, subclass, or interface. It returns a boolean value.
Q.125Medium
In the context of constructors, which statement is correct?
Answer: B
Constructor overloading is allowed in Java. A class can have multiple constructors with different parameter lists. Constructors don't have return types and can have any access modifier.
Advertisement
Q.126Medium
What is the difference between 'this' and 'super' keywords in Java?
Answer: B
'this' is a reference to the current object instance, while 'super' is a reference to the parent class object. They are used to access members of current and parent classes respectively.
Q.127Medium
Which of the following best describes polymorphism in Java?
Answer: B
Polymorphism allows objects to be treated as instances of their parent class and to call methods that are overridden in child classes. It includes method overloading and method overriding.
Q.128Medium
An abstract class in Java cannot be instantiated, but it can have concrete methods. Which statement explains why?
Answer: B
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.
Q.129Medium
Which of the following correctly implements method overloading rules in Java?
Answer: B
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.
Q.130Hard
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
Answer: B
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.
Q.131Medium
Which statement about interface implementation in Java is true?
Answer: D
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).
Q.132Hard
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
Answer: B
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.
Q.133Easy
Which of the following correctly describes the relationship between a class and an interface?
Answer: C
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.
Q.134Hard
When a static method is called on an instance of a class, what happens?
Answer: B
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.
Q.135Hard
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
Answer: B
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.
Q.136Medium
Which statement about the 'protected' access modifier is correct?
Answer: B
'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.
Q.137Easy
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(); } }
Answer: A
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.
Q.138Medium
Which of the following is NOT a characteristic of an interface in Java?
Answer: C
Interfaces cannot have instance variables (private or otherwise). They can only have constants (static final). Java 9+ allows private methods but not private instance variables.
Q.139Easy
What is the primary purpose of the 'super' keyword in Java?
Answer: B
The 'super' keyword is used to refer to parent class methods, constructors, and variables. It allows a child class to access parent class members.
Q.140Easy
Consider the code:
class A { A() { System.out.println("A"); } }
class B extends A { B() { super(); System.out.println("B"); } }
What will be printed when 'new B()' is executed?
Answer: B
The super() call invokes the parent class constructor first, printing 'A', then the child constructor completes, printing 'B'.