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?
Answer:
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?
Answer:
Explanation:
The abstract keyword in Java is used to declare an abstract class.
3. Which of these can an abstract class contain?
Answer:
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?
Answer:
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?
Answer:
Explanation:
In Java, a class can implement multiple interfaces, allowing for a type of multiple inheritance.
6. Can you declare variables inside an interface?
Answer:
Explanation:
Variables declared inside an interface are implicitly public, static, and final. They must be initialized.
7. How can you achieve abstraction in Java?
Answer:
Explanation:
Abstraction in Java can be achieved using both abstract classes and interfaces.
8. In relation to abstraction, what does an abstract method represent?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Interfaces in Java cannot contain constructors. Their purpose is to define method signatures without concrete implementations.