Comm200 quizzes

Réussis tes devoirs et examens dès maintenant avec 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

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. F2. D3. C4. E5. B 6. A

Suppose we have the following code in an application program that uses the Time class: int main() { Time T1{1, 2, 3}; Time T2{T1}; Time T3{4, 5, 6}; T3 = T2; } 1. The line Time T2{T1}; invokes the overloaded assignment operator. 2. The line T3 = T2; invokes the copy constructor.

1. False 2. False

The & operator is used to dereference a pointer The * operator is the address operator

1. False 2. False

1. Composition is referred to as a "is-a" relationship. 2. Suppose we have defined a class named Student. One of the member variables in the Student class is for the student's firstname, of type String. This is an example of composition.

1. False 2. True

Suppose there are four classes: Shape, Rectangle, Triangle, and Circle. All four classes have a virtual function named draw. Shape is an abstract base class, and Triangle, Circle, and Rectangle are all non-abstract derived classes that inherit from the Shape class. Suppose the application program has the following statement: ShapePtr->draw(); Where ShapePtr is a pointer of type Shape*. Assume that we have already set shapePtr to point at something before the shapePtr->draw(); statement. 1. When the statement ShapePtr->draw(); is executed, the program will choose the correct derived-class function to call based on the pointer type -- not the object type. 2. We can create Rectangle, Triangle, and Circle objects in the application program. NOTE: Base class is another term for parent class. Derived class is another term for child class.

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

Both of the statements below are about the overloaded operator[] function in the Array class: 1. The function int& operator[](int); returns an lvalue (value that can be used on the left hand side of an assignment statement). 2. The function int operator[](int) const; returns an rvalue (value that can be used on the right hand side of an assignment statement).

1. True 2. True

1.. If a class has at least one "regular" virtual function (e.g., a function with a prototype like virtual void print();), then it automatically becomes an abstract class. 2.. If a class is both a base class and a derived class (e.g., the class inherits from a parent class, and also has a child class that inherits from it), then it CANNOT be an abstract class. NOTE: Base class is another term for parent class. Derived class is another term for child class.

1.. False2.. False

class Vehicle { public: virtual void print() final; }; Another class, such as a class named Train, could inherit from the Vehicle class. The override keyword could be added to the prototype for the print() function, after the word final.

1.. True2.. False

1..A program has the following classes and inheritance relationships: A commissionEmployee is an Employee A basePlusComissionEmployee is a commissionEmployee In the application program, the declaration Employee* e is made. Using e, it is possible to call the print function in the basePlusComissionEmployee class with the statement e->print(). 2.. If a function is declared virtual in the base class but that same function is NOT declared virtual in the derived class, then using a base class pointer it is still possible to call the derived class version of the function. NOTE: Base class is another term for parent class. Derived class is another term for child class.

1.. True2.. True

Which of the options below will make it to where the Supervisor class CANNOT override the print function that it inherits from the Employee class (in other words, the print function from the Employee class will be the last version of the print function)? class Person { 1 ________ }; class Employee: public Person { 2 _________ }; class Supervisor: public Employee { };

1.. virtual void print();2.. virtual void print() final;

class Computer { 1 ____________ }; class Laptop: public Computer { 2 ___________ }; int main() { 3 __________ 4 ___________ 5 ____________ } Which of the options below represents the case where downcasting is needed to call the print function?

2.. void print() {cout<<"LAPTOP\n"; }3.. Laptop L;4.. Computer* C = &L;5.. dynamic_cast<Laptop*>(C)->print();

A destructor receives no parameters. A destructor does not return a value.

A-) 1. True2. True

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? HINT: With a true composition relationship, a class should directly have a member variable of the other classes's type.

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

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

Suppose there is a class named Animal that has a function named speak. Which of the options below contains the correct prototype for the speak function that will make the Animal class an abstract base class?

A-) virtual void speak() = 0;

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

A-)1. False2. 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).

A-)1. True2. False

1.. A final function in a base class can be overridden in a derived class. 2.. An entire class can be declared final.

A-)1.. False2.. True

1.. A program has two classes, Animal and Dog. There is no inheritance relationship between these two classes. In the application program, the declaration Animal* a is made. Using a, it is possible to call the print function in the Dog class with the statement a->print(). 2.. For polymorphism to be possible with a particular function, the function needs to be declared with the same signature (e.g., same function named and parameter list) in both the base class and the derived class. The function needs to be virtual as well. NOTE: Base class is another term for parent class. Derived class is another term for child class.

A-)1.. False2.. True

class A { public: A() {cout<<"A CONSTRUCTOR\n";} void printmsg() { cout<<"A PRINTMSG\n";} }; class B: public A { public: B() {cout<<"B CONSTRUCTOR\n";} void printmsg() { cout<<"B PRINTMSG\n";} }; class C: public B { public: C() 1 ________ {cout<<"C CONSTRUCTOR\n";} void printmsg() { 2 _____________ cout<<"C PRINTMSG\n";} }; Line 1 should be :A() On line 2 it is legal to insert A::printmsg();

A-)1.. False2.. True

class Vehicle { public: virtual void print(); }; class Train: public Vehicle { public: void print(); }; int main() { Vehicle *v; ... (omitted) return 0; } If the pointer v is pointing at a train object, the function call v->print() can be used to invoke the print function in the train class. The vehicle class is abstract.

A-)1.. True2.. False

In an inheritance relationship: A base class can have a virtual destructor. A derived class can have a virtual destructor. NOTE: Base class is another term for parent class. Derived class is another term for child class.

A-)1.. True2.. True

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

Accessor

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

Array (deep copy) overloaded assignment operator

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

B-) 1. False2. False

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

Suppose a program has a templated function sum: 1 ______ T sum(T value1, T value 2) { return value1+value2; } Which of the options below contains the correct template header to insert at line 1 in the code shown above?

B-) template<class T>

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

B-) (*this).y

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

You can use either the word "type" or the word "class" in the declaration of a templated function. An entire class can be templated.

B-)1.. False2.. True

1.. An abstract class can have non-virtual functions. 2.. A derived class cannot be an abstract class. NOTE: Base class is another term for parent class. Derived class is another term for child class.

B-)1.. True2.. False

Suppose a program has the following classes: class Animal { ~Animal() { cout<<"ANIMAL DESTRUCTOR\n"; } }; class Dog: public Animal { ~Dog() { cout<<"DOG DESTRUCTOR\n"; } }; And the following application program: int main() { Animal* a = new Dog; delete a; } What is the expected output of this program in most cases?

B-)ANIMAL DESTRUCTOR

Suppose a program has the following classes: class Person { virtual ~Person() { } }; class Student: public Person { virtual ~Student() { } }; And the following application program: int main() { Person* p = new Student; delete p; } What will happen when the "delete p;" statement is executed in the application program?

Both the Student part and the Person part of the Student object will be destroyed

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

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?

C-) Date* set_day(int);

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

C-) Delegating constructor

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. False2. True

1.. If a function in a base class is a pure virtual function, and if the derived class does not override this pure virtual function, then the derived class becomes an abstract class. 2.. If a class is an abstract class, then this means the class cannot be instantiated (i.e., objects of this class type cannot be created). NOTE: Base class is another term for parent class. Derived class is another term for child class.

C-)1.. True2.. True

1.. With polymorphism, you aim a _ class pointer 2.. At a _ class object

C-)1.. base2.. derived

Which of the following is like a blueprint or template that you can use to create things from?

Class

Suppose a program has the following classes: class Computer { ........ }; class Laptop: public Computer { ...... }; Which of the options below correctly shows (1) what a Computer object looks like and (2) what a Laptop object looks like?

Computer object: Computer part Laptop object: Computer partLaptop part

Suppose there is a class named Computer, which can be used to create objects that store information about a particular computer. This class is developed by the class developer. A different person, the application developer, needs to use the Computer 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:

Computer.cpp

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 a program has the following Course class: class Course { private: string instructor; string textbook; }: And suppose there is only going to be ONE constructor for this class to handle all initializations. Which of the following options below is the correct prototype for the constructor of this class?

Course(string = "", string = "");

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

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. False2. False

The Circle/Square/Triangle classes all inherit from the TwoDimensionalShape class, but these three classes do NOT inherit from the Shape class. The TwoDimensionalShape class is both a base class and a derived class.

D-)1.. False2.. True

Suppose a program has the following classes: class Animal { virtual ~Animal() { cout<<"ANIMAL DESTRUCTOR\n"; } }; class Dog: public Animal { virtual ~Dog() { cout<<"DOG DESTRUCTOR\n"; } }; And the following application program: int main() { Animal* a = new Dog; delete a; } What is the output of this program?

D-)DOG DESTRUCTORANIMAL DESTRUCTOR

Suppose the person-employee-supervisor project is implemented with polymorphism. The person, employee, and supervisor classes all have a function named print that works as follows: Person print function: displays the text "I am a person" Employee print function: displays the text "I am an employee" Supervisor print function: displays the text "I am a supervisor" All of the options below contain legal polymorphic function calls EXCEPT:

D-)Employee* e = new Person;e->print();delete e;

All of the following options below can have default arguments EXCEPT:

Destructor

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_year(y); set_month(m); set_day(d); } 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 year

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

Encapsulation

Programming assignment 7 involved a class named integerSet. One of the functions in this class was unionOfSets. Suppose the following code is in an application program that uses class integerSet: const int SIZE1 = 5;const int SIZE2 = 3; int arr1[SIZE1] = {33, 55, 77, 99, 22};int arr2[SIZE2] = {77, 26, 51}; IntegerSet a(arr1, SIZE1);IntegerSet b(arr2, SIZE2);IntegerSet c; c = a.unionOfSets(b); a.printSet();b.printSet();c.printSet(); And the output of this program is: 22 33 55 77 9926 51 7722 26 33 51 55 77 99 NOTE: set is the name of the vector member variable in the IntegerSet class. It is a vector of type bool (each index in the vector stores either true or false). Which of the options below shows the correct implementation for the unionOfSets member function in the IntegerSet class?

IntegerSet IntegerSet::unionOfSets( const IntegerSet &r ) const{IntegerSet temp; for ( int i = 0; i < setSize; ++i )temp.set[ i ] = set[ i ] || r.set[ i ]; return temp;}

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;

What is the default access specifier in a class?

Private

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

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

Variables

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

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");

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;

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)

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

The doubleVal function is supposed to be passed a pointer to an integer, and it doubles the value of the number. Which of the options below is the correct implementation of the doubleVal function?

void doubleVal(int *ptr) { *ptr *= 2; }

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

The default constructor takes _ argument(s)

zero (0)


Ensembles d'études connexes

exam 2 - upper extremity & shoulder girdle

View Set

Organization behavior, structure, process 4375 Chpt 14

View Set

Physics Conceptual Questions (Exam #2 Practice)

View Set

Exponents and Order of Operations

View Set

15.4.4 - Network Security (Practice Questions)

View Set