Docsity

C++ Conditional Statements Analysis - Prof. Thomas D. Walker, Exams of Statistics

Toggle sidebar
bg1
1. What does the following statement display?
int a = 5;
int b = 10;
if (!(a*2 <= b))
cout << a*2 << " is less than or equal to " << b;
else
cout << a*2 << " is greater than or equal to " << b;
a. 10 is less than or equal to b
b. A*2 is less than or equal to b
c. 10 is greater than or equal to 10
d. a*2 is greater than or equal to 10
e. 5 is greater than or equal to 10
f. 10 is less than or equal to 10
g. a*2 is greater than or equal to b
h. 5 is less than or equal to 10
i. a*2 is greater than or equal to 10
j. nothing will be printed
2. What is the effect of the following code fragment? (Assume no run-time errors).
int num1 = 2;
int num2;
cin >> num2;
if (num2 % num1 == 0)
cout << "first option" << endl;
else
cout << "second option" << endl;
a. displays first option if num1 and num2 are equal, and second option
otherwise.
b. displays first option if num1 and num2 are not equal, and second option
otherwise.
c. displays first option if num1 and num2 are not equal to 0, and second option
otherwise.
d. assigns the character '%' to the variable num2 if num1 is 0, and displays first option
e. displays first option if num1 and num2 are equal to 0, and second option
otherwise.
f. displays first option if an even number is entered as input, and second option
if an odd number is entered as input.
g. displays first option if an odd number is entered as input, and second option if
an even number is entered as input.
3. Which C++ condition is TRUE if and only if the value of x is not between 0 and 100 (inclusive)?
a. 0 <= x && x <= 100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download C++ Conditional Statements Analysis - Prof. Thomas D. Walker and more Exams Statistics in PDF only on Docsity!

  1. What does the following statement display? int a = 5; int b = 10; if (!(a2 <= b)) cout << a2 << " is less than or equal to " << b; else cout << a2 << " is greater than or equal to " << b; a. 10 is less than or equal to b b. A2 is less than or equal to b c. 10 is greater than or equal to 10 d. a2 is greater than or equal to 10 e. 5 is greater than or equal to 10 f. 10 is less than or equal to 10 g. a2 is greater than or equal to b h. 5 is less than or equal to 10 i. a*2 is greater than or equal to 10 j. nothing will be printed
  2. What is the effect of the following code fragment? (Assume no run-time errors). int num1 = 2; int num2; cin >> num2; if (num2 % num1 == 0) cout << "first option" << endl; else cout << "second option" << endl; a. displays first option if num1 and num2 are equal, and second option otherwise. b. displays first option if num1 and num2 are not equal, and second option otherwise. c. displays first option if num1 and num2 are not equal to 0, and second option otherwise. d. assigns the character '%' to the variable num2 if num1 is 0, and displays first option e. displays first option if num1 and num2 are equal to 0, and second option otherwise. f. displays first option if an even number is entered as input, and second option if an odd number is entered as input. g. displays first option if an odd number is entered as input, and second option if an even number is entered as input.
  3. Which C++ condition is TRUE if and only if the value of x is not between 0 and 100 (inclusive)? a. 0 <= x && x <= 100

b. 0 <= x <= 100 c. 0 == 0 && x < 100 d. 0 <= x | x <= 100 e. 0 <= x && x < 100 f. 0 <= x || x <= 100 g. -1 < x < 101 h. 0 <= x & x <= 100 i. 0 < x < 100 j. x < 0 || x > 100

  1. What do the following statements display, if the value of the input is 0? cin >> color; switch (color) { case 0: case 1: cout << "red "; case 2: cout << "blue "; case 3: cout << "green "; case 4: cout << "yellow "; } cout << endl; a. green b. purple c. blue d. red blue green yellow e. black f. no output g. blue green yellow h. red blue i. red

g. Once h. 40 times i. 41 times j. 50 times

  1. Which statements are true? [select the best answer] a. The value of the expression (15 < 20 || 20 >= 20) is TRUE. b. The three primary control structures are int, double and char. c. The statement if (x = 10) cout << "x is 10" << endl; always displays "x is 10", regardless of the value of x when the statment is encountered. d. Statements (2) and (3) are true. e. Statements (3) and (1) are true. f. Statements (2) and (1) are true. g. Statements (2),(3) and (1) are true. h. All the above statements are false.
  2. What does the following program segment display? int a = 8; int b = 7; int c = 10; if (ab/70 >= c/25) cout << ab/70 << " is greater than or equal to " << c/25 << endl; else cout << ab/70 << " is less than or equal to " << c/25 << endl; a. 0.8 is greater than or equal to 25 b. 0.8 is greater than or equal to 25 c. 0 is less than or equal to 25

d. 0.8 is less than or equal to 1 e. 1 is less than or equal to 25 f. 1 is greater than or equal to 1 g. ab/70 is greater than or equal to c/2 h. ab/70 is less than or equal to c/2 i. 0 is greater than or equal to 1 j. 0 is less than or equal to 1

  1. What does the following statement display? int a = 2; int b = 3; if (a++ > --b) cout << a << " is greater than or equal to " << b << endl; else cout << a << " is less than " << b << endl; a. 2 is greater than or equal to 2 b.3.0 is less than 2 c. 3.0 is greater than or equal to 2. d. 2.0 is less than 2. e. 2 is less than 2 f. 3.0 is greater than or equal to 2 g. a is less than b h. 3 is less than 2 i. a is greater than or equal to b j. 3 is greater than or equal to 2
  2. Examine the following piece of code: int x = 0, y = 0, z = 0; for (int i=0; i<100; i=i+1) { cin >> x; y = y + x; if (x % 2 == 0) z = z + 1; } The value of the variable y can best be described as a. the standard deviation of the values entered

int x = 10, y = 0; for (int i=10; i>50; i=i+1) { cin >> x; y = y + x; } a. Until a number larger than 10 is entered b. 49 times c. 50 times d. 40 times e. Twice f. Zero g. 51 times h. 41 times i. 39 times j. Once

  1. Consider the following code segment and assume that the file "data" contains a series of 30 integer numbers. What output is displayed? #include > #include > using namespace std; int main() { ifstream infile ("data", ios::in); int i, n = 10; while (!infile.fail()) { infile >> i; n += 1; if (infile.eof()) break; } cout << n << endl; return 0; } a. 10 b. 40 c. 0 d. 29 e. 11 f. 41 g. 30 h. 31 i. 39 j. Logic errors make output indeterminable because code may act differently on different compilers
  1. Consider the following code segment and assume that the file "data" contains a series of 30 integer numbers. What output is displayed? All include files and namespaces are in effect. int main() { ifstream infile ("data.txt", ios::in); int i, n = 10; infile >> i; while (!infile.fail()) { infile >> i; n += 1; } cout << n << endl; return 0; } a. 29 b. 40 c. 31 d. 10 e. 30 f. 41 g. 11

e. 6.0e+ f. 73. g. 60. h. 61 i. 70. j. 73

  1. You are designing a while loop that is to exit when the values of both x and y are not 0. Which of the following would you use for your loop repetition condition? a. x == 0 || y == 0 b. !(x != 0) || (y != 0) c. x != 0 && y != 0 d. x != 0 & y != 0 e. x != 0 | y != 0 f. x == 0 | y == 0 g. !(x != 0) && !(y != 0) h. !(x != 0) | (y != 0) i. !(x != 0) & !(y != 0) j. x != 0 || y != 0
  2. Which of the following error types is generally the hardest to find and correct? a. Syntax errors b. Run-time errors c. I/O errors d. Logic errors e. Linking errors f. Typographical errors g. Include errors h. Character matching errors i. Output errors j. Compilation errors
  3. What does the following statement display? int a = 10; int b = 11; if (a < --b) cout << a << " is greater than " << b << endl; else cout << a << " is less than " << b << endl; a. 10.0 is less than 10.

h. a is less than b

  • b. 10.0 is less than 10.
  • c. 10 is less than
  • d. 10.0 is less than 11.
  • e. 10 is less than
  • f. 10.0 is greater than 11.
  • g. 10 is greater than
  • j. 10 is greater than i. a is greater than b

On the other hand, if num2 is an odd value, say, 21 , then the if condition would be evaluated as 21 % 2 == 0 which reduces to 1 == 0 which is FALSE , and which will cause the cout statement within the else block to be executed: second option Hence the condition num2 % num1 == 0 causes the if block to be executed whenever num2 is even, and the else block otherwise. Correct Answer: x < 0 || x > 100 Feedback: Since the value of x is to be checked against two ranges, the condition must be expressed using a logical OR expression. The expression x < 0 || x > 100 will be evaluated as (x < 0) || (x > 100) The first expression checks if x is less than 0 , and the second expression checks if x is greater than 100. The entire expression evaluates to TRUE if either expression evaluates to TRUE. Hence, the expression x < 0 || x > 100 is TRUE if and only if the value of x does not lie between 0 and

Correct Answer: red blue green yellow Feedback: When the switch statement is executed with a value of 0, the label case 0: will be executed. However, due to absence of any break or return statements, execution will continue down until the end of the switch block. This will execute all the cout statements in sequence. The output therefore will be: red blue green yellow Correct Answer: central processing unit Correct Answer: 41 times Feedback: The variable i has an initial value of 10 and is incremented by 1 for

every execution of the for loop. The statements within the for loop do not change the value of the variable i. The for loop is executed aslong as the variable i is less than or equal to 50 ; that is, the variable i is incremented from 10 through 51. When i reaches the value of 51 , the condition i<= will evaluate to FALSE and the loop will not be executed. Hence, the loop will be executed for values of i from 10 up through 50. This means that the body of the loop will be executed 41 times. Correct Answer: Statements (3) and (1) are true. Feedback: Explanation (1) This statement is FALSE: The three primary control structures are: 1. Sequential (Statements executed sequentially one after the other) 2. Selective (if-then-else, switch-case-default statements) 3. Repetition (while, for and do-while loops) (2) This statement is TRUE: The statement if (x = 10) assigns 10 to x before evaluating the expression. The expression returns the value of x, which is 10. This condition evaluates to TRUE and the cout statement will be executed. As a result, x is 10 is always displayed regardless of the earlier value of x. (3) This statement is TRUE: The expression ( < 20 || 20 >= 20) will be evaluated as: (TRUE || TRUE) The result of the expression will be TRUE. Correct Answer: 0 is less than or equal to 25 Feedback: The if statement will be evaluated as: if ((87)/70 >= (10/2)5) which reduces to: if (56/70 >= 5*5) which reduces to (the integer-integer division 56/70 is 0): if (0 >= 25) The if statement will therefore evaluate to FALSE; and hence, the else block will be executed. The output will therefore be: 0 is less than or equal to 25

value returned by the pow function is a double, the output displayed will be: 1. Correct Answer: 3 is greater than or equal to 4 Feedback: The if statement will be evaluated as: if ( ((8/10)5)+3 > (10/5)2 )** which reduces to: if ((05)+3 > (22))** which reduces to: if ((0+3) > 4) which reduces to: if (3 > 4) The if statement will therefore evaluate to FALSE ; and hence, the else block will be executed. The output will therefore be: 3 is greater than or equal to 4 Correct Answer: zero Feedback: Explanation The condition i> will be evaluated before every execution of the loop, including the very first time. The variable i has an initial value of 10, and hence the condition will immediately evaluate to FALSE and the loop will not be executed. This means that the body of the loop will be executed 0 times. Correct Answer: Logic errors make output indeterminable because code may act differently on different compilers. Feedback: Correct Answer: 40 Correct Answer: Statements (3) and (1) are true. Feedback: (1) This statement is TRUE.

Note that the character string "program.data" contains the name of the file to be read from. This particular filename is perfectly legal (it does not have to end with .TXT or .DAT). Note that outfile is a variable associated with file input because of ifstream. This particular variable name is perfectly legal (as would be aaa or infile). (2) This statement is FALSE. It is not necessary for a for loop to be executed at least once. For instance, consider the following: int i; for (i=101; i<100; i++) { // ... } The expression i< is evaluated before every iteration of the loop, including the very first time. In the above case, the variable i has an initial value of 101 and hence the expression i< will evaluate to FALSE , and the for loop will not be executed at all. (3) This statement is TRUE Correct Answer: line 3 Feedback: The output operator for a cout object is '<<' and not '>>' Correct Answer:

Feedback: The ***** and / operators have higher precedence than the + operator. Hence, the expression int n = (4.0 + 6.1) * 7 + 5 / 2. will be evaluated as int n = (10.1) * 7 + (5 / 2.0) which reduces to int n = 70.7 + (5 / 2.0) which reduces to int n = 70.7 + 2. which reduces to int n = 73. However, since n is an int , it cannot have any fractions. The value of