CMPS 350 HW 8 - Control Structures

Click 'check' on each question or your score will not be recorded.

01. An implicit branch in a multiple selection statement 
    A. ensures that only one selection will be executed.
    B. eliminates the need for a break statement.
    C. prevents fall through.
    D. is the method used by C.
A B C D
02. Select the statements that are true of C's multiple selection statement 
   (the switch).
    A. C supports implicit fall through 
    B. there is a mechanism for a default choice
    C. the branch is explicit
    D. condition control supports integers and floating point numbers  
A B C D
03. The syntax of C#'s switch statement is shown below:

   switch (expression) {
      case constant-expression:
         statement
         jump-statement  
      [default:
         statement
         jump-statement]
   }

   What can you conclude about C#'s form of switch statement?
   A. It does not allow fall through.
   B. It could result in redundant code.
   C. both A and B are true. 
A B C
04. What is true concerning user-controlled loop control structures?
    A. You can jump out from anywhere inside the loop body.
    B. You can only jump out at the top or the bottom of the loop. 
    C. You can jump in anywhere inside the loop body.
    D. This type of control is implemented by unconditional branches.
A B C D
05. Select the true statements regarding break, continue and goto statements.
    A. they are all unconditional control statements 
    B. break can only be used in a loop 
    C. the continue statement exits a loop prematurely 
    D. they all require a corresponding label 
A B C D
06. What is true in this Perl loop mechanism? 
          foreach (@words)  {....}

   A. the number of loop iterations is the number of elements in the array 
   B. this mechanism is called an iterator    
   C. both A & B 
A B C
07. What is the output of this Fortran code?
      i = 3 
      j = 3 
   10 PRINT "x"
   15 PRINT "y" 
      i = i - 1
      IF (i .gt. 0) GOTO 15    // .gt. is "greater than"
      j = j - 1
      IF (j .gt. 0) GOTO 10       

A. xyyyyxyxyxy
B. xxxyyy
C. xyyyxyxy
A B C
08. Reading values from an array of integers on unknown size until you hit a 
    flag (say -999) can be solved with what type of looping control mechanism?
    A. counter controlled      
    B. logically controlled  
    C. A or B
A B C
09. Reading values from a file of unknown size until you hit EOF must be solved
    with what type of looping control mechanism?
    A. counter controlled      B. logically controlled    C. either type
A B C
10. Assume this is a legal statement in your language, where '=' is assignment
    and 'i' can be any numeric variable type. What do you know about the 
    language?

          while (i = floor(x))    
  
   A. a condition statement may be any arithmetic expression
   B. a condition statement must be a boolean expression
   C. both A and B are true    
A B C
11. Assume the following legal statement in the C language, where '=' is 
    assignment and 'i' can be any numeric variable type. What is true? 

                  while (i = 5)  {
                      i--;
                  }
    A. the loop will execute 5 times 
    B. the loop will execute 6 times 
    C. the loop will execute forever
A B C
12. What is true about the default enumeration value option in a switch?
    A. all unrepresented enumeration values will be caught by this option 
    B. this option should be omitted only if all possible enumeration values 
       have been tested
    C. it can lead to programmer laziness
    D. it is not supported by C 
A B C D
13. The following two control structures in C share the same semantics.
     // #1
     k = 1;
     loop:
     if (k > 10) then goto out
     k = k + 1;
     goto loop
     out: 

     // #2
     for (k = 1; k <=10; k++) 
         ;
T F
14. The following two control structures in C share the same semantics.

     int i;
     int j = 0;

     // #1
     for (i = 0; i < 3; i++) {
       switch (j+2) {
         case 3:
         case 2: j--; break;
         case 0: j++; break;
         default: j = 0;
       }
       if (j > 0) break;
       j = 3 - i;
     }

     // #2
     i = 0;
     while (i < 3) {
        if ((j+2 == 3) || (j+2 == 2))
           j--;
        else if (j+2 == 0)
           j++;
        else
           j = 0;
        if (j > 0)  
           break;
        j = 3 - i;
        i++;
     }
T F
15. What does the following C code do? (X has been initialized with ints)

    for (i = 0; i < n; i++) {
       for (j = 0; j < n; j++) {
          if (X[i][j] != 0)
             goto foo;
       }
       printf ("Found it at: %d\n", i);
       break;
       foo: 
     }
   A. finds the first row that contains all zeros
   B. finds the first row that contains no zeros
   C. finds all the rows that contain all zeros
   D. uses a goto to exit the loop prematurely at the first instance of 0  
A B C D
16. The following C code will accomplish what?

    while (i < n) {
       flag = 0;
       for (j = 0; j < n; j++) {
          if (X[i][j] < 0)        // assume X contains integers
             flag = 1;
       }
       if (flag == 1)  {
           printf ("value: %d\n", i);
           i = n ;  
       } 
       i++;
    }
   A. exit the while loop upon encountering a negative value 
   B. display the row containing the first negative value
   C. both A and B will occur   
A B C
17. What does it mean to use negative logic in a control structure? 
    A. control is released when the condition becomes false 
    B. control is released when the condition becomes true  
A B
18. This code is an example of a dangling else. Under C syntax which 'if' 
    is attached to the 'else'?  (C++,Java and C# share the same semantics)

      if (sum == 0)    // first
      if (count == 0)  // second 
      result = 0;
      else result = 1;  

   A. the first if 
   B. the second if
A B
 
19. C's switch statement does not have an implicit break thus supports fall 
    through (also called flow through). What will be printed when this C code 
    is executed for num = 2?

   switch (num) {
      case 1: printf("one ");   
      case 2: printf("two ");   
      case 3: printf("three ");   
      default: printf("whatever "); 
   }
   A. one two three whatever    B. two three whatever    C.  two three   
A B C
20. This function is tail-recursive.
  
   int stuff (int n) {
       if (n == 0) return 1
          return n * stuff (n-1);
   } 
T F
21. What is always true in counter-controlled loops regardless of the language?
    A. The loop variable must be an integer.
    B. The loop variable cannot be changed in the loop.
    C. The loop variable is evaluated no more than once per iteration.
    D. There must be an explicit loop variable.  
A B C D
22. What is true in the output of the three chunks of code below for n = 4?

    // Chunk #1 
    void junk1 (int n) {
        print n;
        if (n > 0)
           junk1 (n-1);
    }
    // Chunk #2 
    void junk2 (int n) {
        if (n > 0)
           junk2 (n-1);
        print n;
    }
    // Chunk #3 
    while (n > 0) {
         print n;
         n--;
    }
   A. #1 and #3 are identical
   B. #2 and #3 are identical
   C. #1, #2 and #3 are identical
   D. the output is different in all cases   
A B C D
23. This pseudo-code describes the control structure of a pre-test loop. 

     start 
     if (ctrl_expr)  
     do loop body
     goto start
T F
24. What is displayed in this C code if num is -1 and sum is 1 to start with?

     1 while (sum) {
     2    if (num < 0) {
     3       break;
     4    }
     5    sum--;
     7 }
     8 printf("Sum Value: %d",sum);

   A. 1       B. 0
A B
25. Which of the following cannot be accomplished without the use of an 
    unconditional labeled jump; i.e., a goto?

    A. entering a loop at somewhere other than the top
    B. exiting a loop at somewhere other than the bottom or top
    C. both require a goto   
A B C
The next questions refer to the Ada selective accept statement shown below. You can assume that concurrent threads are executing this code at the same time.
 
    loop
       select                                
          when x =>          
              accept pickup;
       or
          when y =>          
              accept dropoff;
       or 
         terminate;
       end select;
    end loop;  
26. It is an error if a selective accept statement in Ada has a guard on each 
    of its alternatives and all the guards evaluate to false. 
T F
27. Which statement is true concerning the Ada code?
    A. x and y are both evaluated when the select statement is hit
    B. if x and y are both true, the pickup request is always selected first
    C. both A & B   
A B C
28. The terminate call occurs when 
    A. there are no more requests pending  
    B. both guards are closed
    C. both A & B
A B C

  Right: Wrong: Percent: (Must be 100% for credit)

Errors:

Name (first name only): Sleipnir Username: 3-digit ID: