## Relational and Logical Operators

Published:  June 25, 2007
By Richard G. Baldwin

File: Alice0170PracticeTest.htm

## Questions

1.  What is the value of C after the code in Listing 170-110 is executed?

Listing 170-110.
 ```public void main ( ) { Number A = 5 ; Number B = 5 ; Number C = 0 ; if ( ( A >= B ) ) { if ( ( A > B ) ) { C .set( value , 1 ); } else { C .set( value , 2 ); } } else { C .set( value , 3 ); } print( C ); }```

• A. 1
• B. 2
• C. 3
• D. none of the above

2.  Given the code in Listing 170-111, what is the range of the value of the variable result that is displayed after the while loop has finished executing?

Listing 170-111.
 ```public void main ( ) { Number number = 0 ; Number result = 0 ; number.set(value,(NumberDialog(question=Enter num))); while ( ( ( ( number < 0 ) || ( number > 100 ) ) ) ) { number.set(value,(NumberDialog(question=Enter num)))); } result .set( value , number ); print( result ); }```

• A. result contains any value greater than zero.
• B. result contains a value less than zero or greater than 100.
• C. result contains a value between 0 and 100, inclusive (that is, result is 0 or 100 or any value between 0 and 100).
• D. None of the above.

3.  Assume x and y are Boolean variables.  If x has the value true, and y has the value false, which of the following expressions evaluates to true?

• A. !(x && y)
• B. (!x || y)
• C. !(x || y)
• D. (x && y)

4.  True or False:  The following operators with the symbols given are supported by Alice:

1. Less than:  <
2. Greater than:  >
3. Less than or equal to:  <=
4. Greater than or equal to:  >=
5. Logical AND:  &
6. Logical inclusive OR:  ||
7. Logical NEGATION:  !

5.  True or False:  Operators 1 through 4 in the following list are relational operators.  Operators 5 through 7 in the list are combinational logic operators.

1. Less than:  <
2. Greater than:  >
3. Less than or equal to:  <=
4. Greater than or equal to:  >=
5. Logical AND:  &&
6. Logical inclusive OR:  ||
7. Logical NEGATION:  !

6.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A < B) will evaluate to true if A is algebraically less than B, and will evaluate to false otherwise.

7.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A > B) will evaluate to true if A is algebraically less than B, and will evaluate to false otherwise.

8.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A <= B) will evaluate to true if A is algebraically less than or is equal to B, and will evaluate to false otherwise.

9.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A >= B) will evaluate to true if A is algebraically less than or is equal to B, and will evaluate to false otherwise.

10.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A == B) will evaluate to true if A is equal to B, and will evaluate to false otherwise.

11.  True or False:  Assume that A and B are numeric variables or expressions that evaluate to numeric values.  The expression (A != B) will evaluate to true if A is equal to B, and will evaluate to false otherwise.

12.  True or False:  Assume that X and Y are Boolean variables or expressions that evaluate to the Boolean values of true and false.  The expression (X && Y) will evaluate to true if both X and Y are true, and will evaluate to false otherwise.

13.  True or False:  Assume that X and Y are Boolean variables or expressions that evaluate to the Boolean values of true and false.  The expression (X || Y) will evaluate to true if both X and Y are true, and will evaluate to false otherwise.

14.  True or False:  If X is true, !X evaluates to false.

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.

True

False

True

False

True

False

True

False

True

True

### Explanation 5

False Logical AND:  & is not the correct symbol.

A. !(x && y)

### Explanation 3

C. result contains a value between 0 and 100, inclusive (that is, result is 0 or 100 or any value between 0 and 100).