Professional Documents
Culture Documents
The exam has 2 sections: Multiple choice questions and programming questions.
You can answer the questions by highlighting your answer, by circling your answer or by typing
the letter for your answer. All of them are fine as long as your choice is clear.
1) Consider the customer management system and the inventory management system of a
coffee shop. Which items should be a part of the CustomerBillDetails object type?
a. orderQuantity
b. coffeeStock
c. orderFrequency
d. minimumOrder
3) Consider a classStudent. The following code is used to create objects and access its
member functions. Which statement is true about the code?
int main() {
Student newStud;
Student stud;
Student oldStud;
stud.GetDetails();
return 0;
}
a. There are three objects created of Student class and they each have their own memory
allocations.
The GetDetails() function is invoked for all objects of the class Student.
b. There are three objects created of Student class and they each have their own memory
locations.
The GetDetails() function is invoked for only the stud object of the class Student.
c. There are three objects created of Student class and the compiler allocates only one memory
location. The GetDetails() function is invoked for only the stud object of the class Student.
d. There are three objects created of Student class and the compiler allocates only one memory
location. The GetDetails() function is invoked for all objects of the class Student.
1
4) Which of the following statements is true about private data members of a class?
a. A class user can access the private data members to operate on the function.
b. A class user and member functions can access the private data members to operate on the
function.
c. A member function can access the private data members, but class users cannot.
d. A member function cannot access the private data members to operate on the function.
5) Consider a class Student with public member functions and private data members. Which
XXX is valid for the code?
class Student {
public:
void SetName(string studentName);
void SetGrade(int studentGrade);
void Display();
private:
string name;
int grade;
};
int main() {
Student stu1;
XXX
…
}
a. stu1.name = "Harry";
b. myString = stu1.name;
c. stu1.Display();
d. stu1.SetGrade("Harry");
2
8) Identify the statement that will throw an error in the following code?
class Student {
public:
void SetName(string studentName);
void Display();
private:
string name;
char grade;
int Grade();
};
void Student::Display() {
grade = Grade();
…
}
int Student::Grade(){
…
}
void SetName(string studentName) {
name = studentName;
}
int main() {
Student stu1;
stu1.Display();
…
stu1.Grade();
}
a. stu1.Grade();
b. stu1.Display();
c. int Student::Grade()
d. void Student::Display() {
grade = Grade();
}
9) What is output?
#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void SetName(string studentName);
void SetScore(int studentScore);
string GetGrade() const;
3
private:
string name;
int score;
};
int main() {
Student stu1;
stu1.SetScore(80);
cout << stu1.GetGrade();
return 0;
}
a. 80
b. C
c. B
d. A
11) Which of the following is true if a programmer has to create a class Clinic with the following
data members and functions?
4
class Clinic {
int patientRecNum;
string patientName;
void Set Details(int num, string name);
Clinic GetDetails();
};
a. Data members should be public items and function declarations should be private items.
b. Data members should be private items and function declarations should be public items.
c. The data member patientName should be a private item and all other members should be
public items.
d. The function SetDetails should be a public item and all other members should be private
items.
12) For the given outline, identify the correct statement for defining classes for a program that
stores electronic items’ details.
The program will have an Electronic class with item code, item description, and item price.
An item may be a gaming item with console details and brand.
a. Create a Gaming class as separate files and then an Electronic class which includes the
Gaming.h file.
b. Create an Electronic class as separate files and then a Gaming class which includes the
Electronic.h file.
c. Create separate files for the classes Electronic and Gaming and include both the .h files in
main.cpp.
(either answer acceptable)
d. Create one file containing all the classes and their definitions.
int main() {
int num1;
int num2;
cin >> num1;
cin >> num2;
if (num1 > num2)
cout << num1 << " is greater" << endl;
else
cout << num2 << " is greater" << endl;
return 0;
}
5
a. Since no strings have been used in the program, the include statement for string would
generate an error.
b. Since Shopping class has been included, not making its object in main would generate an
error.
c. The variables num1 and num2 should be declared in a class and accessed through functions.
d. Only .h files can be included, hence including Shopping.cpp is invalid.
15) The _______ member access operator is used to access a member using the this pointer.
a. ::
b. ->
c. .
d. :
16) Which XXX and YYY complete the class School's member function correctly?
#include <iostream>
#include <string>
using namespace std;
class School {
public:
void SetDetails(int numStudents, string name);
void Print();
private:
int nStudents;
string schoolName;
};
void School::Print() {
cout << this->nStudents << " Students are in " <<
this->schoolName << endl;
6
}
int main() {
School mySchool;
mySchool.SetDetails(100, "New School");
mySchool.SetDetails(35, "New School");
mySchool.Print();
return 0;
}
a. this->numStudents = nStudents;
this->name = schoolName;
b. this->nStudents;
this->schoolName;
c. this->numStudents;
this->name;
d. this->nStudents = numStudents;
this->schoolName = name;
class Shapes {
public:
void Print();
Shapes(int x);
private:
int sides = 4;
};
Shapes::Shapes(int x) {
this->sides = x;
}
void Shapes::Print() {
if(this->sides == 4) {
cout << "It’s a Square!" << endl;
}
else {
7
cout << "Oops!";
}
}
int main() {
Shapes s(7);
s.Print();
return 0;
}
a. Oops, It’s a Square!
b. Oops!
c. It’s a Square!
d. It’s a Square, Oops!
18) The process of redefining the functionality of a built-in operator, such as +, -, and *, to
operate on programmer-defined objects is called operator _____.
a. overriding
b. overloading
c. initializing
d. testing
19) In the following code, what is the correct return statement for an overloaded + operator?
Game Game::operator+(Game newGame) {
Game myGame;
myGame.score = score + newGame.score;
//Return statement to be written here
}
a. return Game;
b. return myGame;
c. return newGame;
d. return score;
20) A programmer is overloading the equality operator (==) and has created a function named
operator==. Which of the following statements are true about this function?
a. It returns a class type and takes one const reference argument of bool type.
b. It returns bool and takes two const reference arguments of the class type.
c. It does not return anything and takes two const reference arguments of the class type.
d. It returns an int and takes two reference arguments of bool type.
21) Which of the following is the correct way to overload the >= operator to use School class
type and a function GetTotalStudents()?
a. void operator>=(const School&lhs, const School&rhs) {
8
If(lhs.GetTotalStudents() >= rhs.GetTotalStudents()) cout << “Is
Greater!”;
}
b. void operator>=(School&lhs, School&rhs) {
If(this->GetTotalStudents() >= GetTotalStudents()) cout << “Is
Greater!”;
}
c. bool operator>=(School&lhs, School&rhs) {
return this->.GetTotalStudents() >= lhs.GetTotalStudents();
}
d. bool operator>=(const School&lhs, const School&rhs) {
return lhs.GetTotalStudents() >= rhs.GetTotalStudents();
}
class Movies {
public:
int rating = 1;
void Print() {
cout << "Rating is: " << rating << " stars" << endl;
}
};
int main() {
Movies mov1;
Movies mov2;
int currentRating;
cout << "Enter the rating for Movie 1: " << endl;
cin >> mov1.rating;
cout << "Enter the rating for Movie 2: " << endl;
cin >> mov2.rating;
if (mov1 != mov2) {
mov1.Print();
9
}
else {
mov2.Print();
}
return 0;
}
a. Rating is: 0 stars
b. Rating is: 1 stars
c. Rating is: 4 stars
d. Rating is: 5 stars
class Cars {
//code for the class data members and member functions
}
int main() {
vector<Cars> carsInTheShowroom;
Cars newCars;
// Code for adding cars’ details into the vector
//Code for sorting the cars based on their price in the vector
sort(carsInTheShowroom.begin(), carsInTheShowroom.end());
// Code for displaying sorted cars’ details from the vector
}
a. No additional library required
b. #include <algorithm>
c. #include <sort>
d. #include <math>
24) A soccer coach scouting players has to enter the jersey number of players and print a list of
those numbers when required. Identify the function that can help with this.
10
a. at()
b. sort()
c. append()
d. push_back()
class School {
public:
School() { y++; }
static int getY() {return y;}
private:
static int y;
};
int School::y = 0;
int main() {
cout << School::getY() << " ";
School t[5];
cout << School::getY();
}
a. 0 1 2 3 4 5
b. 5 5
c. 0 5
d. 0 0
11
Programming Questions
3 questions.
You can complete the programming questions in repl.It or your chosen programming
environment. If using repl.it:
1. Before you copy-paste the code, fix the indentation using "clang-format -i
main.cpp" in the repl.it shell
2. After you paste the code, change the font to "Courier New" if it is not already in a
fixed-size font, so the spacing looks right in the Microsoft Word Document.
Write a function called getNums that reads a list of integers until it reads -1, and outputs those
integers in reverse. The function should not output -1.
void getNums() {
int n;
vector<int> nums;
cout << "Get number: ";
cin >> n;
while (n != -1) {
nums.push_back(n);
cout << "Get number: ";
cin >> n;
}
for (int i = nums.size() - 1; i >= 0; i--) {
cout << nums[i] << " " << endl;
}
}
12
4. getMeaning - takes a string. Returns the meaning of the word if the word is found in the
dictionary, returns empty string otherwise.
5. getSize - returns the number of words in the dictionary
You should define additional private variables and functions as needed. The dictionary will not
contain more than 100 words for the initial implementation, but your implementation should be
as flexible as possible.
The main.cpp file for initial testing has been provided as below
iostream>
#include <
#include " Dictionary.h"
int main() {
Dictionary d;
d.add("circuit", "an electrical device providing a path for
current to flow");
d.add("electromagnet", "a magnet made by coiling wire around an
iron core");
d.add("resistor", "a device that resists the flow of electrical
current");
cout << boolalpha;
cout << "Size: " << d.getSize() << endl;
cout << "Contains circuit? " << d.contains("circuit") << endl;
cout << "Contains voltage? " << d.contains("voltage") << endl;
cout << "Meaning of resistor is: " << d.getMeaning("resistor")
<< endl;
return 0;
}
/**
* Output from above code:
Size: 3
Contains circuit? true
Contains voltage? false
Meaning of resistor is: a device that resists the flow of
electrical current
**/
Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
13
#include <vector>
class Dictionary {
public:
Dictionary();
void add(string newWord, string newMeaning);
bool contains(string aWord) const;
string getMeaning(string aWord) const;
int getSize() const;
private:
vector<string> w ords;
vector<string> m eaning;
};
#endif
Dictionary.cpp
Dictionary.h"
#include "
#include < string>
#include < vector>
Dictionary::Dictionary() {}
void Dictionary::add(string newWord, string newMeaning) {
words.push_back(newWord);
meaning.push_back(newMeaning);
}
bool Dictionary::contains(string aWord) const {
for (int i = 0; i < words.size(); i++) {
if (words[i] == aWord) {
return true;
}
}
return false;
}
14
}
int Dictionary::getSize() const { return words.size(); }
15