Docsity

IT 212: OBJECT-ORIENTED PROGRAMMING — COMPREHENSIVE PRACTICE EXAMINATION | COMPLETE APPROV, Exams of Information Integration

Toggle sidebar
bg1
IT 212: OBJECT-ORIENTED PROGRAMMING —
COMPREHENSIVE PRACTICE EXAMINATION |
COMPLETE APPROVED PREP EXAM | 2027
LATEST UPDATED !!
1. What is the primary purpose of object-oriented programming (OOP)?
(a) To eliminate the use of variables in software development
(b) To organize software around objects that combine data and behavior
(c) To replace all algorithms with database queries
(d) To ensure programs execute without errors
Answer: (b) To organize software around objects that combine data and
behavior
Rationale: Object-oriented programming organizes software around objects that
encapsulate state (data) and behavior (methods). This promotes modularity,
reusability, and maintainability. Option (a) is incorrect because OOP still uses
variables and data structures. Option (c) is incorrect because OOP is not a
replacement for algorithms or database queries. Option (d) is incorrect because
OOP does not guarantee error-free execution; bugs can still occur in object-
oriented systems.
2. In object-oriented programming, what is a class?
(a) A running instance of a program
(b) A variable that stores only numerical values
(c) A blueprint defining the attributes and behaviors of objects
(d) A method that automatically deletes objects
Answer: (c) A blueprint defining the attributes and behaviors of objects
Rationale: A class defines the structure and behavior that objects created from it
may possess, including fields, constructors, and methods. Option (a) describes an
executing program rather than a class. Option (b) describes a restricted kind of
variable, not a class. Option (d) describes neither the purpose nor the defining
characteristics of a class.
3. Which term describes a specific object created from a 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

Partial preview of the text

Download IT 212: OBJECT-ORIENTED PROGRAMMING — COMPREHENSIVE PRACTICE EXAMINATION | COMPLETE APPROV and more Exams Information Integration in PDF only on Docsity!

IT 212: OBJECT-ORIENTED PROGRAMMING —

COMPREHENSIVE PRACTICE EXAMINATION |

COMPLETE APPROVED PREP EXAM | 2027

LATEST UPDATED !!

1. What is the primary purpose of object-oriented programming (OOP)? (a) To eliminate the use of variables in software development (b) To organize software around objects that combine data and behavior (c) To replace all algorithms with database queries (d) To ensure programs execute without errors Answer: (b) To organize software around objects that combine data and behavior Rationale: Object-oriented programming organizes software around objects that encapsulate state (data) and behavior (methods). This promotes modularity, reusability, and maintainability. Option (a) is incorrect because OOP still uses variables and data structures. Option (c) is incorrect because OOP is not a replacement for algorithms or database queries. Option (d) is incorrect because OOP does not guarantee error-free execution; bugs can still occur in object- oriented systems. 2. In object-oriented programming, what is a class? (a) A running instance of a program (b) A variable that stores only numerical values (c) A blueprint defining the attributes and behaviors of objects (d) A method that automatically deletes objects Answer: (c) A blueprint defining the attributes and behaviors of objects Rationale: A class defines the structure and behavior that objects created from it may possess, including fields, constructors, and methods. Option (a) describes an executing program rather than a class. Option (b) describes a restricted kind of variable, not a class. Option (d) describes neither the purpose nor the defining characteristics of a class. 3. Which term describes a specific object created from a class?

(a) Instance (b) Compiler (c) Operator (d) Package Answer: (a) Instance Rationale: An instance is an individual object created from a class. For example, if Student is a class, a particular student with a name and registration number is an instance of that class. A compiler (b) translates source code, an operator (c) performs an operation on operands, and a package (d) groups related programming components.

4. A banking application creates an Account object with a balance of KSh 5,000. Which concept allows the account to maintain its balance while also providing deposit and withdrawal operations? (a) Compilation (b) Recursion (c) Iteration (d) Encapsulation Answer: (d) Encapsulation Rationale: Encapsulation combines data and operations within an object while controlling access to its internal state. An account can protect its balance and expose deposit and withdrawal methods that enforce rules. Compilation (a) translates code, recursion (b) involves a function calling itself, and iteration (c) involves repeating operations through loops. None of these provides the data- protection structure described. 5. Which statement best distinguishes an object from a class? (a) Objects define blueprints, while classes contain runtime values. (b) A class defines a structure, while an object is a concrete instance of that structure. (c) Objects cannot contain methods, but classes can. (d) A class must always contain exactly one object. Answer: (b) A class defines a structure, while an object is a concrete instance of that structure.

(a) Encapsulation (b) Polymorphism (c) Compilation (d) Data persistence Answer: (b) Polymorphism Rationale: Polymorphism enables a common interface or method call to produce type-specific behavior. For example, calling calculateArea() on different shape objects may produce different calculations. Encapsulation (a) protects internal state, compilation (c) translates code, and persistence (d) concerns storing data beyond program execution.

9. What is the principal purpose of a constructor? (a) To initialize an object when it is created (b) To return an object to the operating system (c) To prevent all inheritance (d) To execute a program's main loop Answer: (a) To initialize an object when it is created Rationale: Constructors establish an object's initial state, often assigning values to fields and validating initialization parameters. Option (b) incorrectly describes object destruction or resource management. Option (c) is unrelated to constructors, and option (d) describes application execution logic rather than object initialization. 10. A class contains a private field named salary. Which approach best preserves encapsulation while allowing controlled access to the field? (a) Make the field public and remove all validation. (b) Duplicate the salary field in every method. (c) Store salary in an unrelated global variable. (d) Provide controlled methods that validate access or modification. Answer: (d) Provide controlled methods that validate access or modification. Rationale: Controlled access methods allow the class to enforce business rules, such as preventing negative salaries or restricting unauthorized changes. Option (a) exposes the internal state and bypasses validation. Option (b) creates

unnecessary duplication and inconsistent data. Option (c) separates the state from the object that should manage it, weakening encapsulation.

11. Which of the following is an example of abstraction? (a) Copying an entire source-code file (b) Exposing only essential functionality while hiding implementation details (c) Making every field accessible to every class (d) Repeating the same calculation in several methods Answer: (b) Exposing only essential functionality while hiding implementation details Rationale: Abstraction focuses on what an object does while hiding unnecessary details about how it does it. A user of a payment service may call pay() without knowing the internal processing steps. Option (a) is file duplication, option (c) weakens access control, and option (d) introduces code duplication rather than abstraction. 12. A class Employee has fields name and department. A developer creates three employees with different values. Which statement is correct? (a) All three employees must share identical field values. (b) Each employee must be a separate class. (c) Each object can maintain its own state while sharing the class definition. (d) The class ceases to exist after the first object is created. Answer: (c) Each object can maintain its own state while sharing the class definition. Rationale: Multiple instances of the same class can possess independent field values while sharing the methods and structural definition. Option (a) confuses shared class structure with instance-specific state. Option (b) unnecessarily creates separate classes. Option (d) is incorrect because the class definition remains available to create additional objects and support existing instances. 13. Which statement best describes a static field? (a) It belongs to the class and is generally shared among its instances. (b) It can only be accessed inside a constructor.

Rationale: Instance methods can access instance-specific state through their object context. Static methods do not have an implicit instance context and generally operate on supplied arguments or class-level data. Option (a) is false because instance methods contain executable statements. Option (c) is not a universal performance guarantee. Option (d) is false because static methods may have different access levels.

16. A class has a field balance and a method withdraw(amount). Which implementation best protects the object's integrity? (a) Always subtract the amount, regardless of its value. (b) Allow direct access to the balance from every class. (c) Check that the amount is positive and that sufficient funds exist before subtracting. (d) Delete the balance field after each withdrawal. Answer: (c) Check that the amount is positive and that sufficient funds exist before subtracting. Rationale: Validating withdrawal amounts and checking available funds preserve the account's invariants. A withdrawal should not accept invalid amounts or create an unauthorized negative balance when overdrafts are prohibited. Option (a) ignores business rules. Option (b) bypasses protection. Option (d) destroys essential state and makes account management impossible. 17. What is the meaning of the term this in languages such as Java and C++? (a) A reference to the current object in an instance context (b) A reference to the operating system (c) A keyword for creating a package (d) A command that permanently deletes a class Answer: (a) A reference to the current object in an instance context Rationale: The this reference identifies the object whose instance method or constructor is currently executing. It can be used to distinguish instance fields from parameters or to call another constructor in supported contexts. Option (b) refers to the operating system, not the object. Option (c) is unrelated to package creation. Option (d) is not the purpose of this. 18. What is a common benefit of using a well-designed object-oriented class?

(a) It eliminates the need for testing. (b) It prevents developers from using algorithms. (c) It guarantees that no future changes will be required. (d) It groups related data and behavior into a reusable component. Answer: (d) It groups related data and behavior into a reusable component. Rationale: A class can group state and operations that belong together, making the component easier to reuse, test, maintain, and understand. Option (a) is false because classes still require testing. Option (b) is false because algorithms are central to methods. Option (c) is unrealistic because software requirements and implementation often change.

19. Which relationship is most accurately described as a "has-a" relationship? (a) A subclass inherits from a superclass. (b) A Library contains Book objects as part of its managed collection. (c) A method calls itself repeatedly. (d) Two variables store the same integer. Answer: (b) A Library contains Book objects as part of its managed collection. Rationale: A "has-a" relationship typically represents composition or aggregation, in which one object contains, references, or manages another object. Option (a) represents an "is-a" relationship through inheritance. Option (c) represents recursion. Option (d) is a value relationship, not an object ownership or containment relationship. 20. A developer wants a program to support several types of notification systems through a common send() operation. Which design best demonstrates polymorphism? (a) Create unrelated classes with no shared interface and hard-code every call. (b) Use one global string to identify the notification type. (c) Define a common notification interface and implement send() in each notification class. (d) Store every notification as a numerical variable. Answer: (c) Define a common notification interface and implement send() in each notification class.

(a) temperatureValue (b) calculateTemperature() (c) displayTemperature() (d) convertTemperature() Answer: (a) temperatureValue Rationale: A field stores data representing the object's state. A temperature value is therefore appropriately represented by a field. Options (b), (c), and (d) are method-like names representing actions rather than stored state.

24. Which statement best explains why objects are useful when modeling real- world systems? (a) Objects eliminate all relationships between entities. (b) Objects prevent the creation of complex software. (c) Objects can represent entities with identity, state, and behavior. (d) Objects can only represent physical items. Answer: (c) Objects can represent entities with identity, state, and behavior. Rationale: Objects can model physical or conceptual entities by combining identity, state, and operations. Examples include customers, bank accounts, transactions, appointments, and abstract services. Option (a) is incorrect because objects commonly have relationships. Option (b) is the opposite of OOP's purpose. Option (d) is incorrect because objects can represent intangible concepts. 25. A developer creates a Car class but never creates an instance of it. What is true? (a) The class automatically becomes a running vehicle. (b) The class definition can exist without any object being instantiated. (c) The class must be deleted from the project. (d) The class automatically creates ten instances. Answer: (b) The class definition can exist without any object being instantiated. Rationale: A class definition can be declared and compiled without creating any instances. Instances are created only when the program executes an appropriate

instantiation operation. Option (a) confuses a software model with a real-world object. Options (c) and (d) describe requirements that do not exist.

26. What does the expression new Student() commonly accomplish in languages such as Java? (a) It permanently closes the application. (b) It converts a class into an interface. (c) It deletes the student class definition. (d) It creates a new Student instance and initializes it. Answer: (d) It creates a new Student instance and initializes it. Rationale: The new operator generally allocates or creates an object and invokes an appropriate constructor. Option (a) is unrelated to object creation. Option (b) is not a function of the new operator. Option (c) is incorrect because object creation does not delete the class definition. 27. Which statement about object state is correct? (a) State is determined by the values of an object's attributes at a particular time. (b) State refers only to the methods an object contains. (c) State cannot change after object creation in any language. (d) State is identical for every instance of a class. Answer: (a) State is determined by the values of an object's attributes at a particular time. Rationale: An object's state is represented by its data values at a given point in execution. For example, an account's balance or a student's enrollment status contributes to its state. Option (b) describes behavior. Option (c) is incorrect because many objects are mutable. Option (d) is incorrect because instances commonly have different state values. 28. Which scenario demonstrates object behavior rather than object state? (a) A student's registration number is 2045. (b) A product's price is KSh 700. (c) An order calculates its total cost. (d) A customer's account status is active.

(a) A variable specified in the method definition that receives input (b) A value stored only in the operating system (c) A class that cannot be instantiated (d) A statement that terminates a loop Answer: (a) A variable specified in the method definition that receives input. Rationale: A parameter is a named variable in a method declaration that receives a value when the method is called. Option (b) has no relationship to method parameters. Option (c) describes an abstract class in some languages. Option (d) describes loop-control behavior.

32. Consider a method calculateTax(double income). What is income? (a) A return value (b) A class name (c) A parameter (d) An object constructor Answer: (c) A parameter Rationale: income is the parameter specified in the method signature and receives the value supplied by the caller. Option (a) refers to what a method returns. Option (b) identifies a type or class. Option (d) refers to object initialization, not a method input. 33. A method is declared to return an integer but reaches its end without returning a value. What is the most likely outcome in a language requiring explicit return values? (a) The method automatically returns the largest integer. (b) The program may fail to compile because the required return value is missing. (c) The method automatically becomes static. (d) The method converts itself into a constructor. Answer: (b) The program may fail to compile because the required return value is missing. Rationale: In languages such as Java, a non-void method must provide a return value compatible with its declared type along every reachable path. Missing

returns commonly result in compile-time errors. Option (a) invents a default value. Options (c) and (d) have no connection to return-value requirements.

34. What is the purpose of the return statement? (a) To declare a new class (b) To repeat a method indefinitely (c) To increase the visibility of a variable (d) To exit a method and optionally provide a result Answer: (d) To exit a method and optionally provide a result. Rationale: The return statement terminates a method's execution and may pass a result to the caller. Option (a) concerns class declarations. Option (b) describes repetition rather than termination. Option (c) concerns access control and is not performed by return. 35. What is method overloading? (a) Defining multiple methods with the same name but different parameter lists (b) Replacing every method with a field (c) Creating subclasses without methods (d) Defining a method that cannot be called Answer: (a) Defining multiple methods with the same name but different parameter lists. Rationale: Method overloading permits methods to share a name while differing in parameter number, types, or arrangement, subject to language rules. Option (b) incorrectly removes behavior. Option (c) describes neither overloading nor meaningful inheritance. Option (d) describes an inaccessible or unusable method, not overloading. 36. Which pair correctly demonstrates method overloading? (a) add(int a) and subtract(int a) (b) display() and print() (c) calculate(int a) and calculate(double a) (d) run() in two unrelated applications Answer: (c) calculate(int a) and calculate(double a)

(c) It is the method's body of executable statements. (d) It always includes the access modifier. Answer: (b) It includes the method name and parameter types, but not the return type alone as a distinguishing element. Rationale: In Java, a method signature is based on the name and parameter types; return type and access modifier alone do not distinguish overloads. Option (a) is incomplete. Option (c) describes the implementation body. Option (d) is incorrect because access modifiers are not part of the method signature used to distinguish overloads.

40. A method accepts an object as an argument and modifies one of its fields. Which statement best describes this operation in a reference-based language? (a) The object is automatically converted into a primitive. (b) The original class is destroyed. (c) The method may modify the state of the referenced object. (d) The method must become a constructor. Answer: (c) The method may modify the state of the referenced object. Rationale: When a reference to an object is passed to a method, the method can often access and modify that object's mutable state. In Java, the reference value itself is passed by value, but it refers to the same object. Option (a) confuses objects and primitive values. Options (b) and (d) are unrelated to modifying object state. 41. What is the purpose of a default constructor in a language such as Java? (a) It provides a constructor with no explicit parameters when applicable, initializing an object using default initialization logic. (b) It always deletes the object's fields. (c) It converts every instance field to a static field. (d) It prevents the class from being instantiated. Answer: (a) It provides a constructor with no explicit parameters when applicable, initializing an object using default initialization logic. Rationale: A default constructor commonly refers to the compiler-provided no- argument constructor when a class declares no constructors, although

terminology may vary across languages. It initializes an object according to the language's initialization rules. Option (b) is destructive and incorrect. Options (c) and (d) describe unrelated operations.

42. What happens when a class declares its own parameterized constructor but no no-argument constructor in Java? (a) Java always generates both constructors. (b) The class becomes abstract. (c) Every field becomes final. (d) Java does not automatically provide a no-argument constructor. Answer: (d) Java does not automatically provide a no-argument constructor. Rationale: Java supplies a default no-argument constructor only when the class declares no constructors. Once a constructor is explicitly declared, the compiler does not automatically add a no-argument constructor. Option (a) is incorrect. Options (b) and (c) do not follow from declaring a parameterized constructor. 43. Which situation most strongly justifies creating a parameterized constructor? (a) The developer wants to remove all object behavior. (b) An object must receive essential values at creation time. (c) A program should execute without any classes. (d) The object must never contain fields. Answer: (b) An object must receive essential values at creation time. Rationale: Parameterized constructors allow required values to be supplied during object creation, helping ensure objects begin in a valid state. Option (a) eliminates behavior unnecessarily. Option (c) contradicts the use of class-based object construction. Option (d) eliminates state, which is not a requirement of parameterized constructors. 44. A constructor contains the statement this.name = name;. What does this accomplish? (a) It deletes the parameter. (b) It calls a static method. (c) It assigns the parameter value to the current object's field. (d) It creates another class.

(c) Constructors cannot receive parameters. (d) Constructors are always inherited by subclasses. Answer: (b) Constructors typically have the same name as the class in Java. Rationale: In Java, constructors use the class name and do not declare an ordinary return type. They may accept parameters and are not inherited like normal methods. Option (a) is incorrect because constructors have no declared return type. Option (c) is false because constructors may accept parameters. Option (d) is false because constructors are not inherited.

48. Which statement best describes object initialization? (a) It only occurs when an object is deleted. (b) It removes all attributes from a class. (c) It establishes initial field values and prepares the object for use. (d) It converts the class into a package. Answer: (c) It establishes initial field values and prepares the object for use. Rationale: Initialization establishes the starting state of an object, including field values and potentially other required setup. Option (a) incorrectly associates initialization with deletion. Option (b) removes data rather than establishing it. Option (d) is unrelated to object initialization. 49. What is the most appropriate design when an object must be initialized with a valid email address? (a) Validate the email address during construction or through a controlled setter before accepting it. (b) Accept every possible string without validation. (c) Make the email field publicly writable by every class. (d) Replace the email field with a loop. Answer: (a) Validate the email address during construction or through a controlled setter before accepting it. Rationale: Validation at controlled initialization or update points helps prevent invalid object state. Option (b) permits malformed values. Option (c) allows uncontrolled modifications that may bypass validation. Option (d) is irrelevant because loops do not replace data validation or fields.

50. Which statement best explains why constructors should not contain excessive unrelated logic? (a) Constructors cannot contain expressions. (b) Constructors must always be empty. (c) Constructors are incapable of calling methods. (d) Excessive initialization logic makes object creation difficult to understand, test, and maintain. Answer: (d) Excessive initialization logic makes object creation difficult to understand, test, and maintain. Rationale: Constructors should focus on establishing a valid object state. Excessive unrelated work, such as performing network operations, generating reports, or handling complex workflows, makes object creation unpredictable and harder to test. Options (a), (b), and (c) are incorrect because constructors can contain executable logic and method calls. 51. A Vehicle superclass defines startEngine(). A Motorcycle subclass inherits from Vehicle. What is the principal benefit of this design? (a) The motorcycle must duplicate all vehicle code. (b) The motorcycle can reuse inherited functionality while adding specialized behavior. (c) The superclass automatically becomes an interface. (d) Inheritance eliminates the need to instantiate objects. Answer: (b) The motorcycle can reuse inherited functionality while adding specialized behavior. Rationale: Inheritance supports code reuse and specialization. A subclass can use accessible inherited methods and introduce additional functionality. Option (a) defeats the purpose of inheritance. Option (c) is incorrect because inheritance does not convert a class into an interface. Option (d) is incorrect because objects may still be instantiated normally. 52. Which relationship best represents inheritance? (a) A Car is a type of Vehicle. (b) A Car contains an Engine.