<< Test Bank For Absolute Java 5th Edition by Walter Savitch | Test Bank For Young Family Focused Nursing Care By Denham Eggenberger >> |
Chapter 2 Test Questions
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 of 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.
Answer: True
Explanation: The one statement may be a block (statements that are enclosed with curly braces { }) or a simple statement.
int x = 0;
The following expression causes a divide by zero error:
(x !=0) || (2/x < 1);
Answer: False.
Explanation: The || operator uses short-circuit evaluation. The first member of this expression is true; the truth value of the complete expression can be determined from this; consequently, the second expression is not evaluated. There is no divide-by-zero error.
int x = -1, y = 0, z = 1;
This Boolean expression is correct and it does what the programmer intends.
x < y < z
Answer: False
Explanation: Unfortunately, the expression compiles without error and runs. The < operator associates (groups) left to right, so the expression evaluates as
(x < y) < z
The left hand expression evaluates to true, which, if compared to a numeric type, converts to 1. When compared to 1, this is false. What the programmer intends, expressed as mathematacs might is -1 < 0< 1, a result that is clearly true.
!time > limit
Answer: False.
Explanation: The expression always evaluates to false. This cannot be what the programmer intended. The compiler doesnt catch the problem because the code is legal, correct C++. Corrected code is !(time > limit)
Code execution proceeds as follows: The operator ! takes a bool argument. It returns the opposite bool value. The value of time is converted to a bool. The value of time is certainly nonzero, hence !time is !true, i.e., false. The > compares this result with a numeric value, limit, (an int or perhaps some kind of floating point). The value on the left (false) is converted to a 0 value of that type. The value of limit is unlikely to be a negative number and we are concerned about time running out, so it is unlikely that time is zero. Consequently, the inequality becomes 0>limit, where limit is nonzero. This is false.
(count == 0)&&(limit < 20)
Answer: true
count == 0 && limit < 20
Answer: true
Explanation: The operators == and < have higher precedences than &&, hence the expressions, count == 0 and limit < 10 are evaluated (to true) then the && is executed.
(count != 0)||(limit < 20)
Answer: true.
Explanation: The first expression evaluates to false, the value of the || expression is determined by the second expression. The second expression is true so the || expression evaluates to true.
Answer: true
Answer: False
Explanation: The continue statement causes the Boolean_Expression to be executed. If true, the body executes, otherwise the loop terminates.
Answer: False.
Explanation: In addition to its use in loops, a break statement is used in the switch statement to transfer control to the next statement after the switch block.
Answer: False
Explanation: A break or continue terminates or restarts only the innermost loop containing the break or continue.
Answer:
// double first, second;
// these have been initialized
if (!(first < second))
{
double temp = first;
first = second;
second = temp;
}
//assert: first <= second
Answer:
if (boiler_pressure > 1000)
cout << Boiler Pressure: TOO LOW ;
else if (boiler_pressure < 100)
cout << Boiler Pressure: TOO LOW ;
else
cout << Boiler Pressure: within normal limits. ;
if the numeric grade is not less than 90, the letter grade is an A,
if the numeric grade is not less than 80, the letter grade is a B,
if the numeric grade is not less than 70, the letter grade is C,
if the numeric grade is not less than 60, the letter grade is D,
otherwise the letter grade is F.
Answer:
if (numeric_grade >= 90)
letter_grade = A;
else if (numeric_grade >= 80)
letter_grade = B;
else if (numeric_grade >= 70)
letter_grade = C;
else if (numeric_grade >= 60)
letter_grade = D;
else
letter_grade = F;
//double first, second, max
//first and second have been initialized
if ( (first < second) )
max = second;
else
max = first;
//assert: max >= first && max >= second
if the numeric_grade is not less than 90, the letter_grade is an A,
if the numeric_grade is not less than 80, the letter_grade is a B,
if the numeric_grade is not less than 70, the letter_grade is C,
if the numeric_grade is not less than 60, the letter_grade is D,
otherwise the letter_grade is F.
Answer:
int value = numeric_grade/10;
switch(value)
{
case 9: letter_grade = A;
break;
case 8: letter_grade = B;
break;
case 7: letter_grade = C;
break;
case 6: letter_grade = D;
break;
default: letter_grade = F;
break;
}
Answer:
Answer:
Answer: This remark means that if you supply a program with bad data, you are guaranteed useless results from your program.
Answer: max = (n1>n2) ? n1 : n2;
Explanation: The parentheses are present only for readability. That the > operator has higher precedence makes the parentheses unnecessary.
cout << X;
cout << endl;
cout << X;
while ( y != y );
cout << endl;
while (i < 9)
{
cout i;
i++;
}
cout << endl;
do
{
cout << c << ;
c = c + 2;
} while ( c <= M )
cout << endl;
while (i < 50)
{
if ( i < 20 && i != 15 )
cout << X;
i++;
}
cout << endl;
Answer:
The output of each loop is:
x = 10;
do
{
cout << x << endl;
x = x 3;
} while ( x > 0 );
Answer: A simple minded change from do while to while and insertion of the loop body gives this:
x = 10;
cout << x << endl;
x = x 3;
while ( x > 0 )
{
cout << x << endl;
x = x 3;
}
A look at the code suggests that the following is somehow smarter
x = 10;
while ( x > 0 )
{
cout << x << endl;
x = x 3;
}
Answer:
#include
//loop to accept 10 integers and sum
int main()
{
using namespace std;
int x, sum = 0;
cout << Enter 10 integers, each followed by
<< I will give the sum. << endl;
for(int i =0; i < 10; i++)
{
cout << Enter integer << i << : ;
cin >> x;
sum = sum + x;
}
cout << sum: << sum << endl;
return 0;
}
Answer:
// file ch2nu36.cc
// test question 36 for chapter 2
>>Delete above comment
#include
using namespace std;
//loop to accept positive integers until nonnegative
//integer, then return sum
int main()
{
int x = 1, i = 0, sum = 0;
cout << Enter positive integers, followed by
<< 0 or negative stops. << endl
<< I will give the sum. << endl;
while ( x > 0 )
{
cout << Enter integer << i << : ;
cin >> x;
sum = sum + x;
i++;
}
cout << sum: << sum << endl;
return 0;
}
Answer:
int n = 1;
while(n <= 10)
{
int m = 10;
while(m>=1)
{
cout << n << times << m
<< = << n*m << endl;
m;
}
n++;
}
Answer: The output is a multiplication table
1 times 10 = 10
1 times 9 = 9
. . .
1 times 1 = 1
2 times 10 = 20
. . .
2 times 1 = 2
3 times 10 = 30
. . .
3 times 1 = 3
4 times 10 = 40
4 times 9 = 36
. . .
4 times 2 = 8
4 times 1 = 4
. . .
9 times 10 = 90
. . .
9 times 1 = 9
10 times 10 = 100
. . .
10 times 1 = 10
while(i<=10)
{
if (i<5 && i !=2)
cout << X;
i++;
}
while(i<=10)
{
cout << X;
i = i + 3;
}
do
{
cout << X;
n = n + 100;
}while(n < 1000);
Answer:
if (i<5 && i !=2)
cout << X;
cout << X;
cout << X;
There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.
Answer: a), c)
Explanation:: b) repeats 1 or more times, d) and e) are selection statements
Answer: c) This expression is always true. The value does not depend on x. This is the mathematicians shorthand for what the programmer probably meant:
(1 < x)&&(x < 10)
Explanation: The < operator associates (groups) left to right, so the expression evaluates as (1 < x) < 10. The expression (1 < x) evaluates to either false or true, regardless of x. These bool values convert to int values 0 or 1 when compared to int value 10. The expression evaluates to true for all values of x.
Answer: a), c), and d).
Explanation: b) is a wrong number of argments, e) is the OR command.
Answer: a) c) d) and e)
Explanation: b) is a wrong number of operands
Answers: d)
Answer: a), b), c) and d)
Explanation: c) is correct too, though strange. Here is one solution:
#include
using namespace std;
int main()
{
int x , z = 10, w =-10;
bool y = true;
while( y || (x = w))
{
while (y)
{
x = z;
break;
}
break;
}
cout << x << endl;
y = false;
while( y || (x = w))
{
while (y)
{
x = z;
break;
}
break;
}
cout << x << endl;
return 0;
}
/* Output is:
10
-10
*/
Answer: a), c), and d) e) F is coerced char to bool, perhaps through an intermediate type
Answer: b)
// Display the sum of all entered int values
// greater than 5
#include
int main()
{
using namespace std;
int x, sum;
while (x < 10)
{
cin >> x;
if (x > 5);
sum = sum +x;
}
cout << The sum is values > 5 is << sum << endl;
}
Answer: c) and d) are the errors. (Perhaps the user should have been prompted.)
Answer: b)
Answer: d)
Answer: all, a), b), c), and d), are correct.
Answer: a) b) c).
Explanation: d) is wrong, the correct statement is: A comma expression has value equal to the value of the last expression in the list.
Answer: a) e) are correct.
Answer: a) c) d) are correct
int a = 3, b = 2, c = 5
if (a > b)
a = 4;
if ( b > c)
a = 5;
else
a = 6;
cout << a < endl;
Answer: d)
Explanation: The else belongs to the second if, despite indentation suggesting otherwise. Consequently, the first if statement executes, a is assigned the value 4, the second if executes, b is 2, c is 5, so b > c, evaluates to false so the else clause executes, so that a is assigned the value 6.
a = b;
else
b = a;
a = b;
else;
b = a;
a = b;
else
b = a;
a = b
else
b = a;
a = a / x
Answer: c) is correct and is likely to be the programmers intent. b) compiles but is unlikely to be the programmers intent.
Explanation:
iii. what compiler error will the rest generate?
Assume all variables have been declared, but not necessarily initialized.
while (-1 != n)
{
sum = 0;
sum = sum + n;
}
while ( value != -1 )
sum = sum + value;
cin >> value;
int i = 1,
>>Semicolon not comma
while ( i < n );
sum = sum + i;
i++;
do
count++
while ( count * count > limit );
do
x++;
while( x > x );
Answer:
x = 0;
if (x = 12)
yes_statement;
else
no_statement;
Answer: d)
Explanation: The expression x = 12 has the value 12, which is converted to true, causing the if always to execute the yes_statement.
Answer: d)
Explanation: The other control constructs operate on a single statement, which may be, but is not required to be, a compound statement.
Answer: b)
Explanation: The precedences, higher to lower are: +, >, ==, &&. So the order is j+1, j>0, then (j+1)==10, then (finally) the ((j>0) && ((j+1) == 10))
Answer: c) is perhaps best and easiest, but a) and d) are reasonable if you arent taking an exam.
if(condition) yes_clause; else no_clause;
Under which of the following circumstances will both the yes_clause and the no_clause will be executed?
Answer: d)
Answer: a) c) and d)
Answer: a) b) and d)
Answer: b) d) and e)
Answer: b) d) and e)
int x = 10;
while (x > 0)
{
cout << x << ;
x = x + 3;
}
cout << endl;
Answer: a) and c)
Answer: b) Control constructs may be nested arbitrarily.
sum = sum +x;
x = 3
else
x =4;
Answer: a) Not wrong, but the semicolon is probably an intent error. b) Semicolon missing in the yes_clause. c) The = probably should be ==. d) needs parenthese to be correct C++. e) C++ does not use then as a keyword. If we blindly fix the syntax, whatever the then might be, there needs to be a semicolon after it, and the sequence then; x=3; should probably be in curly braces. Actually, there no way to guess what the programmer might mean here. There is a good chance the programmer is a refugee from Pascal, and then should be deleted.
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 of 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.
An explanation is required.
Answer: False.
Explanation: There is also call-by-reference. Call-by-value makes a copy of the argument then uses that, hence does not make changes to the argument. Call-by-reference effectively passes the argument hence can make changes to the argument.
Answer: False
Explanation: The character is an ampersand, &, not a $. It is placed between the type and the parameter to indicate a call-by-reference parameter.
Answer: True
Explanation: It makes no difference whether the ampersand appears next to the type or the variable.
void example3(double &ref_to_dbl);
void example3(double& ref_to_dbl);
//In a call to this function ref_to_dbl becomes a
// reference to double that refers to the argument.
Answer: True. The value parameter is essentially a local variable initialized to the value of the argument at the time of a call.
Answer: False. The reference parameter is an alias for the callers argument. The mechanism is to pass the address of the argument which is used where the parameter occurs. Hence, a reference parameter makes available to the function the initial and ongoing values of the callers argument as well as providing a mechanism for the function to change the callers argument.
Answer: False.
Explanation: Functions that call other functions can be tested independently of the called functions by writing stub functions. Stub functions are very simple functions that do nothing much more than sending just enough data to the caller to allow testing..
Answer:. Functions that call other functions can be tested independently of the called functions by writing stub functions. Stub functions are substitute functions that are as simple as possible yet send data enough back to the caller to allow testing of the caller.
Answer: False
Explanation: There is no connection between the return mechanism and the parameter passing mechanism.
Answer: True
Explanation: Technically true, nevertheless for the following reasons, names should be selected with care. (I accept an answer of false from a student who provides a good explanation.)Functions should be self-contained modules that are designed separately from the rest of the program. On large projects, different programmers may be assigned to write of different functions. The programmer must choose meaningful names else the fellow programmers will have more difficulty than necessary in understanding the code. That several functions may have the same parameter names is of no matter.
Answer: False
Explanation:. The compiler compiles what the programmer writes, not the intent.
void func(int x){/**/}
int func(double x){/**/ return something_double;}
Answer: True
Explanation: C++ allows function name overloading. C++ distinguishes the function by inferring a list of types from the argument list then matching this to lists of parameter types for the specified function name. This ignores the issue of coercion (automatic type conversions).
void func(double x){/**/}
int func(double x){/**/ return something_double;}
Answer: False
Explanation: C++ does not distinguish functions based solely on the return value.
void func(double &x){/**/}
void func(double x){/**/}
Answer: False
Explanation: C++ does not distinguish functions based solely on call-by-value vs. call-by-reference.
Answer: False
Explanation: Whether a given parameter is call-by-value or call-by-reference depends on whether there is an ampersand present. It is perfectly legitimate to mix call-by-value and call-by-reference parameters.
Answer: False
Explanation: The text says [Default arguments] do not make sense for call-by-reference parameters. Suppose the function writes to the parameter. What does the parameter cause to be changed in the caller?
Answer:
//precondition: arguments arg1, arg2 are initialized
//postcondition: arg1post == arg2pre && arg2post == arg1pre
void swap( int & lhs, int & rhs)
{
int temp = lhs;
lhs = rhs;
rhs = tmp;
}
Answer: A call-by-value parameter is a local variable initialized to the value of the argument, which is computed at the time of the call.
Answer:
//file: testProblem.cc
//illustrates value parameters in a void-function,
//fetching values, and the calling of that function
//to multiply and display arguments.
#include
using namespace std;
void func(int x, int y)
{
cout << x * y << endl;
return;
}
int main()
{
int f, g;
cout << Enter two integers, Ill return the product.
<< endl;
cin >> f >> g;
func(f, g);
return 0;
}
void func(int& x, int& y)
{
int t = x;
x = y;
y = t;
}
Answer: Nothing. The function has no effect at all.
Explanation: With the parameters changed from reference to value, the function can do nothing. There is no effect external to this function. There are no changes are made to the callers arguments. The only changes are made to local variables and value parameters.
Answer: The main function is tested with stub functions replacing each` function. Stubs should be small enough that their correctness is assured. Stubs return enough information to let the main function compile and run, so that it can be tested. Then test the main function. Having done this, each function in turn can be tested.
An alternative is to test the functions with driver functions. Drivers are functions that call the function to be tested. Drivers, like stubs, should be simple enough that their correctness is assured. Then the function is called and tested. Clearly the assumptions of small enough that correctness is assured are really questionable. However, this procedure is better than writing a 200 line program or worse a 2000 line program and then testing it.
Answer:. Drivers are programs that call a function to be tested. Drivers should be simple enough that their correctness is assured.
Answer: A stub is a small function that replaces another as yet untested function. The stub allows the caller to be tested without the added complexity of the untested function.
double root( double a, double b, double c, int i);
// Precondition: a != 0 and a, b, c are coefficients of
// a quadratic equation a*x*x + b*x + c = 0 The value
// of i is either +1 or -1 to choose which root.
// Postcondition: return value, x, satisfies the
// equation a*x*x + b*x + c = 0
Answer:
#include
// stub functions example
double root(double a, double b, double c, int i)
{
using namespace std;
cout << executing function
<< double root(double, double, double)
<< endl;
return 2.0;
}
Hint: This is very short.
double yield (double pressure,
double density, double temp);
// Precondition: pressure is newtons per square meter
// density is in kilograms per cubic meter
// temp is in degrees Celcius
// Postcondition: Return value is the relative yield of
// a chemical process.It is a number between 0 and 1.
// 0 means no output and 1 means ideal yield.
Answer:
// THIS IS JUST A STUB
double yield (double pressure,
double density, double temp);
{
return 0.25; // Not correct, but
// sufficient for testing
}
double unitPrice(int diameter, double price);
double unitPrice(int length, int width, double price);
Suppose we are faced with the need for the unit price on a square pizza. The problem here is to devise in a natural way to overload unitPrice to compute the price per square inch of a square as well as a round pizza?
Answer: There is no nice way to do this. A simple minded approach is to write
double unitPrice(int edge, double price);
This attempt involves having two overloaded unitPrice function versions both of which have argument lists with types int and double, which results in an ability.
A student might overload using two doubles for arguments:
double unitPrice(double edge, double price);
This leads to the problem of remembering always to use a double for the size (edge length) of a square pizza and an int for the size (diameter). This tends to be error prone.
We are faced with writing a function with a different name. (Of course, the easiest way to solve the problem of finding a unit price for a square pizza is to call the function for rectangular pizza, passing the edge size to the length and the width parameters, but that begs the question of this test question: how to overload in a natural way.)
Answer: A functions signature is the functions name with the sequence of types as listed in the parameter list, including any const keywords, but not ampersands.
Answer:
void zeroBoth(int & n1, int& n1)
{
n1 =0;
n2=0;
}
There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.
void One( int first, int & second )
{
first = 17;
second = first + 1;
}
int main()
{
// other code
int j = 4;
int k = 3;
One(j, k);
// other code ..
}
After the call to One(j, k); what are the values of j and k? Why?
Answer: c) j == 4, k == 18
Explanation: The first parameter is called by value, so the parameter first is assigned 4 by the call. The parameter first is immediately assigned in the function, but the first argument is not changed, hence, j==4.
The second parameter is call-by-reference, so when first + 1 is computed to be 18, and is assigned to second, second is changed, and the corresponding argument, k is changed, hence, k==18.
#include
using namespace std;
void func ( int& x, int & y)
{
int t = x;
x = y;
y = t;
}
int main()
{
int u = 3; v = 4;
//
cout << u << << v << endl;
func ( u, v )
cout << u << << v << endl;
//
}
3 3
4 3
3 4
4 4
Answer:. b)
Explanation: The code swaps the values stored in the callers arguments
#include
using namespace std;
void func(int & x, int & y)
{
int t = x;
x = y;
y = t;
}
int main()
{
int u = 3; v = 4;
//
cout << u << << v << endl;
func ( u, v )
cout << u << << v << endl;
//
}
3 3
4 3
3 4
4 4
Answer: a)
Explanation: The first argument is not changed by the function. The second argument is changed to 3.
g(1,2);
Answer: c)
Explanation: The match is exact.
g(1.0,2.0);
Answer: d)
Explanation: The compiler tries to find an exact match, and there is none. Then the compiler tries to find a match using automatic type conversion. Part c) is excluded because use would result in two double to int conversions. The compiler says there is an ambiguity between a) and b).
Answer: a) and c)
Explanation: C++ allows default arguments. If a default argument is provided for a parameter, all parameters to the right of this must be provided with default arguments.
Answer: All of these except a) and b) are true.
Explanation: a) is way too restrictive. b) is optimistic beyond dreams. An assertion may well be just a comment, but can be much more, as c)-e) state. Assertions are used like this: If you know that some of variables in your program should have a specific values, or these variable should make an expression take on a value you know, you can write this out as a Boolean expression (as an assertion) and test it.
Answer: b) and c) are correct.
Explanation: a) is wrong. (And horribly pessimistic.) Easy options exist as pointed out in b) and c) to prevent the macros from being compiled into your code.
Answer: b) d) and e) are correct.
Explanation: a) is unreasonably pessimistic. The stubs in b) should be short enough that their correctness may reasonably assumed, hence c) is unreasonable, if possible. In d) we add just one function at a time, and may need to change other stubs to vary data passed to successive functions. If inconvenient to have a main program to drive the function, a driver that provides typical data to a call is an alternative.
Answer: b) and c)
Explanation: c) is necessary to provide access to the macro definition so that you can say assert(x==y), and b) is necessary for turning these off without an error prone edit session.
Answer: a) c) d) are correct.
Explanation: a) you may, but you dont have to have default arguments. b) is clearly wrong, there is no mandate that all value parameters have default arguments. If there are reference parameters mixed in, as in c) then value parameters to the left of the reference parameter cannot have default arguments. d) follows from the rules that reference parameters cannot have default arguments and that if any variables are missing default arguments, variables to the left may not have them either. Part e) is clearly wrong. Default arguments for reference parameters make no sense.
Answer: b) d) e) are correct.
Explanation: This testing technique is essentially divide and conquer. You avoid testing a large program all at once, allowing the testing of the i/o routines, for example, with drivers to assure that they work properly.
Answer: a) d) e) are correct.
Explanation: b) a call-by-value formal parameter is a local variable that is initialized to the value of the argument at the time of the call. c) a call-by-reference parameter is modeled on a substitution mechanism. The behavior is as if the parameter were substituted in the body the function wherever the parameter is used. Fetching the value of the parameter fetches the value of the argument, assigning the parameter assigns the argument.
Answer: b) and d)
Explanation: c) is wrong, as is e). In e) the correct answer is C++ uses automatic type conversions to find a match between the list of parameter types and the list of argument types
Answers: b) d) and e) are correct.
Explanation: a) should be: Formal parameters are listed between parentheses following the function name in the function header. b) is correct. c) should be: Arguments appear in a function call between parentheses following the function name. d) and e) are correct.
void doStuff(int parValue, int& parRef)
{
parValue = 100;
cout << parValue in call to doStuff =
<< parValue << endl;
parRef =222;
cout << parRef in call to doStuff =
<< parRef << endl;
}
and consider the call, which we assume is in a complete and correct program
int n1 = 1, n2 =2;
doStuff(n1, n2);
Answer: b) is correct
Explanation: The parameter parRef is a reference parameter, the argument corresponding to this is n2. The function doStuff contains an assignment, parRef = 222; The function doStuff behaves the assignment n2 = 222; were executed.
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
int local = lhs;
lhs = rhs;
rhs = local;
Answer: d) and e) are the answer.
Explanation: a) is argumentum ad baculum argument by the stick. Shout it loud and it is the truth.. b) is incorrect, both parameters are call-by-reference. c) reversing the order of the assignments does not fix the fundamental problem of irretrievably destroying data.
void doStuff(int parValue, int parRef)
{
parValue = 100;
cout << parValue in call to doStuff =
<< parValue << endl;
parRef =222;
cout << parRef in call to doStuff =
<< parRef << endl;
}
and consider the call, which we assume is in a complete and correct program
int n1 = 1, n2 =2;
doStuff(n1, n2);
cout << n1 after call to doStuff = << n1 << endl;
cout << n2 after call to doStuff = << n2 << endl;
Once the order is placed, the order will be delivered to your email less than 24 hours, mostly within 4 hours.
If you have questions, you can contact us here