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.2Easy
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.3Easy
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.4Easy
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.5Easy
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.
Advertisement
Q.6Easy
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.7Easy
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.8Easy
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.9Easy
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.
Q.10Easy
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.11Easy
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.12Easy
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.13Easy
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.14Easy
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.15Easy
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'.
Q.16Easy
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.17Easy
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.18Easy
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.19Easy
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.20Easy
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.