Which of the following is NOT a pillar of Object-Oriented Programming?
Answer: C
The four pillars of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism. Compilation is a process, not a pillar of OOP.
Q.2Medium
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.3Easy
Which access modifier allows a member to be accessed only within the same package?
Answer: D
Default access (no modifier) allows access only within the same package. Protected allows same package and subclasses.
Q.4Easy
What will be the output?
class Parent {
void show() {
System.out.println("Parent");
}
}
class Child extends Parent {
void show() {
System.out.println("Child");
}
}
parent obj = new Child();
obj.show();
Answer: B
This demonstrates method overriding and polymorphism. The actual object type is Child, so Child's show() method is called.
Q.5Medium
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.
Advertisement
Q.6Easy
What is the purpose of the 'super' keyword in Java?
Answer: B
The 'super' keyword is used to refer to the immediate parent class object. It's used to access parent class methods and constructors.
Q.7Easy
Which interface in Java represents a collection that does not allow duplicate elements?
Answer: B
Set interface ensures uniqueness and doesn't allow duplicate elements. HashSet, TreeSet are implementations of Set.
Q.8Medium
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.9Medium
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.10Medium
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.
Q.11Medium
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.12Hard
What will be the output?
interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("B");
}
}
class C extends B {
public void show() {
System.out.println("C");
}
}
A obj = new C();
obj.show();
Answer: C
Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.
Q.13Easy
Which of the following correctly describes the 'this' keyword?
Answer: B
'this' is a reference to the current object instance. It's used to refer to instance variables, call other constructors, or pass object reference.
Q.14Medium
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.15Easy
Which keyword is used to make a variable immutable in Java?
Answer: B
'final' keyword makes a variable immutable. Once assigned, its value cannot be changed. 'const' is not a valid Java keyword.
Q.16Hard
What will be the output?
class Test {
int x = 5;
{
x = 10;
}
Test() {
x = 15;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
Answer: C
Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.
Q.17Medium
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.18Hard
What is the output?
interface I1 {
void method1();
}
interface I2 extends I1 {
void method2();
}
class C implements I2 {
public void method1() { System.out.println("M1"); }
public void method2() { System.out.println("M2"); }
}
public class Test {
public static void main(String[] args) {
I1 obj = new C();
obj.method2();
}
}
Answer: C
obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.
Q.19Easy
Which of the following access modifiers allows a variable to be accessed only within the same class?
Answer: A
The 'private' access modifier restricts access to only within the same class. It provides the highest level of encapsulation.
Q.20Easy
In Java, which keyword is used to create a reference variable that cannot point to a different object after initialization?
Answer: B
The 'final' keyword when applied to a reference variable makes it immutable, meaning it cannot be reassigned to point to another object.