C++ Final

Ace your homework & exams now with Quizwiz!

Which of the following is true if a programmer has to create a class Clinic with the following data members and functions? class Clinic{ int patientRecNum; string patientName; void SetDetails(int num, string name); Clinic GetDetails(); };

Data members should be private items and function declarations should be public items.

A(n) _____ can be used to implement an abstract data type (ADT).

class

A ___ is a program used to thoroughly test another program (or portion of a program) through a series of input/output checks.

testbench

What is output? #include <iostream> using namespace std; class Number { public: int x; }; int main() { Number a; int sum = 0; a.x = 10; Number b = a; Number c; sum = a.x + b.x + c.x; cout << sum; return 0; }

20

What is output? #include <iostream> using namespace std; void CheckValue(int*pointVar1, int*pointVar2){ if(pointVar1 == nullptr && pointVar2 == nullptr){ cout<<"Pointer is null"<<endl; } else if(*pointVar2 > *pointVar1){ cout<< *pointVar2 <<endl; } else if( *pointVar1 > *pointVar2){ cout<< *pointVar1 <<endl; } } int main(){ int num1=5; int num2=9; CheckValue(&num1,&num2); return 0; }

9

Which is true about accessors?

An accessor reads a class' data members

Which constructor will be called by the statement House myHouse (12); for the given code? class House {

Constructor D

What is output? #include <iostream> using namespace std; class Greet { public: Greet(); }; Greet::Greet() { cout << "Hello"; } int main() { Greet t1; return 0; }

Hello

Which of the following statements describes an abstraction?

Hiding the implementation and showing the important features

The codes for class Hotel has been defined in two separate files Hotel.h, and Hotel.cpp. Which of the following statements is true?

Hotel.cpp contains member function definitions and includes Hotel.h

Convert the following constructor to a constructor initialization list. Houses::Houses() { houseNum = 1; price = 1.00; }

Houses::Houses():houseNum(1).price(1.00){}

Which of the following statements is not true for a static data member?

It is a data member of each class object.

Which XXX completes the following code, assuming a class Movies has been defined with public member functions GetDetails() and SetDetails()? int main() { double movieRating; string actors; XXX cout << "The movie's rating and its actors are: " << new.Movie.GetDetails << endl; return 0; }

Movies newMovie; newMovie.SetDetails();

What is the return type for constructors?

None

What is output? class Shapes {

Oops!

Which of the following statements is the correct replacement for the member initialization in the following code? class PromNight {

PromNight::PromNight():boys(100), girls(200){}

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<<"AddTeamRanking:"; cin>>teamsNum; AddSport(sports, teamsNum); } cout<<"TeamRanking:"; Print(sports); Delete(sports); Print(sports); return 0; }

Team Ranking: 1 2 3 4 5 1 3 4 5

Which of the following lines in the below code causes an error? vector <int> identityNums(5);

identityNums.at(5) = 5;

The following program generates an error. Why? #include <Shopping.cpp>

only .h files can be included, hence shopping.cpp is invalid

What line of code assigns a char variable outputGames with the value the gamesPointer points to?

outputGames = *gamesPointer

Which XXX statement can be used to remove the third element from vector teamMember? #include <iostream> #include <vector> using namespace std; intmain(){ vector<int> teamMember; vctr.push_back(27); vctr.push_back(44); vctr.push_back(9); vctr.push_back(17); XXX return 0; }

teamMember.erase(teamMember.begin() + 3);

Which XXX statement correctly assigns the data member runTime with the value of the argument t?

this->runtime = t

A pointer is a(n) ____ that contains a ____.

variable, memory address

Which of the following declaration is an accessor?

void GetName() const;

Identify the correct statement for defining the public function. class School { ...

void SetDetails(int crNum, int grNum, int tNum)

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.

push_back()

In the following code, what is the correct return statement for an overloaded + operator? Game Game::operator+(Game newGame) {

return myGame;

Given the following class definition in the file ClassRoom.h, which XXX and YYY complete the corresponding .cpp file? #ifndef CLASSROOM_H

#include "ClassRoom.h" void ClassRoom::Print() const

Three classes are defined as follows. Choose the correct syntax to include all three classes in the main file. <im not typing all of this>

#include"ClassC.h"

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();}

05

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;}

1 2 3 4

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; }

3 5 7

What is the value of myInt?

30

What is output? #include <string> #include <iostream> using namespace std; class Student { public: void SetName(string studentName); void SetScore(int studentScore); string GetGrade() const; private: string name; int score; }; void Student::SetScore(int studentScore) { score = studentScore; } string Student::GetGrade() const { string grade; if (score < 40) { grade = "C"; } else if (score < 70) { grade = "B"; } else { grade = "A"; } return grade; } int main() { Student stu1; stu1.SetScore(80); cout << stu1.GetGrade(); return 0; }

A

Which of the following statements is true about public member functions of a class?

A class user can call the functions to operate on the object. ?

Which of the following statements is true about private data members of a class?

A member function can access the private data members, but class users cannot.

Which of the following should undergo regression testing?

A tested class where three new functions have been added.

Which of these is a dynamically allocated array?

An array whose size can change during runtime.

Which of the following statements is true about inline member functions of a class?

An inline function is typically a short function definistion with compact code

What is output? #include <iostream> using namespace std; class Line{ public: Line(){ cout<<"NoSuchline";,} }; int main(){ Line L1,*L2; return 0; }

Compiler error

What is the output? #include <iostream> #include <vector> using namespace std; int main(){ unsigned int i; vector<int>Xvector; Xvector.push_back(1); Xvector.push_back(2); Xvector.clear(); for(i=0;i<Xvector.size();++i) cout<<''<<Xvector.at(i); return 0; }

Compiler error

For the given outline, identify the correct statement for defining classes for a program that stores electronic items' details.

Create Electronic class as a separate files and then a Gaming class which includes the Electronnic.h file.

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; }

Hi! Good morning!

What is the output? #include<iostream> using namespace std; class Shapes{ public: Shapes(intl = 5, intb = 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(intl, intb){ length=l; breadth=b; } voidShapes::Print(){cout<<"Length="<<length<<"and"<<"Breadth="<<breadth<<endl; } int main(){ Shapess1(20,20); Shapess2; Shapess3; s3=s1-s2; s3.Print(); return 0; }

Length = 15 and Breadth = 15

Convert the following constructor to use a constructor initializer list. Restaurant::Restaurant() { Pizza = 10; Burger = 25; }

Restaurant::Restaurant()::Pizza(10),Burger(25){}

Consider that there is a class Team for soccer team members under Soccer namespace and another class Team for basket team members under Basketball namespace. What is the correct syntax that will help the compiler identify the correct Team?

Soccer::Team SoccerTeamMembers BasketBall:Team BasketBallTeamMembers

What is the output with the given input? 85 Lisa 76 Tina 56 Bell 65 Ben -1 #include <iostream> #include <string> #include <vector> using namespace std; class Student { public: void SetScoreAndName(int studentScore, string studentName) { score = studentScore; name = studentName; }; int GetScore() const { return score; }; string GetName() const { return name; }; private: int score; string name; }; int main() { vector<Student> studentList; Student currStudent; int currScore; int totalScore = 0; double average; string currName; unsigned int i; cout << "Type Score + Name." << endl << "To end: -1" << endl; cin >> currScore; while (currScore > 0) { cin >> currName; currStudent.SetScoreAndName(currScore, currName); studentList.push_back(currStudent); totalScore += currScore; cin >> currScore; } cout << "Students: "; for(i = 0; i < studentList.size(); i++) cout << studentList.at(3 - i).GetName() << " "; average = (totalScore / studentList.size()); cout << endl << "Class Average: " << average << endl; return 0; }

Students: Ben Bell Tina Lisa Class Average: 70

The following program generates an error. Why? #include <iostream> using namespace std; class CurrencyConverter{ ...

The function ConvMoney should be made public to call it in main.

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;}

The number is 20

The following program generates an error. Why? class Arcade {

The object creation should be Arcade myArc("Ganes Ablaze", 5);

Which of the following is true for overloading a class constructor?

The parameter types of the constructors should be different.

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; }

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.

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; }

Timer( ) {minutes = 00; seconds = 00; }

Which of the following statements is the correct replacement for member initialization in the following code? class Toys {

Toys::Toys():toyID(1),desc("New To"){}

Which XXX and YYY complete the School's member function correctly?

XXX) this->nStudents = numStudents; YYY) this->schoolName = name;

The _____ construct defines a new type that can group data and functions to form an object

class

Which XXX is used to declare GetAmount() member function and return the variable values? class Bill { public: void SetProduct (string prodName); void SetAmount (int prodAmount); XXX private: string name; double amount; }; int main() { Bill myBill; cout << myBill.GetAmount(); }

double GetAmount();

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; }

inline function definition

Consider the customer management system and the inventory management system of a coffee shop. Which items should be apart of the CustomerBillDetails object type?

orderQuantity

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 ... }

stu1.Display();

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(); }

stu1.Grade();

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; }

studentList.push_back(newStudent);

A commuter swipes a public transport card at a kiosk and sees his account balance displayed on the screen. The commuter would be viewing an abstraction if _____.

there is other information about his account that is not displayed on screen

Assuming a class Student has been defined, which of the following statements is correct for declaring a vector?

vector studList;

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; ... }

void Student::Display()


Related study sets

HEMA 2 LAB - PRELIMS - BLOOD SMEAR

View Set

NURS1140: CH.3 Drugs Across the Lifespan

View Set

Chapter 2 - Behavior Modification

View Set

Quickbooks Section 4 (3rd pending Attempt)

View Set

Chapter 8 & 9: True/False Practice

View Set