Study Guide 3: Control Statements

Be aware that some of the following questions are properly characterized as "trick questions." Nevertheless, I include them here because they represent common programming errors, and you need to begin training yourself to see them. Please study Null Statements and The Dangling Else Problem carefully.

  1. Choose a C++ expression that uses a relational operator to evaluate to true if the variable dilbert is not equal to alice.
    1. dilbert == alice
    2. dilbert != alice
    3. dilbert <> alice
    4. dilbert =! alice
  2. Is -1 considered to be true or false?
    1. true
    2. false
  3. Is 0 considered to be true or false?
    1. true
    2. false
  4. In a for loop with a multi-statement loop body, semicolons must appear following
    1. the for statement itself.
    2. the closing brace in a multi-statement loop body.
    3. each statement within the loop body.
  5. True or false: The update expression in a for loop can decrement the loop variable.
    1. true
    2. false
  6. Choose a for loop that displays the numbers from 100 to 110.
    1. for (int i = 100; i <= 110; i++) cout << i << endl;
    2. for (int i = 100; i < 111; i++) cout << i << endl;
    3. for (int i = 100; i >= 110; i++) cout << i << endl;
    4. for (int i = 100; i > 111; i++) cout << i << endl;
  7. A block of code is delimited by
    1. < and >
    2. ( and )
    3. [ and ]
    4. { and }
  8. A variable defined within a block is visible
    1. from the point of definition to the end of the program.
    2. throughout the program.
    3. from the point of definition to the end of the block.
    4. throughout the block.
  9. Write a while loop and any additional statements needed to display the numbers from 100 to 110.
    1. aint i = 100; while (i <= 110) cout << i++ << endl;
    2. int i = 99; while (i <= 109) cout << ++i << endl;
    3. int i = 100; while (i < 111) cout << i++ << endl;
    4. int i = 99; while (i < 110) cout << ++i << endl;
  10. True or false: Relational operators have a higher precedence than arithmetic operators.
    1. true
    2. false
  11. How many times is the loop body executed in a do-while loop?
    1. only once
    2. depends entirely on the controlling expression
    3. at least once but maybe more depending on the controlling expression
  12. Choose a do-while loop that displays the numbers from 100 to 110.
    1. int i = 99;
      do
      	cout << ++i << endl;
      while (i < 110);
    2. int i = 99;
      do
      {
      	cout << ++i << endl;
      } while (i < 110);
    3. int i = 100;
      do
      	cout << i++ << endl;
      while (i <= 110);
    4. int i = 100;
      do
      {
      	cout << i++ << endl;
      } while (i <= 110);
  13. Choose an if statement that prints Yes if a variable age is greater than 21.
    1. if (age > 21) cout << "Yes" << endl;
    2. if (21 <= age) cout << "Yes" << endl;
    3. if (age < 21) cout << "Yes" << endl;
    4. if (21 >= age) cout << "Yes" << endl;
  14. The library function exit() causes an exit from
    1. the loop in which it occurs.
    2. the block in which it occurs.
    3. the function in which it occurs.
    4. the program in which it occurs.
  15. The break statement causes an exit from
    1. the loop in which it occurs.
    2. the block in which it occurs.
    3. the function in which it occurs.
    4. the program in which it occurs.
  16. Choose an if-else statement that displays Yes if a variable age is greater than 21, and displays No otherwise.
    1. if (age > 21)
      	cout << "Yes" << endl;
      else
      	cout << "No" << endl;
    2. if (21 <= age)
      	cout << "Yes" << endl;
      else
      	cout << "No" << endl;
    3. if (age < 21)
      	cout << "Yes" << endl;
      else
      	cout << "No" << endl;
    4. if (21 >= age)
      	cout << "Yes" << endl;
      else
      	cout << "No" << endl;
  17. The _getche() library function
    1. returns and displays a character when a key is pressed on the keyboard.
    2. displays but does not return a character on the screen.
    3. returns but does not display a character on the screen.
    4. returns and displays a character when the Enter key is pressed.
  18. Choose a switch statement that prints Yes if a variable ch is 'y', prints No if ch is 'n', and prints Unknown response otherwise.
    1. switch (ch)
      {
      	default:
      		cout << "Unknown response" << endl;
      		break;
      	case 'y' :
      		cout << "Yes" << endl;
      		break;
      	case 'n' :
      		cout << "No" << endl;
      		break;
      }
    2. switch (ch)
      {
      	case 'y' :
      		cout << "Yes" << endl;
      		break;
      	case 'n' :
      		cout << "No" << endl;
      		break;
      	default:
      		cout << "Unknown response" << endl;
      		break;
      }
    3. switch (ch)
      {
      	case 'y' :
      		cout << "Yes" << endl;
      		break;
      	case 'n':
      		cout << "No" << endl;
      		break;
      	default:
      		cout << "Unknown response" << endl;
      }
    4. switch (ch)
      {
      	default:
      		cout << "Unknown response" << endl;
      		break;
      	case 'y' :
      		cout << "Yes" << endl;
      		break;
      	case 'n':
      		cout << "No" << endl;
      }
  19. Choose a statement that uses a conditional operator to set ticket to 1 if speed is greater than 55 and to 0 otherwise.
    1. ticket = (speed > 55) ? 1 : 0;
    2. ticket = speed > 55 ? 1 : 0;
    3. ticket = (speed <= 55) ? 0 : 1;
    4. ticket = speed <= 55 ? 0 : 1;
  20. Choose an expression involving a logical operator that is true if limit is 55 and speed is greater than 55.
    1. limit == 55 && speed > 55
    2. speed > 55 && limit == 55
    3. limit > 55 && speed = 55
    4. limit = 55 && speed > 55
  21. The break statement causes an exit
    1. only from the innermost loop.
    2. only from the innermost switch.
    3. from all loops and switches.
    4. from the innermost loop or switch.
  22. Executing the continue operator from within a loop causes control to go to
    1. pot
    2. the next iteration of the loop
    3. the next statement following the loop
  23. What does the following code print?
      int x = 10;
      {
      	int x = 5;
      }
      cout << x << endl;
    1. 5
    2. 10
    3. It will not compile because it defines two variables with the name.
    4. It will compile but crash during execution because there are two variables with the same name.
  24. What does the following code print?
      int x = 10;
      {
      	int x = 5;
      	cout << x << endl;
      }
    1. 5
    2. 10
    3. It will not compile because it defines two variables with the name.
    4. It will compile but crash during execution because there are two variables with the same name.
  25. How many lines of output will the following code fragment print?
    for (int i = 0; i < 10; i++)
    	cout << "Hello world" << endl;
    1. 0
    2. 9
    3. 10
    4. 11
    5. Creates an infinite loop
  26. How many lines of output will the following code fragment print?
    for (int i = 0; i <= 10; i++)
    	cout << "Hello world" << endl;
    1. 0
    2. 9
    3. 10
    4. 11
    5. Creates an infinite loop
  27. How many lines of output will the following code fragment print?
    int counter = 0;
    while (counter < 100)
    {
    	cout << "counter = " << counter << endl;
    	counter++;
    }
    1. 0
    2. 1
    3. 100
    4. 101
    5. Creates an infinite loop
  28. How many lines of output will the following code fragment print?
    int counter = 100;
    while (counter > 0)
    {
    	cout << "counter = " << counter << endl;
    	counter--;
    }
    1. 0
    2. 1
    3. 100
    4. 101
    5. Creates an infinite loop
  29. How many lines of output does the following code fragment print?
    int a = 10;
    int b = 20;
    
    if (a > b)
    	cout << "It is bigger\n";
    1. 0
    2. 1
    3. Will not compile: missing else part of statement
  30. What output does the following code fragment produce?
    int flag = 0;
    
    if (flag)
    	cout << "It is true\n";
    else
    	cout << "It is false\n";
    1. It is true
    2. It is false
    3. The code will not compile because there is no relational operator in the if-statement test
    4. The code will compile but will not run because there is no relational operator in the if-statement test
  31. What value does the following statement store in variable c if a = 5 and b = 10?
    c = (a < b) ? (b - a) : (b + a);
    1. 0
    2. 5
    3. 10
    4. 15
    5. It is a malformed statement and will not compile
  32. What output does the following code fragment produce?
    double d = 3.14159;
    int i = 0;
    
    if (d > i)
    	cout << "It is greater\n";
    1. It is greater
    2. No output is produced
    3. The code will not compile because you can’t compare a double with an int
  33. How many lines of output will the following code fragment print?
    for (int i = 0; i < 10; i++)
    	cout << "Looping\n";
    	cout << "Hello world" << endl;
    1. 9
    2. 10
    3. 11
    4. 19
    5. 20
    6. 21
  34. What does the following code print?
    int a = 0;
    int b = 0;
    
    if (a < 10)
    	if (b > 100)
    		cout << "Hello\n";
    else
    	cout << "World\n";
    1. Hello
    2. World
    3. It does not print anything
    4. It is a malformed if-statement and will not compile
  35. How many lines of output will the following code fragment print?
    for (int i = 0; i < 10; i--)
    	cout << "Hello world" << endl;
    1. 0
    2. 9
    3. 10
    4. 11
    5. Creates an infinite loop
  36. How many lines of output will the following code fragment print?
    int i = 0;
    
    for (; i < 10; i++)
    	cout << "Hello world" << endl;
    1. 9
    2. 10
    3. 11
    4. The code will not compile because the for-statement is missing the first expression.
    5. Creates an infinite loop
  37. How many lines of output will the following code fragment print?
    int counter = 0;
    
    do
    {
    	cout << "counter = " << counter << endl;
    	counter++;
    } while (counter < 10);
    1. 0
    2. 1
    3. 10
    4. 11
    5. Creates an infinite loop
  38. How many lines of output will the following code fragment print?
    int counter = 100;
    
    while (counter < 100)
    {
    	cout << "counter = " << counter << endl;
    	counter++;
    }
    1. 0
    2. 1
    3. 100
    4. 101
    5. Creates an infinite loop
  39. How many lines of output will the following code fragment print?
    for (int i = 10; i < 10; i--)
    	cout << "Hello world" << endl;
    1. 0
    2. 1
    3. 9
    4. 10
    5. 11
    6. Creates an infinite loop
  40. How many lines of output will the following code fragment print?
    int counter = 100;
    do
    {
    	cout << "counter = " << counter << endl;
    	counter--;
    } while (counter < 10);
    1. 0
    2. 1
    3. 10
    4. 11
    5. Creates an infinite loop
  41. What output does the following code fragment produce?
    int i = 5;
    
    switch(i)
    {
    	default :
    		cout << "sixth\n";
    		break;
    	case 0 :
    		cout << "first\n";
    		break;
    	case 1 :
    		cout << "second\n";
    	case 10 :
    		cout << "third\n";
    	case 100 :
    		cout << "fourth\n";
    		break;
    	case 1000 :
    		cout << "fifth\n";
    		break;
    }
    1. first
    2. second
    3. third
    4. fourth
    5. third and fourth
    6. sixth
    7. Does not compile: the default statement must come at the end
  42. What output is produced by the following code fragment?
    int i = 100;
    
    switch(i)
    {
    	case 0 :
    		cout << "first\n";
    		break;
    	case 1 :
    		cout << "second\n";
    		break;
    	case 10 :
    		cout << "third\n";
    	case 100 :
    		cout << "fourth\n";
    	case 1000 :
    		cout << "fifth\n";
    		break;
    	default :
    		cout << "sixth\n";
    		break;
    }
    1. first
    2. second
    3. third
    4. fourth
    5. fourth and fifth, on separate lines
    6. fourth, fifth, and sixth, on separate lines
  43. How many lines of output are produced by the following code?
    for (int i = 0; i < 10; i++)
    	for (int j = 0; j < 10; j++)
    		cout << i * j << endl;
    1. 0
    2. 1
    3. 10
    4. 100
    5. 1000
    6. Will not compile or run
  44. How many lines of output are produced by the following code?
    for (int i = 0; i < 10; i++)
    {
    	if (i == 0)
    		break;
    		
    	for (int j = 0; j < 10; j++)
    		cout << i * j << endl;
    }
  45. How many lines of output are produced by the following code?
    for (int i = 0; i < 10; i++)
    {
    	if (i == 0)
    		continue;
    		
    	for (int j = 0; j < 10; j++)
    		cout << i * j << endl;
    }
  46. How many lines of output are produced by the following code?
    int counter = 0;
    
    while (counter++ < 10);
    	cout << "Hello world" << endl;
  47. How many lines of output will the following code fragment print?
    for (int i = 10; i < 10; i--);
    	cout << "Hello world" << endl;
    1. 0
    2. 1
    3. 9
    4. 10
    5. 11
    6. Creates an infinite loop
  48. How many lines of output will the following code fragment print?
    int counter = 100;
    while (counter < 100);
    {
    	cout << "counter = " << counter << endl;
    	counter++;
    }
    1. 0
    2. 1
    3. 100
    4. 101
    5. Creates an infinite loop
  49. How many lines of output will the following code fragment print?
    do
    {
    	int i = 0;
    	cout << i++ << endl;
    } while (i < 10);
    1. 0
    2. 1
    3. 9
    4. 10
    5. 11
    6. The code will not compile.
  50. Write a for loop that counts from 0 to 10 by twos: 0, 2, 4, ..., 10. You may not use if-statements, or the comma or conditional operators.
  51. Write a for loop that counts backwards from 10 to 0 by twos: 10, 8, 6, ..., 0. You may not use if-statements, or the comma or conditional operators.
  52. Write a complete program that: