CS 1401, Quiz #5

Date: Monday, October 1, 2007
Name: ____________________________________________________________
Class section (9:30 am or 11: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:

for loops yes

2-dimensional arrays no

while loops yes

linked lists no

2. Draw truth tables for "and" and "or" and use these truth tables to find the truth value of the statement (('a' > 'b') || ('c' > 'b')) && (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, 'a' > 'b' is F, 'c' > 'b' is T, so ('a' > 'b') || ('c' > 'b')
is F || T, which is T.

Since 2 * 2 == 4 is T, the whole expression
(('a' > 'b') || ('c' > 'b')) && (2 * 2 == 4) is T && T, which is T (true).

3. Write down a statement that, given three real numbers a, b, and x, prints "in" if x is strictly between a and b (i.e., if a < x < b) and "out" otherwise.

Solution:

if (a < x && x < b)
  {System.out.println("in");}
else
  {System.out.println("out");}