Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Repetition Structures: Lecture Questions - Prof. Keith E. Hellman, Exams of Computer Science

A set of questions related to c++ repetition structures, including 'for' and 'while' loops. Students are encouraged to explain false answers, provide enough work and explanation for pre-exam review, and ask for help during office hours or via email. Examples of c++ code and prompts for user input.

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-zoe
koofers-user-zoe 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI261 Lecture Questions C++ Repetition Structures
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. C++, like many programming languages, has repetition statements or looping structures. Explain precisely what
is being repeated when these are used.
2. Which repetition structure is most likely to have a “loop counter”?
3. How many question marks will be printed to the console?
1
fo r ( int i (0 ); i <5 ; i ++ ) {
2
fo r ( i nt j ( i ); j >= 1 ; j - - ) {
3
co ut << ’? ;
4
}
5
}
6
cout << endl ;
4. Using an
if
statement and the
break
keyword, add code to the following program so that it terminates instead of
attempting the
sqrt()
of a negative number.
1
# in c lu de < io st re am >
2
# in c lu de < cm at h >
3
using namespace std;
4
5
int main()
6
{
7/ / V a r i a b l e t o be re a d fr o m us e r
8
do u b le x(0 );
9
wh i l e ( t rue ) {
10
11
co ut < < " En te r a nu mb e r fo r S QR T : " < <
12
endl;
13
ci n > > x ;
14
15
co ut < < " The S QRT of " << x << " is " < <
16
sq rt ( x ) << e nd l ;
17
18
}
19
20
return 0;
21
}
pf2

Partial preview of the text

Download C++ Repetition Structures: Lecture Questions - Prof. Keith E. Hellman and more Exams Computer Science in PDF only on Docsity!

CSCI261 Lecture Questions C++ Repetition Structures

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a

useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in

general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be

happy to review your answers with you during office-hours, via Email, or instant messaging.

1. C++, like many programming languages, has repetition statements or looping structures. Explain precisely what

is being repeated when these are used.

2. Which repetition structure is most likely to have a “loop counter”?

3. How many question marks will be printed to the console?

1 for ( int i (0); i <5; i ++ ) { 2 for ( int j ( i ); j >=1; j - - ) { 3 cout << ’? ’ ; 4 } 5 } 6 cout << endl ;

4. Using an if statement and the break keyword, add code to the following program so that it terminates instead of

attempting the sqrt() of a negative number.

1 # include < iostream > 2 # include < cmath > 3 using namespace std ; 4 5 int main () 6 { 7 / / V a r i a b l e t o be r e a d f r o m u s e r 8 double x (0); 9 while ( true ) { 10 11 cout << " Enter a number for SQRT : " << 12 endl ; 13 cin >> x ; 14 15 cout << " The SQRT of " << x << " is " << 16 sqrt ( x ) << endl ; 17 18 } 19 20 return 0; 21 }

CSCI261 Lecture Questions C++ Repetition Structures

5. Write a user input loop that waits until a user enters an upper-case vowel (A, E, I, O, or U).

6. What are the four control structures we have studied so far (the if-then and if-then-else do not count as two)?

Of the four, one does not use the implicit notion of a Boolean expression in its definition. Which is it?

7. Explain the difference between a sentinel loop and a counting loop. Which C++ repetition structure is most easily

used for each?

8. Explain the difference between a sentinel loop and a conditional loop.

Page 2