Which access modifier allows a member to be accessed only within the same class?
Answer: B
The 'private' access modifier restricts access to only the members of the same class. It provides the highest level of encapsulation.
Q.142Medium
What will happen if you try to override a final method in a child class?
Answer: B
Final methods cannot be overridden. The compiler will throw an error indicating that you cannot override a final method.
Q.143Medium
Which statement about 'this' keyword is correct?
Answer: B
'this' is a reference variable that refers to the current object instance. It is commonly used to distinguish instance variables from parameters with the same name.
Q.144Medium
Consider:
interface I1 { void method(); }
interface I2 { void method(); }
class C implements I1, I2 { public void method() { System.out.println("Method"); } }
What will be the behavior?
Answer: B
When a class implements multiple interfaces with the same method signature, a single method implementation satisfies all interfaces. This is resolved at compile time.
Q.145Medium
What is method overloading?
Answer: A
Method overloading allows multiple methods with the same name but different parameters (number, type, or order). It is a compile-time (static) polymorphism.
Advertisement
Q.146Medium
Which of the following is true about abstract classes?
Answer: A
Abstract classes can have constructors to initialize instance variables. They can contain both abstract and concrete methods, and they cannot be directly instantiated.
Q.147Easy
Which concept best describes the relationship between Parent and Child classes in 'class Child extends Parent'?
Answer: C
The 'extends' keyword creates an inheritance relationship where Child inherits properties and methods from Parent. This is an 'is-a' relationship.
Q.148Hard
What is the output of this code?
class A { int x = 10; }
class B extends A { int x = 20; }
public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.x); } }
Answer: A
Variable shadowing occurs here. The reference type is A, so a.x accesses A's x field which is 10. Method overriding works with methods, not instance variables.
Q.149Hard
In Java, can you create an object of an abstract class?
Answer: C
Abstract classes cannot be directly instantiated, but you can create objects using anonymous inner classes that implement the abstract methods.
Q.150Medium
Consider the code:
abstract class Animal { abstract void sound(); void sleep() { System.out.println("Zzz"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
What can Dog objects do?
Answer: C
Dog inherits the concrete method sleep() from Animal and provides implementation for the abstract method sound(). Therefore, Dog objects can call both methods.
Q.151Hard
What is the main difference between method overriding and method overloading?
Answer: C
Method overloading is compile-time (static) polymorphism with same method name but different parameters in the same class. Method overriding is runtime (dynamic) polymorphism where a child class provides a specific implementation of a parent method.
Q.152Easy
Which of the following correctly describes encapsulation?
Answer: A
Encapsulation is the bundling of data (variables) and methods into a single unit (class) while hiding the internal implementation details from the outside world. It is achieved using access modifiers.
Q.153Medium
Which statement is true about the default access modifier (package-private) in Java?
Answer: B
The default (package-private) access modifier allows access to members from any class within the same package, but not from classes in other packages.
Q.154Easy
What happens when you try to instantiate an interface in Java?
Answer: B
Interfaces cannot be instantiated directly. You must create a concrete class that implements the interface or use an anonymous inner class.
Q.155Easy
Which keyword is used to achieve runtime polymorphism in Java?
Answer: D
Java achieves runtime polymorphism through method overriding using inheritance. Unlike C++, Java doesn't use the 'virtual' keyword; it's implicit for all non-final methods.
Q.156Medium
What is the output of the following code?
class Parent { void show() { System.out.println("Parent"); } }
class Child extends Parent { void show() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.show(); } }
Answer: B
This demonstrates runtime polymorphism. Although the reference is of type Parent, the actual object is Child. The overridden show() method in Child class is executed.
Q.157Easy
Can a class extend multiple classes in Java?
Answer: B
Java supports only single class inheritance to avoid the diamond problem. However, multiple interface implementation is allowed.
Q.158Medium
Which of the following statements about the 'final' keyword is correct?
Answer: C
The 'final' keyword when applied to a variable makes it a constant. When applied to a class, it cannot be extended. When applied to a method, it cannot be overridden.
Q.159Medium
What is the difference between 'this' and 'super' keywords?
Answer: B
'this' is a reference to the current object instance, while 'super' is a reference to the parent class. They serve different purposes in inheritance hierarchy.
Q.160Medium
Consider a scenario where class B extends class A. If class A has a constructor with parameters, what must class B do?
Answer: B
If a parent class has a parameterized constructor and no default constructor, the child class must explicitly call it using super() to initialize parent class members.