Docsity

Exam I Review: Essential C++ and Control Structures - Prof. Keith E. Hellman, Study notes of Computer Science

Partial preview of the text

Download Exam I Review: Essential C++ and Control Structures - Prof. Keith E. Hellman and more Study notes Computer Science in PDF only on Docsity!

EXAM I REVIEW

August 5, 2009

Exam I Review Essential C++

Identify each term that is a valid C++ identifier, or explain why it is not.

A oxygen%

B The_Hot_Tomatoes

C double

D 2TooMany

E Ozone

Exam I Review Essential C++

Consider the snippet of code at the

right. What are the values of x, y, and

z after the variables have been

initialized?

1 d o u b l e x ( 3 / 4 ) ;

2 d o u b l e y ( 3%4 ) ;

3 d o u b l e z ( 3. 0 / 4 ) ;

Exam I Review Control Structures

Write the English statement as a C++ Boolean expression (a test condition

that might be used in an if() or while() statement).

A The real value x is between 1.5 (exclusive) and 2 (inclusive).

B The integer z is greater than zero and divides the integer y evenly.

C m is less than zero, or m is greater than or equal to 4.

Exam I Review Control Structures

What does the following snippet print to the screen?

1 # i n c l u d e < i o s t r e a m > 2 u s i n g n am esp ace s t d ; 3 4 i n t main ( v o i d ) 5 { 6 i n t x ( 3 + 4 % 6 ) ; 7 8 i f ( x <= 4 ) { 9 c o u t << " Treat your password like your toothbrush. " << e n d l ; 10 } e l s e { 11 s w i t c h ( x ) { 12 c a s e 5 : 13 c o u t << " Don ’ t let anybody else use it , " << e n d l ; 14 b r e a k ; 15 c a s e 6 : 16 c o u t << " and get a new one " << e n d l ; 17 18 c a s e 7 : 19 c o u t << " every six months. " << e n d l ; 20 b r e a k ; 21 d e f a u l t : 22 c o u t << " -- Clifford Stoll " << e n d l ; 23 b r e a k ; 24 } 25 } 26 r e t u r n 0 ; 27 }

Exam I Review Control Structures

Rewrite the code

snippet at the right

using a for loop.

Declare the counter

variable such that it’s

lifetime and scope is

limited to the for loop.

2 i n t c o u n t e r ( 3 ) ;

3 i n t v a l u e ( 1 0 ) ;

4 w h i l e ( c o u n t e r >= 0 ) {

5 v a l u e ∗= ++ v a l u e ∗ −−c o u n t e r ;

Exam I Review Control Structures

finis