📚 IB CIE Computer Science: Worked Examples | IB CIE 计算机:典型例题详解
This article presents a collection of typical exam-style questions for IB and CIE Computer Science, covering key topics like binary arithmetic, logic, data structures, and programming. Each question is followed by a detailed, step-by-step solution to reinforce understanding.
本文汇集了IB和CIE计算机科学考试中的典型例题,涵盖二进制运算、逻辑、数据结构、编程等核心主题。每道例题均配有详细的逐步解析,以巩固理解。
1. Binary Addition and Overflow | 二进制加法与溢出
Question: Using 4-bit two’s complement representation, add 10102 and 01102. State whether overflow occurs and explain why.
题目:使用4位二进制补码表示,计算 10102 与 01102 的和。判断是否发生溢出并解释原因。
Solution: First, interpret the two’s complement values: 10102 is decimal -6, and 01102 is +6. Adding them:
解答:首先,解释补码值:10102 是十进制 -6,01102 是 +6。将它们相加:
10102 + 01102 = (1) 00002
In 4-bit arithmetic, we discard the carry‑out, giving 00002 (0 in decimal). Since adding -6 and +6 yields 0, which is within the representable range (-8 to +7), no overflow occurs. Overflow in two’s complement addition happens only when the carry into the MSB differs from the carry out of the MSB. Here, both are 1, so no overflow.
在4位运算中,我们丢弃进位,得到 00002(十进制 0)。由于 -6 加 +6 得 0,在可表示的范围内(-8 到 +7),因此没有发生溢出。补码加法的溢出仅当进入最高位的进位与出最高位的进位不同时才发生。这里两者都是 1,所以无溢出。
2. Logic Gates and Truth Tables | 逻辑门与真值表
Question: Construct the truth table for the Boolean expression Q = A AND (B OR NOT C). Then draw the corresponding logic circuit diagram.
题目:为布尔表达式 Q = A AND (B OR NOT C) 构造真值表,并画出对应的逻辑电路图。
Solution: The truth table requires 23 = 8 rows. We evaluate NOT C, then (B OR NOT C), and finally AND with A.
解答:真值表需要 23 = 8 行。我们依次计算 NOT C,然后是 (B OR NOT C),最后与 A 进行 AND。
| A | B | C | NOT C | B OR NOT C | Q |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 0 |
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | 1 | 1 |
The circuit uses one NOT gate for C, one OR gate with inputs B and NOT C, and one AND gate with inputs A and the OR output. Q is 1 only when A=1 and at least one of B or NOT C is 1.
电路需要为一个非门(用于C),一个或门(输入为 B 和 NOT C),以及一个与门(输入为 A 和或门的输出)。只有当 A=1 且 B 或 NOT C 中至少有一个为 1 时,Q 才为 1。
3. Karnaugh Map Simplification | 卡诺图简化
Question: A function F(A,B,C) is defined as Σ(1,3,4,5,6). Use a Karnaugh map to find the minimal sum-of-products expression.
题目:函数 F(A,B,C) 定义为 Σ(1,3,4,5,6)。使用卡诺图求出最简的积之和表达式。
Solution: We construct a 3-variable K-map (A as the row, BC as the columns). The order of BC is 00, 01, 11, 10. Minterms: 1 (001), 3 (011), 4 (100), 5 (101), 6 (110).
解答:我们构建一个3变量卡诺图(A 为行,BC 为列)。BC 的顺序为 00, 01, 11, 10。最小项:1 (001), 3 (011), 4 (100), 5 (101), 6 (110)。
| A \ BC | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 0 | 1 |
Groups are formed: (1,3) in the A=0 row and columns 01,11 → A’C. (4,5) in A=1 row, columns 00,01 → AB’. (4,6) in A=1 row, columns 00,10 → AC’. Thus, the minimal expression is:
分组如下:(1,3) 位于 A=0 行,列 01和11 → A’C。(4,5) 在 A=1 行,列 00和01 → AB’。(4,6) 在 A=1 行,列 00和10 → AC’。因此,最简表达式为:
F = A’C + AB’ + AC’
This expression uses three two-input AND gates and one three-input OR gate, minimising the hardware.
该表达式使用三个两输入与门和一个三输入或门,最大程度减少了硬件。
4. Pseudocode and Algorithm Tracing | 伪代码与算法追踪
Question: Study the following pseudocode and determine the final value of x.
x ← 2
y ← 10
while y > 0
x ← x * 2
y ← y - 3
endwhile
print x
题目:研究以下伪代码,确定最终 x 的值。
x ← 2
y ← 10
while y > 0
x ← x * 2
y ← y - 3
endwhile
print x
Solution: We trace the loop iteration by iteration.
解答:我们逐次迭代追踪循环。
| Iteration | x before | y before | x after multiplication | y after decrement |
|---|---|---|---|---|
| 1 | 2 | 10 | 4 | 7 |
| 2 | 4 | 7 | 8 | 4 |
| 3 | 8 | 4 | 16 | 1 |
| 4 | 16 | 1 | 32 | -2 |
After the 4th iteration, y becomes -2, which is no longer > 0, so the loop terminates. x is now 32. The printed output is 32.
在第4次迭代后,y 变为 -2,不再满足 > 0,循环终止。此时 x 为 32。程序输出 32。
5. Recursion Example | 递归示例
Question: The following recursive function calculates the factorial of n. Illustrate the call stack and compute factorial(4).
function factorial(n)
if n = 0 then return 1
else return n * factorial(n-1)
endfunction
题目:下面的递归函数计算 n 的阶乘。画出调用栈并计算 factorial(4)。
function factorial(n)
if n = 0 then return 1
else return n * factorial(n-1)
endfunction
Solution: The recursion unfolds as follows:
解答:递归展开过程如下:
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1 * factorial(0)
factorial(0) = 1
Now substitute back upwards: factorial(1) = 1 * 1 = 1; factorial(2) = 2 * 1 = 2; factorial(3) = 3 * 2 = 6; factorial(4) = 4 * 6 = 24. The base case n=0 stops the recursion and ensures a finite stack.
现在向上代入:factorial(1) = 1 * 1 = 1;factorial(2) = 2 * 1 = 2;factorial(3) = 3 * 2 = 6;factorial(4) = 4 * 6 = 24。基线条件 n=0 终止了递归,保证了栈的有限性。
6. Data Structures: Stacks | 数据结构:栈
Question: Starting with an empty stack, perform the following operations: push(‘A’), push(‘B’), pop(), push(‘C’), pop(), pop(). List the elements accessed by each pop and the final state of the stack.
题目:从一个空栈开始,执行以下操作:push(‘A’),push(‘B’),pop(),push(‘C’),pop(),pop()。列出每次 pop 获取的元素以及栈的最终状态。
Solution: We trace the stack (LIFO) after each step.
解答:我们追踪每一步后的栈(后进先出)。
-
After push(‘A’): stack contains [‘A’]
push(‘A’)后:栈含 [‘A’]
-
After push(‘B’): [‘A’, ‘B’] (B is top)
push(‘B’)后:[‘A’, ‘B’](B 在栈顶)
-
pop() returns ‘B’, stack now [‘A’]
pop() 返回 ‘B’,栈变为 [‘A’]
-
push(‘C’): [‘A’, ‘C’]
push(‘C’):[‘A’, ‘C’]
-
pop() returns ‘C’, stack [‘A’]
pop() 返回 ‘C’,栈 [‘A’]
-
pop() returns ‘A’, stack becomes empty []
pop() 返回 ‘A’,栈变为空 []
The pops retrieved ‘B’, ‘C’, ‘A’ in that order. The stack finishes empty.
各次 pop 依次获取 ‘B’, ‘C’, ‘A’。栈最终为空。
7. Object-Oriented Programming: Class Design | 面向对象编程:类设计
Question: Design a class BankAccount with a private attribute balance (real number) and public methods deposit(amount) and withdraw(amount). The withdraw method must not allow the balance to become negative. Write pseudocode for the class and demonstrate creating an account, depositing 500.00, and attempting to withdraw 600.00.
题目:设计一个类 BankAccount,包含私有属性 balance(实数)以及公有方法 deposit(amount) 和 withdraw(amount)。withdraw 方法不允许余额变为负数。写出该类的伪代码,并演示创建账户、存入 500.00 以及尝试取出 600.00 的过程。
Solution: The class encapsulates the balance and provides controlled access.
解答:该类封装了余额,并提供受控访问。
class BankAccount
private balance : real
public constructor(initialAmount : real)
if initialAmount >= 0 then
balance = initialAmount
else
balance = 0.0
endif
endconstructor
public deposit(amount : real)
if amount > 0 then
balance = balance + amount
endif
endprocedure
public withdraw(amount : real)
if amount > 0 and amount <= balance then
balance = balance - amount
else
output "Insufficient funds"
endif
endprocedure
endclass
// Demonstration:
account = new BankAccount(0.0)
account.deposit(500.00)
account.withdraw(600.00) // prints "Insufficient funds"
When withdrawing 600.00, the condition amount <= balance fails (600 > 500), so the balance remains 500.00 and an error message is shown.
当取款 600.00 时,条件 amount <= balance 不成立(600 > 500),因此余额保持为 500.00,并显示错误信息。
8. Operating System: Paging | 操作系统:分页
Question: A computer uses paging with a page size of 4 KB (4096 bytes). The logical address is 0x3A5B (hexadecimal). The page table entry for page 3 maps to frame 12. Find the physical address.
题目:一台计算机采用分页,页面大小为 4 KB(4096 字节)。逻辑地址为 0x
Published by TutorHao | IB Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导