Name: 
 

COP1220 ch1-6 Review Fall'03



True/False
Indicate whether the sentence or statement is true or false.
 

1. 

There is only one unique way to translate a given algorithm into a C++ program.
 

2. 

All algorithms can be implemented as computer programs.
 

3. 

RAM stands for random access memory.
 

4. 

The two components of the central processing unit (CPU) are the arithmetic/logic unit and the control unit.
 

5. 

Every C++ program must have a function named main.
 

6. 

A C++ identifier cannot start with a digit.
 

7. 

The C++ compiler considers the identifier CanOfWorms to be the same as the identifier canofworms.
 

8. 

Some C++ reserved words can also be used as programmer-defined identifiers.
 

9. 

In C++, a block (compound statement) is not terminated by a semicolon.
 

10. 

A literal string can continue onto more than one line, as long as it is enclosed in double quotes.
 

11. 

In a C++ expression, all additions are performed before any subtractions.
 

12. 

In C++, the value of the expression  3 + 2 * 6  is 15.
 

13. 

Execution of the statement
        
someInt = 3 * int(someFloat);

does not change the contents of the variable someFloat in memory
 

14. 

If someFloat is a variable of type float, the statement
   someFloat = 395;
causes someFloat to contain an integer rather than floating-point value
 

15. 

C++, the expression (a + b / c) / 2  is implicitly parenthesized as ((a + b) / c) / 2.
 

16. 

Integer values and floating-point values are stored differently inside the computer.
 

17. 

Assuming x and y are variables of type float, the expression
           
    sqrt(fabs(3.8 * x + 9.4 * y))
           
is a valid use of the sqrt and fabs library functions.
 

18. 

When a floating-point value is assigned to an integer variable, the fractional part is truncated.
 

19. 

A void function is always called (invoked) by using its name and argument list as a complete, stand-alone statement.
 

20. 

If the int variable someInt contains the value 26, the statement
                           
      cout << "someInt";
           
outputs the value 26.
 

21. 

The single statement
           
                cin >> alpha >> beta;
           
                may be used in place of the two statements
           
                cin >> alpha;
                cin >> beta;
 

22. 

The input statement
           
                cin >> someInt;
           
                could also be written as
           
                someInt << cin;
 

23. 

Assuming no input errors, an execution of the >> operator leaves the reading marker at the character immediately following the last data item read.
 

24. 

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.
 

25. 

If a C++ program attempts to input invalid data, the computer system immediately terminates the program and displays an error message.
 

26. 

A hierarchical implementation of a functional decomposition is one in which some or all of the modules are implemented as separate C++ functions.
 

27. 

Syntactically, the only expressions that can be assigned to Boolean variables are the literal values true and false.
 

28. 

If ch1 contains the value 'C' and ch2 contains the value 'K', the value of the C++ expression
           
                ch1 <= ch2
           
                is true.
 

29. 

If P and Q are logical expressions, the expression  P AND Q  is TRUE if either P or Q is TRUE or both are TRUE.
 

30. 

The expression  !(n < 5)  is equivalent to the expression  n > 5.
 

31. 

To test whether someInt equals 25 or 30, the C++ expression
           
                someInt == 25 || 30
           
                has the correct semantics but produces a syntax (compile-time) error.
 

32. 

According to DeMorgan's Law, the expression
           
                !(x <= y || s > t)
           
                is equivalent to
           
                x <= y && s > t
 

33. 

The statement
           
                if (grade == 'A' || grade == 'B' || grade == 'C')
                    cout << "Fail";
                else
                    cout << "Pass";
           
                prints Pass if grade is 'A', 'B', or 'C' and prints Fail otherwise.
 

34. 

      If a C++ If statement begins with
           
                if (age = 30)
           
                the If condition is an assignment expression, not a relational expression.
 

35. 

The code segment
           
              if (speed <= 40)
                  cout << "Too slow";
              if (speed > 40 && speed <= 55)
                  cout << "Good speed";
              if (speed > 55)
                  cout << "Too fast";
           
                could be written equivalently as
           
              if (speed <= 40)
                  cout << "Too slow";
            else if (speed <= 55)
                  cout << "Good speed";
            else
                  cout << "Too fast";
 

36. 

If the code fragment
           
              if (a >= 10)
              if (a < 20)
           
            a = a + 2;
            else
           
            a = a + 1;
           
                is indented according to the manner in which it is executed, the correct indentation is
           
              if (a >= 10)
                  if (a < 20)
                      a = a + 2;
            else
                  a = a + 1;
 

37. 

Testing is performed only in the implementation phase of a program's life cycle.
 

38. 

Given the module description
           
                PRINT AVERAGE (Level 1)
                  Print a heading
                  Print sumOfValues / countOfValues
           
                the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0."
 

39. 

Given the module description
           
                PRINT AVERAGE (Level 1)
                  Print a heading
                  IF countOfValues isn't 0
                    Print sumOfValues / countOfValues
                  ELSE
                    Print an error message
           
                the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0."
 

40. 

The logical order of statements in a program may be different from their physical order.
 

41. 

When a While expression evaluates to false, the loop terminates and control goes back to the statement immediately before the While statement.
 

42. 

In the C++ program fragment
           
                count = 1;
                while (count < 10)
                    count++;
                    cout << "Hello";
           
                the output statement that prints "Hello" is not part of the body of the loop.
 

43. 

An infinite loop is one in which the While expression always has the value false.
 

44. 


It is possible for the body of a While statement never to be executed.
 

45. 

If a While loop's termination condition becomes true in the middle of the loop body, the loop is exited immediately.
 

46. 


In the design of a flag-controlled loop, the loop condition is initialized by giving the flag variable an initial value of true or false, whichever is appropriate.
 

47. 

In C++, an infinite loop results from using the assignment operator in the following way:
           
                while (gamma = 2)
              {
                    ...
               }
 

48. 

The term "complexity" is a measure of the effort expended by the computer in performing a computation.
 

49. 

The number of steps performed by an O(N) algorithm is directly proportional to the number of data values that are processed.
 

50. 

A program is said to be robust if it can recover from erroneous input and keep running.
 

Multiple Choice
Identify the letter of the choice that best completes the statement or answers the question.
 

51. 

Which of the following terms describes the repetition of statements (instructions) while certain conditions are met?
a.
sequence
b.
selection
c.
looping
d.
subprogram
 

52. 

Which of the following terms describes the execution of different statements (instructions) depending on certain conditions?
a.
sequence
b.
selection
c.
looping
d.
subprogram
 

53. 

Which of the following terms describes the execution of a series of statements (instructions) one after another?
a.
sequence
b.
selection
c.
looping
d.
subprogram
 

54. 

Which of the following statements about the C++ main function is false?
a.
Every program must have a function named main.
b.
Program execution begins with the first executable statement in the main function.
c.
The main function must call (invoke) at least one other function.
d.
The word int in the function heading means that the main function returns an integer value (to the operating system).
 

55. 

Which one of the following is not a valid identifier in C++?
a.
Hi_There
b.
top40
c.
UpAnDdOwN
d.
3BlindMice
 

56. 

Which one of the following is  not a valid identifier in C++?
a.
myName
b.
little
c.
X123Y
d.
Go Home
e.
IdEnTiFiEr
 

57. 

Which of the following is a legal string assignment?
a.
"name" = Jones;
b.
name = "Jones";
c.
name = 'D' + "Jones";
d.
b and c above
 

58. 

Which of the following can be assigned to a char variable?
a.
't'
b.
'2'
c.
'$'
d.
All of the above
 

59. 

Which assignment statement could be used to store the letter A into the char variable someChar?
a.
someChar = "A";
b.
someChar = A;
c.
someChar = 'A';
d.
a and b above
e.
a, b, and c above
 

60. 

Which of the following statements prints HappyBirthday on one output line?
a.
cout << "Happy" << endl;
cout << "Birthday" << endl;
b.
cout << "Happy";
cout << "Birthday" << endl;
c.
cout << "HappyBirthday" << endl;
d.
b and c above
e.
a, b, and c above
 

61. 

The value of the C++ expression  3 / 4 * 5  is:
a.
0.0
b.
0
c.
3.75
d.
3
e.
0.15
 

62. 

Given that x is a float variable and num is an int variable containing the value 5, what will x contain after execution of the following statement:           
                x = num + 2;
a.
7
b.
7.0
c.
5
d.
5.0
e.
nothing; a compile-time error occurs
 

63. 

The value of the C++ expression  11 + 22 % 4  is:
a.
13
b.
1
c.
8
d.
16
e.
none of the above
 

64. 

If the int variables int1 and int2 contain the values 4 and 5, respectively, then the value of the expression  float(int1 / int2)  is:
a.
0.8
b.
0
c.
0.0
d.
1.0
e.
1
 

65. 

What is the output of the following program fragment?
     
                age = 29;
                cout << "Are you" << age << "years old?" << endl;
a.
Are you29years old?
b.
Are you 29 years old?
c.
Are you29 years old?
d.
Are you 29years old?
e.
Are you age years old?
 

66. 

If plant is a string variable and the statement
              plant = "Dandelion";
           
  is executed, then the value of the expression  plant.find('d')  is
a.
0
b.
1
c.
3
d.
4
e.
string::npos
 

67. 

If name is a string variable, what is the output of the following code?
              name = "Marian";
              cout << "Name: " << name.substr(1, 3) + "sta";
a.
Name: Marsta
b.
Name: arista
c.
Name: ista
d.
Name: rsta
e.
Name: Mar+sta
 

68. 

A value can be stored into a variable by execution of:
a.
an input statement
b.
an output statement
c.
an assignment statement
d.
a and b above
e.
a and c above
 

69. 

Given the constant declaration
           
            const int FACTOR = 95;
           
                which of the following is not a valid use of FACTOR?
a.
cout << FACTOR * 3;
b.
FACTOR = 24;
c.
cin >> FACTOR;
d.
a and c above
e.
b and c above
 

70. 

Which of the following is a valid input statement?
a.
cin >> studentAge;
b.
cin << studentAge;
c.
studentAge >> cin;
d.
studentAge << cin;
e.
a and c above
 

71. 

Which of the following statements sends a newline character to the standard output device?
a.
cout << endl;
b.
cout << '
c.
cout <<
d.
a and b above
e.
a, b, and c above
 

72. 

Which of the following statements about C++ stream input is true?
a.
When an integer data value is read into a float variable, the value is first converted into floating-point form.
b.
When an integer data value is read into a float variable, the float variable becomes an int variable.
c.
It is an error to read an integer data value into a float variable.
d.
Input of a floating-point data value stops when a decimal point is encountered.
e.
c and d above
 

73. 

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
 

74. 

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;
a.
333
b.
444
c.
555
d.
777
e.
none of the above
 

75. 

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.)
           
            cin >> str;
            cin.get(ch);
a.
'B'
b.
'
c.
'C'
d.
'D'
e.
none of the above
 

76. 

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);
a.
'B'
b.
'
c.
'C'
d.
'D'
e.
none of the above
 

77. 

Which of the following is not one of the things a programmer must do in order to use files in a C++ program?
a.
Use a preprocessor directive to include the header file fstream.
b.
Declare each file stream in a variable declaration.
c.
Prepare each file for reading or writing by calling the open function.
d.
Specify the name of the file stream in each input or output statement that uses it.
e.
Erase the contents of each output file before running the program.
 

78. 

When used with an input file stream, which of the following statements about the open function is false?
a.
It associates the name of a stream variable with the name of a physical disk file.
b.
It puts the stream into the fail state if the file does not already exist.
c.
It sets the reading marker at the first character in the file.
d.
It creates a new, empty file if the file does not already exist.
e.
none of the above
 

79. 

When used with an output file stream, which of the following statements about the open function is false?
a.
It associates the name of a stream variable with the name of a physical disk file.
b.
It erases the old contents of the file if the file already exists.
c.
It sets the writing marker at the beginning of the file.
d.
It creates a new, empty file if the file does not already exist.
e.
none of the above
 

80. 

What happens when a C++ input stream enters the fail state?
a.
The system displays an error message, and program execution is terminated.
b.
The system displays an error message, the program continues running, and further input operations with that stream are ignored.
c.
The system does not display an error message, and program execution is terminated.
d.
The system does not display an error message, the program continues running, and further input operations with that stream are ignored.
e.
none of the above
 

81. 

If DeMorgan's Law is used to negate the expression
           
                (i < j) && (k == l)
           
                then the result is:
a.
(i < j) || (k == l)
b.
(i > j) && (k != l)
c.
(i >= j) || (k != l)
d.
(i > j) || (k != l)
e.
(i >= j) && (k != l)
 

82. 

Which C++ logical expression correctly determines whether the value of beta lies between 0 and 100?
a.
0 < beta < 100
b.
0 < beta && beta < 100
c.
(0 < beta) && (beta < 100)
d.
b and c above
e.
a, b, and c above
 

83. 

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
 

84. 

After execution of the following code, what will be the value of angle if the input value is 10?
           
            cin >> angle;
              if (angle > 5)
                  angle = angle + 5;
            else if (angle > 2)
                  angle = angle + 10;
a.
0
b.
5
c.
10
d.
15
e.
25
 

85. 

After execution of the following code, what will be the value of angle if the input value is 10?
           
            cin >> angle;
              if (angle > 5)
                  angle = angle + 5;
              if (angle > 2)
                  angle = angle + 10;
a.
0
b.
5
c.
10
d.
15
e.
25
 

86. 

After execution of the following code, what will be the value of angle if the input value is 0?
           
            cin >> angle;
              if (angle > 5)
                  angle = angle + 5;
            else if (angle > 2)
                  angle = angle + 10;
            else
                  angle = angle + 15;
a.
0
b.
5
c.
10
d.
15
e.
25
 

87. 

After execution of the following code, what will be the value of angle if the input value is 0?
           
            cin >> angle;
              if (angle > 5)
                  angle = angle + 5;
            else if (angle > 2)
                  angle = angle + 10;
a.
0
b.
5
c.
10
d.
15
e.
25
 

88. 

What is the output of the following C++ code fragment? (Be careful here.)
           
              int1 = 120;
            cin >> int2;                     // Assume user types 30
              if ((int1 > 100) && (int2 = 50))
                  int3 = int1 + int2;
            else
                  int3 = int1 - int2;
            cout << int1 << ' ' << int2 << ' ' << int3;
a.
120 30 150
b.
120 30 90
c.
120 50 170
d.
120 50 70
e.
120 30 70
 

89. 

Consider the following If statement, which is syntactically correct but uses poor style and indentation:
           
               if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x - y;
           
                Assume that x and y are int variables containing the values 9 and 3, respectively, before execution of the above statement. After execution of the statement, what value will x contain?
a.
9
b.
1
c.
6
d.
27
e.
none of the above
 

90. 

Consider the following If statement, which is syntactically correct but uses poor style and indentation:
           
                if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x - y;
           
                Assume that x and y are int variables containing the values 3 and 9, respectively, before execution of the above statement. After execution of the statement, what value will x contain?
a.
9
b.
-6
c.
6
d.
27
e.
none of the above
 

91. 

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";
a.
Curly
b.
Moe Larry Curly
c.
Larry Curly
d.
no output; there is a compile-time error
e.
no output; there is a run-time error
 

92. 

Assuming alpha and beta are int variables, what is the output of the following code (which is indented poorly)?
           
            alpha = 3;
            beta = 2;
              if (alpha < 2)
              if (beta == 3)
            cout << "Hello";
            else cout << "There";
a.
Nothing is output
b.
Hello
c.
There
d.
HelloThere
 

93. 

What is the missing If condition in the following code fragment? The program is supposed to halt if the input file does not exist.
           
              ifstream inFile;
           
              inFile.open("myfile.dat");
              if (           )
            {
                  cout << "Cannot open input file." << endl;
                  return 1;
            }
a.
inFile
b.
myfile.dat
c.
!inFile
d.
!myfile.dat
e.
inFile != myfile.dat
 

94. 

What is the value of loopCount after control exits the following loop?
           
                loopCount = 1;
                while (loopCount <= 145)
                {
                    alpha = alpha + 7;
                    loopCount++;
                }
a.
1
b.
144
c.
145
d.
146
 

95. 

What is the termination condition for the following While loop?
           
                while (beta > 0 && beta < 10)
                {
                    cout << beta << endl;
                    cin >> beta;
                }
a.
beta > 0 && beta < 10
b.
beta >= 0 && beta <= 10
c.
beta < 0 || beta > 10
d.
beta <= 0 || beta >= 10
 

96. 

What is the output of the following code fragment?
           
                n = 1;
                while (n < 5)
                {
                    cout << n << ' ';
                    n++;
               }
a.
1 2 3 4 5
d.
2 3 4 5
b.
1 2 3 4
e.
2 3 4 5 6
c.
1 1 1  forever
 

97. 

What is the output of the following code fragment?
           
                n = 1;
                while (n <= 5)
                {
                    n++;
                    cout << n << ' ';
               }
a.
1 2 3 4 5
b.
1 2 3 4
c.
1 1 1  forever
d.
2 3 4 5
e.
2 3 4 5 6
 

98. 

Which of the following would be a poor choice for a sentinel value?
a.
a value of 999 for voterAge
b.
a value of 1 for testScore
c.
a value of 13 for birthdayMonth
d.
a value of 75 for carSpeed
e.
b and d above
 

99. 

Given the input data
           
              25 10 6 -1
           
                what is the output of the following code fragment? (All variables are of type int.)
           
                sum = 0;
                cin >> number;
                while (number != -1)
              {
                    cin >> number;
                    sum = sum + number;
                }
                cout << sum << endl;
a.
15
b.
41
c.
40
d.
16
e.
no output--this is an infinite loop
 

100. 

In the following code fragment, a semicolon appears at the end of the line containing the While condition.
           
                cout << 'A';
                loopCount = 1;
                while (loopCount <= 3);
                {
                    cout << 'B';
                    loopCount++;
                }
                cout << 'C';
           
                The result will be:
a.
the output  AC
b.
the output  ABC
c.
the output  ABBBC
d.
a compile-time error
e.
an infinite loop
 



 
Check Your Work     Reset Help