You are on page 1of 4

Part I

Name: _________________________________

Part I.A - Reading Programs (34 points)


1. (10 points) What is printed by each of these statements. (One "free" miss since there are 11 questions). __________ cout << 7 / 2; __________ cout << 7 % 2; __________ cout << 2 % 3; __________ cout << 5.0 / 2; __________ cout << 1 + 2 * 3 / 4; __________ int x = 1; cout << x++; __________ int x = 1; cout << ++x; __________ int y = 2; y*= 3; cout << y; __________ cout << 4+3 * 4-3; __________ cout << (1 > 2) ? 'a' : 'b'; __________ int x=1, y=2; cout << ((x>y)?5:0 > (x!=y)?6:-1) ? (x<=y)?'a':'b') : (x==y)?'c':'d'; 2. (1 point) What is printed by these statements?
int x=1, y=2; if (x != y) { cout << "www"; }else{ cout << "ppp"; }

Output = ____________________________ 3. (1 point) What is printed by these statements?


int x=1, y=2; if (x < y || x > y) { cout << "www"; }else{ cout << "ppp"; }

Output = ____________________________

4. (1 point) What is printed by these statements?


int x=1, y=2; if (x < y && x > y) { cout << "www"; }else{ cout << "ppp"; }

Output = ____________________________ 5. (1 point) What is printed by these statements?


int x=1, y=2; if (x == 1) { cout << "www"; }else if (x < y) { cout << "ppp"; }else{ cout << "bbb"; }

Output = ____________________________ 6. (2 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
for (int j=0; j<4; j++) { cout << j << " "; }

Output = ____________________________ 7. (2 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
for (int j=5; j<0; j--) { cout << j << " "; }

Output = ____________________________ 8. (2 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
for (int j=5; j>=5; j--) { cout << j << " "; }

Output = ____________________________ 9. (2 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
for (int j=1; j<10; j*=2) { cout << j << " "; }

Output = ____________________________

10. (3 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
int n = 0; for (int j=0; j<4; j++) { cout << "/"; if (j % 3 >= 2) { break; }else{ cout << j << " "; } cout << "$"; }

Output = ____________________________ 11. (4 points) What is printed by these statements? Write "infinite" if it never stops. Write "nothing" if it doesn't print anything.
for (int j=0; j<4; cout << " j = " for (int k=j+1; cout << k << } } j++) { << j << " k = "; k<4-1; k++) { " ";

Output = ____________________________ 12. (5 points) Given this program:


#include <iostream> using namespace std; int main() { int x; while (cin >> x) { for (int i=x; i>0; i--) { cout << '*'; } cout << endl; } return 0; }

What would this program print with input 1 3 5 1 3 5?

Part I.B - Writing Programs (36 points)


1. (3 points) Translate this for loop into an equivalent while loop.
. . . int power2 = 1; for (int i=0; i<n; i++) { power2 *= power2; } cout << power2; . . .

2. (3 points) Translate this switch statement into equivalent if statements.

. . . switch (xx) { case 1: cout << "Hello"; break; case 3: cout << "Goodbye"; break; default: cout << "Never mind."; } . . .

3. (5 points) Write a program that reads two numbers from the input and prints their sum. You don't need a loop for this. Assume there are always two numbers. 4. (10 points) Write a program to read a sequence of integers, ignoring all numbers that are less than 1. Count how many even and odd numbers there are, and print these counts. For example, with input 7 25 100 2 -6 1 0 3, the output should be
Even = 2, odd = 4

There were two even numbers (100 and 2), four odd numbers (7, 25, 1, and 3), and two numbers were ignored (-6 and 0). Your program should work for any input, not just this example. 5. (15 points) A toll booth needs a program that tells how much to charge drivers. The charge is based on how many people are in the vehicle. To encourage car-pooling, the cost is lower for more people. The cost is $6.00 for one person, 1/2 of that ($3.00) for two people, 1/3 ($2.00) for three people, and in general 6/n for n people. Write a program that reads the number of people in the vehicle and prints the cost. Print "ERROR" if a number less than 1 is entered. It continues reading input in a loop.

You might also like