0% found this document useful (0 votes)
8 views4 pages

C++ Object-Oriented Programming Exam

An exam on object oriented programming

Uploaded by

emmajulienne63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

C++ Object-Oriented Programming Exam

An exam on object oriented programming

Uploaded by

emmajulienne63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FIRST SEMESTER EXAMINATION 2024/2025 ACADEMIC YEAR

DEPARTMENT: SOTWARE ENGINEERING DAY SESSION


COURSE TITLE: PROGRAMMING II
DURATION : 1 – 3PM - 2H DATE: 17 -02 – 25 CREDIT VALUE : 3
COURSE INSTRUCTOR: NGONG CONSTANTINE LEVEL: 200

SECTION A : MULTIPLE CHOICE QUESTIONS – 17 marks Answer all questions.


1. Which of the following is a feature of OOP?
A. Procedural programming C. Memory management
B. Abstraction D. Function pointers
2. What does a class in C++ represent?
A. emplate for objects C. A runtime variable
B. function definition D. A pointer
3. How do you create an object of a class in C++?
A. object = new Class(); C. Class->object;
B. Class object; D. object(Class);
4. What is the access specifier that allows members of a class to be accessible only within the
same class?
A. Public C. Protected
B. Private D. Static
5. What is the output of the following code?
class A {
public:
int x;
A() { x = 10; }
};
int main() {
A obj;
cout << obj.x;
}
A. 0 C. Compiler error
B. 10 D. Garbage value
6. Which of the following correctly implements inheritance in C++?
A. class A : inherits B C. class A -> B
B. class A : public B D. class A = new B
7. Which concept allows a derived class to use methods of its base class?
A. Polymorphism C. Inheritance
B. Encapsulation D. Abstraction
8. What is a constructor in C++?
A. A function used to destroy objects
B. A special function used to initialize objects
C. A function used to create pointers
D. A function used for overloading
9. What does the this pointer represent in C++?
A. The memory address of the base class
B. The current object of a class
C. A pointer to the destructor of the class
D. The global scope of the class
10. What is function overloading in C++?
A. Using multiple classes to define a function
B. Defining multiple functions with the same name but different arguments
C. Overriding a base class function in a derived class
D. Using a global function instead of a member function
11. What is polymorphism in C++?
A. Defining many classes in one file
B. The ability of a function to behave differently based on input types
C. A function with multiple arguments
D. Using global variables in functions
12. What type of function is called when an object is destroyed?
A. Constructor C. Virtual function
B. Destructor D. Inline function
13. Which of the following is not a type of inheritance in C++?
A. Single inheritance C. Multilevel inheritance
B. Multiple inheritance D. Object inheritance
14. What is the correct syntax to declare a friend function in C++?
A. friend void function(); C. void friend function();
B. friend: void function(); D. void function() friend;
15. What keyword is used to prevent a class from being inherited?
A. final C. protected
B. sealed D. private
16. What is the default access specifier for members of a class in C++?
A. Public
B. Private
C. Protected
D. None
17. What is an abstract class in C++?
A. A class with only private members
B. A class with no member functions
C. A class that cannot be instantiated and contains at least one pure virtual function
D. A class that can only have derived classes

SECTION B : STRUTURAL QUESTIONS. Answer all question : 28 marks


1. Define the following as seen in object oriented programming
a. Object
b. Classes
c. Methods 3 * 1 = 3 marks
2. What are the four major principles that make a language to be object oriented ? 4 marks
3. What are the difference between private and public class members ? 2 marks
4. What are the differences between data hiding and encapsulation ? 2 marks
5. What are the two major components of an object ? 2 marks
6. What is the relationship between a class and objects in that class ? 3 marks
7. What is a constructor ? Why are constructors used ? 2 marks
8. Evaluate the following using operator precedence in C++ to come out with the output.
#include<iostream>
using namesapce std ;
int main() {
int a = 20 ;
int b = 5 ;
int c = 15 ;
int d = 5 ;
int e ;
e = (a + b) * c/d ;
cout << « The Value of e (a + b) * c/d : » << e<<endl ;
e = ((a + b) * c)/d ;
cout << « The Value of e ((a + b) * c)/d : » << e<<endl ;
e = (a + b) *( c/d) ;
cout << « The Value of e (a + b) *( c/d) : » << e<<endl ;
e = a +( b * c)/d ;
cout << « The Value of e a +( b * c)/d : » << e<<endl ;
e=a&b;
cout << « The Value of e : » << e<<endl ;
e = a|b ;
cout << « The Value of e : » << e<<endl ;
e = a^1 ;
cout << « The Value of e : » << e<<endl ;
e = a>>1 ;
cout << « The Value of e : » << e<<endl ;
e = a>>1 ;
cout << « The Value of e : » << e<<endl ;
e = a<<1 ;
cout << « The Value of e : » << e<<endl ;

return 0 ;
}
10 marks

SECTIOB C : ESSAY – CASE STUDY 25 marks – Answer question 1 and Select any one
1. Write a program that reads the age of a person and categorizes them into one of the
following: 10 marks

"Child" (under 12)


"Teen" (13-19)
"Adult" (20-64)
"Senior" (65 and above)

SELECT ONE Question from this section


2. Design a class called Cube that has the following data member side of data type double.
Supply the following methods : a constructor with one argument, a getter and setter
method for the side of the cube, the volume equals to side raised to the power three.
Implement the class Cube and write a test class Cube Test in C++ to verify thhe
functionalities of the class Cube. 15 marks
OR
3. Define a class BankAccount with the following private attributes :
 Balance (double)
 accountNumber (string)
Provide the following public methods :
 deposit(double amount) – Adds the givien amount to the balance
 withdraw(double amount) – substracts the given amount from the balance
 getBalance() – Returns the current balance
In main(), create an object of BankAccount, deposit money, withdraw money and print the
balance. 15 marks

Good Luck

Common questions

Powered by AI

Abstraction in object-oriented programming allows for the simplification of complex systems by hiding unnecessary details and exposing only essential attributes and behaviors. In C++, abstraction is implemented through classes and access specifiers, where classes encapsulate data and functions and use access specifiers (public, private, and protected) to control which parts of the class are visible to the outside. Thus, abstraction helps in focusing on what an object does, rather than how it does it, promoting the use of interfaces and enhancing maintainability .

A destructor in C++ is a special member function triggered automatically when an object goes out of scope or is explicitly deleted. Its primary role is to release resources like memory, files, or network connections acquired by the object, helping to prevent leaks and deallocate memory. Unlike constructors that initialize objects upon creation, destructors clean up and finalize the object's timeline before it is removed from memory. They are unique in that they have no parameters and cannot be overloaded, ensuring a single cleanup function per class .

The four major principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation bundles data and operations on data into a single unit or class, ensuring controlled access and reducing complexity. Abstraction simplifies complex realities by modeling classes based on essential features, enhancing focus and understanding. Inheritance allows new classes to inherit properties and methods from existing ones, fostering code reuse and logical hierarchy structuring. Polymorphism enables entities to be treated as instances of their parent class, improving flexibility in program design and the ability to handle diverse data types uniformly. These principles contribute to creating modular, maintainable, and scalable software .

Constructors in C++ are special member functions invoked automatically when an object of a class is created. They initialize the object and can set initial values for class attributes, ensuring that objects start life in a valid state. Constructors enhance code robustness by preventing uninitialized data and provide a mechanism for overloading, allowing for different ways to instantiate objects. This automatic initialization helps in managing resources correctly (like dynamic memory) and aligns with RAII (Resource Acquisition Is Initialization) to prevent resource leaks .

Access specifiers in C++ (public, private, and protected) promote encapsulation by controlling the visibility and accessibility of class members. Public access allows members to be accessible from outside the class, facilitating interaction with object properties and behaviors. Private access restricts members only to be accessed within the class itself, preventing external interference and protecting object integrity. Protected access permits access within the class and derived classes, supporting inheritance while maintaining member confidentiality. These access control levels allow designers to hide class implementation details, exposing only necessary interfaces for interaction while safeguarding internal state .

Function overloading enhances C++ functionality by allowing multiple functions with the same name to exist, differentiated by parameter types, numbers, or order. This permits similar operations to be defined under a unified nomenclature, improving code readability and consistency. Functions can be overloaded if they have different parameter lists; however, they cannot differ only by return type. Overloading allows polymorphic behavior, where a single function call can operate differently depending on the arguments passed, thus providing greater flexibility and usability in programming .

Operator precedence determines the order in which operations are performed in an expression. In the expression e = (a + b) * c/d, the operations follow C++ precedence rules: first parentheses are evaluated (a + b), resulting in 25. Next, the multiplication is performed 25 * 15, equating to 375. Finally, division takes precedence over addition resulting in 375 / 5, giving a final value of 75 for e. This order of operations ensures consistent and predictable execution of arithmetic processes .

Virtual functions in C++ facilitate runtime polymorphism by allowing derived classes to override base class methods. This feature enables a base class pointer or reference to invoke derived class functions, ensuring that the correct method is executed based on the actual object type. This mechanism supports dynamic binding, improving flexibility and enabling polymorphic behavior in code, crucial for implementing generic algorithms. Virtual functions are typically used in abstract classes and interfaces where method signatures remain the same across derived classes, allowing developers to redefine behaviors as needed without altering interfaces .

Private class members in C++ are accessible only within the class they are declared in, limiting their visibility to member functions and friends of the class, enhancing data protection. Public class members, on the other hand, can be accessed from outside the class, making the data and behaviors available to other parts of a program. This distinction affects class design by influencing encapsulation; private members are used to hide internal details and maintain control over data and how it interacts with the outside world, while public members expose functionalities intended for external use .

Friend functions in C++ allow access to private and protected members of a class, defined outside of the class's scope. They are essential in scenarios where two or more classes need to operate closely, such as implementing operator overloading or managing class relationships where mutual access to communities is required. Although they break encapsulation by exposing internal states, their benefit comes in providing a controlled and explicit way to access class internals, essential for operations that naturally need more visibility across class boundaries .

You might also like