Attention Java developers! Test your object-oriented prowess with our ultimate java quiz online: a motivating challenge to sharpen your skills. This quiz java adventure covers inheritance, polymorphism and encapsulation in quick, engaging questions. Tune in for a live stream oops demo to reinforce core ideas, then dive into the java online quiz test to measure your command. Whether you're prepping for certification or coding for fun, our free java quiz and interactive Java Certification Quiz await. Sign up now to track your score, share with peers and replay until you ace every concept. Jump in and start conquering Java today!
Which Java keyword is used to create a subclass from a superclass?
implements
inherits
super
extends
In Java, the keyword extends is used to declare that one class derives from another, establishing an inheritance relationship. This allows the subclass to inherit fields and methods from the superclass. The 'implements' keyword is used for interfaces, and 'super' is a reference to the parent class instance. For more details see Java Tutorial: Subclasses.
Which of the following best describes polymorphism in Java?
The ability of an object to take many forms at runtime
The reuse of code through inheritance
The hiding of implementation details from the user
Compile-time type checking of variables
Polymorphism in Java refers to the ability of objects to be treated as instances of their parent class while invoking overridden methods at runtime. This dynamic method dispatch enables flexible and reusable code. It's not about hiding details (encapsulation) or compile-time checks. Learn more at Java Tutorial: Polymorphism.
What does encapsulation promote in object-oriented programming?
Hiding internal state and requiring all interaction through methods
Creating multiple forms of a method
Bundling unrelated functions into a single function
Allowing a class to inherit from multiple classes
Encapsulation is the principle of hiding an object's internal state and requiring all interaction through its public methods or interfaces. This protects the integrity of the data and prevents unauthorized access. It's not about polymorphism or multiple inheritance. See Java Tutorial: Encapsulation for more.
Which of these is NOT a core feature of object-oriented programming in Java?
Polymorphism
Pointer arithmetic
Inheritance
Encapsulation
Java does not support pointer arithmetic, which is common in languages like C or C++. Inheritance, polymorphism, and encapsulation are fundamental features of Java's OOP model. For more information refer to Java Tutorial: Object-Oriented Concepts.
Which annotation is used to indicate that a method is intended to override a method in a superclass?
@Implemented
@Overload
@Super
@Override
The @Override annotation tells the compiler that the method is intended to override a method in the superclass, and the compiler will generate an error if it does not. It helps catch typos and ensures correct overriding behavior. Details at Java Tutorial: Predefined Annotations.
Which of the following statements about Java interfaces is true?
Interfaces can have instance fields with different values per instance
Interfaces cannot declare static methods
Interfaces can contain abstract methods and constants
Interfaces can contain constructors
Java interfaces can declare abstract methods (implicitly public) and static final constants. They cannot have instance fields or constructors, and since Java 8 they can also have static and default methods. Learn more at Java Tutorial: Interfaces.
Given the code below, what will be printed when calling a.sound()?
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
@Override void sound() { System.out.println("Woof"); }
}
Animal a = new Dog();
Compile error
Animal sound
No output
Woof
Because of dynamic method dispatch and polymorphism, the overridden sound() in Dog is called at runtime, printing "Woof". The reference type is Animal, but the object type is Dog. See Java Tutorial: Polymorphism for details.
Which access modifier allows a member to be accessed within its own package and by subclasses in other packages?
protected
private
default (no modifier)
public
The protected modifier allows access within the same package and to subclasses in different packages. Default (package-private) only allows access within the same package, private restricts to the class, and public allows everywhere. Read more at Java Tutorial: Access Control.
In Java, an overriding method may do which of the following?
Have a covariant return type
Change the parameter types
Throw broader checked exceptions than the original
Reduce visibility compared to the overridden method
When overriding a method in Java, you can use a covariant return type (a subtype of the original return type). You cannot throw broader checked exceptions, reduce visibility, or alter the parameter list. See Java Tutorial: Overriding Methods.
Which problem is avoided in Java by disallowing multiple inheritance of classes?
Deadlock in threading
Unchecked cast errors
The diamond problem
Type erasure conflicts
Java prevents multiple inheritance of classes to avoid the diamond problem where a class could inherit conflicting implementations from two superclasses. Interfaces with default methods handle this more cleanly. More info at Java Tutorial: Multiple Inheritance.
Which principle states that objects of a superclass should be replaceable with objects of a subclass without affecting program correctness?
Single Responsibility Principle
Liskov Substitution Principle
Open/Closed Principle
Dependency Inversion Principle
The Liskov Substitution Principle (LSP) requires that subclasses can substitute their base classes without altering the correctness of the program. It's a key concept in SOLID design. See Baeldung: LSP in Java for details.
What is true about default methods in Java interfaces?
They must be static
They allow interfaces to provide method bodies
They cannot be overridden by implementing classes
They require a separate implementation class
Default methods, introduced in Java 8, allow interfaces to provide concrete method implementations. Implementing classes can override them if needed. They are neither required to be static nor need a separate implementation class. Learn more at Java Tutorial: Default Methods.
How does a class resolve a conflict when it implements two interfaces that define the same default method signature?
Default methods are not allowed in interfaces with conflicts
It must override the conflicting method and can invoke a specific default with InterfaceName.super.method()
The class fails to compile unless one interface is marked @Primary
The compiler chooses one of the defaults arbitrarily
When two interfaces provide the same default method, the implementing class must override that method and can explicitly call one using InterfaceName.super.method(). Neither compiler arbitration nor @Primary exists. See Java Tutorial: Default Methods for more.
Which effect does type erasure have on method overloading involving generics in Java?
It improves performance of generic methods at runtime
It allows two methods with same name and generic parameters to coexist
It enforces runtime checks on generic types
You cannot overload methods whose signatures differ only by generic type parameters
Java's type erasure removes generic type information at compile time, so methods that differ only by generic parameters end up with the same signature and cannot be overloaded. This is a direct result of erasure. More details at Java Tutorial: Erasure.
0
{"name":"Which Java keyword is used to create a subclass from a superclass?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Which Java keyword is used to create a subclass from a superclass?, Which of the following best describes polymorphism in Java?, What does encapsulation promote in object-oriented programming?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}
Score4/14
Easy0/4
Medium2/4
Hard1/4
Expert1/2
AI Study Notes
Email these to me
You can bookmark this page to review your notes in future, or fill out the email box below to email them to yourself.
Study Outcomes
Understand Java OOP Fundamentals -
Engage with targeted questions to grasp the core principles of inheritance, polymorphism, and encapsulation in Java.
Differentiate Inheritance and Composition -
Analyze code examples to distinguish between inheritance hierarchies and alternative design approaches.
Apply Polymorphism Techniques -
Identify and implement method overloading and overriding patterns to solve real-world problems.
Implement Encapsulation Best Practices -
Enforce data hiding and use proper accessors to ensure class integrity and maintainability.
Evaluate Your OOP Skill Level -
Engage with the java online quiz test to receive instant scores and pinpoint areas for improvement.
Interpret Detailed Feedback -
Review comprehensive explanations for each question to solidify understanding and prepare for interviews.
Cheat Sheet
Inheritance Hierarchies -
Java's extends keyword enables one class to inherit fields and methods from a parent class (Oracle Java Tutorials). For example, if class Dog extends Animal, Dog automatically gets Animal's eat() method but can override speak() to customize behavior.
Polymorphism Mechanics -
Polymorphism lets you treat objects of different subclasses through a common superclass reference (Stanford CS106A). At compile time you decide which overloaded method to call, and at runtime the JVM picks the correct overridden version - think List list = new ArrayList<>();.
Encapsulation Best Practices -
Keep fields private and expose behavior through public getters and setters to maintain control over internal state (IEEE Software Engineering). A simple mnemonic is "PEMDAS" for Protect, Encapsulate, Manage, Distribute, Audit, Secure your data.
Abstraction via Interfaces & Abstract Classes -
Abstract classes let you define shared code, while interfaces (Java 8+) support default methods and multiple inheritance of type (ACM digital library). Use interfaces to define contracts like Comparable and abstract classes to factor common behavior.
SOLID OOP Principles -
The "O" in SOLID stands for the Open/Closed Principle: classes should be open for extension but closed for modification (Robert C. Martin's papers). Remember SOLID by the acronym "Single, Open, Liskov, Interface, Dependency" to design robust, testable code.