Deck 9: Classes: a Deeper Look; Throwing Exceptions
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/35
Play
Full screen (f)
Deck 9: Classes: a Deeper Look; Throwing Exceptions
1
A default constructor:
A) Is a constructor that must receive no arguments.
B) Is the constructor generated by the compiler when no constructor is provided by the programmer.
C) Does not perform any initialization.
D) Both (a) and (b).
A) Is a constructor that must receive no arguments.
B) Is the constructor generated by the compiler when no constructor is provided by the programmer.
C) Does not perform any initialization.
D) Both (a) and (b).
D
2
Assuming the following constructor is provided for class Time explicit Time(int = 0, int = 0, int = 0);
Which of the following is not a valid way to initialize a Time object?
A) Time t1;
B) Time t2{22, 40};
C) Time t3(22, 40);
D) a), b) and c) are all valid ways to initialize a Time object.
Which of the following is not a valid way to initialize a Time object?
A) Time t1;
B) Time t2{22, 40};
C) Time t3(22, 40);
D) a), b) and c) are all valid ways to initialize a Time object.
D
3
A class's functions can throw exceptions, such as __________to indicate invalid data.
A) invalid_data
B) bad_data
C) invalid_argument
D) bad_argument
A) invalid_data
B) bad_data
C) invalid_argument
D) bad_argument
C
4
Member access specifiers (public and private) can appear:
A) In any order and multiple times.
B) In any order (public first or private first) but not multiple times.
C) In any order and multiple times, if they have brackets separating each type.
D) Outside a class definition.
A) In any order and multiple times.
B) In any order (public first or private first) but not multiple times.
C) In any order and multiple times, if they have brackets separating each type.
D) Outside a class definition.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
5
Member function definitions:
A) Always require the scope resolution operator (::).
B) Require the scope resolution operator only when being defined outside of the definition of their class.
C) Can use the scope resolution operator anywhere, but become public functions.
D) Must use the scope resolution operator in their function prototype.
A) Always require the scope resolution operator (::).
B) Require the scope resolution operator only when being defined outside of the definition of their class.
C) Can use the scope resolution operator anywhere, but become public functions.
D) Must use the scope resolution operator in their function prototype.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following statements is false (assume we're referring to class Time)?
A)Often a class's interface and implementation will be created and compiled by one programmer and used by a separate programmer who implements the client code that uses the class.
B) To hide the class's member-function implementation details, the class-implementation programmer would provide the client-code programmer with the header Time.h (which specifies the class's interface and data members) and the Time object code (i.e., the machine-code instructions that represent Time's member functions).
C) The client-code programmer is not given Time.cpp, so the client remains unaware of how Time's member functions are implemented.
D) The client-code programmer needs to know only Time's interface to use the class and must be able to link its object code.
A)Often a class's interface and implementation will be created and compiled by one programmer and used by a separate programmer who implements the client code that uses the class.
B) To hide the class's member-function implementation details, the class-implementation programmer would provide the client-code programmer with the header Time.h (which specifies the class's interface and data members) and the Time object code (i.e., the machine-code instructions that represent Time's member functions).
C) The client-code programmer is not given Time.cpp, so the client remains unaware of how Time's member functions are implemented.
D) The client-code programmer needs to know only Time's interface to use the class and must be able to link its object code.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
7
The type of function a client would use to check the balance of a bank account would be:
A) A utility function.
B) A predicate function.
C) An access function.
D) A constructor.
A) A utility function.
B) A predicate function.
C) An access function.
D) A constructor.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
8
Parameterized stream manipulator setfill specifies the fill character that's displayed when an output is displayed in a field wider than the number of characters or digits in the output. The effect of setfill applies:
A) Only to the current value being displayed.
B) Only to outputs displayed in the current statement.
C) Until explicitly set to a different setting.
D) Until the output buffer is flushed.
A) Only to the current value being displayed.
B) Only to outputs displayed in the current statement.
C) Until explicitly set to a different setting.
D) Until the output buffer is flushed.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
9
Given the class definition: class CreateDestroy
{
Public:
CreateDestroy() {cout << "constructor called, ";}
~CreateDestroy() {cout << "destructor called, ";}
};
What will the following program output?
Int main()
{
For (int i = 1; i <= 2; ++i) {
CreateDestroy cd;
}
Return 0;
}
A) constructor called, destructor called, constructor called, destructor called,
B) constructor called, constructor called,
C) constructor called, constructor called, destructor called, destructor called,
D) Nothing.
{
Public:
CreateDestroy() {cout << "constructor called, ";}
~CreateDestroy() {cout << "destructor called, ";}
};
What will the following program output?
Int main()
{
For (int i = 1; i <= 2; ++i) {
CreateDestroy cd;
}
Return 0;
}
A) constructor called, destructor called, constructor called, destructor called,
B) constructor called, constructor called,
C) constructor called, constructor called, destructor called, destructor called,
D) Nothing.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following is not true of a constructor and destructor of the same class?
A) They both have the same name aside from the tilde (~) character.
B) They are both usually called once per object created.
C) They both are able to have default arguments.
D) Both are called automatically, even if they are not explicitly defined in the class.
A) They both have the same name aside from the tilde (~) character.
B) They are both usually called once per object created.
C) They both are able to have default arguments.
D) Both are called automatically, even if they are not explicitly defined in the class.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
11
A class-scope variable hidden by a block-scope variable can be accessed by preceding the variable name with the class name followed by:
A) ::
B) :
C) .
D) ->
A) ::
B) :
C) .
D) ->
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
12
Variables defined inside a member function of a class have:
A) File scope.
B) Class scope.
C) Block scope.
D) Class or block scope, depending on whether the binary scope resolution operator (::) is used.
A) File scope.
B) Class scope.
C) Block scope.
D) Class or block scope, depending on whether the binary scope resolution operator (::) is used.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following statements is false (ssume we're referring to class Time)?
A) Since the interface of the class is part of the class definition in the Time.h header, the client-code programmer must have access to this file and must #include it in the client's source-code file.
B) When the client code is compiled, the compiler uses the class definition in Time.h to ensure that the main function creates and manipulates objects of class Time correctly.
C) The linker's output is the executable Time application that users can execute to create and manipulate a Time object.
D) Compilers and IDEs typically invoke the linker for you after compiling your code.
A) Since the interface of the class is part of the class definition in the Time.h header, the client-code programmer must have access to this file and must #include it in the client's source-code file.
B) When the client code is compiled, the compiler uses the class definition in Time.h to ensure that the main function creates and manipulates objects of class Time correctly.
C) The linker's output is the executable Time application that users can execute to create and manipulate a Time object.
D) Compilers and IDEs typically invoke the linker for you after compiling your code.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
14
Given the class definition: class CreateDestroy
{
Public:
CreateDestroy() {cout << "constructor called, ";}
~CreateDestroy() {cout << "destructor called, ";}
};
What will the following program output?
Int main()
{
CreateDestroy c1;
CreateDestroy c2;
Return 0;
}
A) constructor called, destructor called, constructor called, destructor called,
B) constructor called, destructor called,
C) constructor called, constructor called,
D) constructor called, constructor called, destructor called, destructor called,
{
Public:
CreateDestroy() {cout << "constructor called, ";}
~CreateDestroy() {cout << "destructor called, ";}
};
What will the following program output?
Int main()
{
CreateDestroy c1;
CreateDestroy c2;
Return 0;
}
A) constructor called, destructor called, constructor called, destructor called,
B) constructor called, destructor called,
C) constructor called, constructor called,
D) constructor called, constructor called, destructor called, destructor called,
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
15
Every object of the same class:
A) Gets a copy of every member function and member variable.
B) Gets a copy of every member variable.
C) Gets a copy of every member function.
D) Shares pointers to all member variables and member functions.
A) Gets a copy of every member function and member variable.
B) Gets a copy of every member variable.
C) Gets a copy of every member function.
D) Shares pointers to all member variables and member functions.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following statements is false?
A) You can overload a classes constructors.
B) There is no mechanism in C++ for a constructor to call another constructor in the same class.
C) Just as a constructor can call a class's other member functions to perform tasks, C++11 allows constructors to call other constructors in the same class.
D) To overload a constructor, provide in the class definition a prototype for each version of the constructor, and provide a separate constructor definition for each overloaded version.
A) You can overload a classes constructors.
B) There is no mechanism in C++ for a constructor to call another constructor in the same class.
C) Just as a constructor can call a class's other member functions to perform tasks, C++11 allows constructors to call other constructors in the same class.
D) To overload a constructor, provide in the class definition a prototype for each version of the constructor, and provide a separate constructor definition for each overloaded version.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following is not true of a destructor?
A) It performs termination housekeeping.
B) It is called before the system reclaims the object's memory.
C) If the programmer does not explicitly provide a destructor, the compiler creates an "empty" destructor.
D) It releases the object's memory.
A) It performs termination housekeeping.
B) It is called before the system reclaims the object's memory.
C) If the programmer does not explicitly provide a destructor, the compiler creates an "empty" destructor.
D) It releases the object's memory.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following preprocessor directives does not constitute part of the preprocessor wrapper?
A) #define
B) #endif
C) #ifndef
D) #include
A) #define
B) #endif
C) #ifndef
D) #include
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
19
If a member function of a class already provides all or part of the functionality required by a constructor or another member function then:
A) Copy and paste that member function's code into this constructor or member function.
B) Call that member function from this constructor or member function.
C) That member function is unnecessary.
D) This constructor or member function is unnecessary.
A) Copy and paste that member function's code into this constructor or member function.
B) Call that member function from this constructor or member function.
C) That member function is unnecessary.
D) This constructor or member function is unnecessary.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
20
Utility functions:
A) Are private member functions that support operations of the class's other member functions.
B) Are part of a class's interface.
C) Are intended to be used by clients of a class.
D) Are a type of constructor.
A) Are private member functions that support operations of the class's other member functions.
B) Are part of a class's interface.
C) Are intended to be used by clients of a class.
D) Are a type of constructor.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
21
The code fragment: Increment::Increment(int c, int i)
: increment (i)
{
Count = c;
}
Does not cause any compilation errors. This tells you that:
A) count must be a non-const variable.
B) count must be a const variable.
C) increment must be a non-const variable.
D) increment must be a const variable.
: increment (i)
{
Count = c;
}
Does not cause any compilation errors. This tells you that:
A) count must be a non-const variable.
B) count must be a const variable.
C) increment must be a non-const variable.
D) increment must be a const variable.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
22
Assume that t is an object of class Test, which has member functions a(), b(), c() and d(). If the functions a(), b() and c() all return references to an object of class Test (using the dereferenced this pointer) and function d() returns void, which of the following statements will not produce a syntax error:
A) t.a().b().d();
B) a().b().t;
C) t.d().c();
D) t.a().t.d();
A) t.a().b().d();
B) a().b().t;
C) t.d().c();
D) t.a().t.d();
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
23
When composition (one object having another object as a member) is used:
A) The host object is constructed first and then the member objects are placed into it.
B) Member objects are constructed first, in the order they appear in the host constructor's initializer list.
C) Member objects are constructed first, in the order they are declared in the host's class.
D) Member objects are destructed last, in the order they are declared in the host's class.
A) The host object is constructed first and then the member objects are placed into it.
B) Member objects are constructed first, in the order they appear in the host constructor's initializer list.
C) Member objects are constructed first, in the order they are declared in the host's class.
D) Member objects are destructed last, in the order they are declared in the host's class.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
24
Static data members of a certain class:
A) Can be accessed only if an object of that class exists.
B) Cannot be changed, even by objects of the same that class.
C) Have class scope.
D) Can only be changed by static member functions.
A) Can be accessed only if an object of that class exists.
B) Cannot be changed, even by objects of the same that class.
C) Have class scope.
D) Can only be changed by static member functions.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
25
A client changing the values of private data members is:
A) Only possible by calling private member functions.
B) Possible using public functions and references.
C) Never possible.
D) Only possible if the private variables are not declared inside the class.
A) Only possible by calling private member functions.
B) Possible using public functions and references.
C) Never possible.
D) Only possible if the private variables are not declared inside the class.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
26
If Americans are objects of the same class, which of the following attributes would most likely be represented by a static variable of that class?
A) Age.
B) The President.
C) Place of birth.
D) Favorite food.
A) Age.
B) The President.
C) Place of birth.
D) Favorite food.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
27
The assignment operator (=) can be used to:
A) Test for equality.
B) Copy data from one object to another.
C) Compare two objects.
D) Copy a class.
A) Test for equality.
B) Copy data from one object to another.
C) Compare two objects.
D) Copy a class.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
28
If the line: friend class A;
Appears in class B, and the line:
Friend class B;
Appears in class C, then:
A) Class A is a friend of class C.
B) Class A can access private variables of class B.
C) Class C can call class A's private member functions.
D) Class B can access class A's private variables.
Appears in class B, and the line:
Friend class B;
Appears in class C, then:
A) Class A is a friend of class C.
B) Class A can access private variables of class B.
C) Class C can call class A's private member functions.
D) Class B can access class A's private variables.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
29
Static member functions:
A) Can use the this pointer.
B) Can access only other static member functions and static data members.
C) Cannot be called until an object of their class is instantiated.
D) Can be declared const as well.
A) Can use the this pointer.
B) Can access only other static member functions and static data members.
C) Cannot be called until an object of their class is instantiated.
D) Can be declared const as well.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
30
An error occurs if:
A) A non-reference, non-const, primitive data member is initialized in the member initialization list.
B) An object data member is not initialized in the member initialization list.
C) An object data member does not have a default constructor.
D) An object data member is not initialized in the member initialization list and does not have a default constructor.
A) A non-reference, non-const, primitive data member is initialized in the member initialization list.
B) An object data member is not initialized in the member initialization list.
C) An object data member does not have a default constructor.
D) An object data member is not initialized in the member initialization list and does not have a default constructor.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
31
Inside a function definition for a member function of an object with data member x, which of the following is not equivalent to this->x:
A) *this.x
B) (*this).x
C) x
D) None of the above are equivalent.
A) *this.x
B) (*this).x
C) x
D) None of the above are equivalent.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
32
For a non-constant member function of class Test, the this pointer has type:
A) const Test *
B) Test * const
C) Test const *
D) const Test * const
A) const Test *
B) Test * const
C) Test const *
D) const Test * const
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
33
Returning references to non-const, private data:
A) Allows private functions to be modified.
B) Is only dangerous if the binary scope resolution operator (::) is used in the function prototype.
C) Allows private member variables to be modified, thus "breaking encapsulation."
D) Results in a compiler error.
A) Allows private functions to be modified.
B) Is only dangerous if the binary scope resolution operator (::) is used in the function prototype.
C) Allows private member variables to be modified, thus "breaking encapsulation."
D) Results in a compiler error.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements will not produce a syntax error?
E) Defining a const member function that modifies a data member of the object.
F) Invoking a non-const member function on a const object.
G) Declaring an object to be const. h. Declaring a constructor to be const.
E) Defining a const member function that modifies a data member of the object.
F) Invoking a non-const member function on a const object.
G) Declaring an object to be const. h. Declaring a constructor to be const.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following statements about friend functions and friend classes is false?
A) A class can either grant friendship to or take friendship from another class using the friend keyword.
B) A friend declaration can appear anywhere in a class definition.
C) A friend of a class can access all of its private data member and member functions.
D) The friendship relationship is neither symmetric nor transitive.
A) A class can either grant friendship to or take friendship from another class using the friend keyword.
B) A friend declaration can appear anywhere in a class definition.
C) A friend of a class can access all of its private data member and member functions.
D) The friendship relationship is neither symmetric nor transitive.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck