Chapter 5
Answers to Exam Preparation Exercises
1. Evaluating Boolean expressions.
(a) T
(b) T
(c) T (AND has higher precedence than OR)
(d) F
3. Now
I'm
Adam
6. Output from code fragments:
(a) 10
10
(b) The value of X is 3
(c) 3
7
6
5. What is written from nested IFs.
(a) Eligible to serve.
(b) Too short and too light to serve.
7. Rewriting expressions in Pascal.
(a) (X < Y) AND (Y <= Z)
(b) (X > 0) AND (Y > 0) AND ( Z > 0)
(c) (X <> Y) AND (X <> Z)
(d) (X = Y) AND (X = Z)
8. Evaluating Boolean expressions
(a) T
(b) F
(c) F
(d) T
(e) T
11. Inserting a BEGIN-END pair.
IF TypeA OR TypeB
THEN
BEGIN
IF TypeA AND TypeB
THEN
Writeln('Type AB')
END
ELSE
Writeln('Type O');
12. Data sets that test each of five branches.
A B C Expected
Output
Set 1 'A' 'A' 'A' All initials are the same.
Set 2 'A' 'A' 'B' First two are the same.
Set 3 'B' 'A' 'A' Last two are the same.
Set 4 'A' 'B' 'A' First and last are the same.
Set 5 'A' 'B' 'C' All initials are different.
Answers to Programming Warm-Up Exercises
2. Declaration and assignment for a Boolean variable.
VAR
Eligible:
Boolean;
Eligible := True
3. Candidate := (SATScore >= 1100) AND (GPA >= 2.5) AND (Age > 15);
5. Biggest of three variables I, J, and K, using IF-THEN statements:
Biggest := K;
IF J > Biggest
THEN
Biggest := J;
IF I > Biggest
THEN
Biggest := I;
using IF-THEN-ELSE statements:
IF I > J
THEN
IF I > K
THEN
Biggest := I
ELSE
Biggest := K
ELSE
IF J > K
THEN
Biggest := J
ELSE
Biggest := K;
8. Correcting syntax errors.
PROGRAM Exercise (Input, Output);
CONST
A = 10;
VAR
D,
E,
F:
Integer;
BEGIN
Read(D, E, F);
IF (D > A)
THEN
D := A + D
ELSE
D := A;
E := D + F;
Write('This program does not make sense', E, F, D)
END.
10. Program segment for slope of a line:
IF X1 = X2
THEN
Writeln('Slope undefined')
ELSE
BEGIN
M := (Y1 - Y2)/(X1 - X2);
Writeln('The slope is ',M)
END
11. Quadratic formula code segment:
Discriminant := B * B - 4 * A * C;
IF Discriminant < 0
THEN
Writeln('No real roots.')
ELSE
BEGIN
Root1 := (-B + Discriminant)/(2 * A);
Root2 := (-B - Discriminant)/(2 * A)
END
12. Smallest of three variables A, B, and C, using nested IF statements:
IF A < B
THEN
IF A < C
THEN
SmallestInteger := A
ELSE
SmallestInteger := C
ELSE
IF B < C
THEN
SmallestInteger := B
ELSE
SmallestInteger := C;