200 Questions with Answers & Explanations
Java OOP, Data Structures & Algorithms
Study Guide
SECTION 1: OBJECT-ORIENTED PROGRAMMING BASICS (Questions
1-30)
Question 1:
What is encapsulation in Java?
A) Hiding implementation details and exposing only necessary methods
B) Inheriting properties from parent classes
C) Creating multiple forms of a single method
D) Binding data and methods together
Answer: A
Explanation: Encapsulation is the mechanism of hiding internal implementation
details and providing a public interface through methods. It protects data integrity
by controlling access through getters and setters.
Question 2:
Which keyword is used to inherit a class in Java?
A) implements
B) extends
C) inherits
D) super
Answer: B
Explanation: The 'extends' keyword is used for class inheritance in Java.
'Implements' is used for interfaces.
,Question 3:
What is the output of the following code?
java
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();
}
}
A) Parent
B) Child
C) Compilation Error
D) Runtime Error
Answer: B
Explanation: This demonstrates polymorphism - the overridden method in Child
,class is called even though the reference is of Parent type because the actual object
is Child.
Question 4:
What is an abstract class?
A) A class that cannot be instantiated
B) A class that can only be inherited
C) A class with only static methods
D) A class with no methods
Answer: A
Explanation: An abstract class cannot be instantiated directly. It serves as a
blueprint for subclasses and may contain abstract methods that must be
implemented by concrete subclasses.
Question 5:
What is the difference between an interface and an abstract class?
A) Interface can have implementation, abstract class cannot
B) Abstract class can have implementation, interface cannot (prior to Java 8)
C) Both are the same
D) Interface cannot have variables
Answer: B
Explanation: Abstract classes can have concrete methods with implementations.
Interfaces (before Java 8) could only declare method signatures, not provide
implementations.
Question 6:
What is method overloading?
A) Same method name, different parameters
B) Same method name, same parameters
C) Different method name, same parameters
D) Inheriting method from parent
, Answer: A k0
Explanation: Method overloading occurs when multiple methods in the same class
k0 k0 k0 k0 k0 k0 k0 k0 k0 k0
share the same name but have different parameter lists (type, number, or order).
k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0
Question 7: k0
What is method overriding?
k0 k0 k0
A) Redefining a method in child class with same signature k0 k0 k0 k0 k0 k0 k0 k0
B) Having multiple methods with same namek0 k0 k0 k0 k0
C) Changing return type of parent method k0 k0 k0 k0 k0
D) Hiding parent method k0 k0
Answer: A k0
Explanation: Method overriding allows a subclass to provide a specific
k0 k0 k0 k0 k0 k0 k0 k0 k0
implementation of a method that is already defined in its superclass, using the
k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0
same method signature.
k0 k0 k0
Question 8: k0
What does the 'super' keyword do?
k0 k0 k0 k0 k0
A) Refers to parent class constructor/methods
k0 k0 k0 k0
B) Refers to current class k0 k0 k0
C) Creates a new object k0 k0 k0
D) Static reference k0
Answer: A k0
Explanation: 'super' is used to refer to immediate parent class instance, methods,
k0 k0 k0 k0 k0 k0 k0 k0 k0 k0 k0
or constructor.
k0 k0
Question 9: k0
What is a constructor?
k0 k0 k0
A) A special method called when object is created
k0 k0 k0 k0 k0 k0 k0
B) A method that destroys objects
k0 k0 k0 k0
C) A static method
k0 k0
D) An abstract method
k0 k0