COMSC Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

Suppose a program has the following array defined: int arr[12]; Which of the options below uses pointer notation to store 99 in arr[6[?

*(arr+6) = 99;

In a UML diagram, which of the options below 1) Describes a function that can be accessed anywhere - both inside of the class and outside of the class (such as in main) and 2) Returns an integer?

+some_name(): int

In a UML diagram, which of the options below 1) depicts a function that can be accessed ONLY inside of the class code and 2) returns an integer?

-some_func(): int

Suppose we have the following stored in RAM (memory): Where for each row in the table, the address column lists a hexadecimal memory address, and the value column lists the value that is stored at that memory location. For example, the value 55 is stored at memory location 0X1234ABCD. What is the output of the following cout statements? cout<<&ptr<<endl; cout<<ptr<<endl;

0XFABC7890 0X1234ABCD

#include <iostream> #include "Tree.h" using namespace std; void tree_func(); Tree A; Tree B; int main() { { tree_func(); static Tree C; } Tree D; return 0; } void tree_func() { static Tree E; { Tree F; } } What is the order in which the objects are destroyed? _ is destroyed first. _ is destroyed second. _ is destroyed third. _ is destroyed fourth. _ is destroyed fifth. _ is destroyed sixth.

1. F 2. D 3. C 4. E 5. B 6. A

class first { private: int number; public: int get_number() {return number;} }; class second: protected First { ... }; class Third: public second { ... }; int main() { second second_obj; third third_obj; 1 __________ 2 ___________ } on line 1 in main the function call second_obj.get_number(); can be made on line 2 in main the function call third_obj.get_number(); can be made

1. False 2. False

A utility function is usually a public member function in a class. A utility function is designed to help another member function in the class perform its task.

1. False 2. True

1. Static member variables in a class can be accessed in non-static const member functions. 2. Non-static member variables in a class can be accessed in static member functions.

1. True 2. False

A derived class can directly access its base classes protected members. In the base class, declaring the member variables private can lead to the situation where changes in the base class can break the derived class (i.e., changes in the base class cause the derived class to not work).

1. True 2. False

Suppose a program has the following College class: class College { std::string college_name; College(std::string c); }; How would the constructor be represented in a UML diagram?

<<constructor>> +College(c: string)

class Car { Public: Car(const string &ma, const string &mo, int ye) { make = ma; model = mo; year = ye; cout<<"CAR CON\n"; } ~Car() { cout<<"CAR DES\n"; } private: string make; string model; int year; }; class Person { Person(const string &n, int a, const Car&c) { name = n; age = ag; car= c; cout<<"PERSON CON\n"; } ~Person() { cout<<"PERSON DES\n"; } string name; int age; Car car; }; int main() { Car c("Toyota", "Corolla", 2017); Person p("James Bond", 45, c); } What is the output of this program?

A-) CAR CON PERSON CON PERSON DES CAR DES CAR DES

Suppose there is a class named Course with member variable num_of_students: class Course { Private: int num_of_students; } This class is supposed to have an overloaded ++ (increment) operator to increment num_of_students for any Course object. Which of the following options below is the corrrect prefix ++ operator implemented as a member function?

A-) Course& Course::operator++(){num_of_students+=1;return *this;}

Suppose we want to model the following relationship between a Person class, Computer class, and CPU class: Which of the options below contains the correct class declarations?

A-) class CPU { private: string manufacturer; } class Computer { private: string manufacturer; CPU cpu; } class Person { private: string name; Computer computer; }

Suppose we want to model the following relationship between a Person class, Computer class, and CPU class: Which of the options below contains the correct class declarations?

A-) class CPU { private: string manufacturer; } class Computer { private: string manufacturer; CPU cpu; } class Person { private: string name; Computer computer; }

Suppose a House class has a static member variable count, and a static member function get_count. Which of the options below is the correct implementation of the get_count function?

A-) unsigned int House::get_count() { return count; }

Every member function in any class has a hidden this pointer as one of it's parameters. For example, suppose we have a CPU class with a member function set_CPU. Which of the options below correctly shows the this pointer?

A-) void CPU::set_CPU(CPU *this, string man) { manufacturer = man; }

With the way the assignment operator was overloaded in the Coord class, all of the options below are allowed EXCEPT:

A-) (C1 = C2) = (C3 = C4);

Which of the following operators MUST be overloaded as a member function?

A-) =

Suppose a class named Factory has overloaded the == operator, and now we want to overload the != operator in such a way that the overloaded != operator calls the overloaded == operator. The header of this function is: bool Factory::operator!=(const Factory &f) const Which of the following options below is the correct body of this function?

A-) return !(*this == f);

Static member variables cannot be accessed by non-static member functions. The this pointer is passed to static member functions.

A-)1. False 2. False

Suppose we have the following code in an application program that uses the Array class (shallow copy version): int main() { { Array int1(2); Array int2(3); Array int3 = int2; } } 1. The line Array int3 = int2; invokes the overloaded assignment operator. 2. The code shown above will crash the program.

A-)1. False 2. True

A non-const member function can call a const member function. A const member function can call a non-const member function.

A-)1. True 2. False

The diagram above shows multiple inheritance. The TwoDimensionalShape class is both a base class and a derived class.

A-)1.. True 2.. True

Another name for a get function in a class is what?

Accessor

All of the following options below are valid arithmetic operations that can be performed on pointers EXCEPT:

Add one pointer to another pointer (ptr1 + ptr2)

Suppose there is a class named Airplane, which can be used to create objects that store information about a particular airplane. This class is developed by the class developer. A different person, the application developer, needs to use the Airplane class in their application. However, as described above, the application developer did NOT develop the class - they are just using it. The application code is in a file named Main.cpp. In this scenario, the application developer would normally have access to all of the following files EXCEPT:

Airplane.cpp

Which of the following options below correctly describes the principle of least privilege?

All member variables and all member functions of a class should have private visibility unless it can be proven that the member needs public visibility.

A variable that is declared inside of a class definition (but NOT inside of any function) is known as a/an what?

Attribute

The destructors for all objects are invoked when the main function goes out of scope. While the destructor is running the memory held by the object is released.

B-) 1. False 2. False

#include <iostream> #include "Tree.h" using namespace std; int main() { { Tree A; Tree B } Tree C; Tree D: } What is the order in which the objects are destroyed? _ is destroyed first. _ is destroyed second. _ is destroyed third. _ is destroyed fourth.

B-) 1.. B 2.. A 3.. D 4.. C

Suppose there is a class named Person which has a static member count, and a static member function get_count. Which of the options below shows the way that get_count would typically be called?

B-) int main() { cout << Person::get_count() < < endl; }

Which of the options below is equivalent to this->y?

B-) (*this).y

int main { Coord C1(93, 44); cout<<C1--; cout<<C1 cout<<-C1; cout<<C1; } What is the output of the above program?

B-) (93, 44) (92, 43)(-92, -43) (92, 43)

Which of the following operators can be overloaded?

B-) ->

class Course { private: string course_name; public: void set_course_name(const string &cn) { course_name = cn; } } What is the type of the hidden this pointer in the set_course_name function?

B-) Course* const

Constructors are called for _ objects first.

B-) global

Suppose there is a class named Computer that is supposed to overload the >> operator. Which of the options below is the correct prototype for the overloaded >> operator?

B-) istream& operator>>(istream &input, Computer &comp);

... (header omitted) { size = arr.size; ptr = arr.ptr; } The code shown above is part of the implementation of what from the Array class (shallow copy version)?

B-) operator= function

Which of the options below contains the correct prototypes for BOTH of the overloaded [] operators in the array class?

B-)int& operator[](int);int operator[](int) const;

Select the false statement regarding inheritance.

Base classes are usually more specific than derived classes.

Logically (i.e., as a concept), objects consist of _ Physically (i.e., in memory), objects consist of _

Both member variables and member functions Only member variables

return x != value || y != value; The above is an implementation of: Operator != as a _ function Which compares a Coord object With a _

C-) 1.. Member 3.. integer

Suppose a program has the following Circle class: class Circle: { private: double radius; }; Which of the following is the correct explicit usage of the this pointer?

C-) void set_radius(double radius) { this->radius = radius; }

Which of the following operators MUST be overloaded as a friend function?

C-) <<

What is the name of a constructor that calls another constructor?

C-) Delegating constructor

The _ of an operator CAN be changed by overloading.

C-) functionality

Suppose a program has a Car class: class Car { private: string model; }; Which of the options below is considered a dangerous reference return?

C-) string& set_model(string);

When a function is declared a friend by a class, it becomes a member of that class. An entire class may be declared a friend of another class.

C-)1. False 2. True

In programming assignment 7, one of the functions in the DeckOfCards class was the dealCard function. The dealCard function returns the next Card object from the Deck vector. In the application program, this function is called a total of 52 times to deal each card in the deck. So for example: deck[0] = Card(5, 2) deck[1] = Card(3, 1) deck[2] = Card(10, 3) deck[3] = Card(7, 1) .... deck[51] = Card(6, 0) The first time the dealCard function is called, it will return the card object stored at deck[0]. The second time it is called, it will return the card object stored at deck[1]. And the last time it is called, it will return the Card object stored at deck[51]. Which of the options below is the correct implementation of the dealCard function in the DeckOfCards class? NOTE: Remember that the currentCard member variable in the DeckOfCards class is used to store an index number for the deck vector.

Card DeckOfCards::dealCard() { return deck[currentCard++]; }

In programming 7, one of the functions in the DeckOfCards class was the shuffle function. The shuffle function loops through each index of the deck vector, and on each iteration of the loop a number between 0 and 51 is randomly chosen. The Card object stored at index i of the deck vector is then swapped with the card object at the randomly chosen index. For example, suppose the current index is 0 (i.e., i=0), and the randomly chosen index is 51. Then the Card objects stored at deck[0] and deck[51] will be swapped: deck[0] = Card(12, 3) deck[1] = Card(0, 1) deck[2] = Card(0, 2) deck[3] = Card(0, 3) .... deck[51] = Card(0, 0) The Card class has the following private member variables: face (integer), suit (integer), faceNames (static array of type string), and suitNames (static array of type string). The static faceNames and suitNames arrays have the following values: const string Card::faceNames[ FACES ] ={ "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; const string Card::suitNames[ SUITS ] ={ "Hearts", "Diamonds", "Clubs", "Spades" }; The idea is that each card object stores two numbers -- the face member variable is used as an index for the faceNames array, and the suit member variable is used as an index for the suitNames array. This is so that using all four member variables (face, suit, faceNames, and suitNames), the toString function can print out each card in the form of "face of suit". For example, "Ace of Hearts". For example suppose we have the following card object: Card c(1, 2); // first integer is the face, second integer is the suit For this card object, face is 1 and suit is 2. Index 1 of the faceNames array is "Deuce", and index 2 of the suitNames array is "Clubs". Therefore, if we called the toString member function using this Card object, it would return the string "Deuce of Clubs". Suppose we wanted to enable cascaded member function calls in programming assignment 7. The idea is that after creating a DeckOfCards object in the application program, it would be possible to shuffle the deck first, and then print out the first card (e.g., "Ace of Hearts") all on one line. So the application program would look like this: DeckOfCards d; cout << d.shuffle().toString(); // this would print out the first card What should be the return type of the shuffle function to make this work correctly?

Card&

Which of the options below grants the minimum (least) access?

Constant pointer to constant data

Suppose we have the following declaration: int arr[5] = {1, 2, 3, 4, 5}; In class we discussed the fact that the name of an array (in this case arr) is actually a special kind of pointer. What type of pointer is arr?

Constant pointer to nonconstant data

Which of the following can use a member initializer list?

Constructor

Both the + operator and += operator are overloaded in the Coord class. The Coord class has two private member variables, x and y, which both represent coordinates. One possible way to implement the += operator is to have it call the + operator function and the = operator function. This is possible because if C1 and C2 are both Coord objects, the statement C1+=C2 is equivalent to C1=C1+C2. The body of the operator+= function would then look like this: *this = *this + c; return *this; And if the application program has the following code: int main() { Coord C1(1, 2), C2(3, 4); C1+=C2; } Initially, C1.x=1, C1.y=2, C2.x=3, and C2.y=4. After executing the C1+=C2 statement, C1.x=4 and C1.y=6. Which of the options below is the correct prototype for the operator+= function in the Coord class?

Coord& Coord::operator+=( const Coord &c )

Suppose there is a class named House with member variable num_of_bedrooms: class House { private: int num_of_bedrooms; } This class is supposed to have an overloaded -- (decrement) operator to decrement num_of_bedrooms for any House object. Which of the following options below is the postfix -- operator implemented as a friend function?

D-) House operator--(House &h, int) { House temp = h; h.num_of_bedrooms-=1; return temp; }

A destructor can have parameters. A destructor does not return a value.

D-) 1. False 2. True

return Coord(C1.x + C2.x, C1.y + C2.y); The above is an implementation of: Operator _ As a _ function

D-) 1.. + 2.. Friend

Suppose we want to be able to use the member functions set_CPU, set_RAM, and set_GPU in cascaded member function calls for a computer object. For example, we want to be able to do things like: Computer comp; comp.set_CPU("Intel").set_RAM("Corsair").set_GPU("Nvidia"); Which of the options below is the correct implementation of the set_CPU function?

D-) Computer& Computer::set_cpu(string man) { manufacturer = man; return *this; }

When working with primitive types (such as integers), the + operator has higher precedence than the ^ operator. For example: int main() { int a, b=1, c=2, d=3; a = b^c+d; } In the above program, the expression c+d will be evaluated first. Suppose the Coord class has implemented operator^ to do exponentiation as follows: Coord Coord::operator^(const Coord &c){ return Coord(pow(x, c.x), pow(y, c.y));} int main() { Coord C1(1, 1), C2(1, 1), C3(1, 1); cout<< C3+2^C1^2+C2 <<endl; } What is the output of this program?

D-) (27, 27)

What is the return type of operator+ in the Coord class?

D-) Coord

Suppose a program has the following Coord class: class Coord { private: int x; int y; }; One of the member functions of this class is set_x. Suppose we want to be able to use member function set_x in cascaded member function calls. Meaning it should be possible to do things like c.set_time(5).set_time(7) if c is a Coord object. Which of the options below contains the correct prototype for member function set_x?

D-) Coord& set_x(int);

Suppose a program has the following Date class: class Date { private: int day; int month; int year; }; Suppose we want to be able to use member functions set_day, set_month, and set_year in cascaded member function calls. In particular, in the application program it should be possible to do: dptr->set_day(1)->set_month_(12)->set_year(1970); Where dptr is a pointer to a Date object. Which of the options below contains the correct prototype for member function set_day?

D-) Date& set_day(int);

Which access specifier applies to friend functions?

D-) None of the above

Suppose a program has the following class header: class A { public: int B() const; int C(); } And the following application program: int main(){ A a1; const A a2; } Which of the options below will cause the program to not compile if it is included in the application program?

D-) a2.C();

... (header omitted) :size(arr.size), ptr(arr.ptr) { } The code shown above is part of the implementation of what from the Array class (shallow copy version)?

D-) copy constructor

Suppose there is a class named Ship that has overloaded the >> operator. The following code is in the application program: Ship s1, s2; cin >> s1 >> s2; What do the function calls to operator>> look like?

D-) operator>>(operator>>(cin, s1), s2);

In the Coord class, suppose we wanted to implement operator- (subtraction) in such a way that it uses both operator+ and operator- (unary minus). Which of the options below implements operator- (subtraction) in this way as a friend function that subtracts one Coord object from another?

D-) return C1 + -C2;

Friendship is symmetric (if A is a friend of B, this also means B is a friend of A). Friendship is transitive (if A is a friend of B and B is a friend of C, this also means A is a friend of C).

D-)1. False 2. False

Suppose we have the following code in an application program that uses the Array class (shallow copy version): int main() { { Array int1(2); Array int2(3); Array int3 = int2; } } 1. The line Array int3 = int2; invokes the copy constructor. 2. The code shown above will crash the program.

D-)1. True 2. True

Suppose a program has the following class: class Date{ private:int day;int month;int year;public:void set_date(int, int, int);void set_day(int);void set_month(int);void set_year(int); }; void Date::set_date(int d, int m, int y){set_day(d);set_month(m);set_year(y); } void Date::set_day(int d){if(d >=1 && d<=31)day = d; elsethrow invalid_argument("invalid day"); } void Date::set_month(int m){if(m >=1 && m<=12)month = m; elsethrow invalid_argument("invalid month"); } void Date::set_year(int y){if(y >=1 && y<=2017)year = y; elsethrow invalid_argument("invalid year"); } And the following driver program: int main(){ Date d; try{d.set_date(32, 13, 2018); } catch (invalid_argument &e){cout<<"EXCEPTION: "<<e.what() << endl; } } What is the output of this program?

EXCEPTION: invalid day

One of the concepts we learned about early in chapter 3 is data hiding. Which of the terms below refers to data hiding?

Encapsulation

int x=5, *ptr = nullptr; ptr = &x; ptr = 100; // Store 100 in x cout << x << endl; What is/are the error(s) in the code shown above?

Line 3 should be *ptr = 100;

Which of the following can be used to initialize an object when it is declared in an application program?

Public Constructor

Suppose there are two pointers, ptr1 and ptr2. Both are pointers to integers. if(ptr1 == ptr2) cout<<"Same A"<<endl; else cout<<"Different A"<<endl; if(*ptr1 == *ptr2) cout<<"Same B"<<endl; else cout<<"Different B"<<endl; All of the following combinations of output messages are possible EXCEPT:

Same A Different B

What is the relationship between throw, try, and catch?

Throw signals that an exception has occurred Try contains code that could possibly cause an exception Catch handles an exception

A UML diagram for a class consists of three parts: the top part, the middle part, and the bottom part. What are shown in the middle part of a UML diagram? UML Diagram Top Part Middle Part Bottom Part

Variables

The size of an object depends on the amount of memory required to store the class's what?

Variables

...(header omitted) { size = arr.size; ptr = new int[size]; for (size t, i = 0; i < size; ++i) ptr[i] = arr.ptr[i]; }

array (deep copy) copy constructor

.... (omitted) { delete[] ptr; size = right.size; ptr = new int[size]; } The code shown above is part of what?

array (deep copy) overloaded assignment operator

Suppose a Rectangle class has a function named isEqualTo, which will compare two rectangle objects to see if they are equal to one another. The function returns true if the two Rectanagle objects are equal to one another, otherwise it returns false. If we have Rectangle objects r1 and r2, then the following is an example of how we could call the function: r1.isEqualTo(r2); Which of the options below is the correct function header for the isEqualTo function in the Rectangle class?

bool Rectangle::isEqualTo(const Rectangle& r) const

Suppose there is a Class named Car, which has a member function named set_make that can be used to set the make of a car object to a particular value (e.g. "Toyota", Ford", "Hyundai", etc.): NOTE: Access specifiers have been omitted here because there is another question on this quiz about them. class Car { string make; void set_make(string m) { make = m; } string get_make() { return make; } }; Then in main we have the following code: int main { string value = "Toyota"; Car car_obj; ................ return 0; } Which line of code could be inserted in main below the line Car car_obj; to call the set_make function for the car_obj object with the argument "Toyota"? car_obj->set_make(value);

car_obj.set_make(value);

Suppose there are two classes, Bedroom and House. Which of the options below shows the correct relationship that exists between these two classes?

class House { private: vector<Bedroom> bedroom; }

Suppose a program has the following class: class Computer { private: string gpu; public: void set_gpu(string g) { gpu = g; } }; And the following driver program: int main { Computer comp; Computer &compref = comp; Computer *compptr = &comp; } For which of the options below are BOTH statements 1 AND 2 correct for setting the gpu member variable to the string "NVIDIA GeForce GTX 1080" for the comp object?

compref.set_gpu("NVIDIA GeForce GTX 1080"); compptr->set_gpu("NVIDIA GeForce GTX 1080");

double val = 55.2; double * ptr = &val; For which of the options below will BOTH cout statements print out the ADDRESS of val?

cout<<ptr<<endl; cout<<&val<<endl;

Which of the following options below will print out the addresses of all of the elements of an array?

for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl;

In object oriented programming, ____ allows you to create new classes based on existing classes.

inheritance

Which of the options below will print out all of the values in the array (1, 2, 3, 4 and 5)? const int SIZE = 5; int arr [5] = {1, 2, 3, 4, 5}

int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; }

For operator+ in the Coord class, all of the following are possible valid ways to overload the + operator to add a Coord object and an integer together EXCEPT:

integer + Coord object (member function)

assuming the following is the beginning of the constructor definition for class baseplus-commissionemployee which inherits from class Point: baseplus *The above should read "which inherits from class ComissionEmployee" (NOT "which inherits from class Point")

invokes the CommissionEmployee constructor with arguments

Suppose the following is declared in the application program of the deep copy project: array newarr[5]; where each index of the array stores an array object. For newarr[0], the goal is store the value 7 in index 0 of this array. Which of the options below is correct?

newarr[0], operator[](0) = 7;

All of the following options below are valid parameter lists for a operator* function EXCEPT: HINT: Think of all possible combinations of the operator* function in the Coord class (both member function and friend function implementations).

operator*(int, int)

Suppose a c++ project has classes vehicle, car, and toyota, with the following 3-level inheritance hierarchy: a toyota is a car a car is a vehicle Most of the code for the implementation of operator << in the Toyota class is shown below: std::ostream& operator <<(std::ostream& output, Toyota &t) { 1 ________________________ output << t.model << ""<< t.year << endl; return output; } what should be inserted at line 1?

output << static_cast<Car>(t)<<endl;

int arr[5] = {10, 15, 20, 25, 30}; int *ptr = arr; Which of the options below uses pointer subscript notation?

ptr[2] = 1337;

One of the two classes in programming assignment 7 was the Card class. This class consisted of: Data members face and suit of type int. These are index numbers for the faces and suits arrays described in step c below. A constructor that receives two ints representing the face and suit and uses them to initialize the data members. Two static arrays of strings representing the faces and suits. The faces array stores the strings "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", and "King". The suits array stores the strings "Hearts", "Diamonds", "Clubs", and "Spades". A toString function that returns the Card as a string in the form "face of suit". For example, "Ace of Hearts". You can use the + operator to concatenate strings. So the class has the following private member variables: face (integer), suit (integer), faceNames (static array of type string), and suitNames (static array of type string). The static faceNames and suitNames arrays have the following values: const string Card::faceNames[ FACES ] ={ "Ace", "Deuce", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; const string Card::suitNames[ SUITS ] ={ "Hearts", "Diamonds", "Clubs", "Spades" }; The idea is that each card object stores two numbers -- the face member variable is used as an index for the faceNames array, and the suit member variable is used as an index for the suitNames array. This is so that using all four member variables (face, suit, faceNames, and suitNames), the toString function can print out each card in the form of "face of suit". For example, "Ace of Hearts". For example suppose we have the following card object: Card c(1, 2); // first integer is the face, second integer is the suit For this card object, face is 1 and suit is 2. Index 1 of the faceNames array is "Deuce", and index 2 of the suitNames array is "Clubs". Therefore, if we called the toString member function using this Card object, it would return the string "Deuce of Clubs". Which of the options below is the correct implementation of the toString member function in the Card class?

string Card::toString() const{return faceNames[face] + " of " + suitNames[suit];}

Suppose a program has the following class and main function: NOTE: Access specifiers have been omitted here because there is another question on this quiz about them. class College { string name; string get_name() { return name; } void set_name(string n) { name = n; } }; int main() { College college_obj; return 0; } On which line in the above program would it be appropriate to insert the word "const" ?

string get_name()

Suppose there is a class named Gas_Pedal, which represents a gas pedal in a car. The Gas_Pedal class has a member function named apply_gas_pedal, which takes in an integer (0 - 100) as an argument. The prototype of this member function is: void apply_gas_pedal(int); The integer passed to the function (0 - 100) represents the degree of force applied to the gas pedal (i.e., how hard the gas pedal is being pressed down). The larger the number, the more force is applied to the gas pedal. For example, apply_gas_pedal(0) represents the case were the gas pedal is not being pressed down on at all (i.e., the car is not moving). apply_gas_pedal(100) represents the case were the gas pedal is being pressed down all the way (i.e., the car is traveling at it's maximum speed). Suppose there is also a class named Car. The Car class has a member function named increase_speed, which takes in an integer (0 - 100) as an argument. The prototype of this member function is: void increase_speed(int); The integer passed to the function (0 - 100) represents the degree to which the car's speed is increased. The larger the number, the more the car's speed is increased. For example, increase_speed(0) would not increase the car's speed at all (i.e., the car would continue traveling at it's current speed). increase_speed(100) would bring the car to it's maximum speed. Which of the options below shows the correct relationship between the apply_gas_pedal and increase_speed functions from the Gas_Pedal and Car classes, respectively?

void Car::increase_speed(int amount) { gas_pedal.apply_gas_pedal(amount); }

Suppose a program has the following College class: class College { std::string college_name; void set_college_name(std::string c); std::string get_college_name(); }; The functions will be implemented in another file outside of the class definition. Which of the options below is the correct way to implement the set_college_name function?

void College::set_college_name(string c) { college_name = c; }

In programming assignment 7 one of the classes is DeckOfCards. One of the member variables in the DeckOfCards class is a vector named deck, which is of type Card: class DeckOfCards { private: vector<Card> deck; } The idea is that each index in the deck vector is storing an entire Card object. Each Card object in-turn stores two integers: one to represent the face of the card (the first integer), and one to represent the suit of the card (the second integer). For example, suppose the deck vector is initialized as follows: deck[0] = Card(0, 0) deck[1] = Card(0, 1) deck[2] = Card(0, 2) deck[3] = Card(0, 3) .... deck[51] = Card(12, 3) One of the member functions in the DeckOfCards class is the shuffle index. The shuffle function loops through each index of the deck vector, and on each iteration of the loop a number between 0 and 51 is randomly chosen. The Card object stored at index i of the deck vector is then swapped with the card object at the randomly chosen index. For example, suppose the current index is 0 (i.e., i=0), and the randomly chosen index is 51. Then the Card objects stored at deck[0] and deck[51] will be swapped. The updated state of the deck vector is: deck[0] = Card(12, 3) deck[1] = Card(0, 1) deck[2] = Card(0, 2) deck[3] = Card(0, 3) .... deck[51] = Card(0, 0) Which of the options below is the correct implementation of the shuffle member function in the DeckOfCards class?

void DeckOfCards::shuffle() { Card temp(0, 0); int random_index; for (int i = 0; i < deck.size(); i++) { random_index = rand() % 52; temp = deck[i]; deck[i] = deck[random_index]; deck[random_index] = temp; } }

Suppose a program has the following House class (implementation details are omitted): class House { int num_of_bedrooms; House(); House(int b); void set_num_of_bedrooms(int b); int get_num_of_bedrooms(); }; And for each House object that is created, it needs to be guaranteed that the num_of_bedrooms is at least 1. In other words, input validation logic is needed to make sure that num_of_bedrooms is always at least 1. If code in the application program (i.e., in main) attempts to set num_of_bedrooms to less than 1, the input validation logic will detect this and simply set num_of_bedrooms to 1, and display an error message to the user that the number of bedrooms must be at least 1. Where in the House class would it be appropriate to place this input validation logic?

void set_num_of_bedrooms(int b)

A swap function works as follows: It takes in two pointers as arguments. Both pointers point to integers. The values of the two pointers are swapped. For example if num1 is initially 5 and num2 is initially 10, then num1=10 and num2=5 after the swap function has finished executing. NOTE: Assume the following code is in main: int num1=10, int num2=5; swap(&num1, &num2); Which of the options below is the correct implementation of the swap function?

void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }


Conjuntos de estudio relacionados

Chapter 18 - Gastrointestinal and Urologic Emergencies

View Set

Nutrition: Implement and Take Action; Evaluate

View Set

Chapter 17: Investments & business opportunity brokerage

View Set

PassPoint - Medication and IV Administration

View Set

ARCH 3323 - Test 2 - Chapter 4 Short Answer

View Set

A.D.Banker - Chapter 8 Commercial Property Insurance

View Set