COMSC 200 Quizzes #7-#12

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

.... (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 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 contains the correct prototypes for BOTH of the overloaded [] operators in the array class?

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

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

C-)1.. True2.. True

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

Suppose the following code is in an application program uses the deep copy array class

INSIDE(INT) CONSTRUCTOR INSIDE(INT) CONSTRUCTOR INSIDE DESTRUCTOR INSIDE DESTRUCTOR

Assuming the following is the beginning of the constructor definition for class BasePlus-ComissionEmployee which inherits from class Point: BasePlusCommissionEmployee::

Invokes the CommissionEmployee constructor with arguments

Suppose a C++ project has classes Vehicle, Car, and Toyota, with the following 3-level inheritance hierarchy:

Only a call to the car constructor

Suppose a program has the following classes: class Shape { }; class Rectangle:public Shape { };

1. A shape object has a shape part 2. a rectangle object has a shape part 3. a rectangle object has a rectangle part

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

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.

1. False 2. True

Suppose we have the following code in an application program that uses the Array class:

1. Shallow 2. Because it attempts to delete the same memory twice

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

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

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

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

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

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;

1. Static member variables cannot be accessed by non-static member functions. 2. 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

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

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 part Laptop part

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

D-) 1.. + 2.. Friend

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

D-) Coord

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

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

With public inheritance: 1. private members in the base class become _ in the derived class 2. protected members in the base class become _ in the derived class 3. public members in the base class become public in the derived class

1. hidden 2. protected

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

(header omitted) size = arr size; ptr = new int[size] for(size t; i 0; i < size; ++i) ptr[i] = arr.ptr[i] the code shown above is the implementation of what?

Array (deep copy) copy constructor

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>

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)

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

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

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

C-) <<

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

C-)1.. base2.. derived

Suppose a C++ project has classes Vehicle, Car, and Toyota, with the following 3-level inheritance hierarchy:

Car::operator = (t);

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 )

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)

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

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;

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;

Programming assignment 8 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;}

Suppose a C++ project has classes Vehicle, Car, and Toyota, with the following 3-level inheritance hierarchy:

VEHICLE CONSTRUCTOR CAR CONSTRUCTOR TOYOTA CONSTRUCTOR TOYOTA DESTRUCTOR CAR DESTRUCTOR VEHICLE DESTRUCTOR

When should base class members be declared protected?

When these members should be available only to derived classes (and friends), but not to other clients

In the deep copy project, the == was overloaded. this implementation of this operator begins by comparing the sizes of the two array objects

if(this->size!= right size) return false;

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 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 to store the value 7 in index 0 of this array.

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

Suppose a C++ project has classes Vehicle, Car, and Toyota, with the following 3-level inheritance hierarchy:

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


Set pelajaran terkait

Ch 65 PrepU Assessment of Neurologic Function

View Set

Evolve: Fundamentals Basics of Nursing Practice

View Set

Anatomy Assignments (Review for test)

View Set

Human Geography: Genocide- Multi-Ethnic state

View Set

PROD 315 Dynamic Study Module 12

View Set

Finance Test Test Q's From Old Tests +Problem Sets+

View Set

7th Grade Math Midterm Study Guide

View Set