Interview Questions for Object Oriented Programming Core Java Interview java MCQ OOP Concept by devs5003 - October 28, 2024December 22, 20242 Last Updated on December 22nd, 2024 OOP (Object Oriented Programming) concepts are the essential & the most important concepts in any Object Oriented Programming Language. Any exam or interview of an OOP language is considered incomplete without these questions. Therefore, it becomes crucial to practice them thoroughly time to time. Certainly, we will be talking about the practice test of OOP concepts in this article. It helps in thorough revision of the subject and make the concept crystal clear. Here in this article, we will focus on Interview Questions for Object Oriented Programming in the form of MCQs Practice Test with a variety of questions such as concept-based, code-based, and scenario-based. At the end of each question, we will explore the detailed explanation of each question. In fact, we should try to revise these concepts to make them crystal clear. Table of Contents Toggle What all Concepts are covered in this article?Interview Questions for Object Oriented Programming What all Concepts are covered in this article? Below is the list of concepts that are covered in this article in the form of MCQs Practice Test: 1) Class  2) Object  3) Encapsulation  4) Abstraction 5) Inheritance 6) Polymorphism   7) Method Overloading 8) Method Overriding   9) Constructor 10) Destructor    11) Interface  12) Static Binding  13) Dynamic Binding 14) Message Passing  15) Composition   16) Aggregation 17) Association   18) Dependency Interview Questions for Object Oriented Programming Q#1. Which of the following statement is incorrect about method overloading and method overriding? A. In method overloading, type of method parameters can be different, while in method overriding they must be same. B. In overloading, methods may have different return types, but in case of overriding methods must have same or covariant return types. C. Methods with modifiers ‘private’, ‘static’, and ‘final’ can be overloaded & overridden as well. D. Overriding is an example of Runtime polymorphism, while overloading is an example of compile-time polymorphism. Answer: C Explanation: Methods with modifiers ‘private’, ‘static’, and ‘final’ can be overloaded, but can’t be overridden. JDK 1.5 onward, the overriding method’s return type can be the sub-type of it’s parent class method’s return type. This is called covariant return type. The covariant return type always works only for non-primitive return types. Pleasse check an example of covariant return type. You may also check the complete detail on what is Method Overloading & Method Overriding?. Q#2. Which of the following code snippets correctly demonstrates method overloading? A) public void display(String message) { } public int display(String message) { return 0; } B) public void display(String message) { } public void display(int number) { } C) public int display(String message, int number) { return 0; } public String display(String message, int number) { return number; } D) public void display(String message) { } public void display(String message, String additional) { } Answer: B, D Explanation: In method overloading, methods in the same class share the same name but have different parameter types or counts. Options B and D satisfy these conditions. Q#3. Which OOP concept is used to derive a new class from an existing class, inheriting its attributes and behaviors? A. Composition B. Inheritance C. Encapsulation D. Polymorphism Answer: B Explanation: Inheritance allows one class to derive from another, inheriting attributes and behaviors, promoting code reuse and hierarchy. Q#4. Select the code snippet from below options that best demonstrates the concept of abstraction. A) abstract class Vehicle { abstract void startEngine(); } B) class Vehicle { public void startEngine() { System.out.println("Engine started."); } } C) interface Vehicle { void startEngine(); } D) class Car { private String model; void startEngine() { } } Answer: A, C Explanation: Abstraction hides implementation details, which can be achieved with abstract classes and interfaces, as shown in options A and C. Q#5. In a graphic editor, shapes like Circle, Square, and Triangle need to implement a common draw() method. Which concept allows each shape to implement its own version of draw() having common method signature? A. Inheritance B. Method Overloading C. Method Overriding D. Polymorphism Answer: C, D Explanation: Polymorphism and method overriding enable each shape to provide its specific implementation of draw() method, adhering to a common method signature. Q#6. Which of the following code snippets correctly implements method overriding? A) class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } B) class Animal { static void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { static void makeSound() { System.out.println("Bark"); } } C) class Animal { void makeSound(String sound) { System.out.println(sound); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } D) class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog { void makeSound() { System.out.println("Bark"); } } Answer: A Explanation: In method overriding, the subclass provides a specific implementation for a method already defined in its superclass with the same signature, as in option A. Since static methods can’t be overridden, therefore option B is incorrect. In option C method arguments don’t match. Option D doesn’t have inheritance relationship. Q#7. What is the main purpose of a constructor in a class? A. To initialize objects of the class B. To inherit from another class C. To override methods from the superclass D. To call methods with specific parameters Answer: A Explanation: A constructor is known as a special method used to initialize objects when they are created, setting initial values or configurations as needed. Q#8. You are designing a Library class that contains multiple Book objects, where the Book objects exist only within the Library. Which relationship best fits in this situation? A. Aggregation B. Association C. Composition D. Inheritance Answer: C Explanation: Composition implies a strong relationship where the lifecycle of Book objects is tied to the Library. When Library is destroyed, the Book objects are also destroyed. Q#9. Which of the following code snippets demonstrates polymorphism? A) class Animal { void sound() { System.out.println("Some sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } } Animal animal = new Dog(); animal.sound();  B) class Vehicle { void start() { System.out.println("Vehicle starting"); } } class Car extends Vehicle { void start() { System.out.println("Car starting"); } } Car car = new Car(); car.start();  C) class Shape { void draw() { System.out.println("Drawing"); } } Shape shape = new Shape(); shape.draw();  D) interface Drawable { void draw(); } class Circle implements Drawable { public void draw() { System.out.println("Drawing Circle"); } } Circle circle = new Circle(); circle.draw(); Answer: A Explanation: Polymorphism allows the method call to resolve at runtime. In option A, the sound() method demonstrates polymorphism, as the Animal reference holds a Dog object and calls Dog’s sound() method. Q#10. What is the main goal of dependency injection in OOP? A. To reduce code duplication B. To allow for dynamic method binding C. To reduce coupling between classes D. To allow classes to inherit each other’s attributes Answer: C Explanation: Dependency injection is used to reduce coupling between classes by injecting dependencies rather than creating them within the class, promoting flexibility and testability. Q#11. Which of the following terms best describes the relationship between a superclass and a subclass? A. “Depends-on” relationship B. “Has-a” relationship C. “Uses-a” relationship D. “Is-a” relationship Answer: D Explanation: Inheritance describes an “is-a” relationship, where a subclass is a specialized form of its superclass, sharing attributes and behaviors. Q#12. Which of the following statements are true about abstraction in OOP? Abstraction focuses on hiding the implementation details. Abstract classes cannot contain any implemented methods. Interfaces are a way to achieve abstraction. Only abstract classes can be instantiated. A. 1 & 3 B. 2 & 4 C. 1, 2 & 3 D. 1, 3 & 4 Answer: A Explanation: Abstraction focuses on hiding unnecessary details (1) and can be achieved through interfaces or abstract classes (3). Abstract classes can contain implemented methods, and they cannot be instantiated. Q#13. If a class Animal has a method speak() that is overridden in the subclass Dog, which concept allows the speak() method to call the subclass’s implementation when an Animal reference holds a Dog object? A. Static Binding B. Dynamic Binding C. Overloading D. Aggregation Answer: B Explanation: Dynamic binding (or late binding) allows the method to be resolved at runtime, so the Dog class’s implementation of speak() will be called. Q#14. Identify which code snippets correctly show method overloading. A) public void printInfo(int id) { } public void printInfo(String name) { }  B) public int calculateArea(int side) { return side * side; } public int calculateArea(int length, int width) { return length * width; }  C) public void display() { } public void display() { System.out.println("Hello"); } D) public void setDetails(int age) { } public void setDetails(int age, String name) { } Answer: A, B, D Explanation: Method overloading occurs when multiple methods in the same class have the same name but different parameters. Options A, B, and D matches with this condition, while C does not due to identical parameter lists. Q#15. In which of the following relationships does one class contain another class, and the contained class’s existence is dependent on the container? A. Association B. Aggregation C. Inheritance D. Composition Answer: D Explanation: Composition implies a strong relationship, where the lifecycle of the contained object depends on the lifecycle of the containing object. Q#16. Which relationship type is present if a Teacher class and Course class are associated but independent of each other? A. Composition B. Aggregation C. Association D. Inheritance Answer: C Explanation: Association represents a relationship between classes that are connected but independent in lifecycle. Q#17. Which of the following is an example of static binding? A. Overloading methods B. Overriding methods C. Using interfaces D. Creating instances of abstract classes Answer: A Explanation: Static binding (compile-time binding) occurs with overloaded methods where the compiler determines the method to be called based on the method signature. Q#18. Which code correctly defines a constructor for the Student class? A) public void Student() { } B) public Student() { } C) private Student() { } D) void Student() { } Answer: B, C Explanation: Constructors must have the same name as the class and no return type. Options B and C are valid constructors for Student. Q#19. In an e-commerce system, a Payment interface is defined with a processPayment() method. Different payment types (like CreditCardPayment and PayPalPayment) implement this interface. Which concept does this demonstrate? A. Encapsulation B. Abstraction C. Inheritance D. Dependency Answer: B Explanation: The Payment interface abstracts the processPayment() behavior, allowing different implementations without specifying the actual details. Q#20. What is the main purpose of a destructor in OOP? A. To initialize objects B. To provide additional functionalities to methods C. To clean up resources before an object is destroyed D. To encapsulate data Answer: C Explanation: Destructors are used to release resources (like memory) when an object’s lifecycle ends, especially in languages without garbage collection. Q#21. Which of the following terms best describes the relationship when one class depends on another to perform its functions? A. Aggregation B. Dependency C. Composition D. Polymorphism Answer: B Explanation: Dependency describes a relationship where one class requires another to perform its functions, often implemented through parameters or method calls. Q#22. What is true about interfaces in OOP? A. Interfaces can’t contain static fields B. Interfaces are used for encapsulation C. Interfaces can be instantiated directly D. Methods in interfaces have no implementation Answer: D Explanation: Interfaces define method signatures without implementation, providing a way to enforce certain behaviors without detailing how they are executed. Fields in interfaces are by default public static final. Q#23. Which of the following situations exemplifies dynamic binding? A. A superclass reference variable pointing to a subclass object B. Invoking overloaded methods at compile time C. A constructor setting initial values for fields D. Creating an interface implementation Answer: A Explanation: Dynamic binding allows a superclass reference to invoke overridden methods in the subclass at runtime, that enables polymorphic behavior. Q#24. Which code snippet demonstrates polymorphism? A) Animal animal = new Dog(); animal.speak(); B) Dog dog = new Dog(); dog.speak(); C) Animal animal = new Animal(); animal.speak(); D) Dog dog = (Dog) new Animal(); dog.speak(); Answer: A Explanation: Polymorphism is demonstrated by using a superclass reference (Animal) to refer to a subclass object (Dog) and invoking overridden methods. Q#25. Which of the following options correctly describes encapsulation in OOP? A. Allowing one interface to be used for a general class of actions B. Inheriting properties and methods from a parent class C. Binding data and methods that manipulate the data within a single unit D. Enforcing unique behaviors in each subclass Answer: C Explanation: Encapsulation is the process of bundling data and methods that operate on that data within a single unit, typically through a class. It helps protect data from unauthorized access. Q#26. In the code snippet below, what is myDog? class Dog { String breed; } Dog myDog = new Dog(); A. A class that holds details of various dogs B. An instance (object) of the Dog class C. A method to create dog breeds D. A variable that defines a dog’s breed Answer: B Explanation: myDog is an instance (object) of the Dog class, created using the new keyword to instantiate the Dog class and allocate memory for myDog. Q#27. A class Employee has properties and methods common to all employees, while subclasses like Manager and Developer inherit these common properties but add specific behaviors. What concept does this best illustrate? A. Encapsulation B. Inheritance C. Polymorphism D. Aggregation Answer: B Explanation: Inheritance allows subclasses to inherit common attributes and behaviors from a superclass, making it easier to manage shared code and add specific functionality in each subclass. Q#28. A system includes an interface Shape with a method draw(). Classes Circle, Square, and Triangle implement Shape, each providing its own implementation of draw(). What concept allows the program to call draw() on any Shape object, regardless of its specific type? A. Inheritance B. Polymorphism C. Encapsulation D. Composition Answer: B Explanation: Polymorphism allows a single interface to be used with different implementations, enabling the program to invoke draw() without needing to know the specific type of Shape. Q#29. Which option represents the “depends on” relationship in OOP? A. Aggregation B. Inheritance C. Dependency D. Composition Answer: C Explanation: Dependency represents a weak relationship where one class depends on another to function, but this dependency does not imply ownership or control. Q#30. Which of the following relationships describes a “has-a” relationship where the lifecycle of the contained objects is independent? A. Aggregation B. Association C. Composition D. Dependency Answer: A Explanation: Aggregation represents a “has-a” relationship, where the containing object does not own the lifecycle of the contained object; they can exist independently. Q#31. Which of the following is true about an interface in Java? A. An interface can contain static and default methods. B. An interface can’t be instantiated directly. C. An interface can contain only method signatures and constants. D. An interface enforces private access on its methods. Answer: B Explanation: An interface in Java till JDK 7 contains only method signatures (abstract methods) and constants (public, static, final fields), making it a contract for classes that implement it. JDK 8 onward as a new features introduced in Java 8, an interface can contain static & default methods as well. Default & static methods have implementation (method body). Q#32. Which statement best defines message passing in OOP? A. Sending requests to other classes to execute specific functions B. Directly accessing another class’s private variables C. Cloning objects across classes D. Binding methods statically during compile time Answer: A Explanation: Message passing refers to the process where objects communicate with each other by invoking methods, enabling interaction within an object-oriented system. Q#33. Which of the following constructors is correctly written for the class Book? A) public void Book() { } B) public Book() { } C) Book public() { } D) public int Book() { } Answer: B Explanation: Constructors must have the same name as the class and cannot have a return type, making option B the only valid constructor. Q#34. Given the following code, which statement best describes Person? public class Person { String name; int age; } A. Person is an instance of a class B. Person is a class that serves as a template for creating objects C. Person is an interface with attributes name and age D. Person is an enumeration of attributes Answer: B Explanation: Person is a class that defines attributes (name and age) and serves as a template for creating Person objects. Q#35. A developer creates a class BankAccount where the balance field is private and can only be accessed and modified using methods deposit() and withdraw(). Which concept is being applied here? A. Inheritance B. Polymorphism C. Aggregation D. Encapsulation Answer: D Explanation: Encapsulation is used to restrict direct access to certain properties, enforcing controlled access through methods. Q#36. Which of the following is an example of static binding? A. Method overriding B. Method overloading C. Dynamic polymorphism D. Abstract method implementation Answer: B Explanation: Static binding occurs at compile-time, and method overloading is an example since the compiler resolves the method call based on the method signature. Q#37. A software module defines an abstract class Vehicle with an abstract method startEngine(). Concrete subclasses like Car and Bike provide specific implementations of this method. What concept does this demonstrate? A. Encapsulation B. Inheritance C. Abstraction D. Aggregation Answer: C Explanation: Abstraction involves defining a method in a generic way, allowing specific classes to implement their versions. This hides unnecessary details and allows focus on essential features. Q#38. A Library class contains multiple Book objects. If a Library is deleted, all Book objects are also deleted. What relationship does this illustrate? A. Aggregation B. Composition C. Inheritance D. Dependency Answer: B Explanation: Composition indicates a strong relationship where the lifecycle of the contained objects (Book) is tied to the lifecycle of the container (Library). Q#39. Which statement accurately defines polymorphism? A. Creating a class from another class B. Using a class solely as a data holder C. Enforcing access restrictions on class members D. Allowing multiple forms of a method or an object Answer: D Explanation: Polymorphism allows objects to be treated as instances of their parent class, enabling methods to have multiple forms based on context. Q#40. A software developer designs a Database interface with methods like connect(), disconnect(), and executeQuery(). Classes like MySQLDatabase and MongoDatabase implement these methods. What principle is demonstrated here? A. Encapsulation B. Interface-based design C. Inheritance D. Aggregation Answer: B Explanation: Interface-based design enables classes to define specific behaviors according to a common contract (interface), promoting flexibility and standardization in application design. Q#41. Which of the following statements best defines abstraction in OOP? A. Combining data and methods within a single unit B. Ensuring different classes have different implementations of a method C. Allowing multiple classes to inherit from a single superclass D. Hiding the complexity of certain operations and showing only essential features Answer: D Explanation: Abstraction in OOP is about hiding the complex implementation details and exposing only the essential features, allowing the user to focus on what an object does rather than how it does it. Q#42. Which of the following best describes method overloading? A. Methods in different classes with the same name and parameters B. A method in a subclass with the same name as in the superclass C. Multiple methods in the same class with the same name but different parameters D. A method in an interface that is redefined in its implementing classes Answer: C Explanation: Method overloading occurs within the same class, where methods share the same name but differ in their parameters (number or type). Q#43. A developer needs to initialize a class Car with a specific make and model as soon as an instance of Car is created. What should the developer implement in Car? A. Setter method B. Constructor C. Static method D. Destructor Answer: B Explanation: A constructor is used to initialize an object upon creation, allowing specific attributes to be set when the instance is created. Q#44. In programming languages like C++, what purpose does a destructor serve? A. It creates an object B. It resets all object properties to their default values C. It deletes an object’s memory when it’s no longer needed D. It clones an object Answer: C Explanation: A destructor is used in languages like C++ to clean up resources and free memory when an object is no longer needed, ensuring efficient memory management. Q#45. Which of the following best describes dynamic binding? A. Method resolution occurs at runtime B. Method resolution occurs at compile time C. Binding a static method to a class at compile time D. Linking a variable to a memory location during program startup Answer: A Explanation: Dynamic binding is the process of resolving method calls at runtime, which is common in polymorphic behavior where a specific implementation is selected based on the object type. Q#46. In an online shopping system, a Customer can place multiple Order objects, but Order objects do not own the Customer object. What type of relationship does this describe? A. Composition B. Aggregation C. Association D. Dependency Answer: C Explanation: Association describes a relationship where objects interact without ownership or lifecycle control. Here, Customer and Order interact but do not depend on each other for lifecycle management. Q#47. Consider the following code: class Animal { public void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void sound() { System.out.println("Woof"); } } class Cat extends Animal { @Override public void sound() { System.out.println("Meow"); } } What concept allows different classes (Dog and Cat) to implement their own version of the sound() method? A. Method Overloading B. Method Overriding C. Inheritance D. Encapsulation Answer: B Explanation: Method overriding allows subclasses to provide a specific implementation of a method that is defined in their superclass, enabling polymorphic behavior when sound() is called on an Animal reference. Q#48. Which of the following scenarios best describes a dependency relationship? A. A Teacher has multiple Student objects, and the Student objects can exist independently B. A User class that takes a LoginService object to authenticate the user C. A Library object that contains multiple Book objects that do not depend on the library D. An Order class that contains an Item object and manages its lifecycle Answer: B Explanation: Dependency refers to a relationship where one class relies on another to function. In this case, the User class depends on LoginService for authentication. Q#49. Which of the following examples is a case of static binding? A. A draw() method is called on different shapes at runtime B. A variable is linked to a memory address dynamically C. Method overloading within a single class D. A constructor is called during object creation Answer: C Explanation: Static binding occurs at compile time, as in the case of method overloading, where the compiler determines the method to call based on the parameters. Q#50. Which relationship is described as a “part-of” relationship with lifecycle dependency? A. Dependency B. Association C. Composition D. Aggregation Answer: C Explanation: Composition represents a strong “part-of” relationship where the contained object’s lifecycle is linked to the container object’s lifecycle. If the container is destroyed, so its parts are also destroyed. Q#51. Which OOP concept ensures that data is only accessible through specific methods in a class? A. Inheritance B. Polymorphism C. Encapsulation D. Abstraction Answer: C Explanation: Encapsulation restricts direct access to data within a class, allowing access only through specific methods. This ensures better control over data manipulation and integrity. Q#52. Which component in the code below is responsible for defining the structure and behaviors of Book? class Book { String title; String author; void read() { System.out.println("Reading " + title); } } A. Book object B. Book class C. title variable D. read() method Answer: B Explanation: The Book class defines the blueprint of the Book object, including its properties (title, author) and behaviors (read() method). Q#53. In the following code, what is myCar? Car myCar = new Car(); A. Method B. Class C. Object D. Constructor Answer: C Explanation: myCar is an instance (object) of the Car class, created using the new keyword which allocates memory and calls the constructor. Q#54. Consider the following code. Which of these statements are correct about Laptop laptop1 = new Laptop(“Dell”);? public class Laptop { private String brand; public Laptop(String brand) { this.brand = brand; } } Laptop laptop1 = new Laptop("Dell"); A. laptop1 is an instance of the Laptop class with its brand initialized to “Dell”. B. The new keyword creates a new class template Laptop. C. The constructor Laptop(String brand) initializes the brand attribute of laptop1. D. Laptop must extend another class for laptop1 to be initialized. Answer: A, C Explanation: A and C are correct as laptop1 is an instance with brand initialized through the constructor. B is incorrect as the new keyword creates an object, not a class. D is incorrect as there’s no requirement for inheritance here. Related
OOP (Object Oriented Programming) concepts are the essential & the most important concepts in any Object Oriented Programming Language. Any exam or interview of an OOP language is considered incomplete without these questions. Therefore, it becomes crucial to practice them thoroughly time to time. Certainly, we will be talking about the practice test of OOP concepts in this article. It helps in thorough revision of the subject and make the concept crystal clear. Here in this article, we will focus on Interview Questions for Object Oriented Programming in the form of MCQs Practice Test with a variety of questions such as concept-based, code-based, and scenario-based. At the end of each question, we will explore the detailed explanation of each question. In fact, we should try to revise these concepts to make them crystal clear. Table of Contents Toggle What all Concepts are covered in this article?Interview Questions for Object Oriented Programming What all Concepts are covered in this article? Below is the list of concepts that are covered in this article in the form of MCQs Practice Test: 1) Class  2) Object  3) Encapsulation  4) Abstraction 5) Inheritance 6) Polymorphism   7) Method Overloading 8) Method Overriding   9) Constructor 10) Destructor    11) Interface  12) Static Binding  13) Dynamic Binding 14) Message Passing  15) Composition   16) Aggregation 17) Association   18) Dependency Interview Questions for Object Oriented Programming Q#1. Which of the following statement is incorrect about method overloading and method overriding? A. In method overloading, type of method parameters can be different, while in method overriding they must be same. B. In overloading, methods may have different return types, but in case of overriding methods must have same or covariant return types. C. Methods with modifiers ‘private’, ‘static’, and ‘final’ can be overloaded & overridden as well. D. Overriding is an example of Runtime polymorphism, while overloading is an example of compile-time polymorphism. Answer: C Explanation: Methods with modifiers ‘private’, ‘static’, and ‘final’ can be overloaded, but can’t be overridden. JDK 1.5 onward, the overriding method’s return type can be the sub-type of it’s parent class method’s return type. This is called covariant return type. The covariant return type always works only for non-primitive return types. Pleasse check an example of covariant return type. You may also check the complete detail on what is Method Overloading & Method Overriding?. Q#2. Which of the following code snippets correctly demonstrates method overloading? A) public void display(String message) { } public int display(String message) { return 0; } B) public void display(String message) { } public void display(int number) { } C) public int display(String message, int number) { return 0; } public String display(String message, int number) { return number; } D) public void display(String message) { } public void display(String message, String additional) { } Answer: B, D Explanation: In method overloading, methods in the same class share the same name but have different parameter types or counts. Options B and D satisfy these conditions. Q#3. Which OOP concept is used to derive a new class from an existing class, inheriting its attributes and behaviors? A. Composition B. Inheritance C. Encapsulation D. Polymorphism Answer: B Explanation: Inheritance allows one class to derive from another, inheriting attributes and behaviors, promoting code reuse and hierarchy. Q#4. Select the code snippet from below options that best demonstrates the concept of abstraction. A) abstract class Vehicle { abstract void startEngine(); } B) class Vehicle { public void startEngine() { System.out.println("Engine started."); } } C) interface Vehicle { void startEngine(); } D) class Car { private String model; void startEngine() { } } Answer: A, C Explanation: Abstraction hides implementation details, which can be achieved with abstract classes and interfaces, as shown in options A and C. Q#5. In a graphic editor, shapes like Circle, Square, and Triangle need to implement a common draw() method. Which concept allows each shape to implement its own version of draw() having common method signature? A. Inheritance B. Method Overloading C. Method Overriding D. Polymorphism Answer: C, D Explanation: Polymorphism and method overriding enable each shape to provide its specific implementation of draw() method, adhering to a common method signature. Q#6. Which of the following code snippets correctly implements method overriding? A) class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } B) class Animal { static void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { static void makeSound() { System.out.println("Bark"); } } C) class Animal { void makeSound(String sound) { System.out.println(sound); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } D) class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog { void makeSound() { System.out.println("Bark"); } } Answer: A Explanation: In method overriding, the subclass provides a specific implementation for a method already defined in its superclass with the same signature, as in option A. Since static methods can’t be overridden, therefore option B is incorrect. In option C method arguments don’t match. Option D doesn’t have inheritance relationship. Q#7. What is the main purpose of a constructor in a class? A. To initialize objects of the class B. To inherit from another class C. To override methods from the superclass D. To call methods with specific parameters Answer: A Explanation: A constructor is known as a special method used to initialize objects when they are created, setting initial values or configurations as needed. Q#8. You are designing a Library class that contains multiple Book objects, where the Book objects exist only within the Library. Which relationship best fits in this situation? A. Aggregation B. Association C. Composition D. Inheritance Answer: C Explanation: Composition implies a strong relationship where the lifecycle of Book objects is tied to the Library. When Library is destroyed, the Book objects are also destroyed. Q#9. Which of the following code snippets demonstrates polymorphism? A) class Animal { void sound() { System.out.println("Some sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } } Animal animal = new Dog(); animal.sound();  B) class Vehicle { void start() { System.out.println("Vehicle starting"); } } class Car extends Vehicle { void start() { System.out.println("Car starting"); } } Car car = new Car(); car.start();  C) class Shape { void draw() { System.out.println("Drawing"); } } Shape shape = new Shape(); shape.draw();  D) interface Drawable { void draw(); } class Circle implements Drawable { public void draw() { System.out.println("Drawing Circle"); } } Circle circle = new Circle(); circle.draw(); Answer: A Explanation: Polymorphism allows the method call to resolve at runtime. In option A, the sound() method demonstrates polymorphism, as the Animal reference holds a Dog object and calls Dog’s sound() method. Q#10. What is the main goal of dependency injection in OOP? A. To reduce code duplication B. To allow for dynamic method binding C. To reduce coupling between classes D. To allow classes to inherit each other’s attributes Answer: C Explanation: Dependency injection is used to reduce coupling between classes by injecting dependencies rather than creating them within the class, promoting flexibility and testability.
Q#11. Which of the following terms best describes the relationship between a superclass and a subclass? A. “Depends-on” relationship B. “Has-a” relationship C. “Uses-a” relationship D. “Is-a” relationship Answer: D Explanation: Inheritance describes an “is-a” relationship, where a subclass is a specialized form of its superclass, sharing attributes and behaviors. Q#12. Which of the following statements are true about abstraction in OOP? Abstraction focuses on hiding the implementation details. Abstract classes cannot contain any implemented methods. Interfaces are a way to achieve abstraction. Only abstract classes can be instantiated. A. 1 & 3 B. 2 & 4 C. 1, 2 & 3 D. 1, 3 & 4 Answer: A Explanation: Abstraction focuses on hiding unnecessary details (1) and can be achieved through interfaces or abstract classes (3). Abstract classes can contain implemented methods, and they cannot be instantiated. Q#13. If a class Animal has a method speak() that is overridden in the subclass Dog, which concept allows the speak() method to call the subclass’s implementation when an Animal reference holds a Dog object? A. Static Binding B. Dynamic Binding C. Overloading D. Aggregation Answer: B Explanation: Dynamic binding (or late binding) allows the method to be resolved at runtime, so the Dog class’s implementation of speak() will be called. Q#14. Identify which code snippets correctly show method overloading. A) public void printInfo(int id) { } public void printInfo(String name) { }  B) public int calculateArea(int side) { return side * side; } public int calculateArea(int length, int width) { return length * width; }  C) public void display() { } public void display() { System.out.println("Hello"); } D) public void setDetails(int age) { } public void setDetails(int age, String name) { } Answer: A, B, D Explanation: Method overloading occurs when multiple methods in the same class have the same name but different parameters. Options A, B, and D matches with this condition, while C does not due to identical parameter lists. Q#15. In which of the following relationships does one class contain another class, and the contained class’s existence is dependent on the container? A. Association B. Aggregation C. Inheritance D. Composition Answer: D Explanation: Composition implies a strong relationship, where the lifecycle of the contained object depends on the lifecycle of the containing object. Q#16. Which relationship type is present if a Teacher class and Course class are associated but independent of each other? A. Composition B. Aggregation C. Association D. Inheritance Answer: C Explanation: Association represents a relationship between classes that are connected but independent in lifecycle. Q#17. Which of the following is an example of static binding? A. Overloading methods B. Overriding methods C. Using interfaces D. Creating instances of abstract classes Answer: A Explanation: Static binding (compile-time binding) occurs with overloaded methods where the compiler determines the method to be called based on the method signature. Q#18. Which code correctly defines a constructor for the Student class? A) public void Student() { } B) public Student() { } C) private Student() { } D) void Student() { } Answer: B, C Explanation: Constructors must have the same name as the class and no return type. Options B and C are valid constructors for Student. Q#19. In an e-commerce system, a Payment interface is defined with a processPayment() method. Different payment types (like CreditCardPayment and PayPalPayment) implement this interface. Which concept does this demonstrate? A. Encapsulation B. Abstraction C. Inheritance D. Dependency Answer: B Explanation: The Payment interface abstracts the processPayment() behavior, allowing different implementations without specifying the actual details. Q#20. What is the main purpose of a destructor in OOP? A. To initialize objects B. To provide additional functionalities to methods C. To clean up resources before an object is destroyed D. To encapsulate data Answer: C Explanation: Destructors are used to release resources (like memory) when an object’s lifecycle ends, especially in languages without garbage collection. Q#21. Which of the following terms best describes the relationship when one class depends on another to perform its functions? A. Aggregation B. Dependency C. Composition D. Polymorphism Answer: B Explanation: Dependency describes a relationship where one class requires another to perform its functions, often implemented through parameters or method calls. Q#22. What is true about interfaces in OOP? A. Interfaces can’t contain static fields B. Interfaces are used for encapsulation C. Interfaces can be instantiated directly D. Methods in interfaces have no implementation Answer: D Explanation: Interfaces define method signatures without implementation, providing a way to enforce certain behaviors without detailing how they are executed. Fields in interfaces are by default public static final. Q#23. Which of the following situations exemplifies dynamic binding? A. A superclass reference variable pointing to a subclass object B. Invoking overloaded methods at compile time C. A constructor setting initial values for fields D. Creating an interface implementation Answer: A Explanation: Dynamic binding allows a superclass reference to invoke overridden methods in the subclass at runtime, that enables polymorphic behavior. Q#24. Which code snippet demonstrates polymorphism? A) Animal animal = new Dog(); animal.speak(); B) Dog dog = new Dog(); dog.speak(); C) Animal animal = new Animal(); animal.speak(); D) Dog dog = (Dog) new Animal(); dog.speak(); Answer: A Explanation: Polymorphism is demonstrated by using a superclass reference (Animal) to refer to a subclass object (Dog) and invoking overridden methods.
Q#25. Which of the following options correctly describes encapsulation in OOP? A. Allowing one interface to be used for a general class of actions B. Inheriting properties and methods from a parent class C. Binding data and methods that manipulate the data within a single unit D. Enforcing unique behaviors in each subclass Answer: C Explanation: Encapsulation is the process of bundling data and methods that operate on that data within a single unit, typically through a class. It helps protect data from unauthorized access. Q#26. In the code snippet below, what is myDog? class Dog { String breed; } Dog myDog = new Dog(); A. A class that holds details of various dogs B. An instance (object) of the Dog class C. A method to create dog breeds D. A variable that defines a dog’s breed Answer: B Explanation: myDog is an instance (object) of the Dog class, created using the new keyword to instantiate the Dog class and allocate memory for myDog. Q#27. A class Employee has properties and methods common to all employees, while subclasses like Manager and Developer inherit these common properties but add specific behaviors. What concept does this best illustrate? A. Encapsulation B. Inheritance C. Polymorphism D. Aggregation Answer: B Explanation: Inheritance allows subclasses to inherit common attributes and behaviors from a superclass, making it easier to manage shared code and add specific functionality in each subclass. Q#28. A system includes an interface Shape with a method draw(). Classes Circle, Square, and Triangle implement Shape, each providing its own implementation of draw(). What concept allows the program to call draw() on any Shape object, regardless of its specific type? A. Inheritance B. Polymorphism C. Encapsulation D. Composition Answer: B Explanation: Polymorphism allows a single interface to be used with different implementations, enabling the program to invoke draw() without needing to know the specific type of Shape. Q#29. Which option represents the “depends on” relationship in OOP? A. Aggregation B. Inheritance C. Dependency D. Composition Answer: C Explanation: Dependency represents a weak relationship where one class depends on another to function, but this dependency does not imply ownership or control. Q#30. Which of the following relationships describes a “has-a” relationship where the lifecycle of the contained objects is independent? A. Aggregation B. Association C. Composition D. Dependency Answer: A Explanation: Aggregation represents a “has-a” relationship, where the containing object does not own the lifecycle of the contained object; they can exist independently. Q#31. Which of the following is true about an interface in Java? A. An interface can contain static and default methods. B. An interface can’t be instantiated directly. C. An interface can contain only method signatures and constants. D. An interface enforces private access on its methods. Answer: B Explanation: An interface in Java till JDK 7 contains only method signatures (abstract methods) and constants (public, static, final fields), making it a contract for classes that implement it. JDK 8 onward as a new features introduced in Java 8, an interface can contain static & default methods as well. Default & static methods have implementation (method body). Q#32. Which statement best defines message passing in OOP? A. Sending requests to other classes to execute specific functions B. Directly accessing another class’s private variables C. Cloning objects across classes D. Binding methods statically during compile time Answer: A Explanation: Message passing refers to the process where objects communicate with each other by invoking methods, enabling interaction within an object-oriented system. Q#33. Which of the following constructors is correctly written for the class Book? A) public void Book() { } B) public Book() { } C) Book public() { } D) public int Book() { } Answer: B Explanation: Constructors must have the same name as the class and cannot have a return type, making option B the only valid constructor. Q#34. Given the following code, which statement best describes Person? public class Person { String name; int age; } A. Person is an instance of a class B. Person is a class that serves as a template for creating objects C. Person is an interface with attributes name and age D. Person is an enumeration of attributes Answer: B Explanation: Person is a class that defines attributes (name and age) and serves as a template for creating Person objects. Q#35. A developer creates a class BankAccount where the balance field is private and can only be accessed and modified using methods deposit() and withdraw(). Which concept is being applied here? A. Inheritance B. Polymorphism C. Aggregation D. Encapsulation Answer: D Explanation: Encapsulation is used to restrict direct access to certain properties, enforcing controlled access through methods. Q#36. Which of the following is an example of static binding? A. Method overriding B. Method overloading C. Dynamic polymorphism D. Abstract method implementation Answer: B Explanation: Static binding occurs at compile-time, and method overloading is an example since the compiler resolves the method call based on the method signature. Q#37. A software module defines an abstract class Vehicle with an abstract method startEngine(). Concrete subclasses like Car and Bike provide specific implementations of this method. What concept does this demonstrate? A. Encapsulation B. Inheritance C. Abstraction D. Aggregation Answer: C Explanation: Abstraction involves defining a method in a generic way, allowing specific classes to implement their versions. This hides unnecessary details and allows focus on essential features. Q#38. A Library class contains multiple Book objects. If a Library is deleted, all Book objects are also deleted. What relationship does this illustrate? A. Aggregation B. Composition C. Inheritance D. Dependency Answer: B Explanation: Composition indicates a strong relationship where the lifecycle of the contained objects (Book) is tied to the lifecycle of the container (Library). Q#39. Which statement accurately defines polymorphism? A. Creating a class from another class B. Using a class solely as a data holder C. Enforcing access restrictions on class members D. Allowing multiple forms of a method or an object Answer: D Explanation: Polymorphism allows objects to be treated as instances of their parent class, enabling methods to have multiple forms based on context. Q#40. A software developer designs a Database interface with methods like connect(), disconnect(), and executeQuery(). Classes like MySQLDatabase and MongoDatabase implement these methods. What principle is demonstrated here? A. Encapsulation B. Interface-based design C. Inheritance D. Aggregation Answer: B Explanation: Interface-based design enables classes to define specific behaviors according to a common contract (interface), promoting flexibility and standardization in application design.
Q#41. Which of the following statements best defines abstraction in OOP? A. Combining data and methods within a single unit B. Ensuring different classes have different implementations of a method C. Allowing multiple classes to inherit from a single superclass D. Hiding the complexity of certain operations and showing only essential features Answer: D Explanation: Abstraction in OOP is about hiding the complex implementation details and exposing only the essential features, allowing the user to focus on what an object does rather than how it does it. Q#42. Which of the following best describes method overloading? A. Methods in different classes with the same name and parameters B. A method in a subclass with the same name as in the superclass C. Multiple methods in the same class with the same name but different parameters D. A method in an interface that is redefined in its implementing classes Answer: C Explanation: Method overloading occurs within the same class, where methods share the same name but differ in their parameters (number or type). Q#43. A developer needs to initialize a class Car with a specific make and model as soon as an instance of Car is created. What should the developer implement in Car? A. Setter method B. Constructor C. Static method D. Destructor Answer: B Explanation: A constructor is used to initialize an object upon creation, allowing specific attributes to be set when the instance is created. Q#44. In programming languages like C++, what purpose does a destructor serve? A. It creates an object B. It resets all object properties to their default values C. It deletes an object’s memory when it’s no longer needed D. It clones an object Answer: C Explanation: A destructor is used in languages like C++ to clean up resources and free memory when an object is no longer needed, ensuring efficient memory management. Q#45. Which of the following best describes dynamic binding? A. Method resolution occurs at runtime B. Method resolution occurs at compile time C. Binding a static method to a class at compile time D. Linking a variable to a memory location during program startup Answer: A Explanation: Dynamic binding is the process of resolving method calls at runtime, which is common in polymorphic behavior where a specific implementation is selected based on the object type. Q#46. In an online shopping system, a Customer can place multiple Order objects, but Order objects do not own the Customer object. What type of relationship does this describe? A. Composition B. Aggregation C. Association D. Dependency Answer: C Explanation: Association describes a relationship where objects interact without ownership or lifecycle control. Here, Customer and Order interact but do not depend on each other for lifecycle management. Q#47. Consider the following code: class Animal { public void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void sound() { System.out.println("Woof"); } } class Cat extends Animal { @Override public void sound() { System.out.println("Meow"); } } What concept allows different classes (Dog and Cat) to implement their own version of the sound() method? A. Method Overloading B. Method Overriding C. Inheritance D. Encapsulation Answer: B Explanation: Method overriding allows subclasses to provide a specific implementation of a method that is defined in their superclass, enabling polymorphic behavior when sound() is called on an Animal reference. Q#48. Which of the following scenarios best describes a dependency relationship? A. A Teacher has multiple Student objects, and the Student objects can exist independently B. A User class that takes a LoginService object to authenticate the user C. A Library object that contains multiple Book objects that do not depend on the library D. An Order class that contains an Item object and manages its lifecycle Answer: B Explanation: Dependency refers to a relationship where one class relies on another to function. In this case, the User class depends on LoginService for authentication. Q#49. Which of the following examples is a case of static binding? A. A draw() method is called on different shapes at runtime B. A variable is linked to a memory address dynamically C. Method overloading within a single class D. A constructor is called during object creation Answer: C Explanation: Static binding occurs at compile time, as in the case of method overloading, where the compiler determines the method to call based on the parameters. Q#50. Which relationship is described as a “part-of” relationship with lifecycle dependency? A. Dependency B. Association C. Composition D. Aggregation Answer: C Explanation: Composition represents a strong “part-of” relationship where the contained object’s lifecycle is linked to the container object’s lifecycle. If the container is destroyed, so its parts are also destroyed. Q#51. Which OOP concept ensures that data is only accessible through specific methods in a class? A. Inheritance B. Polymorphism C. Encapsulation D. Abstraction Answer: C Explanation: Encapsulation restricts direct access to data within a class, allowing access only through specific methods. This ensures better control over data manipulation and integrity. Q#52. Which component in the code below is responsible for defining the structure and behaviors of Book? class Book { String title; String author; void read() { System.out.println("Reading " + title); } } A. Book object B. Book class C. title variable D. read() method Answer: B Explanation: The Book class defines the blueprint of the Book object, including its properties (title, author) and behaviors (read() method). Q#53. In the following code, what is myCar? Car myCar = new Car(); A. Method B. Class C. Object D. Constructor Answer: C Explanation: myCar is an instance (object) of the Car class, created using the new keyword which allocates memory and calls the constructor. Q#54. Consider the following code. Which of these statements are correct about Laptop laptop1 = new Laptop(“Dell”);? public class Laptop { private String brand; public Laptop(String brand) { this.brand = brand; } } Laptop laptop1 = new Laptop("Dell"); A. laptop1 is an instance of the Laptop class with its brand initialized to “Dell”. B. The new keyword creates a new class template Laptop. C. The constructor Laptop(String brand) initializes the brand attribute of laptop1. D. Laptop must extend another class for laptop1 to be initialized. Answer: A, C Explanation: A and C are correct as laptop1 is an instance with brand initialized through the constructor. B is incorrect as the new keyword creates an object, not a class. D is incorrect as there’s no requirement for inheritance here.