• Home
  • Textbooks
  • C# Programming: From Problem Analysis to Program Design
  • Repeating Instructions

C# Programming: From Problem Analysis to Program Design

Barbara Doyle

Chapter 6

Repeating Instructions - all with Video Answers

Educators


Chapter Questions

02:12

Problem 1

Loops are needed in programming languages:
a. to facilitate sequential processing of data
b. to enable a variable to be analyzed for additional processing
c. to process files stored on hard drives
d. to allow statements to be repeated
e. all of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
03:05

Problem 2

To write a sentinel-controlled loop to compute the average temperature during the month of July in Florida, the best option for a sentinel value would be:
a. 67
b. 1000
c. 100
d. “high temperature”
e. none of the above

AG
Ankit Gupta
Numerade Educator
00:40

Problem 3

Which loop structure can only be used with a collection or array?
a. foreach
b. for
c. while
d. do...while
e. none of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
01:26

Problem 4

If a loop body must be executed at least once, which loop structure would be the best option?
a. foreach
b. for
c. while
d. do...while
e. none of the above

Prashant Bana
Prashant Bana
Numerade Educator
01:26

Problem 5

If a loop body uses a numeric value that is incremented by three with each iteration through the loop until it reaches 1000, which loop structure would probably be the best option?
a. foreach
b. for
c. while
d. do...while
e. none of the above

Prashant Bana
Prashant Bana
Numerade Educator
01:26

Problem 6

When used with a while statement, which jump statement causes execution to halt inside a loop body and immediately transfers control to the conditional expression?
a. break
b. goto
c. return
d. continue
e. none of the above

Prashant Bana
Prashant Bana
Numerade Educator
02:11

Problem 7

Which of the following is a valid C# pretest conditional expression that enables a loop to be executed as long as the counter variable is less than 10?
a. do while (counter < 10)
b. while (counter < 10)
c. foreach (counter in 10)
d. none of the above
e. all of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
01:45

Problem 8

Which of the following for statements would be executed the same number of times as the following while statement?
intƒnumƒ=ƒ10;
while(numƒ>ƒ0)
Console.WriteLine(num);
num—;
a. for (num = 0; num < 10; num++)
b. for (num = 1; num < 10; num++)
c. for (num = 100; num == 10; num+=10)
d. for (num = 10; num < 0; num--);
e. none of the above

Aditya Sood
Aditya Sood
Numerade Educator
03:11

Problem 9

What would be the output produced from the following statements?
int aValue $=1 ;$
do
aValue+t;
console. Write (avalue++);
while (avalue $<3$ );
a. 23
b. 234
c. 1234
d. 2
e. none of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
05:53

Problem 10

If aValue, i, and n are type int variables, what does the following program fragment do?
$\begin{aligned} \text { avalue }=0 ; & n=10 ; \\ \text { for }(i=n ; \quad i>0 ;&i--) \\ \text { if }\left(i \quad \frac{0}{8} 2=\right.&0) \\ & \text { avalue }=\text { avalue }+i \end{aligned}$
a. computes the sum of the integers from 1 through n
b. computes the sum of the integers from 1 through n - 1
c. computes the sum of the even integers from 1 through n
d. computes the sum of the odd integers from 1 through n
e. none of the above

Anas Venkitta
Anas Venkitta
Numerade Educator
02:57

Problem 11

To produce the output
2 4 6 8 10
which should be the loop conditional expression to replace the question marks?
int $n=0 ;$
do
$n=n+2 ;$
Console.Write $\left("\{0\} \backslash t^{\prime \prime}, n\right) ;$
while (????);
a. n < 11
b. n < 10
c. n < 8
d. n > = 2
e. n > 8

Saurabh Chandra
Saurabh Chandra
Numerade Educator
02:59

Problem 12

What would be the output produced from the following statements?
int $i=0 ;$
while $(i<0)$
Console.Write $\left("\{0\} \backslash t^{\prime \prime}, i\right) ;$
$i++;$
Console.Write (" $\left.\{0\} \backslash t^{\prime \prime}, i\right)$
a. 0
b. an infinite loop
c. an error
d. 00
e. none of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
02:12

Problem 13

Which of the following represents a pretest loop?
a. while
b. do...while
c. for
d. a and b
e. a and c

Ernest Castorena
Ernest Castorena
Numerade Educator
01:26

Problem 14

If you intend to place a block of statements within a loop body, you must use _________ around the block.
a. parentheses
b. square brackets
c. quotation marks
d. curly braces
e. none of the above

Prashant Bana
Prashant Bana
Numerade Educator
01:08

Problem 15

Refer to the following program segment.
\[\text { int } i=0, \quad g=0, \quad s=0, \quad t=0, \quad z=0\]
string sValue;
while $(i<5)$
invalue $=$ console. ReadLine ()$;$
$\mathrm{t}=$ convert. ToInt $32(\text { invalue }) ;$
$\mathbf{s}=\mathbf{s}+\mathbf{t} ;$
if $(t>-1)$ $g=g+1$ else $\begin{aligned} z &=z+1 ; \\ i &=i+1 \end{aligned}$
How many times is the loop body of the while statement executed?
a. once
b. never
c. four times
d. five times
e. until a number 5 or larger is entered

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
08:16

Problem 16

Refer to the following program segment.
\[\text { int } i=0, \quad g=0, \quad s=0, \quad t=0, \quad z=0\]
string sValue;
while $(i<5)$
invalue $=$ console. ReadLine ()$;$
$\mathrm{t}=$ convert. ToInt $32(\text { invalue }) ;$
$\mathbf{s}=\mathbf{s}+\mathbf{t} ;$
if $(t>-1)$ $g=g+1$ else $\begin{aligned} z &=z+1 ; \\ i &=i+1 \end{aligned}$
The value stored in variable z at the end of the execution of the loop could best be described as:
a. the number of positive items entered
b. the sum of all positive items entered
c. the number of negative items entered
d. the sum of all negative items entered
e. the sentinel value

Darren Wilson
Darren Wilson
Numerade Educator
01:08

Problem 17

Refer to the following program segment.
\[\text { int } i=0, \quad g=0, \quad s=0, \quad t=0, \quad z=0\]
string sValue;
while $(i<5)$
invalue $=$ console. ReadLine ()$;$
$\mathrm{t}=$ convert. ToInt $32(\text { invalue }) ;$
$\mathbf{s}=\mathbf{s}+\mathbf{t} ;$
if $(t>-1)$ $g=g+1$ else $\begin{aligned} z &=z+1 ; \\ i &=i+1 \end{aligned}$
The loop can best be categorized as a:
a. counter-controlled loop
b. sentinel-controlled loop
c. state-controlled loop
d. flag-controlled loop
e. none of the above

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
02:22

Problem 18

How many lines of output will be printed by the following program fragment?
\[\begin{array}{c}
\text { for }(i=0 ; i<5 ; i++) \\
\text { for }(j=0 ; \quad j<4 ; \quad j++) \\
\quad \text { Console.Writeline }("\{0\} \backslash n\{1\} ", \quad i, \quad j) ;
\end{array}\]
a. 20
b. 6
c. 9
d. 12
e. none of the above

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
02:22

Problem 19

How many lines of output will be printed by the following program fragment?
\[\begin{array}{c}
\text { for }(i=0 ; \quad i<5 ; \quad i+=2) \\
\text { for } \quad(j=0 ; \quad j<4 ; \quad j=j+2) \\
\quad \text { Console.Writeline }("\{0\} \backslash n\{1\} ", \quad i, \quad j) ;
\end{array}\]
a. 20
b. 6
c. 9
d. 12
e. none of the above

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
01:52

Problem 20

What would be the result of the following conditional expression?
int $i=0$
while $(\mathrm{i}<10)$
$i++i$
Console.Write $(\mathrm{i})$
a. 123456789
b. 012345678910
c. 0123456789
d. an infinite loop
e. none of the above

Ernest Castorena
Ernest Castorena
Numerade Educator
03:32

Problem 21

Convert the following do...while loop into a for loop and a while loop. Did the logic change? If so, explain.
int counter $=100$
do
Console.WriteLine(counter)
counter- - ;
while (counter > 0 ) ;

Ernest Castorena
Ernest Castorena
Numerade Educator
05:46

Problem 22

Write a for loop to display every third number beginning with 10 and continuing through 100.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
02:23

Problem 23

Write a sentinel-controlled while loop that allows any number of temperatures to be entered.The average temperature should be calculated and displayed.

Mohammad Mehran
Mohammad Mehran
Numerade Educator
00:58

Problem 24

Create a loop body that generates random numbers between 25 and 75.Write a state- controlled loop that adds all these randomly generated numbers until a value larger than 60 is generated.When the loop stops, display the number of acceptable generated values and the sum of those values.

Harsh Gadhiya
Harsh Gadhiya
Numerade Educator
00:47

Problem 25

Desk run or trace the following code segment, showing every value that goes into each variable.
$$\begin{aligned}
\text { for }(i=0 ; \quad i<3 ;&i++) \\
\text { for } &(j=4 ; \quad j>0 ; \quad j-) \\
& \quad \text { Console.Writeline }\left("\{0\} \backslash t\{1\}^{\prime \prime}, \quad i, \quad j\right)
\end{aligned}$$

AG
Ankit Gupta
Numerade Educator