21, 22, 23, 24, 25, 26

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

[2437] What prints? class Employee { public: Employee() = default; Employee(const string& n, double s) : name(n), salary(s) {} void setName(const string& n) { name = n; } void setSalary(double s) { salary = s; } string getName() const { return name; } double getSalary() const { return salary; } private: string name; double salary = 0; }; class Manager : public Employee { public: Manager() = default; Manager(double b) { bonus = b; } Manager(const string& n, double s, double b) : Employee(n, s), bonus(b) {} void setBonus(double b) { bonus = b; } void print() const; private: double bonus; }; void Manager::print() const { cout << getName() << " $ " << getSalary() << "; Bonus: " << bonus << endl; } int main() { Manager m1; Manager m2(1000); Manager m3("Peter", 30000, 1000); m2.print(); } Peter $ 30000; Bonus: 1000 $ 30000; Bonus: 1000 Peter $ 0; Bonus: 1000 $ 0; Bonus: 1000

$ 0; Bonus: 1000

[2438] Manager is derived from Employee. Which of the following statements are true? Every Manager constructor will implicitly call the default Employee constructor An Employee constructor will implicitly call the default Manager constructor A Manager constructor can pass data to an Employee constructor All of the above statements are true

A Manager constructor can pass data to an Employee constructor

[2423] Which one of the following is an example of the "substitution principle"? A base-class object must be used in place of a derived class object A base-class object can be used in place of a derived class object A derived class object must be used in place of a base-class object A derived class object can be used in place of a base-class object

A derived class object can be used in place of a base-class object.

[2511] Below is a class hierarchy. Which assignment will fail to compile? class Pet { . . . }; class Puppy : public Pet { . . . }; class Kitty : public Pet { . . . }; class Ducky : public Pet { . . . }; Pet pet; Puppy pup; Kitty kit; Duck duck; pet = kit; pet = pup; Puppy& pr = pup; Pet* p = &duck; All of these will compile

All of these will compile

[2527] What prints when this code is run? (Note that struct is used instead of class only to make all members public and to make the code shorter). #include <string> #include <iostream> using namespace std; struct B { 🌺string str() const { return "B"; }}; struct D1 : public B { virtual string str() const { return "D1"; }}; struct D2 : public B { string str() const { return "D2"; }}; struct D3 : public D1 { string str() const { return "D3"; }}; int main() { B *p1(new D1), *p2(new D2), *p3(new D3); cout << p1->str() << p2->str() << p3->str() << endl; } BBB BBD3 D1BD3 D1D2D3

BBB

[2509] Below is a class hierarchy for card games. Assuming that these are the only classes and that the concrete classes are correctly completed, which of the following definitions will not compile? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; Hand* h = new Hand; BlackjackHand* h = new Hand; Hand* h = new BlackjackHand; GoFishHand gfh; Hand& h = gfh;

BlackjackHand* h = new Hand;

[2420] What does a derived class inherit from its base class? Only data Neither data nor behavior Only behavior Both data and behavior

Both data and behavior

[2427] How can a derived class override a base class function? Nothing is required in the derived class - this is automatically provided by inheritance It is impossible for the derived class to override a base class function By providing a new implementation for a function, tagged with the override reserved word By providing a new implementation for a function with the same name and parameter types

By providing a new implementation for a function with the same name and parameter types

[2517] Below is a class hierarchy. Which assignments are illegal? class Widget { . . . }; class Label: public Widget { . . . }; class Button: public Widget { . . . }; class Text: public Widget { . . . }; class TextArea: public Text { . . . }; class TextLine: public Text { . . . }; class Container: public Widget { . . . }; class Canvas: public Container { . . . }; class Window: public Container { . . . }; Text* p = new TextArea; Widget* p = new Window; Canvas* p = new Container; None of these are illegal Widget* p = new Widget;

Canvas* p = new Container;

[2436] Which one of the following Car member functions is called by this statement? AeroCar acar1(2000.0, 200.0); class Car { public: Car(); Car(double); void setSpeed(double); double getSpeed() const; }; class AeroCar : public Car { public: AeroCar(); AeroCar(double); AeroCar(double, double); void setHeight(double); double getHeight() const; }; AeroCar::AeroCar(double h, double s) : Car(s), height(h) { } int main() { AeroCar acar(2000.0, 200.0); } double getSpeed() const Car(double) void setSpeed(double) Car()

Car(double)

[2430] The Pet base class defines void setName(const string&). Cat is derived from Pet, but does not define setName(). What is true? Cat class inherits the setName function setName() cannot be called on Cat objects Cat overrides the setName function The Cat class will not compile because it does not define setName

Cat class inherits the setName function

[2522] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: 🌺 virtual string toString() const { return "Shape"; } }; class Circle : public Shape { public: string toString() const { return "Circle"; } }; class Triangle : public Shape { public: string toString() const { return "Triangle"; } }; int main() { Shape* s1 = new Circle; Shape* s2 = new Triangle; cout << s1->toString() << s2->toString() << endl; } ShapeShape CircleTriangle ShapeTriangle Compiles but prints something else

CircleTriangle

[2613] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. What happens when a PokerHand object is passed to the non-member draw() function, assuming that the function makes use of the virtual functions overridden in PokerHand? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } void draw(const Hand h) { . . . } The code compiles but fails to link The hand is drawn appropriately The code does not compile because the argument is of the wrong type Code compiles, but the parameter is treated as a Hand object, not a PokerHand, so it is not drawn correctly

Code compiles, but the parameter is treated as a Hand object, not a PokerHand, so it is not drawn correctly

[2610] Which member function is called? class Performer { public: virtual void sing() const; }; class Crooner : public Performer { public: void sing() const; }; int main() { Performer* p = new Crooner; p->sing(); } Crooner::sing() Performer::sing() Neither of these Illegal (does not compile)

Crooner::sing()

[2526] What prints when this code is run? (Note that struct is used instead of class only to make all members public and to make the code shorter). #include <string> #include <iostream> using namespace std; struct B { virtual string str() const { return "B"; }}; struct D1 : public B { string str() const { return "D1"; }}; struct D2 : public B { string str() const { return "D2"; }}; struct D3 : public D1 { string str() const { return "D3"; }}; int main() { B *p1(new D1), *p2(new D2), *p3(new D3); cout << p1->str() << p2->str() << p3->str() << endl; } BBB BBD3 D1D2D3 Compiles but prints something else

D1D2D3

[2534] What prints when this code is run? #include <string> #include <iostream> #include <vector> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape& s) { s.iam(); } int main() { vector<Shape&🌺> v = {Shape(), Square(), Oval()}; for (auto& e : v) iam(e); cout << endl; } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape Does not compile

Does not compile

[2602] What does this code mean? class X : Y { . . . }; Each X object is-implemented in terms of Y Each X object uses- a Y object Every X object is-a Y object Every X object has-a Y object Every Y object is-a X object

Each X object is-implemented in terms of Y

[2604] What does this code mean? class X { double x = Y().balance(); . . . }; Every X object has-a Y object Every Y object is-a X object Every X object is-a Y object Each X object uses- a Y object Each X object is-implemented in terms of Y

Each X object uses- a Y object

[2435] The Manager class is derived from the Employee class. Manager defines a constructor, but does not explicitly call an Employee constructor. Which constructor is called by the Manager constructor? class Employee { public: Employee(); Employee(const string&); Employee(double); Employee(const string&, double); }; Employee(const string&, double); Employee(); Employee(const string&); Employee(double);

Employee();

[2603] What does this code mean? class X { Y y; . . . }; Every X object is-a Y object Every Y object is-a X object Every X object has-a Y object Each X object is-implemented in terms of Y Each X object uses- a Y object

Every X object has-a Y object

[2601] What does this code mean? class X : public Y { . . . }; Each X object uses- a Y object Every Y object is-a X object Each X object is-implemented in terms of Y Every X object is-a Y object Every X object has-a Y object

Every X object is-a Y object

In C++, as in Java pure virtual member functions may not have an implementation The C++ facility that allows a derived class to have multiple base classes is known as interface inheritance In C++, an Abstract Base Class is any class that has one or more virtual member functions The istream class in the C++ standard library uses multiple inheritance Since an abstract class cannot be instantiated, it is illegal to have references of abstract types Using public inheritance to derive Stack from vector is a good design because vector provides all of the capabilities that a Stack requires An abstract class is a class that contains no data members Constructing an instance of an abstract class is legal, provided you do not initialize it Consider the Shape class hierarchy, along with Circle, Square and Star from your text. The Shape class is a concrete class What Java calls a static method is called a pure virtual member function in C++ If a class is abstract, you may create instances, but not pointers of that class An abstract class may, but is not required to, override its pure virtual (abstract) member functions Composition models an IS-A relationship between classes Composition can be used to create adapter classes that change the implementation of one class to meet the needs of another Private inheritance models an IS-A relationship between classes In C++, public inheritance can be used to create adapter classes An abstract class is a class that contains only virtual member functions Using the keyword abstract to the heading of a virtual member function converts it to a pure virtual member function Abstract classes provide a set of capabilities that derived classes my inherit In adapter classes, the member functions override superclass member functions to provide new behavior Consider the Shape class hierarchy, along with Circle, Square and Star from your text. The Circle class is an abstract class

False

Tell the compiler that you intend to override a base class function by adding the keyword override as an annotation before the function header Putting the keyword final at the end of a non-virtual member function heading prohibits derived classes from overriding that function Virtual functions invoked through an object use late binding to decide which function to call The composition relationship is informally known as is-a Virtual member functions are implemented by adding a new pointer, called a vtable, to every object that contains at least one virtual function If you make a class final then you must make all of its member functions final as well In private inheritance derived classes inherit the interface of the base class, but not its implementation The public inheritance relationship is informally known as implemented-with If a derived class redefine a non-virtual base-class function it causes a syntax error The public inheritance relationship is informally known as has-a Non-virtual functions always use late binding to decide which function to call Waiting until runtime to determine which function to call is known as early binding

False

[2612] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which line of code is illegal: class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } Hand h PokerHand ph; Hand* hp = new PokerHand; PokerHand ph; Hand& hr = ph; BlackjackHand* bjp = new BlackjackHand;

Hand h

[2407] A(n) ____________ relationship exists between two classes when one class contains data members that are instances of the other class Is-A Implemented-As Has-A Uses-A

Has-A

[2415] Assume you have a Student object named bill. Which of these statements would be legal? bill.name = "Bill Gates"; // I bill.setName("Bill Gates"); // II cout << bill.getName(); // III bill.studentID = 123L; // IV cout << bill.getID(); // V II, III, IV, V IV and V II, III, V All of them None of them

II, III, V

[2416] Assume that the following code appears inside a member function or constructor of the Student class. Which of these statements would be legal?? name = "Bill Gates"; // I setName("Bill Gates"); // II name = name.substr(1); // III studentID = 123L; // IV studentID = getID() * 2; // V II, IV and V II, III, V II, III, IV, V All of them None of them

II, IV and V

[2506] Below is a class hierarchy for card games. What happens when showScore() is called? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void showScore(const Hand& h 🌺) { cout << h.score() << endl; } . . . PokerHand ph; . . . showScore(ph 🌺); // what happens here? It does not compile because you should pass ph instead of &ph It calls the Hand::score() function because score() is not virtual It calls the PokerHand::score() function if one has been defined It does not compile because ph is not a Hand object so a pointer mismatch error The PokerHand portion of ph is sliced off and it becomes a Hand object

It calls the Hand::score() function because score() is not virtual

[2504] Below is a class hierarchy for card games. What happens when showScore() is called? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void showScore(const Hand* h 🌺) { cout << h.score() << endl; } . . . PokerHand ph; . . . showScore(&ph 🌺); // what happens here? It calls the PokerHand::score() function if one has been defined It does not compile because ph is not a Hand object so a pointer mismatch error The PokerHand portion of ph is sliced off and it becomes a Hand object It does not compile because you should pass ph instead of &ph It calls the Hand::score() function because score() is virtual

It calls the PokerHand::score() function if one has been defined

[2505] Below is a class hierarchy for card games. What happens when showScore() is called? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void showScore(const Hand& h 🌺) { cout << h.score() << endl; } . . . PokerHand ph; . . . showScore(ph 🌺); // what happens here? The PokerHand portion of ph is sliced off and it becomes a Hand object It calls the Hand::score() function because score() is virtual It calls the PokerHand::score() function if one has been defined It does not compile because ph is not a Hand object so a pointer mismatch error It does not compile because you should pass ph instead of &ph.

It calls the PokerHand::score() function if one has been defined

[2421] What is the primary purpose of inheritance? Model one-to-many relationships between different types of objects Model different objects which share similar performance goals Model similar objects with different data values Model similar objects with different behavior

Model similar objects with different behavior

[2426] Consider the following classes. The Vehicle class is a base class. The Car, Truck, and Motorcycle class inherit from the Vehicle class. The Sedan and SUV classes inherit from the Car class. Which of the following lists all the types of objects that cannot be passed into the function calculate_registration_fee(Car& car)? Motorcycle, Truck, and Vehicle objects Motorcycle, and Truck objects Sedan and SUV objects Sedan, SUV, and Car objects

Motorcycle, Truck, and Vehicle objects

[2417] Which of these data members or member functions are inherited by the Person class? getName(), setName(), studentID, getID() None of them name, getName(), setName(), getID() getName(), setName(), name studentID, name, getName(), setName(), getID()

None of them

[2538] Which member function(s) must 🌺 be overridden in Hobbit? class Creature { public: Creature(const string& name); virtual string name() const final; virtual string skills() const; virtual void addSkill(const string& skill); void print() const; }; class Hobbit : public Creature { . . . }; None of them addSkill(), skills() addSkill(), skills(), print() addSkill(), skills(), name()

None of them

[2516] Below is a class hierarchy. Which assignments are illegal? class Widget { . . . }; class Label: public Widget { . . . }; class Button: public Widget { . . . }; class Text: public Widget { . . . }; class TextArea: public Text { . . . }; class TextLine: public Text { . . . }; class Container: public Widget { . . . }; class Canvas: public Container { . . . }; class Window: public Container { . . . }; Button* p = new Button; Widget* p = new Window; Widget* p = new TextLine; Container* p = new Canvas; None of these are illegal

None of these are illegal

[2422] Suppose that we have a Question class that contains two data members - a query and answer both of which are type string. The NumericQuestion class is derived from Question. Which of the following is true? NumericQuestion contains a numerical answer but not a query NumericQuestion contains a query and a numerical answer but no answer string It is impossible to know without examining the definition of the NumericQuestion class NumericQuestions contains both a query and an answer string.

NumericQuestions contains both a query and an answer string

[2520] Below is a class hierarchy. Which statements may result in slicing? class Writer { . . . }; class Pen : public Writer { . . . }; class Pencil : public Writer { . . . }; class FountainPen : public Pen { . . . }; Writer p = Writer (); Pen* p = new Writer(); Pen p = FountainPen(); Writer& p = *(new Pencil);

Pen p = FountainPen();

[2510] Below is a class hierarchy for card games. Assuming that these are the only classes and that the concrete classes are correctly completed, which of the following definitions will not compile? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; GoFishHand gfh; Hand* h = new Hand; PokerHand* = new Hand; Hand& h = *(new PokerHand);

PokerHand* = new Hand;

[2512] Below is a class hierarchy. Which assignment will fail to compile? class Pet { . . . }; class Puppy : public Pet { . . . }; class Kitty : public Pet { . . . }; class Ducky : public Pet { . . . }; Pet pet; Puppy pup; Kitty kit; Duck duck; pet = pup; Puppy* p = &pet; Puppy& pr = pup; Pet* p = &duck;

Puppy* p = &pet;

[2521] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: 🌺virtual string toString() const { return "Shape"; } }; class Circle : public Shape { public: string toString() const { return "Circle"; } }; class Triangle : public Shape { public: string toString() const { return "Triangle"; } }; int main() { Shape s1; Shape s2 = Triangle(); cout << s1.toString() << s2.toString() << endl; } Triangle ShapeShape ShapeTriangle Compiles but prints something else

ShapeShape

[2523] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: 🌺string toString() const { return "Shape"; } }; class Circle : public Shape { public: string toString() const { return "Circle"; } }; class Triangle : public Shape { public: string toString() const { return "Triangle"; } }; int main() { Shape* s1 = new Circle; Shape* s2 = new Triangle; cout << s1->toString() << s2->toString() << endl; } ShapeShape ShapeTriangle CircleTriangle Does not compile

ShapeShape

[2524] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: 🌺string toString() const { return "Shape"; } }; class Circle : public Shape { public: virtual string toString() const { return "Circle"; } }; class Triangle : public Shape { public: virtual string toString() const { return "Triangle"; } }; int main() { Shape* s1 = new Circle; Shape* s2 = new Triangle; cout << s1->toString() << s2->toString() << endl; } ShapeShape ShapeTriangle CircleTriangle Does not compile

ShapeShape

[2533] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape* s) { s->iam(); } int main() { Shape *a = new Shape, *b = new Square, *c = new Oval; iam(a); iam(b); iam(c); } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape

ShapeShapeOval

[2528] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(Shape s) { s.iam(); } 🌺 int main() { iam(Shape()); iam(Square()); iam(Oval()); cout << endl; } ShapeSquareShape ShapeSquareOval ShapeShapeOval ShapeShapeShape Does not compile

ShapeShapeShape

[2531] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape& s) { s.iam(); } int main() { Shape a = Shape(), b = Square(), c = Oval(); ..// Slices iam(a); iam(b); iam(c); } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape

ShapeShapeShape

[2536] What prints when this code is run? #include <string> #include <iostream> #include <vector> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape& s) { s.iam(); } int main() { vector<Shape🌺> v = {Shape(), Square(), Oval()}; for (auto& e : v) iam(e); cout << endl; } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape Does not compile

ShapeShapeShape

[2529] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape& s) { s.iam(); } 🌺 int main() { iam(Shape()); iam(Square()); iam(Oval()); cout << endl; } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape

ShapeSquareOval

[2530] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape& s) { s.iam(); } 🌺 int main() { iam(Shape()); iam(Square()); iam(Oval()); cout << endl; } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape

ShapeSquareOval

[2535] What prints when this code is run? #include <string> #include <iostream> #include <vector> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iam() const; }; void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iam() const { cout << "Oval"; } void iam(const Shape* s) { s->iam(); } int main() { vector<Shape*🌺> v = {new Shape, new Square, new Oval}; for (auto& e : v) iam(e); cout << endl; } ShapeShapeOval ShapeSquareOval ShapeShapeShape ShapeSquareShape Does not compile

ShapeSquareOval

[2537] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual void iam() const; }; class Square : public Shape { public: void iam() const; }; class Oval: public Shape { public: void iamm() const; }; 🌺 void Shape::iam() const { cout << "Shape"; } void Square::iam() const { cout << "Square"; } void Oval::iamm() const { cout << "Oval"; } void iam(const Shape* s) { s->iam(); } int main() { Shape *a = new Shape, *b = new Square, *c = new Oval; iam(a); iam(b); iam(c); } ShapeShapeOval ShapeSquareOval Does not compile ShapeShapeShape ShapeSquareShape

ShapeSquareShape

[2525] What prints when this code is run? #include <string> #include <iostream> using namespace std; class Shape { public: virtual string toString() const { return "Shape"; } }; class Circle : public Shape { public: virtual string toString() const { return "Circle"; } }; class Triangle : public Shape { public: virtual string toString() const { return "Triangle"; } }; int main() { Shape s1 = Circle(); Shape* s2 = new Triangle; cout << s1.toString() << s2->toString() << endl; } ShapeShape ShapeTriangle CircleTriangle Compiles but prints something else

ShapeTriangle

[2429] What is the output? class Car { public: virtual void setSpeed(double s) { speed = s; } double getSpeed() const { return speed; } private: double speed = 0; }; class AeroCar : public Car { public: void setSpeed(double s) { Car::setSpeed(10 * s); } void addSpeed(double s) { Car::setSpeed(getSpeed() + s); } }; int main() { AeroCar ac1; ac1.setSpeed(10); ac1.addSpeed(250); cout << "Speed: " << ac1.getSpeed(); } Speed: 260 Speed: 350 Speed: 250 Speed: 420

Speed: 350

[2519] Below is a class hierarchy. Which statements may result in slicing? class Widget { . . . }; class Label: public Widget { . . . }; class Button: public Widget { . . . }; class Text: public Widget { . . . }; class TextArea: public Text { . . . }; class TextLine: public Text { . . . }; class Container: public Widget { . . . }; class Canvas: public Container { . . . }; class Window: public Container { . . . }; Text p = TextLine(); Widget p = Widget(); Widget* p = new TextArea; Container& p = *(new Window);

Text p = TextLine();

[2428] AeroCar is derived from Car. What must be done for AeroCar to override the setSpeed function? class Car { public: Car(); virtual void setSpeed(double newSpeed); double getSpeed() const; private: double speed; }; The AeroCar class must define the function void override(string setSpeed, double newSpeed); The AeroCar class must define the function void overrideSetSpeed(double) The AeroCar class must define the function void setSpeed(double) The AeroCar class cannot override the setSpeed member function.

The AeroCar class must define the function void setSpeed(double)

[2424] Suppose that we have a function that registers a Vehicle object. We also have a Car object that is a specialized Vehicle (defined by inheritance). The substitution principle states ___________. The Car object can never be used in any function that is written to use a Vehicle object. A new registration function that is written to use a Car object can be used in place of the Vehicle registration function The Car object can be used in the Vehicle registration function because it is a kind of Vehicle The Vehicle object can always be used wherever a Car object is expected

The Car object can be used in the Vehicle registration function because it is a kind of Vehicle.

[2433] Based on the following declaration of the Employee class where Manager is derived from Employee, which of the following are true? class Employee { public: Employee(); Employee(const string&); Employee(double); Employee(const string&, double); void setName(const string&); string getName()const; private: string name; double salary; }; The Manager class does not inherit the private data members The Manager class inherits name and salary, but Manager functions can only change the values of the name data member A Manager object has direct access to the name and salary inherited data members The Manager class inherits name and salary, but Manager functions cannot change the values of either data member.

The Manager class inherits name and salary, but Manager functions can only change the values of the name data member

[2503] Below is a class hierarchy for card games. What happens when showScore() is called? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void showScore(const Hand h 🌺) { cout << h.score() << endl; } . . . PokerHand ph; . . . showScore(ph 🌺); // what happens here? The PokerHand portion of ph is sliced off and it becomes a Hand object It does not compile because ph is not a Hand object so we have a type error It does not compile because you should pass &ph instead of ph The Hand object is converted to a PokerHand object implicitly It prints the score for the PokerHand object named ph

The PokerHand portion of ph is sliced off and it becomes a Hand object

[2434] The Car class inherits from the Vehicle class. The Car class contains one constructor which does not call a particular Vehicle constructor. Which of the following is true? Vehicle constructors can never be called by the Car constructors The Car class will not compile because it does not explicitly call a Vehicle constructor The Vehicle default constructor is implicitly called by the Car constructor All Vehicle constructors are implicitly called by the Car constructor.

The Vehicle default constructor is implicitly called by the Car constructor

The private inheritance relationship is informally known as implemented-with The public inheritance relationship is informally known as is-a The composition relationship is informally known as has-a The keyword override allows the compiler to ensure that the base-class function you are overriding is virtual Non-virtual functions always use early, or compile-time binding to decide which function to call Creating a new class by combining instances of simpler classes as data members is called composition It is always a logic error for a derived class to redefine a non-virtual function Putting the keyword final at the end of the class heading prohibits the creation of subsequent derived classes Waiting until runtime to determine which function to call is known as late binding Waiting until runtime to determine which function to call is known as dynamic dispatch Virtual functions invoked through a pointer to a base-class object use late binding to decide which function to call If a virtual member function does not use the keyword final, then any derived class may override that function In private inheritance derived classes inherit the implementation of the base class, but not its interface Virtual functions invoked through a reference to a base-class object use late binding to decide which function to call Virtual member functions are implemented by adding a new pointer to every object that contains at least one virtual function In private inheritance a using declaration is employed to selectively bring base class members into the derived class scope Tell the compiler that you intend to override a base class function by adding the keyword override to the end of the member function declaration Putting the keyword final at the end of a virtual member function heading prohibits derived classes from overriding that function

True

You may create a reference to a class that is abstract It is illegal to construct an instance of an abstract class Abstract classes specify a set of responsibilities that derived classes must fulfill The iostream class in the C++ standard library uses multiple inheritance In C++, an Abstract Base Class is any class that has one pure virtual member function An abstract class is a class that contains member functions that are specified but not implemented The C++ facility that allows a derived class to have multiple base classes is known as multiple inheritance In C++ pure virtual member functions may have an optional implementation Adding = 0 to the end of the heading of a virtual member function converts it to a pure virtual member function An abstract class requires its concrete derived classes to override all of its pure virtual (abstract) member functions In composition-based adapter classes, the member functions delegate or forward requests to the data member that can satisfy the request In C++, private inheritance can be used to create adapter classes Public inheritance models an IS-A relationship between classes Using public inheritance to derive Stack from vector is a problem because a Stack is really not a vector If a class is abstract, you may create a pointer of that class What Java calls an abstract method is called a pure virtual member function in C++ Consider the Shape class hierarchy, along with Circle, Square and Star from your text. The Shape class is an abstract base class

True

[2425] The Department of Motor Vehicles uses a vehicle registration program that declares a Vehicle class as a base class. The Car class and the Truck class both inherit from the Vehicle class. Which types of objects can be passed to the function register(Vehicle& v)? It is impossible to know without examining the implementation of the Car and Truck classes Vehicle, Car and Truck objects Only Car and Truck objects Only Vehicle objects

Vehicle, Car and Truck objects

[2518] Below is a class hierarchy. Which assignments are illegal? class Widget { . . . }; class Label: public Widget { . . . }; class Button: public Widget { . . . }; class Text: public Widget { . . . }; class TextArea: public Text { . . . }; class TextLine: public Text { . . . }; class Container: public Widget { . . . }; class Canvas: public Container { . . . }; class Window: public Container { . . . }; None of these are illegal Widget* p = new Canvas; Window* p = new Container; Text* p = new TextLine; Widget* p = new TextArea;

Window* p = new Container;

[2611] Using C++ terminology, the member Card::score() is: class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } a virtual function an abstract method a pure virtual function an overridden member an overloaded member

a pure virtual function

[2608] Which member functions in the Performer class must be overridden? class Performer { public: void dance() const; virtual void sing() const; virtual void act() const = 0; }; act() sing() dance() All can be overridden None can be overridden

act()

[2614] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which of the following member functions cannot be overridden in the derived classes? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } add() sort() score() ~Hand()

add()

[2532] Which member function(s) may 🌺 be overridden in Hobbit? class Creature { public: Creature(const string& name); virtual string name() const final; virtual string skills() const; virtual void addSkill(const string& skill); void print() const; }; class Hobbit : public Creature { . . . }; None of them addSkill(), skills() addSkill(), skills(), print() addSkill(), skills(), name()

addSkill(), skills()

[2411] The ostream class is the/a ___________ class of fstream derived base descendent ancestor sibling

ancestor

[2409] The ostream class is the/a ___________ class of ofstream descendent ancestor sibling base derived

base

[2404] Expressed in C++ terminology, the relationship between the Food class and the CherryPie class is one of _____________ (Food) and ________________ (CherryPie) specialized class, generalized class derived class, base class base class, derived class concrete class, abstract class

base class, derived class

[2432] Based on the following code snippet, which of the following function calls are legal? Assume that car is a Car object and aero is an AeroCar object. class Car { public: Car(); virtual void setSpeed(double); double getSpeed() const; void display() const; }; class AeroCar : public Car { public: AeroCar(); void setSpeed(double); void setHeight(double); double getHeight() const; }; Neither A nor B car.getSpeed() and aero.getSpeed() car.getHeight() and aero.getHeight() Both A and B

car.getSpeed() and aero.getSpeed()

[2401] ___________________ is one of the primary mechanisms that we use to understand the natural world around us. Starting as infants we begin to recognize the difference between categories like food, toys, pets, and people. As we mature, we learn to divide these general categories or classes into subcategories like siblings and parents, vegetables and dessert classification specialization generalization encapsulation

classification

[2606] Which member functions in the Performer class may not be overridden? class Performer { public: void dance() const; virtual void sing() const; virtual void act() const = 0; }; sing() dance() None can be overridden All can be overridden act()

dance()

[2408] The ostream class is the/a ___________ class of ios derived ancestor sibling descendent base

derived

[2412] The fstream class is the/a ___________ class of istream ancestor derived sibling base descendent

descendent

[2402] _______________—the specification of attributes and behavior as a single entity—allows us to build on our understanding of the natural world as we create software encapsulation generalization inheritance polymorphism

encapsulation

[2413] Which of these is an example of the principle of substitutability? void f1(fstream& out) { . . .} void f2(int n) { . . . } void f3(const string& s) { . . . } void f4(ios& i) { . . . } f4(cout); f1(cout); None of these f2(3.5); f3("hello");

f4(cout);

[2405] A classification hierarchy represents an organization based on _____________ and _____________. encapsulation and polymorphism abstraction and generalization abstraction and encapsulation generalization and specialization specialization and encapsulation

generalization and specialization

[2616] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which of the following member functions cannot be overridden in the derived classes? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } get() sort() score() ~Hand()

get()

[2418] Which of these data members or member functions are inherited (and accessible) by the Student class? name, getName(), setName(), getID() getName(), setName(), name studentID, name, getName(), setName(), getID() getName(), setName() None of them

getName(), setName()

[2609] The member function Mime::dance() is: class Performer { public: void dance() const; }; class Mime : public Performer { public: void dance() const; }; final overloaded overridden hidden or shadowed Illegal (does not compile)

hidden or shadowed

[2406] When you create your own new, user-defined types, there are three different strategies you can use. Which of these is not one of those strategies? defining a class from scratch extending an existing class by adding new features combining simpler classes to create a new classes modifying an existing class

modifying an existing class

[2419] Which of these data members or member functions are inherited but not directly accessible by the Student class? getID() studentID name setName() getName()

name

[2539] Which member function(s) should not be overridden in Hobbit? class Creature { public: Creature(const string& name); virtual string name() const final; virtual string skills() const; virtual void addSkill(const string& skill); void print() const; }; class Hobbit : public Creature { . . . }; None of them name(), print() skills(), name(), print() addSkill(), skills(), name()

name(), print()

[2540] Which member function(s) in Hobbit cause a compiler error? class Creature { public: Creature(const string& name); virtual string name() const final; virtual string skills() const; virtual void addSkill(const string& skill); void print() const; }; class Hobbit : public Creature { public: string name() const override; string skills() const override; void addSkill(const string&) override; void print() override; }; None of them name(), print() skills(), name(), print() addSkill(), skills(), name()

name(), print()

[2615] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which of the following member functions cannot be overridden in the derived classes? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } sort() score() ~Hand() operator<()

operator<()

[2414] Which of these is an example of the principle of substitutability? void f1(ostream& out) { . . .} void f2(double n) { . . . } void f3(const char * s) { . . . } void f4(ofstream& i) { . . . } f2(3); ostringstream out; f1(out); None of these f4(cout); f3("hello");

ostringstream out; f1(out);

[2605] Specialization inheritance means that the derived class may add new data members and member functions, and may also _________________ the virtual member functions in the base class. hide override overload cast delete

override

[2515] Below is a class hierarchy. Which assignment results in slicing? class Pet { . . . }; class Puppy : public Pet { . . . }; class Kitty : public Pet { . . . }; class Ducky : public Pet { . . . }; Pet pet; Puppy pup; Kitty kit; Duck duck; pet = kit; kit = duck; pup = pet; duck = pet;

pet = kit;

[2514] Below is a class hierarchy. Which assignment results in slicing? class Pet { . . . }; class Puppy : public Pet { . . . }; class Kitty : public Pet { . . . }; class Ducky : public Pet { . . . }; Pet pet; Puppy pup; Kitty kit; Duck duck; pet = pup; pup = pet; Pet* p = &pet; Pet& p = duck;

pet = pup;

[2513] Below is a class hierarchy. Which assignment will fail to compile? class Pet { . . . }; class Puppy : public Pet { . . . }; class Kitty : public Pet { . . . }; class Ducky : public Pet { . . . }; Pet pet; Puppy pup; Kitty kit; Duck duck; pup = pet; Pet* p = &pet; Pet& p = duck; Puppy& pr = pup;

pup = pet;

[2403] Inheritance gives your programs the ability to express _______________ between classes dependencies composition encapsulation relationships

relationships

[2501] Below is a class hierarchy for card games. Which of the Hand member functions may be overridden in the GoFishHand class? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; get() add() score() all of them none of them

score()

[2617] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which of the following member functions must be overridden in the derived classes? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } add() get() score() sort()

score()

[2431] Which member function from the Question class is overridden in the ChoiceQuestion class? class Question { public: virtual void setText(const string&); virtual void setAnswer(const string&); virtual void display() const; }; class ChoiceQuestion : public Question { public: void setText(const string&); void setAnswer(int, const string&); void display(const string&) const; }; setText() Question() display() setAnswer()

setText()

[2410] The ostream class is the/a ___________ class of istream ancestor derived descendent sibling base

sibling

[2607] Which member functions in the Performer class may be overridden (but need not be)? class Performer { public: void dance() const; virtual void sing() const; virtual void act() const = 0; }; act() sing() dance() All can be overridden None can be overridden

sing()

[2618] Examine the class hierarchy below. Assume that both derived classes are concrete and completely defined. Which of the following member functions are the derived classes allowed (but not required to) override? class Hand { std::vector<Card> cards; public: Hand() = default; virtual ~Hand() = default; void add(const Card&); virtual int score() const = 0; virtual void sort(); bool operator<(const Card& rhs) const; }; class PokerHand : public Hand { . . . } class BlackjackHand : public Hand { . . . } get() score() add() sort()

sort()

[2507] Below is a class hierarchy for card games. Assuming that these are the only classes and that the concrete classes are correctly completed, which of the following non-member functions are polymorphic? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void draw(const Hand h) { . . . } void draw(const Hand& h) { . . . } void draw(const PokerHand* h) { . . . } void draw(const GoFishHand& h) { . . . }

void draw(const Hand& h) { . . . }

[2508] Below is a class hierarchy for card games. Assuming that these are the only classes and that the concrete classes are correctly completed, which of the following non-member functions are polymorphic? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void draw(const Hand h) { . . . } void draw(const Hand* h) { . . . } void draw(const PokerHand& h) { . . . } void draw(const GoFishHand* h) { . . . }

void draw(const Hand* h) { . . . }

[2502] Below is a class hierarchy for card games. Which is the correct signature for a function that can print the score of any playing card hand? class Hand { std::vector<Card> cards; public: void add(const Card&); Card get(size_t index) const; virtual int score() const; }; class PokerHand : public Hand { . . . }; class BlackjackHand : public Hand { . . . }; class GoFishHand : public Hand { . . . }; void printScore(Hand h); void printScore(const Hand h); void printScore(const Hand* h); void printScore(BlackjackHand& h); void printScore(const PokerHand& h);

void printScore(const Hand* h);


संबंधित स्टडी सेट्स

Chapter 48: Assessment and Care of Patients with Ear and Hearing Problems

View Set

A Separate Peace: Ch. 1-2 questions

View Set

Conceptual questions from Chapter 8

View Set

Psych 401 Chapter 9- Industrial Organizational Psychology

View Set

Biology Lab Quiz 1: Cell Diversity

View Set