You are on page 1of 6

Function Basics

These test questions are true-false, fill in the blank, multiple choice, and free form questions that may
require code. The multiple-choice questions may have more than one correct answer. You are required to
mark and comment on correct answers. Mark all the correct answers for full credit. The true false questions
require an explanation in addition to the true/false response, and, if false, also require a correction.
d) z = area area
True False:
An explanation is required.
1. Procedural abstraction involves information x2 + y
e) p = 2
hiding in that only the 'contract' between the x −y
programmer using the function (the client) and 2. Write code that declares x, y, and z as
author of a function is known to either. double variables. Then write code that causes
2. Code after a return or a call to the exit function is z to be assigned the result of x divided by y,
executed will not be executed. rounded as indicated below. Be sure to
3. It is legal to replace the prototype #include the header file that declares the
double totalCost(int numberParam, library functions you use.
double priceParam);
a) round up
with the more terse, alternate form b) round down
double totalCost(int, double);
c) round to the nearest integer. Does your
4. Extensive use of global variables is a satisfactory
code round an exact half up or down? say
replacement for the difficulties of parameter
2.5?
passing with functions.
3. Each of the following lines of code purport to
5. A variable declared outside any function is said to
round the results of the division of doubles to
be a local variable.
6. A variable declared within a function block is the nearest integer value (but still of type
said to be local to the function. double). All are correct C++ code but some do
7. Consider two blocks, one within another. If an not round correctly. Tell which rounds down, up,
identifier is declared as a variable in the inmost of or to the nearest integer value, or is not
these two blocks, one can access this variable reasonable
from the outer block. Assume that math.h has been included, and that
8. Consider two blocks, one within another. C++ all variables have appropriate values.
double x, y, z;
prohibits an identifier to be declared as a variable
in each of these blocks. a) z = ceil(x/y);
9. Calling something a black box is a figure of b) z = ceil(x/y-0.5);
speech that conveys the idea that you cannot see c) z = floor(x/y-0.5);
inside. You know its behavior and interface but d) z = floor(x/y+0.5);
not its implementation. e) z = floor(x/y);
Free Form Questions: 4. Declare (give a prototype for) a function named
1. Convert the following mathematics expressions average_grade. This function returns a
to C++ expressions. Use the declarations double and has four double arguments, test1,
provided. Indicate the appropriate header file for test2, test3, test4. The return value should
any library function used. Be sure you initialize be the average, or arithmetic mean of the
any variables whose values you are using to four arguments. Be sure to include a "prototype
reasonable values for the library functions you comment" that tells briefly what the function
are using. does.
int x, y; //declaration for a)
and b)
5. Define a function named average_grade.
a) y = x3 This function returns a double and has four
b) y <= |x| double arguments, test1, test2, test3,
test4. The return value should be the average,
double x, y, z, area; or arithmetic mean of the four arguments.
//declaration for c) through e). Be sure to include a comment that tells briefly
c) z = x1.6 what the function does.
6. Give an outline for the general form of a }
programmer defined function. 12. Write a function definition called even that takes
7. In your own words discuss similarities and one argument of type int and returns a bool
differences between a function and a small value. The function returns true if its one
program. argument is an even number; otherwise it returns
8. Given the function declaration (prototype), does false.
the compiler complain or compile if you call this 13. Write a function definition for a isDigit
using the following line? If the compiler function that takes one argument of type char
complains, what is the complaint? and returns a bool value. The function returns
//if score >= min_to_pass, returns
'P' for passing, true if the argument is a decimal digit;
//else returns 'F' for failing. otherwise it returns false.
char grade (int score, int 14. Write a function definition for a function called
min_to_pass); inOrder that takes three arguments of type
int main() int. The function returns true if the arguments
{
are in increasing order left to right; otherwise
double fscore;
char fgrade; inOrder returns false. For example,
int need_to_pass; inOrder(1, 2, 3) returns true, whereas
inOrder(1,3,2) returns false.
//omitted code to get values for 15. Write a prototype and prototype comments for
variables the sqrt library function.
//fscore and need Multiple Choice
fgrade = grade(fscore, need);
There may be more than one correct answer. You
return 0;
} must give all the correct answers for full credit. An
9. What is the error in the following code? Why is explanation is required.
this wrong? 1. A C++ predefined functions
int f(int x) a) are usually provided in libraries
{ b) make C++ harder than necessary.
int x; c) must #include the proper header file
// code for function body d) are usually provided with the C++ compiler.
} 2. The sqrt function
10. Procedural abstraction requires the author of a a) is provided in the <cmath> library header.
function and the client (user) of that function to b) returns the square of the argument
know and not to know certain things. Remark on c) returns the square root of the argument
who needs to know and who need not know each d) the argument type is int
of the following items e) the return type is double.
a) the requirements on the parameter values, 3. A C++ predefined function
b) exactly what the return value is to be, a) argument is the value the predefined function
c) the implementation details, and starts with
d) the details of how the function is used by the b) may be called any number of times in a
client. program.
11. Here is a complete function that purports to return c) computed value is the return value
one of the roots of a quadratic given suitable d) call is an expression that has the type
parameters. It looks good but fails to compile. specified as the return type of the function
Why? and can be used anywhere any other
//returns one of the roots of the expression of that type could be used.
quadratic equation e) #include is all that is ever necessary to
//a*x*x + b*x + c = 0 a!= 0 provide access.
double root1 (double a, double b,
double c)
4. A call to a C++ function is
{ a) The name of the function followed by empty
return (-b + sqrt(b*b- parentheses.
4*a*c))/(2*a);
b) The name of the function is followed by any c) no more than 3 arguments
number of arguments, regardless of the d) exactly one argument
number of parameters in the definition. 9. Which of the following code fragments gives a
c) The name of the function followed by a random double value between 2 and 5
number of arguments not greater than the inclusive? You can assume that the random
number of parameters in the definition. number seed has been set and any needed
d) The name of the function followed by exactly definitions and initializations have been made.
the number of arguments as there are a) 3.0*rand() + 2
parameters in the definition. b) 3.0*(RAND_MAX-rand())/RAND_MAX
e) The name of the function only. + 2
5. A void function c) ((RAND_MAX-
a) performs some action and returns a value rand())/static_cast<double>(RA
b) performs some action but does not return a ND_MAX))*3 + 2
value d) (RAND_MAX-
c) is a statement rand())/static_cast<double>(RA
d) call is written much like a call to a value ND_MAX)*5 -2
returning function but is terminated with a e) rand()/static_cast<double>(RAN
semicolon. D_MAX)*2 + 3
e) A void function may have a return statement 10. , A definition of a variable outside any function is
but is not required to have one. called a
6. What do the calls to exit(…) do? When a) local function definition
exit(0) and exit(1) are called, what b) global variable definition
receives these arguments and what is done with c) global function header
them? d) global function definition
a) The exit( ) function stops the program. e) local variable definition
The argument is discarded. 11. Concerning return statements that functions
b) The exit( ) function is obsolete. There is can have:
no longer any such function in the C++ a) Value returning functions can have the
libraries. statement return computed_value;
c) The exit( ) function stops the program. b) void functions can have the statement
The argument is passed to the operating return void;
system which uses it for an error code. c) void functions must have a return;
d) The exit( ) function temporarily stops the statement, with no argument.
program, and sends the argument to the d) void functions may terminate using a
operating system which uses it for an error return; statement without an argument, or
code. The operating system restarts the they may have no return statement at all,
program after fixing the error. terminating by falling off the end of the
e) The exit( ) function allows the systems function block.
programmer to escape when the power supply 12. Where can you not declare a variable in a C++
catches fire. program?
7. Which of the following are not names of a C++ a) Within the parameter list of a function
library function: definition
a) abs b) Within the block of a void function.
b) sqrt c) Within the argument list of a function call
c) random d) Within a block nested within another block
d) floor e) Within the block of a value returning
e) labs function.
8. A void function can have 13. Which of the following statements about the
>>”void” is wrong font. definition and declaration of functions is not
a) no arguments correct?
b) as many arguments as the programmer wishes
a) Function declaration is exactly the same as a) returns 7*2
function prototype. b) returns 7+2
b) Function definition is exactly the same as c) returns 7!
function prototype. d) There is a syntax error in the program so it
c) A function header is exactly the same as won’t run.
function prototype except for the semicolon. e) It compiles but computes none of these.
d) A function definition is a function header 17. Correct statements of similarities and differences
followed by the function block. between calling user defined functions and library
e) A function header syntax is the following: functions are:
return_type function_name pare a) Similarity: A call to either requires the
nthesized_parameter_list function name followed by parenthesized
14. A semicolon does not (usually) go after these comma separated list of arguments that match
with the exception of the prototype in number and type.
>>”with the exception of” seems to make no b) Difference: Library functions require
sense here inclusion of header files for declarations but
a) while(condition) user defined functions do not require any
b) if(condition) declaration before use.
c) a function header to make it a function c) Similarity: Either library or user defined
declaration functions may be value returning or void
d) int main( ) functions.
e) an expression to make it a statement d) Difference: Library functions all provide a
15. In the function round of Display 3.6, which of return value, user functions must be void
these explains what happens in the body of the functions.
function? We reproduce the one line from the e) Difference Library function declarations
function body here: (sometimes definitions) come from
return #include <header> statements. User
static_cast<int>(floor(number+ functions require either explicit prototypes
0.5)); prior to use or a #include
a) This is overkill, it would be sufficient to use “user_header.h” for the prototypes
the floor function alone.
18. Suppose the function from Display 3.7 has the
b) Adding 0.5 to number pushes the range up so return statement removed. Which of the
floor can produce the correct rounding.
statements below regarding this change are
c) The static_cast<int> is used because correct? Why?
floor returns a double. If the double void iceCream(int number, double
value were returned, there would be at least a totalWeight)
warning of a double to int conversion in {
returning the value. if(number == 0)
d) This is wrong. The argument for the floor {
cout << “cannot divide among
function should be number-0.5. zero customers.\n”;
16. Given the function definition, which of the return;
following are correct? }
int func(int n, double d) portion = totalWeight/number;
{ cout << “Each one receives “
int j = n; << portion << “ ounces of
double sum = 0; ice cream.” <<endl;
while( j >= 0) }
{ a) The code will not compile.
sum += d; b) The code will compile and run. The output
-j;
}
will be unchanged.
return sum; c) The code will compile. For a zero number of
} customers the code would produce a divide
With arguments 7and 2.0 by zero run-time error.
d) The program will compile and run correctly double numTimes(int x)
for 1 or more customers. The error for zero {
number of customers could be repaired by double d;
d = NUM * x;
placing an else after the block belonging to
return d;
the if. }
19. Here is a small program. Which of the statements a) The variable x is a parameter in function
about the variables is correct? numTimes
#include <iostream>
b) The variable value is an argument in a call
const double NUM = 2.9345358;
double num = 3; to numTimes.
double numTimes(int x); c) The line double numTimes(int x);
int main( ) is a function definition.
{ d) The line return d; in the function
using namespace std; numTimes is necessary.
int value;
e) The lines of code are a function declaration:
cout << “Enter a value, I’ll
double numTimes(int x)
multiply it by “
<< NUM << endl; { . . .}
cin >> value; 21. Consider the following pair of functions. You are
cout << “You entered “ << value to assume that the ellipses (. . .) contain correct
<< “ NUM times this is “ code sufficient that it with the code you can see
<< numTimes(value) will compile apart from the two variables named
<< endl; sam in the two functions.
return 0; . . . .
} void func1( )
double numTimes(int x) {
{ int sam;
double d; . . .
d = NUM * x; }
return d; void func2( )
} {
a) NUM is a global variable. int sam;
b) num is a global constant. . . .
c) value is local variable in the main function }
int main( )
d) d is a local variable in the numTimes
{
function. func1();
20. Here is a small program. Which of the statements func2();
about this code is correct? . . .
#include <iostream> }
const double NUM = 2.9345358; a) This code will not compile because of the
double num = 3; multiply defined variable sam. In the
double numTimes(int x);
int main( ) explanation give the compiler message you
{ get on compiling.
using namespace std; b) This code will compile just fine.
int value; c) This code will compile but will give a runtime
cout << “Enter a value, I’ll error due to the multiply defined variable
multiply it by “ sam. In the explanation give the error
<< NUM << endl; message you get when this is run.
cin >> value;
d) This code compiles and runs without any
cout << “You entered “ << value
<< “ NUM times this is “ error messages. Explain why in the
<< numTimes(value) explanations.
<< endl; 22. What does it mean when we say a programmer
return 0; using a function should be able to treat the
} function like a black box?
a) This is meaningless. One must know how a 25. We studied the rand() and srand(int) library
function does its job to effectively use it. functions. The function rand() returns
b) One must be able to rely on the description of pseudorandom numbers. What does
the preconditions (requirements for use of the pseudorandom mean? What is srand for?
function) and the postconditions (promise of a) Repeated calls to the rand() function
behavior to use the function). returns a string of numbers that are mostly
c) If one codes an application that uses a different but they aren’t random in any sense.
function with a knowledge of the internal b) Pseudorandom numbers are things that
mechanism of that function, then when the resemble numbers but aren’t numbers.
function’s maintainer changes its internal c) In scientific use the word pseudo means
mechanism, the application programmer resembling. A pseudorandom number
could be faced with changing the application sequence resemble random number sequence,
code extensively. but are not strictly random.
d) The most efficient programs are the ones that d) The function srand(arg) is a helper
each function is designed to maximally use function for rand(). It must be called with
the internal behavior of every other function the same argument, just before rand(),
to speed up the code. every time rand() is called.
23. Assume this code fragment is embedded in an e) The function srand(arg) is the seeding
otherwise correct and complete program. What
function for rand(). Each call to s
should be the output from this code segment?
{ srand(arg) with a different argument
for( int i = 0; i < 10; i++) enables a sequence of calls to rand() to
{ generate a different sequence of
. . . pseudorandom numbers.
} 26. Given the following include directive (to get the
cout << i << endl; declaration for the pow function from the math
}
library):
a) 10 #include <math.h>
b) 9 Now make these declarations:
c) 0 double base = 2, exponent = 3,
d) The variable i is undefined in this scope, so power = 4;
this should not compile Which of the following are correct invocations
24. What is the difference between executing the for the pow function? If any of the following is
return 0; statement and its rough equivalent, correct, give the value returned or assigned, and if
a call to the exit(0); function, or the difference apparently incorrect, explain.
between return 1; and exit(1);? a) power = pow(base, exponent);
a) These are very nearly equivalent anywhere b) pow(power, base, exponent);
they are encountered. c) pow(base, exponent) = power;
b) These are very different if encountered in a d) base = pow(exponent, power);
function other than main();.The exit
function terminates the program, returning
control to the operating system, whereas
return only terminates the function,
returning control to the calling function.
c) These are very nearly equivalent when
executed in the main function. In main, these
both terminate main and send a success code
to the operating system, returning control to
the operating system.
d) Both these return control to the free store
manager by way of the exception controller,
sending the argument as an error code.

You might also like