addition to the true/false response, and, if false, also require a correction.
True False:
An explanation is required.
- Every programmer must know all the details of what that programmer’s team mates are doing in their projects to do the work assigned. Why?
Answer:. False
Explanation: Only the client programmer needs to know the use to which a function is put. Only the author of the function needs to know the internals. The only thing these two share is the contract or specification of the function.
- Procedural abstraction involves information hiding in that only the ‘contract’ between the programmer using the function (the client) and author of a function is known to either.
Answer: True.
Explanation: Under procedural abstraction, the author (or programmer) of a given function should know about how the application programmer (or the client) will use the function is IN the ‘contract.’ The contract is what the function is to do, and the conditions set on the parameters to guarantee that the function can do IT. The author should not need not know how the function is to be used by the client programmer. The client programmer should know only only needs to know what the function does, the requirements on the parameters, but not how the function carries out the task. This way each can do his job independently of the other.
- Code after a return or a call to the exit function is executed will not be executed.
Answer: True. Control returns to the caller if return is executed out of a called function, and returns to the operating system if return is executed out of main. If exit is executed anywhere, the program is terminated and control is returned to the operating system.
- A sequence of calls to the library function rand() generates mathematically correct random number sequences.
Answer: False.
Explanation: The sequence is called pseudorandom because it appears to be random. The results are good enough to obtain usable answers with computer simulation.
- It is legal to replace the prototype
double totalCost(int numberParam, double priceParam);
with the more terse, alternate form
double totalCost(int, double);
Answer: True.
Explanation: The alternate form applies only to function declarations. You will see this form in manuals. It is possible to omit variable names that will not be used in function definitions, but the text does not do this.
- In C++ Boolean value are represented only with the int values 0 for false and 1 for true.
Answer: False.
Explanation: In C++ the bool type is a real type, with values true and false, but 0 is interpreted as true and 0 as false, and true and false will be converted to 1 and 0 respectively.
- Extensive use of global variables is a satisfactory replacement for the difficulties of parameter passing with functions.
Answer: False.
Explanation: Global variable use ties functions together so tightly that it is impossible to understand any one function’s behavior apart from the whole of the program. This complexity makes understanding a program very difficult.
- A variable declared outside any function is said to be a local variable.
Answer: False.
Explanation: This is a global variable.
- A variable declared within a function block is said to be local to the function.
Answer: True
Explanation: Such a variable is said to be local to the function or to have the function as its scope, and is also known as a local variable if the scope is clear from the context.
- Consider two blocks, one within another. If an identifier is declared as a variable in the inmost of these two blocks, one can access this variable from the outer block.
Answer: False:
Explanation: The scope of a local variable extends from the definition to the end of the block in which the variable is defined. This excludes the outer block.
- Consider two blocks, one within another. C++ prohibits an identifier to be declared as a variable in each of these blocks.
Answer: False
Explanation: Without the inner variable, the scope of the outer variable would be the entire outer block, including the inner block. The variable with the inner block for its scope shadows the outer variable’s scope, making a hole in the scope of the outer variable.
Chapter 2 C++ by Savitch
Which of the following is a valid identifier?
- three-com
- 3com
- 3_com
- three_com
It is legal to declare more than one variable in a single statement.
true
Which of the following statements is NOT legal?
- char ch='b';
- char ch='0';
- char ch=65;
- char ch="cc";
char ch="cc";
Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myFloat?
- cin >> myFloat;
- cin << myFloat;
- cin >> "myFloat";
- cin >> myFloat >> endl;
cin >> myFloat;
<< is called the stream ________ operator.
insertion
What is the output of the following code?
cout << "This is a \\" << endl;
This is a \
What is the value of x after the following statements?
double x;
x = 0;
x += 3.0 * 4.0;
x -= 2.0;
10.0
What is the value of x after the following statements?
float x;
x = 15%4;
3.0
What is the value of x after the following statements?
int x;
x = 15/4;
3
The opposite of (x >3 && x < 10) is (x < 3 && x > 10)
False
The body of a while loop may never execute
True
A loop that always executes the loop body at least once is known as a ________ loop.
do-while
Executing one or more statements one or more times is known as
- selection.
- iteration.
- sequence.
- algorithm.
iteration
Given the following code fragment and the input value of 2.0, what output is generated?
float tax;
float total;
cout << "enter the cost of the item\n";
cin >> total;
if ( total >= 3.0)
{
tax = 0.10;
cout << total + (total * tax) << endl;
}
else
{
cout << total << endl;
}
2.0
Given the following code fragment, what is the final value of y?
int x, y;
x = -1;
y = 0;
while(x < 3)
{
y += 2;
x += 1;
}
8
Given the following code fragment, which of the following expressions is always TRUE?
int x;
cin >> x;
- if( x < 3)
- if( x==1)
- if( (x / 3) >1 )
- if( x = 1)
if( x = 1)
What is the final value of x after the following fragment of code executes?
int x=0;
do
{
x++;
}while(x > 0);
a. 8
b. 9
c. 10
d. 11
e. infinite loop.
infinite loop
Every line in a program should have a comment. T/F
False
Which of the following will have infinite loop?
a - int x = 0;
do{
x++;
}while(x > 0);
b - int x = 1;
do{
x--;
}while(x > 0);
c - int x = 0;
while(x > 0);{
x++;
}
d - int x = 0;
while(x >0){
x++;
}
a - int x = 0;
do{
x++;
}while(x > 0);
Another way to write the value 3452211903 is
3.452211903e09
3.452211903e-09
3.452211903x09
3452211903e09
3.452211903e09
Chapter 5 C++ by Savitch
A function that does not return a value is known as a ________ function.
void
The following is legal in a void function.
return 0;
(True/False)
False
In a function with call-by-reference parameters, any changes to the formal parameters will change the actual arguments passed to the function.
(True/False)
True
It is illegal to call other functions, which will return value, from inside a void function definition.
(True/False)
False
A ________ is a main program that only checks that functions execute correctly.
driver
A stub is a function that is completely defined and well tested.
(True/False)
False
Testing your program should be done
A) only if your instructor requires it.
B) as each function is developed.
C) at the end of the coding.
D) only if there appear to be problems.
as each function is developed.
The values or variables listed in the function declaration are called ________ parameters to the function.
formal
Pre and post conditions for a function should be written (before/after) the function definition is written.
before
When a void function is called, it is known as
A) an output function.
B) a returned value.
C) an executable statement.
D) a comment.
If a function needs to modify more than one variable, it must
A) be pass by value.
B) be a void function.
C) return all values needed.
D) be a call by reference function.
be a call by reference function
Given the function definition
void something ( int a, int& b )
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
what is the output of the following code fragment that invokes something?
(All variables are of type int.)
r = 1;
s = 2;
t = 3;
something(t, s);
cout << r << ' ' << s << ' ' << t << endl;
1 14 3
What is the output of the following code fragments?
int trial( int& a, int b)
{
if(b > a)
{
a = b;
return -a;
}
else
{
return 0;
}
}
float x = 0, y= 10,z;
z = trial(y,x);
cout << z << " " << x <<" " << y << endl;
0 0 10
If you write a function that should use call-by-reference, but forget to include the ampersand
A) the program will not compile.
B) the program will not link.
C) the program will not run without a run-time error.
D) the program will run with incorrect results.
E) it doesn't matter.
the program will run with incorrect results.
Which of the following are true:
A) A function can call another function.
B) As long as the function is defined anywhere in your program, it can be used anywhere else.
C) A function definition can contain another function definition.
D) If you have function prototypes, the order in which you define your functions is important.
A function can call another function.
If you need to write a function that will compute the cost of some candy, where each piece costs some cents, and tax rate is constant 8.5%, which would be an appropriate function declaration?
A) double calculateCost(int count, int cents_per_candy);
B) int calculateCost(int count, int cents_per_candy);
C) int calculateCost int count;
D) int calculateCost(char name);
double calculateCost(int count, int cents_per_candy);
If you were to write a function for finding the maximum value of two given integers, which function prototype would be most appropriate?
A) void max(int x, int y);
B) int max(int x, int y, int z);
C) double max(int x, int y);
D) int max(int x, int y);
int max(int x, int y);
What does the following function do:
double max(int x, int y, int z) {
if (x < y) {
if (y < z) return z;
else return y;
}
else {
if (x < z) return z;
else return x;
}
}
A) there is a logical error
B) it actually return the minimum value among x, y, and z although the function name is max
C) returns the maximum value among x, y, and z
D) there is a syntax error
returns the maximum value among x, y, and z
0
1181