CSCI 130 Principles of Programming I Fall 2002

Name: ______

1. If the reading marker is in the middle of an input line of 25 characters, execution of the statement

cin.ignore(500, '\n');

leaves the reading marker at the character following the next newline character.

A) True B) False

2. An input line consists of a person's first and last initials, separated by a blank:

M H

Which of the following correctly inputs the person's initials into the char variables firstInit and lastInit?

A) cin > firstInit > lastInit;

B) cin > firstInit > ' ' > lastInit;

C) cin > firstInit;

cin.ignore(1, '\n');

cin > lastInit;

D) b and c above

E) a and c above

3. Given the three lines of input data

111 222 333

444 555 666

777 888 999

What value is read into gamma by the following code? (All variables are of type int.)

cin > alpha;

cin.ignore(500, '\n');

cin > beta > gamma;

4. Given the two lines of input data

ABC

DEF

what value is read into ch by the following code? (str is of type string, and ch is of type char.)

getline(cin, str);

cin.get(ch);

5. Which of the following statements about object-oriented design (OOD) is false?

A) OOD focuses on entities (objects) and operations on those objects.

B) The first step in OOD is to identify the major objects in the problem, together with their associated operations.

C) In OOD, data plays a secondary role in support of actions to be performed.

D) Object-oriented programming languages have been developed specifically to support OOD.

E) OOD and functional decomposition can be used in combination with each other to solve problems.

6. If p is a Boolean variable, which of the following logical expressions always has the value false?

A) p & p

B) p || p

C) p & !p

D) p || !p

E) b and d above

7. Given a Boolean variable isEmpty, which of the following is a valid C++ assignment statement?

A) isEmpty = true;

B) isEmpty = !isEmpty;

C) isEmpty = m > n;

D) a and b above

E) a, b, and c above

8. If the int variables i, j, and k contain the values 10, 3, and 20, respectively, what is the value of the following logical expression: j < 4 || j == 5 & i <= k

A) 3

B) false

C) 20

D) true

9. Given the following code:

string name1;

string name2;

name1 = "Mark";

name2 = "Mary";

what is the value of the relational expression string1 < string2 ?

A) true

B) false

C) none; it causes a compile-time error

D)  none; it causes a run-time error

10. What is the output of the following code fragment if the input value is 20? (Be careful here.)

cin > someInt;

if (someInt > 30)

cout < "Moe ";

cout < "Larry ";

cout < "Curly";

11. Describe the output of the following code fragment? (Be careful here.)

n = 1;

while (n <= 5)

cout < n < ' ';

n++;

12. Which type of loop would be most appropriate for solving the problem "Calculate the sum of all the odd integers in a data file of unknown length"?

A) a count-controlled loop

B) a flag-controlled loop

C) a sentinel-controlled loop

D) an EOF-controlled loop

E) either b or c above

13. Which type of loop would be most appropriate for solving the problem "Input an integer value, then print 'Happy Birthday' that many times"?

A) a count-controlled loop

B) a flag-controlled loop

C) a sentinel-controlled loop

D) an EOF-controlled loop

E) either b or c above

14. What is the output of the following code fragment? (All variables are of type int.)

sum = 0;

outerCount = 1;

while (outerCount <= 3)

{

innerCount = 1;

while (innerCount <= outerCount)

{

sum = sum + innerCount;

innerCount++;

}

outerCount++;

}

cout < sum < endl;

15. What is the value of someInt after control exits the following loop?

someInt = 273;

while (someInt > 500)

someInt = someInt - 3;

16. Write a program to determine how much to tip to the waiter in a restaurant. The tip should be 15% of the check, with a minimum of $1.

17. Write a program that determines the total sales for each salesperson. This program should read the data from a file and output to the screen. For example after I run the program a list of names and total sales will be displayed to the screen. Don’t worry about formatting.

Sample Input file:
George 500 200 500
Tom 100 0 300
John 200 1000 1300
Note that there will always be 3 sales figures after each name. / Sample output to the screen:
George 1200
Tom 400
John 2500
  1. True
  2. E
  3. 555
  4. D
  5. C
  6. C
  7. E
  8. D
  9. A
  10. Larry Curly
  11. 1 1 1 forever
  12. D
  13. A
  14. 10
  15. 273