1
C++ Programming Final Exam Student No:
Name:
Part 1: Multiple Choice (84 points - 3 points per question)
(B) 1. Which statement is true?
(A) boolean is a C++ keyword. (B) 123 is a C++ valid identifier.
(C) C++ provides automatic garbage collection. (D) none of the above
(D) 2. Which of the following does the C++ language not support?
(A) class templates (B) multiple inheritance (C) virtual function (D) automatic garbage collection
(C) 3. Assume a = 3 and b = 6, which of the following is true?
(A) a > 3 && b > 12 (B) b * b > a + b && a/b > a % b (C) b - a > b % a (D) none of the above
(C) 4. Which of the following is not a C++ library function? (A) abs (B) sqrt (C) random (D) floor
(B) 5. Which of the following is global variable operator? (A) : (B) :: (C) . (D) none of the above
(C) 6. Which of these array definitions will set all the indexed variables to 0?
(A) int array[5]; (B) int array[5] = {}; (C) int array[5] = {0}; (D) none of the above
(D) 7. A class member that is to be shared among all objects of a class is called
(A) A value member (B) A reference member (C) A const member (D) A static member
(A) 8. Which of the following is true?
(A) The delete operator destroys a dynamically allocated object. (B) A reference is a pointer.
(C) A constructor can have a return type. (D) none of the above
(D) 9. Which is used initialize const data members? (A) destructor (B) accessor (C) mutator (D) none of the above
(A) 10. Which operator has the highest precedence? (A) . (B) *= (C) ++ (D) %
(D) 11. In C++, which keyword that every typed function must have? (A) const (B) static (C) void (D) none of the above
(A) 12. Which function is used to change the values of private data members of a class?
(A) mutator (B) constructor (C) accessor (D) none of the above
(C) 13. Which operator allocates storage of the proper size for an object at execution time?
(A) init (B) create (C) new (D) allocate
(A) 14. How can a random number between -a and a be generated?
(A) a - rand() % (2 * a + 1); (B) a + rand() % a + 1; (C) a + rand() % (a + 1); (D) none of the above
(C) 15. A When the class A has been defined, which of following declaration is false?
(A) A a; (B) A *a[5]; (C) A &a = *aPtr; (D) A *aptr = &aRef;
(B) 16. If a class is named Job, what must the destructor be named? (A) Job (B) ∼Job (C) Job (D) none of the above.
(C) 17. Which of the following operators can be overloaded? (A) ?: (B) :: (C) sizeof (D) none of the above
(A) 18. Which C++ function is used to fetch an entire line of input into a string?
(A) getline (B) getstring (C) getchar (D) none of the above
(D) 19. Which is the correct way to use member initializer if Count is a class and x is a const data member?
(A) Count(int x) : x(i) { } (B) Count(int x) : i(x) { } (C) Count(int i) { x = i; } (D) none of the above
(A) 20. which is used to specify that an object is not modifiable? (A) const (B) static (C) fix (D) none of the above
(B) 21. How many times will the following program print hi? for (int i = 2; i < 1000; i *= 2) cout << ”hi ”;
(A) 8 (B) 9 (C) 10 (D) none of the above
(C) 22. Which of the following about C++ is true?
(A) The this pointer can be used in a static function. (B) std::cio is the standard input and output.
(C) throw is a C++ keyword. (D) None of the above
(A, B) 23. Which function can return the size of the string class? (A) size() (B) length() (C) len() (D) none of the above
(B) 24. Multiple functions are invoked in the same statement is called:
(A) call by value (B) cascaded member-function calls (C) call by reference (D) none of the above
(D) 25. Which statement about friendship in C++ is true?
(A) Friendship is taken. (B) Friendship relation is symmetric and transitive.
(C) Overloaded functions cannot be specified as friends of a class. (D) none of the above
(A) 26. Which of the following can be virtual? (A) friend function (B) static functions (C) destructors (D) none of the above
(D) 27. Which C++ function is used to retrieve input until newline is encountered?
(A) getchar (B) getString (C) getStr (D) getline
(B) 28. Which header file defines cin, cout, cerr and clog?
(A) <iomanip> (B) <iostream> (C) <fstream> (D) none of the above
Part 2: Questions and Answers (60 points)
1. (a) (5 points) Define a template function to swap two variables.
(b) (5 points) Define a template function to return the minimum value of the one-dimensional array.
(a) template <class T>
void swap(T &a, T &b) {
2
T temp = a;
a = b;
b = temp;
}
(b) template <class T>
T min(T a[], int n) {
T min = a[0];
for (int i = 1; i < n; i++) min = a[i] < main ? a[i] : min;
return min;
}
2. (6 points) Write the result after executing the following program.
#include <iostream>
using namespace std;
int sub (int &a, int &b)
a *= 2;
cout << "a = " << a <<
return --a * b;
}
int sum(int n) {
if (n < 2) return 1;
else return sum(n - 1)
}
int main() {
int x = 4, y = 3;
y = sub(x, x);
cout << "x = " << x <<
y = sum(x);
cout << "x = " << x <<
}
{
", b = " << b << "\n";
+ sum(n/2);
", y = " << y << "\n";
", y = " << y << "\n";
Ans:
a = 8, b = 8
x = 7, y = 49
x = 7, y = 13
3. (6 points) Write the result after executing the following program.
#include <iostream>
using namespace std;
class A {
public:
A(int v) {
val = ++v;
num += v;
}
void print() { cout << "[" << val << ", " << num << "]" << endl; }
private:
int val;
static int num;
};
int A::num = 0;
int main() {
A var1(1), var2(6);
var1.print();
A var3(8);
var2.print();
var3.print();
}
Ans:
[2, 9]
[7, 18]
[9, 18]
3
4. (28 points) Consider the following class for a point: class Point { private: double x, y; }. Create a polygon based on the
Point class: class Polygon { protected: Point *points; };
(a) (6 points) Complete the Point class including the constructors, accessors, and mutators.
(b) (4 points) Overload the operator || to create a function that passes two points and returns the distance between them.
(c) (10 points) Based on the Point class to complete the Polygon class including the constructors, accessors, and mutators.
(d) (8 points) Based on the Polygon class to create a derived class called Triangle which includes 3 points and the area
function that returns the triangle area.
p
Hint: The area of the triangle is s(s − a)(s − b)(s − c) where a, b, and c are three sides of the triangle and s is a+b+c
.
2
(a) class Point {
public:
Point(double _x = 0, double _y = 0){ x = _x; y = _y; }
void setPoint(double _x = 0, double _y = 0) { x = _x; y = _y; }
double getX() { return x; }
double getY() { return y; }
private:
double x, y;
};
(b) double operator||(Point a, Point b) {
return sqrt((a.getX() - b.getX()) * (a.getX() - b.getX()) +
(a.getY() - b.getY()) * (a.getY() - b.getY()));
}
(c) class Polygon {
protected:
Point *points;
int n;
public:
Polygon(int _n){
n = _n;
points = new Point[n];
}
void setPoints(Point p[]) {
for (int i = 0; i < n; i++) points[i] = p[i];
}
void getPoints(Point p[]) {
for (int i = 0; i < n; i++) p[i] = points[i];
}
double area() { return 0; }
};
(d) class Triangle : public Polygon {
public:
Triangle(Point a, Point b, Point c) : Polygon(3) {
points[0] = a; points[1] = b; points[2] = c;
}
double area() {
Point a = points[0], b = points[1], c = points[2];
double ab = a || b, bc = b || c, ca = c || a;
double s = (ab + bc + ca) / 2;
return sqrt(s * (s - ab) * (s - bc) * (s - ca));
}
};
5. (10 points) Consider a class for an animal as follows:
class Animal {
protected:
string name;
Animal(std::string strName) : name(strName)
public:
string GetName() { return name; }
virtual string Speak() { return "Bark"; }
};
{}
4
(a) (3 points) Based on the Animal class to create a derived class called Cat which speaks ”Meow”.
(b) (3 points) Based on the Animal class to create a derived class called Dog which speaks ”Woof”.
(c) (4 points) Write the main function to use Cat and Dog.
class Cat: public Animal {
public:
string Speak() { return "Meow"; }
};
class Dog: public Animal {
public:
string Speak() { return "Woof"; }
};
int main () {
Cat cat;
Dog dog;
Animal *a1 = &cat;
Animal *a2 = &dog;
cout << a1->Speak() << endl;
cout << a2->Speak() << endl;
}
© Copyright 2026 Paperzz