Deck 6: Functions and an Introduction to Recursion
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
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/48
Play
Full screen (f)
Deck 6: Functions and an Introduction to Recursion
1
To make numeric literals more readable, C++14 allows you to insert between groups of digits in numeric literals the digit separator ' (a single-quote character).
A) , (a comma)
B) ^ (a caret)
C) ' (a single quote)
D) * (an asterisk)
A) , (a comma)
B) ^ (a caret)
C) ' (a single quote)
D) * (an asterisk)
C
2
Which of the following C++ Standard Library headers does not contain a C++ Standard Library container class?
A).
B)
A)
B)
- .
C)
D)
D
3
Which of the following expressions returns the trigonometric sine of x?
A) sin(x).
B) sine(x).
C) trig_sin(x).
D) trig_sine(x).
A) sin(x).
B) sine(x).
C) trig_sin(x).
D) trig_sine(x).
A
4
The rand function generates a data value of the type:
A) unsigned int.
B) int.
C) long int.
D) short int.
A) unsigned int.
B) int.
C) long int.
D) short int.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following is not a valid enumeration?
A) enum class Person {ME, YOU, THEM};.
B) enum class Person {ME = 1, YOU = 2, THEM = 3};.
C) enum class Person {ME = 0, YOU = 0, THEM = 0};.
D) enum class Person {ME, YOU, ME};.
A) enum class Person {ME, YOU, THEM};.
B) enum class Person {ME = 1, YOU = 2, THEM = 3};.
C) enum class Person {ME = 0, YOU = 0, THEM = 0};.
D) enum class Person {ME, YOU, ME};.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
6
Functions can:
A) Be used as building blocks to create new programs.
B) Return a result to the caller function.
C) Be reused any number of times.
D) Do all of the above.
A) Be used as building blocks to create new programs.
B) Return a result to the caller function.
C) Be reused any number of times.
D) Do all of the above.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
7
The function prototype double mySqrt(int x);
A) Declares a function called mySqrt which takes an integer as an argument and returns a double.
B) Defines a function called double which calculates square roots.
C) Defines a function called mySqrt which takes an argument of type x and returns a double.
D) Declares a function called mySqrt which takes a double as an argument and returns an integer.
A) Declares a function called mySqrt which takes an integer as an argument and returns a double.
B) Defines a function called double which calculates square roots.
C) Defines a function called mySqrt which takes an argument of type x and returns a double.
D) Declares a function called mySqrt which takes a double as an argument and returns an integer.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following is true?
A) C++ does not provide any capabilities for generating random numbers that cannot be predicted.
B) C++11 provides random number capabilities that can produce nondeterministic random numbers-a set of random numbers that can't be predicted.
C) Random numbers produced by the rand function are nondeterministic.
D) None of the above.
A) C++ does not provide any capabilities for generating random numbers that cannot be predicted.
B) C++11 provides random number capabilities that can produce nondeterministic random numbers-a set of random numbers that can't be predicted.
C) Random numbers produced by the rand function are nondeterministic.
D) None of the above.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
9
A variable that can have values only in the range 0 to 65535 is a:
A) Four-byte int.
B) Four-byte unsigned int.
C) Two-byte int.
D) Two-byte unsigned int.
A) Four-byte int.
B) Four-byte unsigned int.
C) Two-byte int.
D) Two-byte unsigned int.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
10
A function prototype does not have to:
A) Include parameter names.
B) Terminate with a semicolon.
C) Agree with the function definition.
D) Match with all calls to the function.
A) Include parameter names.
B) Terminate with a semicolon.
C) Agree with the function definition.
D) Match with all calls to the function.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
11
Each standard library has a corresponding:
A) Function.
B) Variable type.
C) Header.
D) CD-ROM.
A) Function.
B) Variable type.
C) Header.
D) CD-ROM.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
12
All of the following are true of functions except:
A) They define specific tasks that can be used at many points in a program.
B) A function call must specify the name and arguments of the function.
C) The definition of a function usually is visible to other functions.
D) The implementation of a function is hidden from the caller.
A) They define specific tasks that can be used at many points in a program.
B) A function call must specify the name and arguments of the function.
C) The definition of a function usually is visible to other functions.
D) The implementation of a function is hidden from the caller.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following is not included in ?
A) pow.
B) floor.
C) ln.
D) log.
A) pow.
B) floor.
C) ln.
D) log.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
14
Converting from type ________ to type ________ will result in the loss of data.
A) bool, char.
B) float, double.
C) int, char.
D) short, long.
A) bool, char.
B) float, double.
C) int, char.
D) short, long.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
15
A function prototype can always be omitted when:
A) A function is defined before it is first invoked.
B) A function is invoked before it is first defined.
C) A function takes no arguments.
D) A function does not return a value.
A) A function is defined before it is first invoked.
B) A function is invoked before it is first defined.
C) A function takes no arguments.
D) A function does not return a value.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
16
Using the following function definition, the parameter list is represented by: A B (C)
{
D
}
A) A.
B) B.
C) C.
D) D.
{
D
}
A) A.
B) B.
C) C.
D) D.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
17
[C++11] Which of the following statements about scoped enumerations is false?
A) A scoped enumeration is introduced by the keywords enum class, followed by a type name and a set of identifiers representing integer constants.
B) The identifiers in an enum class must be unique, but separate enumeration constants can have the same integer value.
C) By convention, you should capitalize the first letter of an enum class's name.
D) To reference a scoped enum constant, you must qualify the constant with the scoped enum's type name and the scope-resolution operator (:), as in MaritalStatus:SINGLE.
A) A scoped enumeration is introduced by the keywords enum class, followed by a type name and a set of identifiers representing integer constants.
B) The identifiers in an enum class must be unique, but separate enumeration constants can have the same integer value.
C) By convention, you should capitalize the first letter of an enum class's name.
D) To reference a scoped enum constant, you must qualify the constant with the scoped enum's type name and the scope-resolution operator (:), as in MaritalStatus:SINGLE.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
18
Enumeration constants:
A) Must have unique integer values.
B) Can be assigned other values once they've been defined.
C) Must have unique identifiers.
D) Are declared using the keyword const.
A) Must have unique integer values.
B) Can be assigned other values once they've been defined.
C) Must have unique identifiers.
D) Are declared using the keyword const.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
19
In the expression n = x + rand() % y;
A) y is the shifting value.
B) x is the scaling value.
C) y is the scaling value.
D) Both (a) and (b).
A) y is the shifting value.
B) x is the scaling value.
C) y is the scaling value.
D) Both (a) and (b).
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
20
The argument list of a function call must match, or be consistent with, the parameter list of the called function in all of the following details, except:
A) The number of arguments/parameters in the list.
B) The types of arguments/parameters in the list.
C) The names of arguments/parameters in the list.
D) The argument list and parameter list must match in all of the above details.
A) The number of arguments/parameters in the list.
B) The types of arguments/parameters in the list.
C) The names of arguments/parameters in the list.
D) The argument list and parameter list must match in all of the above details.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
21
A reference parameter:
A) Is an alias for its corresponding argument.
B) Is declared by following the parameter's type in the function prototype by an ampersand (&).
C) Cannot be modified.
D) Both (a) and (b).
A) Is an alias for its corresponding argument.
B) Is declared by following the parameter's type in the function prototype by an ampersand (&).
C) Cannot be modified.
D) Both (a) and (b).
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
22
Overloaded functions must have:
A) Different parameter lists.
B) Different return types.
C) The same number of parameters.
D) The same number of default arguments.
A) Different parameter lists.
B) Different return types.
C) The same number of parameters.
D) The same number of default arguments.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following is not included in a function's activation record?
A) The return address of its caller function.
B) Parameter values received from its caller.
C) Local variables it has declared.
D) The name of the function.
A) The return address of its caller function.
B) Parameter values received from its caller.
C) Local variables it has declared.
D) The name of the function.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
24
An activation record will be popped off the function call stack whenever:
A) A function returns control to its caller.
B) A function calls another function.
C) A function calls itself.
D) A function declares a local variable.
A) A function returns control to its caller.
B) A function calls another function.
C) A function calls itself.
D) A function declares a local variable.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
25
Call-by-reference can achieve the security of call-by-value when:
A) The value being passed is small.
B) A large argument is passed in order to improve performance.
C) A pointer to the argument is used.
D) The const qualifier is used.
A) The value being passed is small.
B) A large argument is passed in order to improve performance.
C) A pointer to the argument is used.
D) The const qualifier is used.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
26
If a set of functions have the same program logic and operations and differ only in the data type(s) each receives as argument(s) then a(n) __________ should be used.
A) Overloaded function.
B) Recursive function.
C) Macro.
D) Function template.
A) Overloaded function.
B) Recursive function.
C) Macro.
D) Function template.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
27
Given the following function template template
T maximum(T value1, T value2)
{
If (value1 > value2) {
Return value1;
}
Else {
Return value2;
}
}
What would be returned by the following two function calls?
Maximum(2, 5);
Maximum(2.3, 5.2);
A) 5 and a type-mismatch error.
B) 5 and 5.2.
C) 2 and 2.3.
D) Two error messages.
T maximum(T value1, T value2)
{
If (value1 > value2) {
Return value1;
}
Else {
Return value2;
}
}
What would be returned by the following two function calls?
Maximum(2, 5);
Maximum(2.3, 5.2);
A) 5 and a type-mismatch error.
B) 5 and 5.2.
C) 2 and 2.3.
D) Two error messages.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
28
When an argument is passed-by-value, changes in the called function __________ affect the original variable's value; when an argument is passed call-by-reference, changes in the called function __________ affect the original variable's value.
A) Do not, do.
B) Do not, do not.
C) Do, do.
D) Do, do not.
A) Do not, do.
B) Do not, do not.
C) Do, do.
D) Do, do not.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
29
If the function int volume(int x = 1, int y = 1, int z = 1); is called by the expression volume(3), how many default arguments are used?
A) None.
B) One.
C) Two.
D) Three.
A) None.
B) One.
C) Two.
D) Three.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
30
Labels are the only identifiers with:
A) Function scope.
B) File scope.
C) Block scope.
D) Function-prototype scope.
A) Function scope.
B) File scope.
C) Block scope.
D) Function-prototype scope.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
31
The unary scope resolution operator is used:
A) To access a global variable when a local variable of the same name is in scope.
B) To access any variable in an outer block when a local variable of the same name is in scope.
C) To access a global variable when it is out of scope.
D) To access a local variable with the same name as a global variable.
A) To access a global variable when a local variable of the same name is in scope.
B) To access any variable in an outer block when a local variable of the same name is in scope.
C) To access a global variable when it is out of scope.
D) To access a local variable with the same name as a global variable.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
32
In regards to default arguments, which of the following is false?
A) When an argument is omitted in a function call, the default value of that argument is automatically inserted by the compiler and passed in the function call.
B) They must be the rightmost (trailing) arguments in a function's parameter list.
C) Default values can be constants.
D) Default values cannot be global variables or function calls.
A) When an argument is omitted in a function call, the default value of that argument is automatically inserted by the compiler and passed in the function call.
B) They must be the rightmost (trailing) arguments in a function's parameter list.
C) Default values can be constants.
D) Default values cannot be global variables or function calls.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
33
The inline keyword:
A) Increases function-call overhead.
B) Can reduce a function's execution time but increase program size.
C) Can decrease program size but increase the function's execution time.
D) Should be used with all frequently used functions.
A) Increases function-call overhead.
B) Can reduce a function's execution time but increase program size.
C) Can decrease program size but increase the function's execution time.
D) Should be used with all frequently used functions.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
34
Type-safe linkage is ensured by:
A) Name mangling.
B) Calling the correct function.
C) The agreement of the arguments and parameters.
D) Specifying return types.
A) Name mangling.
B) Calling the correct function.
C) The agreement of the arguments and parameters.
D) Specifying return types.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following statements creates a uniform_int_distribution object for producing values in the range -10 to 20?
A) uniform_int_distribution randomInt{20, -10};
B) uniform_int_distribution randomInt{-10, 20};
C) uniform_int_distribution randomInt{20, -10};
D) uniform_int_distribution randomInt{-10, 20};
A) uniform_int_distribution
B) uniform_int_distribution
C) uniform_int_distribution
D) uniform_int_distribution
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following is true of function templates?
A) All function templates begin with the keyword class.
B) Every formal type parameter is preceded by either keyword typename or template.
C) Formal type parameters act as placeholders for built-in types or user-defined types and are used to specify the types of arguments to the function, to specify the return type of the function, and to declare variables within the body of the function definition.
D) A programmer must define a separate function template for each template function specialization to be used in the program.
A) All function templates begin with the keyword class.
B) Every formal type parameter is preceded by either keyword typename or template.
C) Formal type parameters act as placeholders for built-in types or user-defined types and are used to specify the types of arguments to the function, to specify the return type of the function, and to declare variables within the body of the function definition.
D) A programmer must define a separate function template for each template function specialization to be used in the program.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
37
[C++11]-Which of the following statements is false?
A) An enumeration's constants have integer values.
B) An unscoped enum's underlying type is independent of its constants' values but is guaranteed to be large enough to store those values.
C) A scoped enum's underlying integral type is int, but you can specify a different type by following the type name with a colon (:) and the integral type. For example, we can specify that the constants in the enum class Status should have type unsigned int, as in enum class Status : unsigned int {CONTINUE, WON, LOST};
D) A compilation error occurs if an enum constant's value is outside the range that can be represented by the enum's underlying type.
A) An enumeration's constants have integer values.
B) An unscoped enum's underlying type is independent of its constants' values but is guaranteed to be large enough to store those values.
C) A scoped enum's underlying integral type is int, but you can specify a different type by following the type name with a colon (:) and the integral type. For example, we can specify that the constants in the enum class Status should have type unsigned int, as in enum class Status : unsigned int {CONTINUE, WON, LOST};
D) A compilation error occurs if an enum constant's value is outside the range that can be represented by the enum's underlying type.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
38
Which of the following is false?
A) An engine implements a random-number generation algorithm that produce pseudorandom numbers.
B) A distribution controls the range of values produced by an engine, the types of those and the statistical properties of the values.
C) The default range of a uniform_int_distribution is from 0 to 32767.
D) C++11 provides many classes that represent various random-number generation engines and distributions.
A) An engine implements a random-number generation algorithm that produce pseudorandom numbers.
B) A distribution controls the range of values produced by an engine, the types of those and the statistical properties of the values.
C) The default range of a uniform_int_distribution is from 0 to 32767.
D) C++11 provides many classes that represent various random-number generation engines and distributions.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
39
What happens when two blocks, one nested inside of the other, both declare variables with the same identifier? (Assume that the outer block declares its variable before the opening left-brace of the inner block.)
A) A syntax error occurs.
B) The "outer" variable is hidden while the "inner" variable is in scope.
C) The "outer" variable is irretrievably lost when the "inner" variable is declared.
D) The "inner" declaration is ignored and the "outer" variable has scope even inside the inner block.
A) A syntax error occurs.
B) The "outer" variable is hidden while the "inner" variable is in scope.
C) The "outer" variable is irretrievably lost when the "inner" variable is declared.
D) The "inner" declaration is ignored and the "outer" variable has scope even inside the inner block.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
40
Which of the following does the C++ compiler not examine in order to select the proper overloaded function to call?
A) Types and order of the arguments in the function call.
B) The number of arguments in the function call.
C) The return type of the function.
D) It examines all of the above.
A) Types and order of the arguments in the function call.
B) The number of arguments in the function call.
C) The return type of the function.
D) It examines all of the above.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
41
Recursion is memory-intensive because:
A) Recursive functions tend to declare many local variables.
B) Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack.
C) Many copies of the function code are created.
D) It requires large data values.
A) Recursive functions tend to declare many local variables.
B) Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack.
C) Many copies of the function code are created.
D) It requires large data values.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
42
What value does function mystery return when called with a value of 4? int mystery (int number)
{
If (number <= 1) {
Return 1;
}
Else {
Return number * mystery(number - 1);
}
}
A) 0.
B) 1.
C) 4.
D) 24.
{
If (number <= 1) {
Return 1;
}
Else {
Return number * mystery(number - 1);
}
}
A) 0.
B) 1.
C) 4.
D) 24.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
43
A recursive function is a function that:
A) Returns a double.
B) Takes 3 arguments.
C) Calls itself, directly or indirectly.
D) Is inside of another function.
A) Returns a double.
B) Takes 3 arguments.
C) Calls itself, directly or indirectly.
D) Is inside of another function.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
44
For which of the following operators does C++ not specify the order of evaluation of its operands?
A) +.
B) &&.
C) ,.
D) ?:.
A) +.
B) &&.
C) ,.
D) ?:.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
45
C++11's unsigned long long int type (which can be abbreviated as unsigned long long) enables you to store values in at least ________, which can hold numbers as large as 18,446,744,073,709,551,615.
A) 2 bytes (16 bits)
B) 4 bytes (32 bits)
C) 8 bytes (64 bits)
D) 16 bytes (128 bits)
A) 2 bytes (16 bits)
B) 4 bytes (32 bits)
C) 8 bytes (64 bits)
D) 16 bytes (128 bits)
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
46
Recursion is to the base case as iteration is to what:
A) The counter.
B) A repetition structure.
C) Failure of the loop continuation test.
D) A selection structure.
A) The counter.
B) A repetition structure.
C) Failure of the loop continuation test.
D) A selection structure.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
47
Assuming the following pseudocode for the Fibonacci series, what is the value of the 5th Fibonacci number (fibonacci (5))? fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)
A) 1.
B) 3.
C) 5.
D) 7.
Fibonacci(1) = 1
Fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)
A) 1.
B) 3.
C) 5.
D) 7.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
48
All of the following are reasons to use recursion except:
A) An iterative solution is not apparent.
B) The resulting program is easier to debug.
C) It more naturally mirrors the problem.
D) It maximizes execution performance.
A) An iterative solution is not apparent.
B) The resulting program is easier to debug.
C) It more naturally mirrors the problem.
D) It maximizes execution performance.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck