Boolean Logic - Worksheets, Questions and Revision
15 original exam-style questions - 11 pages of questions with a full mark scheme - free printable PDF.
Boolean Logic revision guideMore GCSE Computer Science practiceMock exams
This topic is chapter 2 of GCSE Computer Science Topic Workbook 2 and chapter 12 of GCSE Computer Science 10-Minute Tests.
2.4 Boolean Logic
| A | NOT A |
|---|---|
| 0 | |
| 1 |
| A | B | A AND B |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
| A | B | A OR B |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
- A) 0
- B) 1
- C) Cannot be determined
- D) Both 0 and 1
- A) 0
- B) 1
- C) Cannot be determined
- D) Both 0 and 1
| A | B | NOT A | X |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
| A | B | C | B OR C | Output |
|---|---|---|---|---|
| 0 | 0 | 0 | ||
| 0 | 0 | 1 | ||
| 0 | 1 | 0 | ||
| 0 | 1 | 1 | ||
| 1 | 0 | 0 | ||
| 1 | 0 | 1 | ||
| 1 | 1 | 0 | ||
| 1 | 1 | 1 |
| P | Q | X |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
| A | B | C | A OR B | NOT C | Output |
|---|---|---|---|---|---|
| 0 | 0 | 0 | |||
| 0 | 0 | 1 | |||
| 0 | 1 | 0 | |||
| 0 | 1 | 1 | |||
| 1 | 0 | 0 | |||
| 1 | 0 | 1 | |||
| 1 | 1 | 0 | |||
| 1 | 1 | 1 |
Explain how Boolean logic operators (AND, OR, NOT) could be used to validate this data, including at least one example of a condition that combines more than one operator.
count = 0
total = 0
for i = 1 to 5
mark = int(input("Enter a mark"))
if mark ≥ 40 AND mark ≤ 100 then
total = total + mark
count = count + 1
endif
next i
if count > 0 then
average = total / count
print(average) print("No valid marks")The program is run with the five inputs, in order: 35, 72, 100, 45, 105.
| i | mark | condition true? | total | count |
|---|---|---|---|---|
| 1 | 35 | |||
| 2 | 72 | |||
| 3 | 100 | |||
| 4 | 45 | |||
| 5 | 105 |
| A | B | C | Output |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
- A) (A AND B) OR (B AND C) OR (A AND C)
- B) A AND B AND C
- C) A OR B OR C
- D) NOT A AND B AND C
Using OCR Exam Reference Language, write an algorithm that repeats the input process until a valid PIN is entered, then outputs "PIN accepted".
| A | B | C | NOT A | NOT C | B AND NOT C | Output |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | ||||
| 0 | 0 | 1 | ||||
| 0 | 1 | 0 | ||||
| 0 | 1 | 1 | ||||
| 1 | 0 | 0 | ||||
| 1 | 0 | 1 | ||||
| 1 | 1 | 0 | ||||
| 1 | 1 | 1 |
Question 1
- NOT A = 1 when A = 0B1
- NOT A = 0 when A = 1B1
- Answer: A=0 -> NOT A=1; A=1 -> NOT A=0
Question 2
- (a) Row 1 (0,0) -> 0B1
- (a) Row 2 (0,1) -> 0B1
- (a) Row 3 (1,0) -> 0B1
- (a) Row 4 (1,1) -> 1B1
- (a) Answer: 0, 0, 0, 1
- (b) Row 1 (0,0) -> 0B1
- (b) Row 2 (0,1) -> 1B1
- (b) Row 3 (1,0) -> 1B1
- (b) Row 4 (1,1) -> 1B1
- (b) Answer: 0, 1, 1, 1
Question 3
- (a) A) 0 selectedB1
- (a) Answer: A) 0
- (b) B) 1 selectedB1
- (b) Answer: B) 1
Question 4
- (a) Row (0,0): NOT A=1, X=0B1
- (a) Row (0,1): NOT A=1, X=1B1
- (a) Row (1,0): NOT A=0, X=0B1
- (a) Row (1,1): NOT A=0, X=0B1
- (a) Answer: X values in row order: 0, 1, 0, 0
- (b) 1, caoB1
- (b) Answer: 1
Question 5
- (a) tempLow AND windowClosed combined correctly, oeM1
- (a) Full expression: heatingOn = (tempLow AND windowClosed) OR override, oe, brackets not requiredA1
- (a) Answer: heatingOn = (tempLow AND windowClosed) OR override
- (b) False (heating off), cao, ft from part (a) expressionB1
- (b) Answer: False (heating stays off)
Question 6
- Correct Output for rows 1-2 (A=0): 0, 0B1
- Correct Output for rows 3-4 (A=0): 0, 0B1
- Correct Output for rows 5-6 (A=1): 0, 1B1
- Correct Output for rows 7-8 (A=1): 1, 1B1
- Answer: Output column in row order: 0, 0, 0, 0, 0, 1, 1, 1
Question 7
- (a) X = NOT P AND Q, oeB1
- (a) Answer: X = NOT P AND Q
- (b) Row (0,0) -> 0B1
- (b) Row (0,1) -> 1B1
- (b) Row (1,0) -> 0B1
- (b) Row (1,1) -> 0B1
- (b) Answer: X values in row order: 0, 1, 0, 0
- (c) P=0 and Q=1, cao, ft from part (b)B1
- (c) Answer: P = 0, Q = 1
Question 8
- (a) NOT gate identifiedB1
- (a) OR gate identifiedB1
- (a) Answer: A NOT gate and an OR gate
- (b) Input B is connected to the input of the NOT gateB1
- (b) The output of the NOT gate and input A are connected to the two inputs of the OR gate, producing output XB1
- (b) Answer: B feeds into the NOT gate; the NOT gate's output and A both feed into the OR gate, which outputs X.
Question 9
- Correct relational operator used for length, e.g. usernameLength ≥ 8M1
- All three conditions correctly combined using ANDM1
- then branch outputs "Valid"M1
- else branch outputs "Invalid", with endif used to close the statement; any correct logic, ignore minor syntax errorsM1
- Answer:if usernameLength ≥ 8 AND hasDigit == True AND hasSpecialChar == True then
print("Valid")elseprint("Invalid")endif
Question 10
- Correct Output for rows 1-2: 0, 0B1
- Correct Output for rows 3-4: 1, 0B1
- Correct Output for rows 5-6: 1, 0B1
- Correct Output for rows 7-8: 1, 0B1
- Answer: Output column in row order: 0, 0, 1, 0, 1, 0, 1, 0
Question 11
- Level 1 Basic or partial explanation of how a single Boolean operator could be used in validation. Little or no reference to combining conditions, or an example given without explanation.1-2
- Level 2 Clear explanation of how AND, OR and/or NOT can be combined to validate both fields, supported by at least one correctly constructed combined condition (e.g. using AND to require the age to be in range and consent to be given).3-4
- Indicative content:
- AND can be used to require the age to be within range AND consent to be True at the same time, e.g. age >= 5 AND age <= 100 AND consent == True.
- The two boundary checks (age >= 5 and age <= 100) must both be True, which is exactly what AND tests for.
- OR could be used if there were multiple acceptable ways to satisfy a rule, e.g. accepting the record if consent == True OR an adult override flag is set.
- NOT could be used to invert a check, e.g. NOT consent to flag records where consent has not been given.
- A single combined condition reduces the need for multiple nested IF statements and makes the validation clearer and more efficient.
Question 12
- (a) i=1, mark=35: condition False, total=0, count=0B1
- (a) i=2, mark=72: condition True, total=72, count=1B1
- (a) i=3, mark=100: condition True, total=172, count=2B1
- (a) i=4, mark=45: condition True, total=217, count=3B1
- (a) i=5, mark=105: condition False, total=217, count=3B1
- (a) Answer: Final values after the loop: total=217, count=3
- (b) average = 217/3, awrt 72.3, ft from part (a) totalsB1
- (b) Answer: awrt 72.3 (217/3 = 72.333...)
Question 13
- (a) A) selected, caoB1
- (a) Answer: A
- (b) A valid row used correctly to support the choice, e.g. row A=1,B=1,C=0 gives Output=1 because A AND B = 1, matching option A but not options B, C or DB1
- (b) Answer: e.g. row A=1, B=1, C=0 gives Output=1; in option A, A AND B=1 so the expression is 1, matching the table. Option B needs all three inputs to be 1 so would give 0 here, so B is wrong.
Question 14
- A loop structure is used that repeats the input process (e.g. do...until or while...endwhile)1 mark
- The length of the entered PIN is correctly checked, e.g. pin.length == 4 (or ≠ 4 to flag invalid)1 mark
- Iteration (e.g. for i = 0 to 3) is used with IsDigit correctly applied to check that every character is a digit2 marks
- The length check and the digit check are correctly combined so that the loop only ends once both conditions are satisfied1 mark
- print("PIN accepted") is produced once, after a valid PIN has been entered1 mark
- Answer:do
pin = input("Enter a 4-digit PIN")
valid = True
if pin.length ≠ 4 then
valid = False
else
for i = 0 to 3
if IsDigit(pin.substring(i, 1)) == False then
valid = False
endif
next i
endif
until valid == Trueprint("PIN accepted")
Question 15
- (a) Rows 1-2 (A=0): Output=1, 1B1
- (a) Rows 3-4 (A=0): Output=1, 1B1
- (a) Rows 5-6 (A=1): Output=0, 0B1
- (a) Rows 7-8 (A=1): Output=1, 0B1
- (a) Answer: Output column in row order: 1, 1, 1, 1, 0, 0, 1, 0
- (b) 3, cao, ft from part (a)B1
- (b) Answer: 3
Mark your answers
Check answers without an account. Some answers can be checked automatically; use the mark scheme to mark the rest.