C++ Test 1
1. Select all that apply. Given the following code fragment, which of the things shown below happen when the statement on line 8 executes? 1 int square(int a) 2 { 3 return a * a; 4 } 5 int main() 6 { 7 int x = 0; 8 x = square(5); 9 cout << x << endl; 10 return 0; 11 } a. The square function is called and the value 5 is passed as an argument. b. The square function calculates 5*5 and stores the result, 25, as a temporary value. c. The temporary value is copied (assigned) to the variable x. d. The temporary value is discarded by the system. e. None of these
(All of the above) a. The square function is called and the value 5 is passed as an argument. b. The square function calculates 5*5 and stores the result, 25, as a temporary value. c. The temporary value is copied (assigned) to the variable x. d. The temporary value is discarded by the system.
1. When a class declares an entire class as its friend, the friendship status is reciprocal. That is, each class's member functions have free access to the other's private members. (T/F)
ANS: False
10. You can overload the conditional operator to make it function as an unconditional operator. (T/F)
ANS: False
10. You must declare all data members of a class before you declare member functions. (T/F)
ANS: False
11. You may overload any C++ operator and you may use the operator function to define non-standard operations, such as @ or ^. (T/F)
ANS: False
11. You must use the private access specification for all data members of a class. (T/F)
ANS: False
12. In C++ if you overload the < operator, you must also overload the > operator. (T/F)
ANS: False
17. A non-static member function may not access a static member variable. (T/F)
ANS: False
17. While a class's member functions may be overloaded, the constructor cannot be overloaded. (T/F)
ANS: False
2. Class objects can be defined prior to the class declaration. (T/F)
ANS: False
3. The constructor function may not accept arguments. (T/F)
ANS: False
4. A destructor function can have zero to many parameters. (T/F)
ANS: False
4. The overloaded = operator copies data from one object to another so it is known as the overload copy operator. (T/F)
ANS: False
5. If you overload the prefix ++ operator, the postfix ++ operator is automatically overloaded. (T/F)
ANS: False
6. C++ permits you to overload the sizeof operator and the this pointer. (T/F)
ANS: False
6. More than one destructor function may be defined for a class. (T/F)
ANS: False
7. When you overload the << operator you must also overload the >> operator. (T/F)
ANS: False
8. A public data member may be declared a friend of a private function. (T/F)
ANS: False
8. In-place member initialization no longer is available in C++11. (T/F)
ANS: False
1. Whereas object-oriented programming centers on the object, procedural programming centers on functions. (T/F)
ANS: True
12. A private member function is useful for tasks that are internal to the class but it is not directly called by statements outside the class. (T/F)
ANS: True
13. A static member function does not need to be called by a specific object of the class. (T/F)
ANS: True
13. If you do not declare a destructor function, the compiler will furnish one automatically. (T/F)
ANS: True
14. It is possible to declare an entire class as a friend of another class. (T/F)
ANS: True
14. When an object is defined without an argument list for its constructor, the compiler automatically calls the object's default constructor. (T/F)
ANS: True
15. An aggregate class's constructor can use a member initialization list to call the constructors for each of its member objects. (T/F)
ANS: True
15. Constructor functions are often used to allocate memory that will be needed by the object. (T/F)
ANS: True
16. Destructor functions are often used to free memory that was allocated by the object. (T/F)
ANS: True
16. The this pointer is a special built-in pointer that is automatically passed as a hidden argument to all non-static member functions. (T/F)
ANS: True
18. A move operation transfers resources from a source object to a target object. (T/F)
ANS: True
18. When using smart pointers to dynamically allocate objects in C++ 11, it is unnecessary to delete the dynamically allocated objects because the smart pointer will automatically delete them. (T/F)
ANS: True
19. In C++11 an rvalue reference is a reference variable that can refer only to temporary objects. (T/F)
ANS: True
2. When you overload an operator, you can change the operator's original meaning to something entirely different. (T/F)
ANS: True
3. By default, when an object is assigned to another object, each member of one object is copied to its counterpart in the other object. (T/F)
ANS: True
5. More than one constructor function may be defined for a class. (T/F)
ANS: True
7. You can use the technique known as a member intialization list to initialize members of a class. (T/F)
ANS: True
9. A static member variable can be used when there are no objects of the class in existence. (T/F)
ANS: True
9. In object-oriented programming, the object encapsulates both the data and the functions that operate on the data. (T/F)
ANS: True
32. In C++11 you can have one constructor call another constructor in the same class by using a. constructor delegation b. in-place initialization c. a member initialization list d. None of these
a. constructor delegation
11. A __________ is a member function that is automatically called when a class object is __________. a. constructor, created b. destructor, created c. static function, deallocated d. utility function, declared e. None of these
a. constructor, created
6. A class is a(n) __________ that is defined by the programmer. a. data type b. function c. method d. attribute e. None of these
a. data type
19. Members of the class object are accessed with the a. dot operator b. cin object c. extraction operator d. stream insertion operator e. None of these
a. dot operator
27. The process of object-oriented analysis can be viewed as the following steps: a. identify objects, then define each object's attributes, behaviors, and relationships b. define data members and member functions, then assign the class name c. declare public and private variables, prototype functions, and then write code d. write the main() function, then determine which classes are needed e. None of these
a. identify objects, then define each object's attributes, behaviors, and relationships
26. In C++11 the __________ operator swaps the members of the object being assigned with the temporary object. a. move assignment b. swap assignment c. temp assignment d. semantic assignment e. None of these
a. move assignment
25. How many default constructors can a class have? a. only one b. two or more c. only two d. any number e. None of these
a. only one
17. The type of member function that may be called from a statement outside the class is a. public b. private c. undeclared d. global e. None of these
a. public
12. If a member is declared __________, all objects of that class have access to that variable. a. static b. dynamic c. inline d. default e. None of these
a. static
26. Objects in an array are accessed with __________. a. subscripts b. parentheses c. #include statements d. output format manipulators e. None of these
a. subscripts
19. A(n) __________ is a special built-in pointer that is available to a class's member functions.. a. this pointer b. &constructor pointer c. ~destructor *ptr d. overload operator, -> e. None of these
a. this pointer
35. What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test(77); return 0; } a. the program runs but there is no output. b. 77 c. Hello! d. the program will not compile
b. 77
15. When a constructor has a member initialization list, the initializations take place a. after any statements in the body of the constructor execute b. before any statements in the body of the constructor execute c. when a member is used in the execution of the program d. None of these
b. before any statements in the body of the constructor execute
5. When objects contain pointers, it is a good idea to create an explicit __________ function. a. destructor b. copy constructor c. static constructor d. inline constructor e. None of these
b. copy constructor
1. Objects are created from abstract data types that encapsulate __________ and __________ together. a. numbers, characters b. data, functions c. addresses, pointers d. integers, floating-point numbers e. None of these
b. data, functions
14. When a constructor function accepts no arguments, or does not have to accept arguments because of default arguments, it is called a(n) a. empty constructor b. default constructor c. stand-alone function d. arbitrator function e. None of these
b. default constructor
4. Where are class declarations usually stored? a. on separate disk volumes b. in their own header files c. in .cpp files, along with function definitions d. under pseudonyms e. None of these
b. in their own header files
31. The following code shows an example of __________ class Point { private: double y = 5.70; double z = 3.0; public: Public member functions go here... }; a. constructor delegation b. in-place initialization c. a default constructor creation d. an illegal initialization
b. in-place initialization
24. In C++11 values that persist beyond the statement that created them and have names that make them accessible to other statements in the program are called a. rvalues b. lvalues c. temporary values d. semantics e. None of these
b. lvalues
1. Each object of a class has its own copy of the class's a. member functions b. member variables c. constructor and destructor functions d. All of these e. None of these
b. member variables
18. To overload the + operator, you would write a function named a. overload + b. operator + c. function + d. operator.overload(+) e. None of these
b. operator +
9. If you do not furnish a(n) __________, an automatic memberwise copy will be performed when one object is assigned to another object. a. overloaded constructor function b. overloaded assignment operator c. default constructor function d. overloaded copy operator e. None of these
b. overloaded assignment operator
18. If you do not declare an access specification, the default for members of a class is a. inline b. private c. public d. global e. None of these
b. private
22. Which type of member function may only be called from a function that is a member of the same class? a. public b. private c. global d. local e. None of these
b. private
8. Which of the following is used to protect important data? a. the public access specifier b. the private access specifier c. the protect member function d. the class protection operator, @ e. None of these
b. the private access specifier
6. A good reason to overload an operator is to enable it to a. outperform its C language counterparts b. work in its usual way, but with programmer-defined data types c. operate on more operands than in its standard definition d. operate on no operands e. None of these
b. work in its usual way, but with programmer-defined data types
9. Which of the following is a directive used to create an "include guard" that allows a program to be conditionally compiled to prevent a header file from accidentally being included more than once? a. #include b. #guard c. #ifndef d. #endif e. None of these
c. #ifndef
34. What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test; return 0; } a. the program runs but there is no output. b. 0 c. Hello! d. the program will not compile
c. Hello!
20. Assuming that Rectangle is a class name, what can you say is true, given the following statement? Rectangle *BoxPtr; a. The statement declares an object of the class Rectangle. b. The statement assigns the value of *BoxPtr to the object Rectangle. c. The statement defines a Rectangle pointer variable named *BoxPtr. d. The statement is illegal in C++. e. None of these
c. The statement defines a Rectangle pointer variable named *BoxPtr.
28. When a member function is defined outside of the class declaration, the function name must be qualified with the a. class name, followed by a semicolon b. name of the first object c. class name, followed by the scope resolution operator d. private access specifier e. None of these
c. class name, followed by the scope resolution operator
10. It is a good idea to make a copy constructor's parameters __________ by specifying the __________ key word in the parameter list. a. inline, inline b. static, static c. constant, const d. global, global e. None of these
c. constant, const
15. A(n) __________ is a special function that is called whenever a new object is created and initialized with another object's data. a. static function b. destructor c. copy constructor d. assignment function e. None of these
c. copy constructor
23. In the following function header, the word int is known as a(n) __________. FeetInches FeetInches::operator++(int) a. parameterless data type b. incomplete argument c. dummy parameter d. incomplete parameter e. None of these
c. dummy parameter
22. A(n) __________ informs the compiler that a class will be declared later in the program. a. static function b. private data member c. forward declaration d. object conversion e. None of these
c. forward declaration
16. In C++11 you can use __________ to initialize a member variable in its declaration statement. a. general member initialization b. default initialization c. in-place initialization d. initialization overload e. None of these
c. in-place initialization
10. When the body of a member function is defined inside a class declaration, it is said to be a. static b. global c. inline d. conditional e. None of these
c. inline
33. Assume that myCar is an instance of the Car class and that the Car class has a member function named accelerate. Which of the following is a valid call to the accelerate member function? a. Car -> accelerate(); b. myCar::accelerate(); c. myCar.accelerate(); d. myCar:accelerate(); e. None of these
c. myCar.accelerate();
8. When a class contains an instance of another class, it is known as a. object overloading b. operator overloading c. object composition d. dynamic composition e. None of these
c. object composition
17. When you redefine the way a standard operator works when it is used with class objects, you have __________ the operator. a. reassigned b. reformatted c. overloaded d. referenced e. None of these
c. overloaded
20. An ___________ operator can work with programmer-defined data types. a. inline b. unconditional c. overloaded d. undefined e. None of these
c. overloaded
7. Examples of access specifiers are the key words a. near and far b. opened and closed c. private and public d. table and row e. None of these
c. private and public
29. If a local variable and a global variable have the same name within the same program, the __________ resolution operator must be used. a. variable b. ambiguity c. scope d. global e. None of these
c. scope
11. A reason to overload the __________ is to allow you to write classes that have array-like behaviors. a. parentheses ( ) operator b. curly braces { } operator c. square brackets [ ] operator d. colon :: operator e. None of these
c. square brackets [ ] operator
4. C++ allows you to redefine the way __________ work when used with class objects. a. compiler errors b. preprocessor directives c. standard operators d. undefined variables e. None of these
c. standard operators
13. A member function that is declared __________ may not access any non-static data members in the class. a. private b. public c. static d. inline e. None of these
c. static
23. The constructor function always has the same name as a. the first private data member b. the first public data member c. the class d. the first object of the class e. None of these
c. the class
24. Which of the following is automatically called when an object is destroyed? a. the constructor function b. the specification deallocator c. the destructor function d. the destruction function e. None of these
c. the destructor function
16. If you do not furnish a __________, a default one will be provided by the compiler. a. constructor b. destructor c. copy constructor d. All of these e. None of these
d. All of these
13. The destructor function's return type is a. int b. float c. char d. Nothing; destructors have no return type e. None of these
d. Nothing; destructors have no return type
2. In OOP terminology, an object's member variables are often called its __________ and its member functions can be referred to as its behaviors or its __________. a. values, morals b. data, activities c. attributes, activities d. attributes, methods e. None of these
d. attributes, methods
5. In a procedural program you typically have __________ stored in a collection of variables and a set of __________ that perform operations on the data. a. numbers, arguments b. parameters, arguments c. strings, operators d. data, functions e. None of these
d. data, functions
14. Which type of function is not a member of a class but has access to the private members of the class? a. static b. constructor c. destructor d. friend e. None of these
d. friend
21. When you overload an operator, you cannot change the number of __________ taken by the operator. a. arguments b. parameters c. operations d. operands e. None of these
d. operands
3. C++ requires that a copy constructor's parameter be a(n) a. integer data type b. floating-point data type c. pointer variable d. reference object e. None of these
d. reference object
25. In C++11 reference variables that can refer only to temporary objects that would otherwise have no name are called __________ and are declared with a __________. a. rvalues, ampersand (&) b. lvalues, ampersand (&) c. lvalues, double ampersand (&&) d. rvalues, double ampersand (&&) e. None of these
d. rvalues, double ampersand (&&)
2. The __________ member variable may be accessed before any objects of the class have been created. a. private b. public c. inline d. static e. None of these
d. static
3. A C++ class is similar to a(n) a. inline function b. header file c. library function d. structure e. None of these
d. structure
36. What is the output of the following program? #include <iostream> using namespace std; class TestClass { private: int val; void showVal() { cout << val << endl; } public: TestClass(int x) { val = x; } }; int main() { TestClass test(77); test.showVal(); return 0; } a. the program runs but there is no output. b. 77 c. 0 d. the program will not compile
d. the program will not compile
30. For the following code, which statement is not true? class Point { private: double y; double z; public: double x; }; a. x is available to code that is written outside the class. b. The name of the class is Point. c. x, y, and z are called members of the class. d. z is not available to code that is written outside the class. e. All of these are true.
d. z is not available to code that is written outside the class.
12. The constructor function's return type is a. int b. float c. char d. structure pointer e. None of these
e. None of these
21. When you dereference an object pointer, use the a. -> operator b. <> operator c. dot operator d. & operator e. None of these
a. -> operator
7. Which of the following operators may be used to assign one object to another? a. = b. == c. <> d. @ e. None of these
a. =