Learn to Program using Alice

Practice Test for the Lesson Titled

Counter Loops, Nested Loops, and Sentinel Loops

Published:  June 28, 2007
By Richard G. Baldwin

File: Alice0175PracticeTest.htm


Questions

1.  Given the following conditions:

Listing 175-113.
public void main ( ) {
// A
  if ( 0 > temp ) {
    temp ++
  } else {
    Do Nothing
  }
  
// B
  if ( temp > 0 ) {
    temp ++
  } else {
    Do Nothing
  }
  
// C
  if ( temp < 0 ) {
    temp ++
  } else {
    Do Nothing
  }
}

Which block of code in Listing 175-113 satisfies the given conditions: A, B, or C?

Answer and Explanation

2.  After the execution of the code in Listing 175-114:    

Listing 175-114.
public void main ( ) {
  Number sum = 0 ; Number count = 0 ; Number num = 0 ; 
  Number average = 0 ;
  while ( ( count <= 3 ) ) {
    num.set( value , (NumberDialog(question = numb ) ) );
    sum.set( value , ( ( sum + num ) ) );
    count.set( value , ( ( count + 1 ) ) );
  }
  average.set( value , ( ( sum / count ) ) );
  print( sum );
  print( average );
  print( count );
}

Answer and Explanation

3.  Which type of loop is shown in Listing 175-115?

Listing 175-115.
public void main ( ) {
  for (int index=1; index< 10 times ; index++) {
    print( index );
  }
}

Answer and Explanation

4.  True or False:  The name counter loop results from the fact that a loop of this type runs an incremental counter and continues looping until the value of the counter reaches or exceeds a pre-specified limit.  The counter may increment by a value of one or by a value of more than one, but it increments by the same amount each time it increments.

Answer and Explanation

5.  True or False:  The name nested loop results from the fact that it consists of two or more loop structures following one another.

Answer and Explanation

6.  True or False:  A sentinel loop is a loop that continues looping until some value (the sentinel) attains a value of zero.

Answer and Explanation

7.  True or False:  The sentinel value used for a sentinel loop must be chosen carefully to ensure that it won't be confused with a valid data value.

Answer and Explanation

8.  True or False:  A while loop is the ideal syntax for a counting loop.

Answer and Explanation

9.  True or False:  The template for a for loop contains four clauses surrounded by parentheses and separated by semicolons:

Answer and Explanation

10.  True or False:  The counter declaration and initialization clause in an Alice for loop consists of everything between the left parentheses following the word for and the first semicolon.

Answer and Explanation

11.  True or False:  Type int is normally available to the Alice programmer

Answer and Explanation

12.  True or False:  It is not necessary to declare and initialize a counter variable for a for loop in Alice.

Answer and Explanation

13.  True or False:  The conditional clause in an Alice for loop is essentially the same as the conditional clause in a while loop or in an if-else construct.

Answer and Explanation

14.  True or False:  The counter update clause in an Alice for loop appears after the third semicolon and before the closing parentheses.

Answer and Explanation

15.  True or False:  Even though all three clauses are physically located in the header of the for loop, that isn't necessarily when and where they are executed.

Answer and Explanation

16True or False:  The counter declaration and initialization clause is executed once during each iteration of a for loop.

Answer and Explanation

17True or False:  The conditional clause is executed once and only once when control first enters the for loop structure.

Answer and Explanation

18True or False:  Like the while loop, the for loop is an exit-condition loop.

Answer and Explanation

19True or False:  If the expression in the conditional clause of a while loop or a for loop evaluates to false, the body of the loop is skipped and control passes to the code immediately following the loop structure.

Answer and Explanation

20True or False:  The counter update clause in a for loop is executed once during each execution of the body of the loop.  However, regardless of the fact that the update clause is physically located in the loop header at the top of the loop, it is not executed until after the last statement in the body of the loop has been executed.  In other words, it is executed immediately before control transfers back up to the conditional clause to make the next test.

Answer and Explanation

21True or False:  Testing two values of type Number for absolute equality is a safe and recommended way to write Alice code.

Answer and Explanation

22True or False:  The starting index value in an Alice for loop must always be zero.

Answer and Explanation

23True or False:  The additive assignment operator can be used in the counter update clause in an Alice for loop to let you specify the amount by which the counter variable will be increased each time the counter update clause is executed.

Answer and Explanation

24True or False:  It is possible to nest while loops and if-else constructs in Alice.  However, it is not possible to nest for loops.

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.


The following image is the splash screen from Alice 2.0, and is the property of the developers of Alice at Carnegie Mellon.

Answers and Explanations


Answer 24

False

Explanation 24

Back to Question 24
 


Answer 23

True

Explanation 23

Back to Question 23
 


Answer 22

False

Explanation 22

Back to Question 22
 


Answer 21

False

Explanation 21

Back to Question 21
 


Answer 20

True

Explanation 20

Back to Question 20
 


Answer 19

True

Explanation 19

Back to Question 19


Answer 18

False

Explanation 18

Back to Question 18


Answer 17

False

Explanation 17

Back to Question 17


Answer 16

False

Explanation 16

Back to Question 16


Answer 15

True

Explanation 15

Back to Question 15


Answer 14

False

Explanation 14

Back to Question 14


Answer 13

True

Explanation 13

Back to Question 13


Answer 12

True

Explanation 12

Back to Question 12


Answer 11

False

Explanation 11

Back to Question 11


Answer 10

True

Explanation 10

Back to Question 10


Answer 9

False

Explanation 9

Back to Question 9


Answer 8

False

Explanation 8

Back to Question 8


Answer 7

True

Explanation 7

Back to Question 7


Answer 6

False

Explanation 6

Back to Question 6


Answer 5

False

Explanation 5

Back to Question 5


Answer 4

True

Explanation 4

Back to Question 4


Answer 3

B. Counter Controlled

Explanation 3

Back to Question 3


 

Answer 2

D. All of the above

Explanation 2

Back to Question 2


Answer 1

B

Explanation 1

Back to Question 1


Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

The following image is the splash screen from Alice 2.0, and is the property of the developers of Alice at Carnegie Mellon.

-end-