Exercises
Challenge your understanding of object-oriented programming in Java. This quiz covers core OOP concepts including inheritance with extends, encapsulation, interfaces and polymorphism, constructors, method overloading, abstract classes, protected access, and the super keyword. You will also test your knowledge of static versus instance methods and predict the output of a simple Java class. Whether you are learning Java fundamentals or reviewing for an exam or interview, these questions will help you assess your grasp of essential Java OOP principles.
Answer the questions below and check the explanation for each answer.
0/10 answered
Auto audio on: the next questions will be read aloud when you click Continue.
The extends keyword in Java is used to indicate that a class is deriving from another class, meaning it is creating a sub-class. Through inheritance, the sub-class can acquire the fields and methods of the parent class, and also add or override methods as needed. Thus, Option 2 is correct.
The correct answer is 1 because a valid constructor in Java for the class Car must have the same name as the class and no return type, which is true for both public Car() {} and Car() {}. Option 2 is invalid because it specifies a return type, making it a method not a constructor.
Encapsulation in Java is the process of binding code and data together into a single unit, which is typically achieved using classes. Through encapsulation, a class can control the accessibility of its data members, allowing for data hiding. This ensures that sensitive data is protected and only accessible through well-defined interfaces.
The code defines a class Test with a private instance variable name initialized to John. The getName() method returns the value of name. In the main method, a Test object is created and getName() is called to print its name, resulting in John.
A static method is associated with the class itself, not any individual object instance, allowing it to be called without creating an instance of the class. Therefore, Option 1 is correct.
In contrast, a non-static method (or instance method) is associated with an object of the class and requires an instance to be called. Option 2 incorrectly describes non-static methods, and Option 3 incorrectly describes static methods.
The given code exemplifies Polymorphism. It is demonstrated by the Circle class implementing the Drawable interface, which allows objects of the class to be treated as objects of the interface type. This means different classes can use the same method in a way that is suited to them, allowing for flexibility and integration.
The protected keyword in Java allows access to the method within any class in the same package and also to subclasses that may exist in other packages. It is more accessible than private but less than public.
The super keyword in Java is used to refer to the immediate parent class of an object. It is commonly used to call methods of the parent class from the subclass when the subclass has overridden specific methods, allowing access to the parent's method implementation. Therefore, the purpose of super is best described by option 1: 'To call a method of the parent class.'
Method overloading occurs when two or more methods in the same class have the same name but different parameters (number, type, or both). This allows methods to perform similar but distinct functions based on the input parameters. Option 3 is an example of method overloading because the methods have the same name but their parameters differ, which fits the overload criteria.
In Java, when an abstract class is instantiated, a compilation error occurs. Abstract classes are meant to be extended by other classes, and they cannot be instantiated directly. This is because abstract classes can contain abstract methods, which do not have implementations and need to be provided by subclasses.

Free CourseLearn Python
13h00m
17 exercises

Free CourseJava Programming
9h15m
48 exercises

Free CourseCreate Desktop App
5h33m
30 exercises

Free CourseRuby on Rails
9h39m
34 exercises

Free CourseJava
22h17m
28 exercises

Free CourseC# Sharp for beginner
3h55m
15 exercises

Free CourseC++ as your first Programming Language
5h12m
23 exercises

Free CourseJava for beginners
26h50m
49 exercises
Thousands of online courses in video, ebooks and audiobooks.
To test your knowledge during online courses
Generated directly from your cell phone's photo gallery and sent to your email
Download our app via QR Code or the links below:.
+ 10 million
students
Free and Valid
Certificate
60 thousand free
exercises
4.8/5 rating in
app stores
Free courses in
video and ebooks