Skip to content

Latest commit

 

History

History
587 lines (398 loc) · 14.6 KB

File metadata and controls

587 lines (398 loc) · 14.6 KB

1. Which of the following is NOT one of the four fundamental principles of OOP?

A) Encapsulation
B) Inheritance
C) Polymorphism
D) Compilation

Answer: D) Compilation


2. What is the process of hiding the internal implementation details and showing only the functionality is called?

A) Inheritance
B) Abstraction
C) Polymorphism
D) Encapsulation

Answer: B) Abstraction


3. In OOP, what does the term "encapsulation" refer to?

A) The ability of different classes to be treated as instances of the same class
B) Combining data and functions that manipulate the data into a single unit
C) Deriving new classes from existing ones
D) Allowing multiple forms through a single interface

Answer: B) Combining data and functions that manipulate the data into a single unit


4. Which of the following keywords is used to inherit a class in Java?

A) implements
B) extends
C) inherits
D) derives

Answer: B) extends


5. What is polymorphism in OOP?

A) The ability of a class to have multiple constructors
B) The ability of different classes to respond to the same function call in different ways
C) The mechanism of hiding data
D) The process of creating a new class from an existing class

Answer: B) The ability of different classes to respond to the same function call in different ways


6. Which of the following is true about a class in OOP?

A) It is an instance of an object
B) It cannot contain methods
C) It defines the properties and behaviors of objects
D) It is always abstract

Answer: C) It defines the properties and behaviors of objects


7. What is a constructor in OOP?

A) A special method used to destroy objects
B) A method that performs a specific function
C) A special method used to initialize objects
D) A method that cannot be overridden

Answer: C) A special method used to initialize objects


8. Which access specifier allows access to the members of a class only within the same class?

A) Public
B) Private
C) Protected
D) Default

Answer: B) Private


9. What is the output of the following Java code?

class A {
    A() {
        System.out.println("A");
    }
}

class B extends A {
    B() {
        System.out.println("B");
    }
}

public class Test {
    public static void main(String[] args) {
        new B();
    }
}

A) A
B) B
C) A B
D) B A

Answer: C) A B


10. Which of the following is an example of method overloading?

A) Two methods with the same name and same parameters
B) Two methods with the same name but different parameters
C) Two methods with different names and same parameters
D) Two methods with different names and different parameters

Answer: B) Two methods with the same name but different parameters


11. What is an abstract class?

A) A class that cannot have any subclasses
B) A class that can have abstract methods
C) A class that cannot have any methods
D) A class that can be instantiated directly

Answer: B) A class that can have abstract methods


12. Which of the following best describes inheritance?

A) The process by which one class takes on the attributes and methods of another
B) The ability to define multiple methods with the same name
C) The technique of restricting access to certain details
D) The creation of a new class that is a modified version of an existing class

Answer: A) The process by which one class takes on the attributes and methods of another


13. In C++, what is the default access specifier for class members?

A) Public
B) Private
C) Protected
D) Internal

Answer: B) Private


14. Which of the following is NOT a feature of OOP?

A) Encapsulation
B) Recursion
C) Inheritance
D) Polymorphism

Answer: B) Recursion


15. What is an interface in Java?

A) A class that can be instantiated
B) A blueprint of a class that contains static methods
C) A reference type that can contain only abstract methods and constants
D) A class that implements all methods of its superclass

Answer: C) A reference type that can contain only abstract methods and constants


16. Which of the following statements is true about multiple inheritance in Java?

A) Java supports multiple inheritance using classes
B) Java does not support multiple inheritance
C) Java supports multiple inheritance using interfaces
D) Both B and C

Answer: D) Both B and C


17. What is the main purpose of the 'super' keyword in Java?

A) To refer to the current class object
B) To refer to the parent class object
C) To create a new object
D) To terminate the current method

Answer: B) To refer to the parent class object


18. Which of the following is a correct way to create an object in C++?

A) ClassName obj = new ClassName();
B) ClassName obj();
C) ClassName obj;
D) object obj = ClassName();

Answer: C) ClassName obj;


19. What does the 'this' keyword represent in Java?

A) The superclass object
B) The current object
C) A static method
D) An interface

Answer: B) The current object


20. Which of the following is NOT a type of constructor in OOP?

A) Default constructor
B) Parameterized constructor
C) Copy constructor
D) Virtual constructor

Answer: D) Virtual constructor


21. What is the purpose of a destructor in C++?

A) To initialize object properties
B) To release resources before the object is destroyed
C) To create a new object
D) To copy an object

Answer: B) To release resources before the object is destroyed


22. Which OOP concept allows a class to use methods of another class without inheriting from it?

A) Polymorphism
B) Composition
C) Encapsulation
D) Abstraction

Answer: B) Composition


23. What is the term for functions that have the same name but different implementations in different classes?

A) Overloading
B) Overriding
C) Hiding
D) Shadowing

Answer: B) Overriding


24. In which scenario would you use an abstract class instead of an interface?

A) When you want to define a contract without any implementation
B) When you want to provide some common base functionality
C) When multiple inheritance is required
D) When you have only static methods

Answer: B) When you want to provide some common base functionality


25. What is the relationship between classes when using inheritance?

A) Has-a
B) Uses-a
C) Is-a
D) Knows-a

Answer: C) Is-a


26. Which of the following best describes a "pure virtual function" in C++?

A) A function with a body that must be overridden
B) A function without a body that must be overridden
C) A function that cannot be overridden
D) A function that is static

Answer: B) A function without a body that must be overridden


27. What is the output of the following C++ code?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base"; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived"; }
};

int main() {
    Base *b = new Derived();
    b->show();
    return 0;
}

A) Base
B) Derived
C) Compilation error
D) Runtime error

Answer: B) Derived


28. Which design principle promotes the idea that classes should be open for extension but closed for modification?

A) Single Responsibility Principle
B) Open/Closed Principle
C) Liskov Substitution Principle
D) Interface Segregation Principle

Answer: B) Open/Closed Principle


29. What is the Liskov Substitution Principle?

A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness
B) A class should have only one reason to change
C) High-level modules should not depend on low-level modules
D) Interfaces should be specific to client needs

Answer: A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness


30. Which of the following is NOT a SOLID principle?

A) Single Responsibility
B) Open/Closed
C) Liskov Substitution
D) Data Hiding

Answer: D) Data Hiding


31. In UML diagrams, what symbol represents a class?

A) A circle
B) A rectangle divided into three sections
C) A diamond
D) A triangle

Answer: B) A rectangle divided into three sections


32. What is the purpose of a getter method in a class?

A) To modify the value of a private variable
B) To retrieve the value of a private variable
C) To delete a private variable
D) To initialize a private variable

Answer: B) To retrieve the value of a private variable


33. Which of the following best describes "association" in OOP?

A) A "has-a" relationship between two classes
B) A "is-a" relationship between two classes
C) A relationship where one class inherits from another
D) A relationship where one class contains only static members

Answer: A) A "has-a" relationship between two classes


34. What is "method overriding"?

A) Defining multiple methods with the same name in the same class
B) Changing the method signature in a subclass
C) Providing a specific implementation of a method already defined in its superclass
D) Hiding a method in the superclass

Answer: C) Providing a specific implementation of a method already defined in its superclass


35. Which of the following is true about interfaces in Java?

A) A class can implement multiple interfaces
B) An interface can contain constructors
C) Interfaces can have instance variables
D) Interfaces can extend classes

Answer: A) A class can implement multiple interfaces


36. What is "composition" in OOP?

A) A class deriving from another class
B) A class containing objects of other classes
C) A class implementing an interface
D) A class hiding its data members

Answer: B) A class containing objects of other classes


37. In C#, which keyword is used to prevent a class from being inherited?

A) sealed
B) static
C) abstract
D) final

Answer: A) sealed


38. What is the difference between an abstract class and an interface?

A) Abstract classes cannot have implemented methods, interfaces can
B) Interfaces cannot have any methods, abstract classes can
C) Abstract classes can have implemented methods, interfaces cannot (prior to Java 8)
D) There is no difference

Answer: C) Abstract classes can have implemented methods, interfaces cannot (prior to Java 8)


39. Which of the following best defines a "singleton" design pattern?

A) A class that can be instantiated multiple times
B) A class that restricts the instantiation to one single object
C) A class that implements multiple interfaces
D) A class that inherits from multiple classes

Answer: B) A class that restricts the instantiation to one single object


40. What does "dependency injection" refer to in OOP?

A) A technique where a class creates its dependencies
B) A technique where dependencies are provided to a class from the outside
C) A way to hide dependencies within a class
D) A method of inheriting dependencies from a superclass

Answer: B) A technique where dependencies are provided to a class from the outside

In Object-Oriented Programming (OOP), association and composition are two fundamental relationships between objects. Let's break them down with explanations and Java examples.


Association

  • Definition: Association represents a "uses-a" or "has-a" relationship between two or more objects.
  • Characteristics:
    • It is a general relationship where objects can exist independently.
    • There are two types of association:
      1. Unidirectional: One object can access the other.
      2. Bidirectional: Both objects can access each other.

Example of Association in Java

class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Course {
    private String courseName;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseName() {
        return courseName;
    }
}

public class AssociationExample {
    public static void main(String[] args) {
        Student student = new Student("Alice");
        Course course = new Course("Mathematics");

        System.out.println(student.getName() + " is enrolled in " + course.getCourseName());
    }
}

Here, Student and Course are associated. They exist independently and are connected through the enrolled concept.


Composition

  • Definition: Composition is a "part-of" relationship where one object is a part of another object.
  • Characteristics:
    • Strong form of association.
    • The lifecycle of the "part" is dependent on the "whole."
    • If the "whole" object is destroyed, the "part" objects are also destroyed.

Example of Composition in Java

class Engine {
    private String type;

    public Engine(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

class Car {
    private String brand;
    private Engine engine;

    public Car(String brand, String engineType) {
        this.brand = brand;
        this.engine = new Engine(engineType); // Composition
    }

    public void showDetails() {
        System.out.println("Car: " + brand);
        System.out.println("Engine: " + engine.getType());
    }
}

public class CompositionExample {
    public static void main(String[] args) {
        Car car = new Car("Toyota", "V8");
        car.showDetails();
        // If the car object is destroyed, the engine object will also be destroyed.
    }
}

In this example, Car contains an Engine. The Engine object does not exist independently; it is part of the Car.


Key Differences Between Association and Composition

Aspect Association Composition
Dependency Objects can exist independently. Part objects depend on the whole object.
Lifetime Independent of each other. Lifecycle of part depends on the whole.
Type General relationship (e.g., "uses-a"). Strong relationship (e.g., "part-of").

Both are critical in modeling real-world relationships in OOP and help in creating robust, modular designs.