C++ Chapter 6 Functions

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

What will the following code display? #include <iostream> using namespace std; void doSomething(int&); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num) { num = 0; cout << num << endl; } a. 2 0 2 b. 2 2 2 c. 0 0 0 d. 2 0 0

2 0 0

What will the following code display? #include <iostream> using namespace std; void doSomething(int); int main() { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; cout << num << endl; } a. 2 0 2 b. 2 2 2 c. 0 0 0 d. 2 0 0

2 0 2

In the following function prototype, how many parameter variables does this function have? int myFunction(double, double, double); a. 1 b. 2 c. 3 d. cannot tell from the prototype

3

What will the following code display? #include <iostream> using namespace std; void showDub(int); int main() { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; } a. 2 2 b. 4 2 c. 2 4 d. 4 4

4 2

What will the following code display? #include <iostream> using namespace std; int getValue(int); int main() { int x = 2; cout << getValue(x) << endl; return 0; } int getValue(int num) { return num + 5; } a. 5 b. 2 c. 7 d. getValue(x)

7

EXIT_FAILURE and ________ are named constants that may be used to indicate success or failure when the exit() function is called. a. EXIT_TERMINATE b. EXIT_SUCCESS c. EXIT_OK d. RETURN_OK

EXIT_SUCCESS

Select all that apply. Which of the following statement(s) about global variables is(are) true? a. A global variable is accessible only to the main function. b. A global variable is declared in the highest-level block in which it is used. c. A global variable can have the same name as a variable that is declared locally within a function. d. A global variable is the same as a named constant. e. If a function contains a local variable with the same name as a global variable, the global variable's name takes precedence within the function

a b c e

Which of the following causes a function to execute? a. a for loop b. a do-while loop c. a function prototype d. a function call e. None of these

a function call

Select all that apply. Which of the following must be included in a function header? a. the data type of each parameter b. the data type of the return value c. the name of the function d. the names of parameter variables

all apply

A(n) __________ is information that is passed to a function, and a(n) __________ is information that is received by a function. a. function call, function header b. parameter, argument c. argument, parameter d. prototype, header e. None of these

argument, parameter

Functions are ideal for menu-driven programs. When the user selects a menu item, the program can __________ the appropriate function. a. call b. append c. define d. declare e. None of these

call

A function is executed when it is a. defined b. prototyped c. declared d. called e. None of these

called

Given the following header for a function named computeValue, which of the following is a valid call to the function? void computeValue(int value) a. computeValue(10) b. computeValue(10); c. void computeValue(10); d. void computeValue(int x);

computeValue(10);

. A __________ argument is passed to a parameter when the actual argument is left out of the function. a. false b. true c. null d. default e. None of these

default

These types of arguments are passed to parameters automatically if no argument is provided in the function call. a. local b. default c. global d. reference e. None of these

default

A function __________ contains the statements that make up the function. a. prototype b. definition c. call d. expression e. parameter list

definition

. It is good programming practice to __________ your functions by writing comments that describe what they do. a. execute b. document c. retrieve d. create e. None of these

document

What is the data type of the following function prototype's parameter variable? int myFunction(double); a. int b. double c. void d. cannot tell from the prototype

double

This function causes a program to terminate, regardless of which function or control mechanism is executing. a. exit() b. terminate() c. return() d. continue() e. None of these

exit()

A function's return data type must be the same as the function's parameters.

false

A local variable and a global variable may not have the same name within a program.

false

Local variables are initialized to zero by default.

false

When a function is called, flow of control moves to the function's prototype

false

You must always furnish an argument with a function call.

false

A collection of statements that performs a specific task is a(n) a. infinite loop b. variable c. constant d. function e. decision

function

If a function does not have a prototype, default arguments may be specified in the __________. a. function call b. function header c. execution d. return type e. None of these

function header

A _________ variable is declared outside all functions. a. local b. global c. floating-point d. counter e. None of these

global

What is the data type of the following function prototype's return value? int myFunction(double); a. int b. double c. void d. cannot tell from the prototype

int

Which line in the following program contains the header for the showDub function? 1 #include <iostream> 2 using namespace std; 3 void showDub(int); 4 int main() 5 { 6 int x = 2; 7 showDub(x); 8 cout << x << endl; 9 return 0; 10 } 11 void showDub(int num) 12 { 13 cout << (num * 2) << endl; 14 } a. line 3 b. line 4 c. line 7 d. line 11

line 11

Which line in the following program contains the prototype showDub function? 1 #include <iostream> 2 using namespace std; 3 void showDub(int); 4 int main() 5 { 6 int x = 2; 7 showDub(x); 8 cout << x << endl; 9 return 0; 10 } 11 void showDub(int num) 12 { 13 cout << (num * 2) << endl; 14 } a. line 3 b. line 4 c. line 7 d. line 11

line 3

Which line in the following program contains a call to the showDub function? 1 #include <iostream> 2 using namespace std; 3 void showDub(int); 4 int main() 5 { 6 int x = 2; 7 showDub(x); 8 cout << x << endl; 9 return 0; 10 } 11 void showDub(int num) 12 { 13 cout << (num * 2) << endl; 14 } a. line 3 b. line 4 c. line 7 d. line 11

line 7

This type of variable is defined inside a function and is not accessible outside the function. a. global b. reference c. local d. counter e. None of these

local

. A function can have no parameters, one parameter, or many parameters and can return __________ value(s). a. zero to many b. no c. only one d. a maximum of ten e. None of these

only one

If a function is called more than once in a program, the values stored in the function's local variables do not _________ between function calls. a. persist b. execute c. communicate d. change e. None of these

persist

A function _________ eliminates the need to place a function definition before all calls to the function. a. header b. prototype c. argument d. parameter e. None of these

prototype

When used as parameters, these types of variables allow a function to access the parameter's original argument: a. reference b. floating-point c. counter d. undeclared e. None of these

reference

This statement causes a function to end. a. end b. terminate c. return d. release e. None of these

return

The value in this type of variable persists between function calls. a. global b. internal c. static d. dynamic e. None of these

static

The value in a __________ variable persists between function calls. a. dynamic b. global c. floating-point d. static local e. counter

static local

This is a dummy function that is called instead of the actual function it represents: a. main b. stub c. a driver d. an overloaded function

stub

A parameter is a special purpose variable that is declared inside the parentheses of a function definition.

true

A static variable that is defined within a function is initialized only once, the first time it is called.

true

Global variables are initialized to zero by default.

true

It is not considered good programming practice to declare all your variables globally

true

It is possible for a function to have some parameters with default arguments and some without.

true

One reason for using functions is to break programs into manageable units or modules

true

You may use the exit() function to terminate a program, regardless of which control mechanism is executing.

true

. __________ functions may have the same name as long as their parameter lists are different. a. Only two b. Two or more c. No d. Un-prototyped e. None of these

two or more

Given the following function: void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code segment that invokes calc(): int x = 1; int y = 2; int z = 3; calc(x, y); cout << x << " " << y << " " << z << endl; a. 1 2 3 b. 1 6 3 c. 3 6 3 d. 1 14 9 e. 2 3 4 5

1 6 3


Set pelajaran terkait

Ch 24 PrepUs: Cognitive Disorders

View Set

Chapter 8 commercial property insurance

View Set

Lecture 6: Aholt Sexually Transmitted Diseases

View Set