C++ Exam Past Question and Answer
C++ Exam Past Question and 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;
}
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.
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";
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;
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;
}