Test Bank Problem Solving and Program Design in C 8th Edition by Jeri R. Hanly, Elliot B. Koffman

$20.00

Description

INSTANT DOWNLOAD WITH ANSWERS AFTER PURCHASED

 

Sample Chapter

Chapter 5   Repetition and Loop Statements

 

 

True/False

 

  1. The loop repetition condition of a for statement is tested at the end of each pass.                                                                       [False]

 

  1. The body of a while statement must cause the loop repetition condition to become false after a finite number of passes to prevent an infinite loop.                                                                                      [True]

 

  1. In counting loops, the counter must be initialized to zero before execution of the loop body begins.

[False]

 

  1. The loop repetition condition of a while or for statement can be false before the loop begins to execute.                                                                                      [True]

 

  1. Loop counter variables are usually of type double.                                                                                      [False]

 

  1. The body of a for statement might not be executed at all.                                                                                [True]

 

  1. Tables are always displayed using counter-controlled while loops.                                                                                      [False]

 

  1. The update expression in a sentinel-controlled for loop usually involves a call to scanf or fscanf.

[True]

 

  1. The sentinel value is always the last value added to a sum being accumulated in a sentinel-controlled loop.                                                                                [False]

 

  1. The loop that follows displays 29 lines of output.

 

for  (i = 0;  i < 30;  i = i + 1)

printf(“%d\n”, i);                                                [False]

 

  1. In a for statement, you may only increment or decrement the loop counter by one. [False]

 

  1. A counting loop with a control variable of type double will not always execute the same number of times on different computers. [True]

 

  1. A for statement cannot be used to implement a sentinel-controlled loop. [False]

 

 

Multiple Choice

 

Questions 1-4 refer to the following program segment.  Assume that all variables are of type int.

 

z = 0;

g = 0;

s = 0;

i = 0;

while (i < 50) {

scanf(“%d”, &t);

s = s + t;

if (t >= 0)

g = g + 1;

else

z = z + 1;

i = i + 1;

}

 

  1. How many times is the loop body of the while statement executed?

 

  1.    once
  2.    never
  3.    49 times

*d.   50 times

  1.    until a number 50 or larger is entered

 

  1. The value stored in variable s at the end of the execution of the loop could best be described as __________.

 

  1.    the average of the numbers scanned

*b.   the sum of the numbers scanned

  1.    the largest of the numbers scanned
  2.    how many numbers were scanned
  3.    the sentinel value

 

  1. The value stored in variable z at the end of the execution of the loop could best be described as __________.

 

  1.    how many positive items were scanned
  2.    the sum of all positive items scanned

*c.   how many negative items were scanned

  1.    the sum of all negative items scanned
  2.    the sentinel value

 

  1. The loop can best be categorized as a __________.

 

*a.   counter-controlled loop

  1.    sentinel-controlled loop
  2.    loop that computes a product
  3.    general conditional loop
  4.    none of the above

 

  1. A special value that marks the end of a list of input data is called a __________.

 

  1.    terminal value

*b.   sentinel value

  1.    loop control value
  2.    input value
  3.    loop termination value

 

 

  1. A C programmer should not use an expression like the one that follows because ______.

 

n = i + x * ++i;

 

*a.   the expression may produce different results on different ANSI C compilers

  1.    it is illegal to have two consecutive operators
  2.    the ++ operator cannot be used in combination with other arithmetic operators
  3.    the increment operator cannot be used in the same expression as the addition operator
  4.    none of the above

 

  1. The expression

 

x *= i + j / y;

 

is equivalent to ______.

 

  1.    x = x * i + j / y;
  2.    x = (x * i) + j / y;
  3.    x = (x * i + j) / y;

*d.   x = x * (i + j / y);

  1.    none of the above

 

  1. In a program that uses an endfile-controlled loop to compute product of a list of numeric input data, the product variable must be initialized to __________.

 

  1.    0

*b.   1

  1.    -1
  2.    the EOF value
  3.    none of the above

 

  1. How many lines of output will be displayed by the following program fragment?

 

i = 0

do {

for  (j = 0;  j < 4;  j = j + 1)

printf(“%d\n”, i + j);

i = i + 1;

} while (i < 5);

 

  1.    0
  2.    7
  3.    9
  4.    16

*e.   20

 

  1. How many lines of output will be displayed by the following program fragment?

 

for  (i = 0;  i < 5;  i = i + 1)

for  (j = 0;  j < i;  j = j + 1)

printf(“%d  %d\n”, i, j);

 

  1.    0
  2.    5
  3.    9

*d.   10

  1.    20

 

  1. When a loop statement contains another loop statement it is said to be:

 

*a.   nested

  1.    compound
  2.    entangled
  3.    conditional
  4.    none of the above

 

  1. The following statement is call a(n) _______.

 

i++;

 

  1.    repetition
  2.    condition
  3.    prefix increment

*d.   postfix increment

  1.    illegal operation

 

  1. When a sentinel-controlled loop performs an extra iteration it is called a(n) _______.

 

  1.    trace error
  2.    compound error
  3.    debugging error
  4.    math error

*e.   off-by-one error

 

 

 

 

 

Short Answer

 

  1. Loops that terminate upon scanning a special input value are called [ sentinel ]-controlled loops.

 

  1. The intention of the following program fragment is to display the positive integers from 1 through n, but it doesn’t work.  Correct the while statement so the fragment achieves the desired effect.

 

i = 1;

while (i <= n)

printf(“%d  “, n);

i = i + 1;

printf(“\n”);

 

[Answer:

 

while (i <= n) {

printf(“%d  “, i);

i = i + 1;

}

]

 

  1. Rewrite the following code segment as an equivalent segment that uses a for statement.

 

product = 1;

next = 1;

while (next <= m) {

product = product * next;

next = next + 1;

}

 

[Answer:

 

product = 1;

for  (next = 1;  next <= m;  next = next + 1)

product = product * next;

]

 

  1. Write a do-while loop that repeatedly scans integer values until a positive even number is input.  You may assume the input data is a list of integers separated by blanks, and that there is a positive even number in the list.

 

[Answer:

 

do

scanf(“%d”, &num);

while (num <= 0 || num % 2 != 0);

]

 

 

  1. What is the output of this code segment?

 

for  (k = 5;  k > 0;  k = k – 1) {

for  (i = 1; i <= 5 – k;  i = i + 1)

printf(“.”);

for  (j = 1;  j <= 2 * k – 1;  j = j + 1)

printf(“B”);

printf(“\n”);

}

 

[Answer:

 

BBBBBBBBB

.BBBBBBB

..BBBBB

…BBB

….B

]

 

  1. Write an expression to represent the following condition:

 

x is either equal to 10 or not greater than 5

 

[Answer:  x == 10 || !(x > 5)]

 

  1. Write a sequence of two statements that is equivalent to the following three-statement sequence.  Use operators with side effects and avoid parentheses where possible.

 

x = y * 10;

y = y + 1;

z = z * (y + 5);

 

[Answer:

x = y++ * 10;

z *= y + 5;

]

 

  1. What is displayed by the following code fragment if all the variables are of type int?

 

k = 0;

m = 0;

for  (p = 0;  p < 10;  p = p + k) {

k = k + 1;

m = m + p;

printf(“%4d%4d%4d\n”, p, k, m);

}

 

[Answer:

0   1   0

1   2   1

3   3   4

6   4  10

]

 

  1. Which variable in the code segment for question 9 is the loop control variable?

 

[Answer:  p ]

 

 

  1. A loop that never terminates is a(n) [infinite] loop.

 

  1. When the value of a variable is changed as the result of carrying out some operation on it, it is called [a side effect].

 

  1. In a debugger, use a(n) [breakpoint] at the end of each major algorithmic step so that you can examine variables to see if your program is executing correctly.

 

  1. What is the output of the following program?

 

count = 5;

while (count > 0) {

print(“Woot! ”);

count  -=  1;

}

 

[Answer:   Woot! Woot! Woot! Woot! Woot! ]

 

  1. Write C code for the following flowchart. Assume the user inputs an integer variable.

 

 

[Answer:

 

scanf(“%d”, &number);

while (number % 2) {

printf(“odd\n”);

scanf(“%d”, &number);

}

 

]