Docsity

Object‑Oriented Programming OOP Ultimate Exam, Exams of Technology

Toggle sidebar
bg1
Object-Oriented Programming OOP
Ultimate Exam
Question 1. Which concept describes the process of creating a new object from a class blueprint?
A) Inheritance
B) Instantiation
C) Encapsulation
D) Polymorphism
Answer: B
Explanation: Instantiation is the process of creating a specific object from a class, which serves as the
blueprint for objects.
Question 2. What is the primary benefit of using encapsulation in OOP?
A) Enhances code readability
B) Allows multiple inheritance
C) Protects data integrity by restricting access
D) Facilitates method overloading
Answer: C
Explanation: Encapsulation restricts direct access to object data, protecting its integrity and hiding
implementation details.
Question 3. Which of the following best describes a class in object-oriented programming?
A) An instance of an object
B) A blueprint for creating objects
C) A function that operates on data
D) A type of variable scope
Answer: B
Explanation: A class acts as a blueprint that defines attributes and behaviors for objects created from it.
Question 4. What does the "this" keyword refer to in most OOP languages?
A) The current class
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d

Partial preview of the text

Download Object‑Oriented Programming OOP Ultimate Exam and more Exams Technology in PDF only on Docsity!

Ultimate Exam

Question 1. Which concept describes the process of creating a new object from a class blueprint? A) Inheritance B) Instantiation C) Encapsulation D) Polymorphism Answer: B Explanation: Instantiation is the process of creating a specific object from a class, which serves as the blueprint for objects. Question 2. What is the primary benefit of using encapsulation in OOP? A) Enhances code readability B) Allows multiple inheritance C) Protects data integrity by restricting access D) Facilitates method overloading Answer: C Explanation: Encapsulation restricts direct access to object data, protecting its integrity and hiding implementation details. Question 3. Which of the following best describes a class in object-oriented programming? A) An instance of an object B) A blueprint for creating objects C) A function that operates on data D) A type of variable scope Answer: B Explanation: A class acts as a blueprint that defines attributes and behaviors for objects created from it. Question 4. What does the "this" keyword refer to in most OOP languages? A) The current class

Ultimate Exam

B) The current object instance C) The parent class D) The method’s return value Answer: B Explanation: "This" is a reference to the current object instance within class methods, allowing access to its members. Question 5. Which constructor type is used to create an object with default values? A) Parameterized constructor B) Copy constructor C) Default constructor D) Static constructor Answer: C Explanation: The default constructor initializes objects with default values when no parameters are provided. Question 6. What is the role of a destructor or finalizer in OOP? A) To initialize object attributes B) To perform cleanup and release resources before object destruction C) To override class methods D) To create a copy of an object Answer: B Explanation: Destructors or finalizers are invoked to clean up resources and perform necessary cleanup before an object is garbage collected. Question 7. Which access modifier allows class members to be accessible from any other class? A) private B) protected C) public

Ultimate Exam

Explanation: Overriding allows runtime polymorphism by replacing base class methods, while hiding (shadowing) occurs at compile-time, often with static methods. Question 11. Which relationship represents objects that contain other objects as part of their state? A) Inheritance B) Composition C) Aggregation D) Both B and C Answer: D Explanation: Both composition and aggregation represent "Has-A" relationships where objects contain references to other objects. Question 12. What distinguishes composition from aggregation? A) Composition implies ownership and lifecycle dependency; aggregation does not B) Aggregation implies ownership; composition does not C) They are interchangeable terms D) Composition is only used in procedural programming Answer: A Explanation: Composition means the contained object’s lifecycle depends on the container, whereas aggregation is a weaker association without lifecycle dependency. Question 13. Which feature of an abstract class allows it to define partial implementation? A) Concrete methods B) Pure virtual functions C) Final methods D) Static methods Answer: B Explanation: Pure virtual functions declare methods without implementation, requiring subclasses to provide concrete behavior.

Ultimate Exam

Question 14. Which of the following best describes an interface? A) A class with complete implementation B) A contract specifying methods without implementations C) A special type of constructor D) A class with only static methods Answer: B Explanation: An interface defines a contract that implementing classes must fulfill, specifying methods without providing their implementation. Question 15. What is static polymorphism primarily achieved through? A) Virtual functions B) Method overloading C) Method overriding D) Dynamic binding Answer: B Explanation: Static polymorphism is achieved via method overloading, where multiple methods have the same name but different parameters. Question 16. Which mechanism enables runtime decision-making to invoke the appropriate method implementation? A) Static binding B) Late binding C) Function overloading D) Compile-time polymorphism Answer: B Explanation: Late binding (dynamic binding) occurs at runtime, determining which method implementation to invoke based on the actual object type.

Ultimate Exam

Question 20. Which behavioral pattern provides a way to encapsulate requests as objects? A) Strategy B) Command C) Observer D) Singleton Answer: B Explanation: The Command pattern encapsulates a request as an object, allowing parameterization and queuing of requests. Question 21. How does exception propagation work in OOP languages? A) Exceptions are caught immediately at the point of error only B) Exceptions are passed up the call stack until caught or the program terminates C) Exceptions are only caught within the same method where they occur D) Exceptions are ignored by default Answer: B Explanation: Exception propagation involves passing exceptions up the call stack, allowing higher-level handlers to process errors. Question 22. Which block structure is used to ensure resources are properly released after use? A) try-catch-finally B) if-else C) switch-case D) loop Answer: A Explanation: The try-catch-finally block guarantees that cleanup code runs regardless of whether an exception occurs. Question 23. What is a custom exception? A) An exception that is built-in to the language

Ultimate Exam

B) An exception derived from the base Exception class, created for specific error conditions C) An exception that cannot be caught D) An exception used only for runtime errors Answer: B Explanation: Custom exceptions extend the base Exception class to represent domain-specific error conditions. Question 24. Which concept allows a method to operate on objects of different classes through a common interface? A) Method overloading B) Polymorphism C) Encapsulation D) Abstraction Answer: B Explanation: Polymorphism enables methods to process objects of different classes through shared interfaces or base classes. Question 25. In Java, which keyword is used to specify that a method can be overridden in a subclass? A) static B) final C) override D) virtual (in C++) Answer: D Explanation: In C++, the "virtual" keyword indicates a method can be overridden; Java uses the @Override annotation for similar purpose. Question 26. Which collection type maintains insertion order and allows duplicate elements? A) HashSet B) TreeSet

Ultimate Exam

Answer: B Explanation: Garbage collection automatically identifies and reclaims memory from objects no longer in use, preventing memory leaks. Question 30. Which feature of reflection allows inspecting class metadata at runtime? A) Serialization B) Annotations C) Reflection API D) Generics Answer: C Explanation: The Reflection API provides mechanisms to examine class details, fields, methods, and constructors during execution. Question 31. In object serialization, what is the main purpose? A) To execute code dynamically B) To convert objects into a byte stream for storage or transmission C) To enforce encapsulation D) To instantiate objects at runtime Answer: B Explanation: Serialization converts objects into byte streams for persistent storage or network transfer, enabling object reconstruction later. Question 32. Which type of inheritance involves a class having multiple parent classes? A) Single inheritance B) Multiple inheritance C) Multilevel inheritance D) Hierarchical inheritance Answer: B

Ultimate Exam

Explanation: Multiple inheritance allows a class to inherit features from more than one parent class, though some languages disallow it. Question 33. Which method signature is necessary for overriding a method in Java? A) Same method name and parameter types B) Same method name and different parameter types C) Different method name but same return type D) Any method signature in the subclass Answer: A Explanation: To override a method, the subclass method must have the same name and parameter types as the superclass. Question 34. What is the main advantage of the Open/Closed Principle? A) Classes should be open for extension but closed for modification B) Classes should be open for modification at any time C) Code should be open to changes during runtime D) Systems should be open to external modification only Answer: A Explanation: The principle promotes extending software behavior without altering existing code, enhancing maintainability. Question 35. Which pattern provides a simplified interface to a complex subsystem? A) Adapter B) Facade C) Decorator D) Observer Answer: B Explanation: The Facade pattern offers a unified, simplified interface to a set of interfaces within a subsystem.

Ultimate Exam

A) To implement a default behavior B) To define a function that must be overridden in derived classes, making the class abstract C) To prevent method overriding D) To provide static method functionality Answer: B Explanation: Pure virtual functions declare methods without implementation, enforcing derived classes to override them, thus making the class abstract. Question 40. Which principle states that clients should not be forced to depend on interfaces they do not use? A) Single Responsibility Principle B) Interface Segregation Principle C) Liskov Substitution Principle D) Dependency Inversion Principle Answer: B Explanation: The Interface Segregation Principle advocates for specific, client-focused interfaces to prevent unnecessary dependencies. Question 41. In the context of memory management, what does "heap" refer to? A) The area of memory used for static variables B) The area of memory used for dynamic allocation of objects during runtime C) The stack for primitive variables D) The cache memory Answer: B Explanation: The heap is used for dynamic memory allocation where objects are created and managed at runtime. Question 42. Which statement best describes method overloading? A) Changing the method implementation in a subclass

Ultimate Exam

B) Defining multiple methods with the same name but different parameter lists within the same class C) Replacing a base class method with a new implementation D) Creating a method that can handle multiple data types at runtime Answer: B Explanation: Method overloading involves defining methods with the same name but different parameter types or counts in the same class. Question 43. Which pattern is used to convert the interface of a class into another interface clients expect? A) Adapter B) Decorator C) Facade D) Strategy Answer: A Explanation: The Adapter pattern allows incompatible interfaces to work together by wrapping an existing class. Question 44. Which concept ensures that subclasses can substitute base class objects without affecting program correctness? A) Encapsulation B) Liskov Substitution Principle C) Dependency Inversion Principle D) Open/Closed Principle Answer: B Explanation: The Liskov Substitution Principle guarantees that subclasses can replace base class objects seamlessly. Question 45. Which of the following is NOT a benefit of polymorphism? A) Code reusability

Ultimate Exam

Answer: B Explanation: Static methods are associated with the class itself and can be called without creating an object instance. Question 49. In C++, what is the purpose of the "virtual" keyword in a base class method? A) To prevent method overriding B) To enable dynamic dispatch for that method C) To make the method static D) To declare a method that cannot be overridden Answer: B Explanation: The "virtual" keyword indicates that the method supports dynamic dispatch, allowing subclass overrides to be invoked at runtime. Question 50. Which data structure in the Collection Framework maintains key-value pairs? A) List B) Set C) Map D) Queue Answer: C Explanation: The Map interface stores data as key-value pairs, allowing efficient retrieval based on keys. Question 51. What does the "protected" access modifier allow? A) Access only within the class B) Access within the class and its subclasses C) Access from any class in the project D) No access outside the package Answer: B Explanation: "Protected" allows access to class members within the class itself and by subclasses.

Ultimate Exam

Question 52. Which principle encourages dependency on abstractions rather than concrete implementations? A) Single Responsibility Principle B) Dependency Inversion Principle C) Liskov Substitution Principle D) Interface Segregation Principle Answer: B Explanation: The Dependency Inversion Principle promotes depending on abstractions, reducing coupling. Question 53. Which is an example of a structural design pattern? A) Observer B) Strategy C) Decorator D) Command Answer: C Explanation: The Decorator pattern is a structural pattern that adds responsibilities to objects dynamically. Question 54. How does method hiding differ from method overriding? A) Hiding occurs at compile time; overriding occurs at runtime B) Hiding can only be done with static methods C) Overriding cannot change method signatures; hiding can D) All of the above Answer: D Explanation: Method hiding (static methods) is resolved at compile time, while overriding (instance methods) occurs at runtime; hiding can involve changing signatures.

Ultimate Exam

B) To restrict instantiation so only one object exists globally C) To generate a factory object for creating instances D) To implement a class with only static methods Answer: B Explanation: Singleton ensures a class has only one instance and provides a global access point. Question 59. What is the role of the "abstract" keyword in class declaration? A) To allow instantiation of the class B) To create a class that cannot be instantiated directly and may contain abstract methods C) To define a class with static members only D) To prevent inheritance Answer: B Explanation: An abstract class cannot be instantiated directly and may include abstract methods needing implementation in subclasses. Question 60. Which collection type provides thread-safe iteration in Java? A) ArrayList B) Vector C) HashMap D) HashSet Answer: B Explanation: Vector is synchronized, making it thread-safe during iteration, unlike ArrayList. Question 61. Which feature in C++ allows a class to specify that certain methods must be implemented by derived classes? A) Abstract class with pure virtual functions B) Virtual functions with default implementations C) Static methods

Ultimate Exam

D) Friend functions Answer: A Explanation: Abstract classes with pure virtual functions enforce derived classes to implement the specified methods. Question 62. What is the primary purpose of the "Facade" pattern? A) To add responsibilities to objects dynamically B) To provide a simplified interface to a complex set of interfaces C) To encapsulate request objects D) To create objects without exposing instantiation logic Answer: B Explanation: The Facade pattern offers a simplified interface, hiding complexity of subsystem components. Question 63. Which principle emphasizes that subclasses should retain the behavior of their parent classes? A) Single Responsibility Principle B) Open/Closed Principle C) Liskov Substitution Principle D) Interface Segregation Principle Answer: C Explanation: The Liskov Substitution Principle states subclasses should behave consistently with their base classes. Question 64. Which mechanism in C++ supports multiple inheritance? A) Using base classes separated by commas in class declaration B) Interfaces only C) Single inheritance only D) Composition only