0% found this document useful (0 votes)
436 views

C++ Exam Past Question and Answer

Uploaded by

goodluckjacob206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
436 views

C++ Exam Past Question and Answer

Uploaded by

goodluckjacob206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ 2022/2022 QUESTIONS AND ANSWERS

Q1.a C++ programs typically go through six phases: edit, preprocess,


compile, link, load and execute, explain this briefly.
ANSWER
1. Edit: This is the first phase that c++ program go through, it involve typing in the c++
program with a text editor and making corrections if necessary. The program is stored
as a text file on the disk, usually with the file extension .cc to indicate that it’s a c++
program.
2. Preprocess: the preprocessor processes the program before the actual compilation. It
involves the use of the preprocessor directives to manipulate the source code before
actual compilation, In this phase, the preprocessor processes directives (like `#include`
and `#define`) and expands macros. The result is a modified source code file.
3. Compilation: A compiler translates the c++ program into machine language(object
code) which is stored on the disk with the extension .
4. Linking: The linker combines the object code with other necessary object codes (like
library functions) to create an executable file. It resolves references between different
parts of the program.
5. Loading: The operating system loads the executable file into memory for execution.
6. Execution: The program is finally executed, and the instructions are processed by the
computer's CPU. The CPU executes the program one instruction at a time.

b. In simple terms, explain the object-oriented technique employed by


modern programming languages such as c++
ANSWER
Object- oriented programming (00P) is a programming paradigm based on the concept of
objects, which can contain data and code: data in the form of fields (often known as
attributes or properties), and code in the form of procedures (often known as methods). In
OOP, the computer programs are designed by making them out of objects that interact with
one another. Classes and objects are the two main aspects of object-oriented programminhg
1
Q2.a Write down the Arithmetic operators supported by c++ in order
of precedence and explain their operations.
ANSWER

b. Write a complete C++ program that prompt the user to enter two
integer numbers. Use "if" statement together with relational operators to
determine whether the numbers are equal, not equal or one is greater
than another. Produce appropriate output for each case.
ANSWER
 #include <iostream>
 Using namespace std;

 int main() {
 int number1;
 int number2;
 cout << "Enter two integers to compare: "; <<endl;
 cin>> number1>>number2;
 if ( number1 == number2 )

2
 cout << number1 << " is equals to " << number2 << endl;
 if ( number1 != number2 )
 cout << number1 << " is not equal to" << number2 << endl;
 if ( number1 > number2 )
 cout << number1 << " is greater than" << number2 << endl;
 if ( number2 > number1 )
 cout << number2 << " is less than " << number1 << endl;
 return 0;
 }

Q3. a. Use the "Car Analogy" or any analogy of your choice to


explain the concept of Class, Object and Member function in Object
Oriented Programming.
ANSWER
Car Analogy
Suppose you want to drive a car and make it go faster by pressing down on its
accelerator pedal.
What must happen before you can do this?
Well, before you can drive a car, someone has to design it and build it which typically
begins with engineering drawings.
These drawings include the design for an accelerator pedal that the driver will use to
make the car go faster.
In a sense, the pedal "hides" the complex mechanisms that actually make the car go
faster, just as the brake pedal "hides" the mechanisms that slow the car, the steering
wheel "hides" the mechanisms that turn the car and so on.
This enables people with little or no knowledge of how cars are engineered to drive a
car easily, simply by using the accelerator pedal, the brake pedal, the steering wheel
and so on.
Now let's use our car example to introduce the key object-oriented programming
concepts.
Performing a task in a program requires a function

3
The function describes the mechanisms that actually perform its tasks. The function
hides from its user the complex tasks that it performs, just as the accelerator pedal of a
car hides from the driver the complex mechanisms of making the car go faster.
We begin by creating a program unit called a class to house a function, just as a car's
engineering drawings house the design of an accelerator pedal.
A function belonging to a class is called a member function.
In a class, you provide one or more member functions that are designed to perform the
class's tasks.
For example, a class that represents a bank account might contain one member
function to deposit money into the account, another to withdraw money from the
account and a third to inquire what the current account balance is.
Just as you cannot drive an engineering drawing of a car, you cannot "drive" a class.
Just as someone has to build a car from its engineering drawings before you can
actually drive the car, you must create an object of a class before you can get a
program to perform the tasks the class describes.
Note also that just as many cars can be built from the same engineering drawing, many
objects can be built from the same class.

b. Consider the string "I am a modern scientist" as an output of a


program consisting of a class that has a member function that
generate the string. Write the code of this program.
ANSWER
• #include <iostream>
• using namespacestd;
• class myclass{
• public:
• void displaymessage(){
• cout<<"I am a modern scientist";
}
};
• int main ( ) {
• myclass myobj;
• myobj.displaymessage();
return 0; }

4
Q4. a. Explain the role of constructor in designing a class in C++
program.
ANSWER
Constructors in c++ is a special method that is automatically called when an object of a class
is created.To create a constructor, use the same name as the class followed by paranthesis ().
The purpose of a constructor is to construct an object and assign values to the object’s
members. A constructor takes the same name as the class to which it belongs, and does not
return any value.

b. Carefully study the following program and add comment on each line
of code to make it more readable.
ANSWER
• #include <iostream> // allow us to work with input and output objects
• using namespacestd; // use names for objects and variables from the standard library

• class Person { // person class definition
• public: // Access specifier
• Person(int x){ /* declaration of member function called person
• level = x; */
• void mylevel () { /* function that display the message I am level –
• cout<<"I am a level "<<level<<" University student!"; university student */
• } // end function mylevel
• private: // Access specifier
• int level; // Attribute (int variable )
• }; // end class person

• int main(){ // main function begins executiom
• Person Student(3); /* create a person object called student and pass the
……………………………………………………………………………………argument 3 */
• Student.mylevel(); // call the member function mylevel
• return 0; //main function has ended successfully
• } // end main

5
Q5. a. Something(S) is/are wrong with the following program's
snippet, correct it and predict the outcome.
• int price = 69;
• If (price >= 70)
• Cout<<"Expensive";
• else:
• cout <<"Cheap".

ANSWER
• int price = 69;
• If (price >= 70)
• Cout<<"Expensive";
• else;
• cout <"Cheap";

b. Rewrite the above program snippet using conditional operator (?:)


int price=60;
string result=(price>= 60)? “Expensive” : “cheap”;
cou<<result;

OR
int price=60;
cout<<(price>= 60)? “Expensive” : “cheap”;

Q6. Write the complete C++ program that interactively accept two
integer numbers and produce their sum. product and the result of
module operation on them. The program should use three functions
sum, prod, and mod to respectively carry out the sum, product and
modulo operations upon calling them.
ANSWER
 #include <iostream>
 using namespace std;

 int sum(int x,int y){

6
return x+y;
}
 int prod(int x,int y){
return x*y;
}
 int mod(int x,int y){
return x%y;
}

 int main () {
int x,y,addition,product,modulo;
cout<<"Enter two integer numbers: "<<endl;
cin>>x>>y;
 addition = sum(x,y);
 product=prod(x,y);
 modulo=mod(x,y);
cout << " The sum of the two numbers is "<<addition<<endl;
cout << " The product of the two numbers is "<<product<<endl;
cout << " The modulus of the two numbers is "<<mod<<endl;
return 0;
}

Q7.a Carefully study the following recursion function together with


how it is called in the main function and generate its outcome.
int sum(int k) {
if (k <= 20) {
return k + sum(k + 1);
}
else {
return 0;
}
int main ( ) {
int result = sum(10);
cout << result;
return 0; }
7
ANSWER
RECURSIVE SUMMATION SUM
10 + sum(10+1) 10
10 +11+sum(11+1) 21
10+11+12+sum(12+1) 33
10+11+12+13+sum(13+1) 46
10+11+1+12+13+ 14+sum(14+1) 60
10+11+1+12+13+ 14+15+sum(15+1) 75
10+11+1+12+13+ 14+15+ 16+sum(16+1) 91
10+11+1+12+13+ 14+15+ 16+17+sum(17+1) 108
10+11+1+12+13+ 14+15+ 16+17+18+sum(18+1) 126
10+11+1+12+13+ 14+15+ 16+17+18+19+sum(19+1) 145
10+11+1+12+13+ 14+15+ 16+17+18+19+ 20+sum(20+1) 165
STOP: Because 21 > 20.

b. How to make the above function in (Q7a) run indefinitely?


changing the if condition in the sum function from (k <= 20) to (k > 0) or (k>=10) will
make the program above to run indefinitely.

You might also like