Exam practice
Every question from the end of every unit, with hints and answers to open. After you check an answer, mark how you did, and come back to the ones you didn’t get.
10 questions
Unit 4 · True or False: Logic
Write the truth tables of AND, OR and NOT, and say in one sentence what each one does.
Answer
| A | B | A AND B | A OR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
- AND gives 1 only when both inputs are 1.
- OR gives 1 when at least one input is 1.
- NOT gives the opposite of its input.
Draw the symbols of the AND, OR and NOT gates, and label the inputs and the output of each.
Answer
As in Figure 4.2: AND is a D shape, flat at the back (left) and rounded at the front (right), with two inputs on the left and one output on the right. OR is a shield, with a curved back and a pointed front, with two inputs and one output. NOT is a triangle pointing right with a small circle at its point, with one input on the left and the output leaving the small circle.
Copy and complete the truth table.
| A | B | NOT A | NOT A OR B |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
Answer
| A | B | NOT A | NOT A OR B |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 |
The output is 0 in only one row: A is 1 and B is 0.
Explain the difference between OR in everyday speech and OR in logic. Give an example.
Answer
In everyday speech, “or” often means one or the other but not both: “Will you have tea or coffee?” expects one. In logic, A OR B is true when A is true, when B is true, and also when both are true. Example: “Students or teachers get a discount”: a student who is also a teacher still gets it. (One-or-the-other-but-not-both is a different operator, exclusive or, XOR.)
Two switches and a bulb are joined to a battery. Draw how the switches must be joined so that the bulb lights i. only when both switches are on, ii. when either switch is on. Which logical operator does each circuit make?
Answer
- i.The two switches one after the other in the same wire, in series: the current must pass through both, so both must be closed. This is AND.
- ii.The two switches side by side on two branches of the wire, in parallel: the current can pass through either one. This is OR.
(Drawings as in Figure 4.1.)
Write each rule as logic, then say which output it gives for the inputs shown.
- i.“The fan turns on when it is hot and someone is in the room.” hot = 1, someone = 0.
- ii.“You may borrow a book if you have a library card or a teacher’s note.” card = 0, note = 1.
- iii.“Go out to play unless it is raining.” play time = 1, raining = 1.
Answer
- i.hot AND someone = 1 AND 0 = 0: the fan stays off.
- ii.card OR note = 0 OR 1 = 1: you may borrow it.
- iii.play time AND NOT raining = 1 AND NOT 1 = 1 AND 0 = 0: you stay in.
A school’s computer lab door unlocks when a teacher’s card is shown, OR when a student’s card is shown AND it is lab time. Write the rule as logic with brackets, and draw its logic circuit with gates.
Hint
Which part must be worked out first: teacher OR student, or student AND lab time?
Answer
teacher OR (student AND labtime).
Circuit: student and labtime go into an AND gate. Its output and teacher go into an OR gate. The OR gate’s output goes to the door’s lock. (Without the brackets, AND would still be done first, so the rule is the same, but brackets make it clear.)
A program should say “Teenager” for ages 13 to 19. Sanjay writes if age > 12 or age < 20 then say “Teenager”. What is wrong? Give an age that shows the bug, and fix it.
Answer
or is true when either side is true, and every age is either more than 12 or less than 20, so the program says “Teenager” for everyone. Age 5 shows the bug (5 is less than 20), and so does 40 (40 is more than 12).
Fix: if age > 12 and age < 20 then say “Teenager”. Test it with 12 (no), 13 (yes), 19 (yes) and 20 (no).
You search for cricket and find too many pages about the insect. Write a better search, and explain how AND, OR and NOT change the number of pages a search finds.
Answer
For example cricket NOT insect, or cricket AND match. (In Google, cricket -insect.)
AND makes a search narrower: only pages with all the words, so fewer pages. OR makes it wider: pages with any of the words, so more pages. NOT leaves out the pages with a word, so fewer pages.
Why are computers built from logic gates? Explain how a gate is related to a switch, a bit, and the true and false values in a program.
Answer
A computer works only with bits, 1 and 0, which are stored and moved as on and off: a voltage on a wire or not. A logic gate is a tiny circuit of transistors (electrically controlled switches) that takes bits in and gives a bit out by a fixed rule, AND, OR or NOT. Because any rule about true and false can be written with AND, OR and NOT, gates wired together can do any rule: adding numbers, comparing them, choosing what to do next. In a program, true and false are the same two values as 1 and 0: when an if checks age > 12 and age < 20, it is finally worked out by gates switching inside the processor.
