CMPSC test 3
What happens if a value-returning function with the prototype: float Average( int, int, int); is called by using the following complete statement? (alpha and beta are int variables.) average(alpha, 34, beta); A) The compiler issues a syntax error message. B) The function is executed, and the function value is discarded. C) The function is executed, and the function value is assigned to alpha. D) The function is not executed, and the program halts with a run-time error message.
B
What is the output of the following code fragment? n = 1; while (n < 5) { cout << n << ' '; n++ } A) 1 2 3 4 5 B) 1 2 3 4 C) 1 1 1 1 forever D) 2 3 4 5 E) 2 3 4 5 6
B
When arguments are passed between the calling code and the called function, parameters and their corresponding arguments are matched by: A) Their data types B) Their relative positions in the parameter and argument lists C) Their names D) Whether they are inputs to or outputs from the function
B
An infinite loop is one in which the While expression always has the value false. A) True B) False
B.
what is the value of x after execution of the following code segment? int x=2; int c=0; while (C < 3 ) { x = x * 2; c = c + 1; } x = x + 1; A) 9 B) 17 C) 23 D) 33
B.
true or false
Boolean
In the following code fragment, the programmer mistakenly placed a semicolon at the end of the For statement heading. What is the result? cout << 'A'; for (count = 1; count <= 3; count++); { cout << 'B'; } cout << 'C'; A) A compile-time error B) An infinite loop C) The output ABC D) The output ABBC E) The output ABBBC
C
a set of common tools which can be used in any program. These predefined functions and classes represent a collection of useful, general purpose abstractions.
C++ Standard Library
After execution of the following code, what is the value of length? (count and length are of type int.) length = 5; count = 4; while (count ,= 6) { if (length >= 100) length = length - 2; else length = count * length; count ++; } A) 600 B) 100 C) 98 D) 20 E) None of the above.
C.
a statement, sentence, or phrase which describes a piece of code. Comments are ignored by the compiler and exist only to document the C++ code with a more natural language (e.g. English). In C++, single-line comments begin with //. Multi-line comments are bounded with /* and */.
Comment
the repeated execution of a statement or sequence of statements until a specified conditions becomes false (or, conversely, while the condition is true)
Conditional repetition
expressions which evaluate to one of two possible values: true of false
Conditions/Conditional Expressions
A variable (usually an integer) which keeps track of the number of loop iterations
Counter Variable
a loop whose number of repetitions is controlled by a counter. These loops always involve three components: initialization of the counter variable to a starting value; testing the counter variable for a terminating condition; and modifying the value of the counter variable to ensure an eventual end to the loop
Counter-Driven Repetition (loop)
Which of the following is the correct function header/heading for a parameterless function named "PrintStars"? A) void PrintStars B) void PrintStars; C) void PrintStars(); D) void PrintStars() E) void PrintStars( int n )
D
What is the value of "loop_count" after control exits the following loop: loop_count = 1; while ( loop_count <= 145 ) { alpha = alpha + 7; loop_count++; } A) 1 B) 144 C) 145 D) 146
D.
the "visibility" or "lifetime" of a variable. All variables have a defined area and time for which they are allowed to be used. In the case of C++ functions, this ________ is limited to the code statements within the beginning and ending brackets ({ }). The lifetime of these variables (i.e. the time from which memory is allocated by the system to the time in which the memory is given back) is the time for which the function is executing.
Scope
A value which signals a loop to "stop".
Sentinel
function parameters for which the arguments provided have their memory address passed to the function; changes to the parameters inside the function are made to the actual argument(s).
Reference Parameters
a special C++ statement that specifies the value that should be returned by the function; begins with the keyword "return".
Return Statemetn
What is the value of sum after execution of the following code? (All variables are of type int.) sum = 0; for (counter = 2; counter <= 5; counter++) { sum = sum + 2 * counter; }
28
Disregarding the possibility of name hiding, the scope of a local identifier extends from the point of declaration to the end of the block in which it is declared. A) True B) False
A
It is possible to supply different arguments every time a function is called. A) True B) False
A
Which of the following statements about value parameters is true? A) The caller's argument is never modified by execution of the called function. B) The parameter is never modified by execution of the called function. C) The caller's argument must be a variable. D) The caller's argument cannot have a Boolean value.
A
It is possible for the body of a While statement never to be executed. A) True B) False
A.
The body of a Do-While loop will always execute at least once, whereas the body of a For loop may never execute. A) True B) False
A.
a value that is provided in a function call to "fill in" a parameter. _______provide functions with the data necessary to complete its task.
Argument
is a block of memory storage that consists of multiple elements, all of the same data type. has one name, but the individual elements can be accessed on their own.
Array
A For loop is a posttest loop, whereas a While loop is a pretest loop. A) True B) False
B
In C++, a function definition may have another function definition nested within it. A) True B) False
B
Using global variables is better style than using local variables. A) True B) False
B
Value-returning functions can only be used to return numeric values. A) True B) False
B
Explain how range and reasonableness checking can help prevent integer overflow.
Range can help prevent integer overflow because it checks the numbers to make sure they are in the range of possible values. Reasonableness can help prevent integer overflow by checking that the numbers are reasonable.
a specific unit of code in C++ containing a header and a body. Can be "called" by other functions to perform tasks, and can "return" a value back to the "caller".
Function
the act of invoking a function. A ________ is made by using the function name and providing the function with any necessary arguments.
Function Call
The actual code that carries out the computation of the function; that part of the function which is enclosed in curly-braces.
Function body
the first line of a function, containing the function name, the parameter list, and the type of data returned by the function.
Function header
Explain the Simpson cartoon in the lab document. What is a "fat finger"?
Given the size of fingers relative to the size of keyboard keys, it is easy for users to incorrectly type in data. This incorrect data entry can lead to processing errors.
An integer which allows us to refer to each element of an array separately. start at zero and end at one less than the size of the array.
Index
Occurs when a conditional expression never becomes false, causing a loop to repeat forever. It appears to the user as a non-responsive or "hanging" program.
Infinite Loop
the "cycles" of a loop; in other words, the number of times the loop body is repeated.
Iterations
a variable declared within a set of { }, such as a function body. Local variables can only be used within that set of { }. Declaring a ____________ tells the C++ compiler that you are going to use it as temporary storage while computing the function value, and that the storage should go away - i.e. be returned to the pool of available storage - when execution of the function is complete.
Local Variable
In C++, a construct that repeats a series of statements.
Loop
the portion of a loop inside { } .
Loop Body
a value that a function expects to be "given" when the function is called. A function can have 0 or more ___________. These are placeholders that must be "filled in" by arguments when the function is called. As a result, ___________ can take on different values each time the function is called. __________act as local variables within the function body.
Parameter
a set of parameters, enclosed in parentheses within the function header. ______________ follow the function name in a function header. Each parameter is declared using a data type and a name, much like a variable declaration. The parameter declarations in the list are separated by commas.
Parameter List
function parameters for which the arguments provided have their value passed to the function; changes to the parameters inside the function have no effect on the actual argument.
Value Parameters
A basic C++ loop construct; compound statements which execute a statement or a series of statements as long as a specified condition is true.
While Loops
Refer back to the lab on integer errors. Can improper input validation lead to integer errors?
Yes, input validation can lead to integer errors. If you do not properly validate your input you can put yourself at risk for some potential integer errors. The amount of space allotted by the integer type determines the range of values that can be stored.
Accepting known good values is known as whitelisting. Rejecting bad values is known as blacklisting. Write the loop construct for whitelisting a body temperature. Write the loop construct for blacklisting a body temperature. Why is whitelisting much stronger?
cout << " Please enter your body temperature: " cin >> user_input while ( (body_temp >= 96.6 ) && ( body_temp < 100.4) ) { cout << body temperature is good } Blacklisting: cout << " Please enter your body temperature: " cin >> user_input (user input outside of range) while ( (body_temp >= 96.6 ) && ( body_temp < 100.4) ) { cout << "Invalid input- please try again " } Whitelisting is stronger because it gives you an actual answer when someone inputs the correct data. If something isn't within the data ranges it gives you an error message.
A conditional repetition control structure (e.g. the while statement) that causes a sequence of statements to be executed repeatedly is sometimes called a(n)
loop
The ____________ of an identifier is the region of program code where it is legal to reference that identifier.
scope
