CIS22B - Chapter 7 Questions (FINAL)
What is the output of the unit testing? #include <iostream> using namespace std; class TempConvert { public: void SetTemp(int tempVal) { temp = tempVal; } int GetTemp() const { return temp; } TempConvert() { temp = 1; } int InFahrenheit() { return (temp * 1.8) - 32; } private: int temp; }; int main() { TempConvert testData; cout << "Beginning tests." << endl; if (testData.GetTemp() != 0) { cout << " FAILED initialise/get temp" << endl; } testData.SetTemp(10); if (testData.GetTemp() != 10) { cout << " FAILED set/get temp" << endl; } testData.SetTemp(10); if (testData.InFahrenheit() != 50) { cout << " FAILED InFahrenheit for 10 degrees" << endl; } cout << "Tests complete." << endl; return 0; } a. Beginning tests. FAILED initialise/get temp FAILED InFahrenheit for 10 degrees Tests complete. b. Beginning tests. Tests complete. c. Beginning tests. FAILED initialise/get temp FAILED set/get temp FAILED InFahrenheit for 10 degrees Tests complete. d. Beginning tests. FAILED InFahrenheit for 10 degrees Tests complete.
a. Beginning tests. FAILED initialise/get temp FAILED InFahrenheit for 10 degrees Tests complete.
Three classes are defined as follows. Choose the correct syntax to include all three classes in the main file. //File ClassA.h #ifndef ClassA_H #define ClassA_H class ClassA { ... }; #endif //File ClassB.h #ifndef ClassB_H #define ClassB_H #include "ClassA.h" class ClassB { ... }; #endif //File ClassC.h #ifndef ClassC_H #define ClassC_H #include "ClassB.h"class ClassC { ... }; #endif a. Class definitions can be included only through .cpp file b. #include "ClassC.h" c. #include "ClassA.h, ClassB.h, ClassC.h" d. Not possible for the given class definitions
b. #include "ClassC.h"
Identify the type of function definition used in the code. #include <iostream> using namespace std; class AgeCalculator { int ageMonths; public: int CalcAge(int age) { ageMonths = age * 12; return ageMonths; } }; int main() { AgeCalculator newAge; newAge.CalcAge(5); return 0; } a. Private function definition b. Inline function definition c. Overloaded function definition d. Nested function definition
b. Inline function definition
What is output? #include <iostream> using namespace std; 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 { 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!
b. Oops!
c. Consider a class Student . 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.
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.
Which XXX statement can be used to remove the third element from vector teamMember? #include <iostream> #include <vector> using namespace std; int main() { vector<int>teamMember; vctr.push_back(27); vctr.push_back(44); vctr.push_back(9); vctr.push_back(17); XXX return 0; } a. teamMember.empty(teamMember.begin() + 3); b. teamMember.erase(teamMember.begin() + 3); c. teamMember.clear(teamMember.begin() + 3); d. teamMember.push_back(teamMember.begin() + 3);
b. teamMember.erase(teamMember.begin() + 3);
What is output? #include <iostream> using namespace std; 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
c. 0 5
What is output? #include <iostream> #include <vector> using namespace std; int main() { unsigned int i; vector<int> Newvector{2, 3, 4, 5, 6, 7, 8}; for (i = 0; i < Newvector.size(); i++) { if (Newvector.at(i) % 2 == 0) { Newvector.erase(Newvector.begin()+i); i--; } } for (i = 0; i < Newvector.size(); i++) cout << ' ' << Newvector.at(i); return 0; } a. 3 5 7 9 b. 2 4 6 8 c. 3 5 7 d. 2 3 4 5
c. 3 5 7
What is output? #include <iostream> using namespace std; class Greet { public: string g1; string g2; string g3; string g4; void Print() const; Greet() { g1 = "Hi! "; g3 = "Good morning! "; g4 = "Good night! "; } }; void Greet::Print() const { cout << g1 << g2 << g3; } int main() { Greet user1; user1.Print(); user1.g2 = "Hello!"; return 0; } a. Hi! Hello! Good morning! Good night! b. Hi! Good morning! Good night! c. Hi! Good morning! d. Hi! Hello! Good morning!
c. Hi! Good morning!
Which of the following statements is the correct replacement for member initialization in the following code? #include <iostream> using namespace std; class PromNight { public: PromNight(); void Print() const; private: int boys; int girls; }; PromNight::PromNight() { boys = 100; girls = 200; } void PromNight::Print() const { cout << "boys: " << boys << endl; cout << "girls: " << girls << endl; } int main() { PromNight myClass; myClass.Print(); return 0; } a. PromNight::PromNight() :: boys(100), girls(200) { } b. PromNight::PromNight() : boys = 100, girls = 200 { } c. PromNight::PromNight():boys(100), girls(200){} d. PromNight::PromNight() :: boys = 100, girls = 200 { }
c. PromNight::PromNight():boys(100), girls(200){}
What is the output if the input values are 4 and 5? #include <iostream> #include <string> using namespace std; class Movies { public: int rating = 1; void Print() { cout << "Rating is: " << rating << " stars" << endl; } }; bool operator!=(const Movies&lhs, const Movies &rhs) { return (lhs.rating != rhs.rating); } 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(); } 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
c. Rating is: 4 stars
What is output? #include <iostream> using namespace std; class Base { int value; public: Base(int value):value(value) { cout << "The number is " << value; } }; int main( ) { Base il(20); return 0; } a. The number is value b. The number is 0 c. The number is 20 d. The number is
c. The number is 20
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");
c. stu1.Display();
Given the following class definition in the file ClassRoom.h, which XXX and YYY complete the corresponding .cpp file? #ifndef CLASSROOM_H #define CLASSROOM_H class ClassRoom { public: void SetNumber(int n); void Print() const; private: int num; }; #endif ______________________________________________________________________________________________________________ #include <iostream> using namespace std; XXX void ClassRoom::SetNumber(int n) { num = n; } YYY { cout << "Number of Class Rooms: " << num << endl; } a. #include "ClassRoom.cpp" void ClassRoom.Print() const b. #include <ClassRoom.h> void Print() const c. #include <ClassRoom.cpp> void ClassRoom::Print() d. #include "ClassRoom.h" void ClassRoom::Print() const
d. #include "ClassRoom.h" void ClassRoom::Print() const
What is output? #include <iostream> using namespace std; class School { public: School( ); static int getNextId(); private: int id = 0; static int nextId; }; School::School( ) { id = nextId; nextId += 1; } int School::getNextId() { return nextId; } int School::nextId = 0; int main( ) { School( ); cout << School::getNextId() << " "; School( ); cout << School::getNextId() << " "; School( ); cout << School::getNextId() << " "; School( ); cout << School::getNextId() << " "; return 0; } a. 3 2 1 0 b. 0 1 2 3 c. 4 3 2 1 d. 1 2 3 4
d. 1 2 3 4
Which constructor will be called by the statement House myHouse(12); for the given code? class House { House(); // Constructor A House(int num, string str); // Constructor B House(int num1, int num2); // Constructor C House(int num); // Constructor D }; a. Constructor A b. Constructor B c. Constructor C d. Constructor D
d. Constructor D
What is output? #include <iostream> using namespace std; class Shapes { public: Shapes(int l = 5, int b = 5); void Print(); Shapes operator-(Shapes shape2); private: int length; int breadth; }; Shapes Shapes::operator-(Shapes shape2) { Shapes newShape; newShape.length = length - shape2.length; newShape. breadth = breadth - shape2.breadth; return newShape; } Shapes::Shapes(int l, int b) { length = l; breadth = b; } void Shapes::Print() { cout << "Length = " << length << " and " << "Breadth = " << breadth << endl; } int main() { Shapes s1(20, 20); Shapes s2; Shapes s3; s3 = s1 - s2; s3.Print(); return 0; } a. Length = 20 and Breadth = 20 b. Length = 5 and Breadth = 5 c. Length = -15 and Breadth = -15 d. Length = 15 and Breadth = 15
d. Length = 15 and Breadth = 15
Which XXX completes the following code, assuming a class called Movies has been defined with public member functions GetDetails() and SetDetails()? int main() { double movieRating; string actors; XXXcout << "The movie's rating and its actors are: " << newMovie.GetDetails() << endl; return 0; } a. Movies myMovie; myMovie.SetDetails(); b. Movies myMovie; myMovie.GetDetails(); c. Movies newMovie; newMovie.GetDetails(); d. Movies newMovie; newMovie.SetDetails();
d. Movies newMovie; newMovie.SetDetails();
What is output? #include <iostream> using namespace std; class Line { public: Line () { cout << "No Such line"; }; int main() { Line L1, *L2; return 0; } a. Compiler error b. L1 * L2 c. No output d. No Such Line
d. No Such Line
The following program generates an error. Why? #include <iostream> #include <string> #include <Shopping.cpp> using namespace std; 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; } 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.
d. Only .h files can be included, hence including Shopping.cpp is invalid.
What is the output for input values 1, 2, 3, 4, and 5? #include <iostream> #include <vector> using namespace std; void AddSport(vector<int>&sports, int teamsNum) { sports.push_back(teamsNum); } void Print(const vector<int>&sports) { unsigned int i; for (i = 0; i < sports.size(); ++i) cout << " " << sports.at(i); } void Delete(vector<int>&sports) { sports.erase(sports.begin() + 1); } int main() { vector<int> sports; int teamsNum = 0; for(int i = 0; i < 5; i++) { cout << "Add Team Ranking: "; cin >> teamsNum;AddSport(sports, teamsNum); } cout << "Team Ranking: "; Print(sports); Delete(sports); Print(sports); return 0; } a. Team Ranking: 1 3 4 5 1 2 3 4 5 b. Team Ranking: 1 3 4 5 1 3 4 5 c. Team Ranking: 1 3 4 5 d. Team Ranking: 1 2 3 4 5 1 3 4 5
d. Team Ranking: 1 2 3 4 5 1 3 4 5
The following program generates an error. Why? #include <iostream> #include <string> using namespace std; class CurrencyConverter { public: string toCurrency; private: int amount; CurrencyConverter ConvMoney(int amt, string toCurr); }; CurrencyConverter CurrencyConverter::ConvMoney(int amt, string toCurr) { CurrencyConverter myMoney; if (toCurr == "Euro") { myMoney.amount = amt * .89; } else if (toCurr == "Yen") { myMoney.amount = amt * 108.67; } return myMoney; } int main() { int amountIn; string strCurr; CurrencyConverter newMoney; cout << "Enter the amount to convert and currency:"; cin >> amountIn; cin >> strCurr; newMoney = newMoney.ConvMoney(amountIn, strCurr); return 0; } a. The class should be made in separate files. b. The object newMoney should be created using a vector. c. Only the amount should be passed as an argument to the function. d. The function ConvMoney should be made public to call it in main.
d. The function ConvMoney should be made public to call it in main.
The following program generates an error. Why? #include <iostream> #include <string> using namespace std; class Arcade { public: Arcade(); Arcade(string name, int r); void Print(); private: string arcName; int rating; }; Arcade:: Arcade() { arcName = "New"; rating = 1; } Arcade:: Arcade(string name, int r) { arcName = name; rating = r; } void Arcade:: Print() { cout << "Name is: " << arcName << endl; cout << "Rating is: " << rating << " stars" << endl; } int main() { Arcade myArc(Games Ablaze, 5); myArc.Print(); } a. The object creation should be Arcade myArc(Games Ablaze, 5) b. The object creation should be Arcade myArc.Arcade(Games Ablaze 5); c. The object creation should be Arcade myArc.Arcade('Games Ablaze', 5); d. The object creation should be Arcade myArc("Games Ablaze", 5);
d. The object creation should be Arcade myArc("Games Ablaze", 5);
For the class Timer, which XXX initializes the variables to 0? #include <iostream> using namespace std; class Timer { public: XXX void Print() { cout << minutes << ":" << seconds; }; private: int minutes; int seconds; }; int main() { Timer a; a.Print(); return 0; } a. Timer(); { minutes = 00; seconds = 00; } b. Timer::Timer() { minutes = 00; seconds = 00; } c. Timer(); d. Timer() { minutes = 00; seconds = 00; }
d. Timer() { minutes = 00; seconds = 00; }
Which XXX completes the code to generate "Enter the student grade:" as the output? #include <iostream> #include <string> #include <vector> using namespace std; class Student { public: void SetDetails(int StuGrade, string name) { studentGrade = StuGrade;studentName = name; } private: int studentGrade; string studentName; }; int main() { vector<Student> studList; Student newStudent; int r; string nm; cout << "Enter the student grade: " << endl; cin >> r; XXX return 0; } a. newStudent.pushBack(studList); b. studList.pushBack(newStudent); c. newStudent.push_back(studList); d. studList.push_back(newStudent);
d. studList.push_back(newStudent);
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::SetDetails(int numStudents, string name) { XXX YYY this->Print(); } void School::Print() { cout << this->nStudents << " Students are in " << this->schoolName << endl; } 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;
d. this->nStudents = numStudents; this->schoolName = name;
Identify the correct statement for defining the public function. #include <iostream> using namespace std; class School { public: XXX { numClassrooms = crNum; numGrades = grNum; numTeachers = tNum; } private: int numClassrooms; int numGrades; int numTeachers; }; int main() { School newSchool; int classroomNum; int gradeNum; int teacherNum; cout << "Enter the number of classrooms in the new school:" << endl; cin >> classroomNum;cout << "Enter the number of grades in the new school:" << endl; cin >> gradeNum;cout << "Enter the number of teachers the new school:" << endl; cin >> teacherNum;newSchool.SetDetails(classroomNum, gradeNum, teacherNum); return 0; } a. School.SetDetails(int crNum, int grNum, int tNum) b. int SetDetails(int crNum, int grNum, int tNum) c. char SetDetails(int crNum, int grNum, int tNum) d. void SetDetails(int crNum, int grNum, int tNum)
d. void SetDetails(int crNum, int grNum, int tNum)
Which XXX is used for the member function Display() definition in the following code? class Student { public: void SetName(string studentName); void SetGrade(int studentGrade); void Display(); private: string name; int grade; }; XXX { ... } int main() { Student stu1; ... } a. Display() b. void Display() c. void Display::Student() d. void Student::Display()
d. void Student::Display()