What is the output of the following code?
class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Test {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}
Answer: A
Variable overriding doesn't work in Java like method overriding. obj.x accesses the variable from reference type A, which has value 10.
Q.2Medium
Which of the following statements about abstract classes is TRUE?
Answer: C
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.
Q.3Medium
What will happen when you try to instantiate an interface in Java?
Answer: C
Interfaces cannot be instantiated directly. You must create a class that implements the interface. Attempting this causes a compilation error.
Q.4Medium
Which of the following best describes encapsulation?
Answer: A
Encapsulation involves bundling data (variables) and methods, hiding implementation details, and providing public methods for access. It protects data integrity.
Q.5Medium
Which of the following is true about method overloading?
Answer: B
Method overloading requires methods to have the same name but different parameter lists (number, type, or order of parameters). Return type alone is insufficient.
Advertisement
Q.6Medium
What is the correct way to prevent a class from being inherited in Java?
Answer: B
The 'final' keyword prevents a class from being extended. String, Integer, and other wrapper classes are marked final for security and immutability.
Q.7Medium
What is the output?
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super();
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}
Answer: B
super() calls parent constructor first. Parent constructor A prints 'A', then child constructor B prints 'B'.
Q.8Medium
Which of the following statements about 'instanceof' operator is correct?
Answer: B
The 'instanceof' operator checks whether an object is an instance of a specific class or implements a specific interface.
Q.9Medium
Which of the following statements about inheritance in Java is correct?
Answer: D
Java supports single inheritance for classes but a class can implement multiple interfaces. An interface can extend multiple interfaces through multiple inheritance.
Q.10Medium
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.11Medium
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.12Medium
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.
Q.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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.