## CS 1401, Quiz #5, Solutions

Date: Tuesday, February 20, 2007
Name: ____________________________________________________________
Class section (9:00 am or 10:30 am): _______________________________

1. For each of the following topics, write "yes" or "no" depending on whether this topic is covered in Chapter 5, the chapter that you were supposed to read before the class:

while loops yes

2-dimensional arrays no

for loops yes

user-defined methods no

2. Draw truth tables for "and" and "or" and use these truth tables to find the truth value of the statement ((3 > 2) || (2 > 3)) && (2 + 2 == 4).

```
Solution:

Let us denote true by T and false by F.

----------------      ----------------
| A | B | A && B |    | A | B | A || B |
----------------      ----------------
| T | T |   T    |    | T | T |   T    |
----------------      ----------------
| T | F |   F    |    | T | F |   T    |
----------------      ----------------
| F | T |   F    |    | F | T |   T    |
----------------      ----------------
| F | F |   F    |    | F | F |   F    |
----------------      ----------------

Here, 3 > 2 is T, 2 > 3 is F, so (3 > 2) || (2 > 3) is T || F, which is
T.

Since 2 + 2 == 4 is T, the whole expression
((3 > 2) || (2 > 3)) && (2 + 2 == 4) is T && T, which is T (true).

```
3. Write down a statement that, given three real numbers a, b, and x, assigns, to a Boolean variable in:
• the value true if x is within the closed interval [a, b] (i.e., if a <= x <= b) and
• value false otherwise.

```
Solution:

if (a <= x && x <= b)
{in = true;}
else
{in = false;}

```