Java Abstraction MCQ

Abstraction refers to the idea of showcasing only the essential features of an object while hiding its complexity. How familiar are you with Java's abstraction mechanism? Let's dive into this quiz and find out!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. What does abstraction in Java focus on?

a) Implementation
b) Visibility
c) Essentials
d) Specifics

Answer:

c) Essentials

Explanation:

Abstraction emphasizes showcasing only the essential features of an object while keeping its intricate details concealed.

2. Which keyword is used to create an abstract class in Java?

a) abstract
b) encapsulate
c) private
d) Virtual

Answer:

a) abstract

Explanation:

The abstract keyword in Java is used to declare an abstract class.

3. Which of these can an abstract class contain?

a) Only concrete methods
b) Only abstract methods
c) Both abstract and concrete methods
d) None of the above

Answer:

c) Both abstract and concrete methods

Explanation:

An abstract class can contain both abstract methods (without a body) and concrete methods (with a body).

4. What is the primary purpose of an interface in Java?

a) To provide a concrete implementation of methods
b) To define the signature of methods without implementing them
c) To instantiate objects
d) To replace abstract classes

Answer:

b) To define the signature of methods without implementing them

Explanation:

An interface in Java is used primarily to define the signature of methods without implementing them. It ensures a form of contract that the implementing classes need to follow.

5. How many interfaces can a Java class implement?

a) None
b) One
c) Two
d) As many as required

Answer:

d) As many as required

Explanation:

In Java, a class can implement multiple interfaces, allowing for a type of multiple inheritance.

6. Can you declare variables inside an interface?

a) Yes, and they are implicitly public and static
b) No, interfaces can only have methods
c) Yes, but they must be private
d) Only if they are final and static

Answer:

a) Yes, and they are implicitly public and static

Explanation:

Variables declared inside an interface are implicitly public, static, and final. They must be initialized.

7. How can you achieve abstraction in Java?

a) Only through abstract classes
b) Only through interfaces
c) Through both abstract classes and interfaces
d) Through private methods

Answer:

c) Through both abstract classes and interfaces

Explanation:

Abstraction in Java can be achieved using both abstract classes and interfaces.

8. In relation to abstraction, what does an abstract method represent?

a) A method with a detailed implementation
b) A method that is complete and cannot be overridden
c) A method with no body, meant to be overridden in derived classes
d) A method that cannot be used in an interface

Answer:

c) A method with no body, meant to be overridden in derived classes

Explanation:

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). Derived classes that extend an abstract class with abstract methods must provide an implementation for those methods.

9. Can an abstract class in Java be instantiated directly?

a) Yes
b) No

Answer:

a) Yes

Explanation:

An abstract class cannot be instantiated directly. Its primary purpose is to be extended (subclassed) by other classes.

10. Can an interface contain a constructor in Java?

a) Yes
b) No

Answer:

b) No

Explanation:

Interfaces in Java cannot contain constructors. Their purpose is to define method signatures without concrete implementations.



Comments