📚 IB & OCR Computer Science: Typical Example Questions Detailed Solutions | IB 与 OCR 计算机科学典型例题详解
This article presents a curated collection of typical exam-style questions drawn from IB Computer Science and OCR A/AS Level Computer Science curricula. Each section targets a core topic, providing a representative question followed by a step-by-step solution in English, immediately paired with the same in simplified Chinese. The explanations focus on reasoning, key concepts, and common pitfalls, helping students consolidate understanding and refine exam technique.
本文精选了来自 IB 计算机科学和 OCR 计算机科学 A/AS 级别的典型考题。每个小节聚焦一个核心主题,展示一道代表性题目,并给出逐步解答,英文与简体中文交替呈现。解析注重推理过程、核心概念与常见易错点,旨在帮助学生巩固理解并提升应试技巧。
1. Number Systems and Data Representation | 数字系统与数据表示
Typical question: Convert the hexadecimal number 2F3A to binary. Then convert the binary result to decimal, showing your working.
典型题目:将十六进制数 2F3A 转换为二进制,再将二进制结果转换为十进制,写出计算过程。
Solution: Each hexadecimal digit corresponds to exactly 4 bits. 2 becomes 0010, F (15) becomes 1111, 3 becomes 0011, A (10) becomes 1010. The binary representation is therefore 0010 1111 0011 1010. To convert to decimal, use place values from the most significant bit: 213 + 211 + 210 + 29 + 28 + 25 + 24 + 23 + 21 = 8192 + 2048 + 1024 + 512 + 256 + 32 + 16 + 8 + 2 = 12090 in decimal. (Alternative: sum directly using hex weights: 2×16³ + 15×16² + 3×16¹ + 10×16⁰ = 2×4096 + 15×256 + 48 + 10 = 8192 + 3840 + 58 = 12090.)
解答:每个十六进制位对应4个二进制位。2 写成 0010,F (15) 写成 1111,3 写成 0011,A (10) 写成 1010,所以二进制为 0010 1111 0011 1010。转换为十进制时,从最高位开始加权:213 + 211 + 210 + 29 + 28 + 25 + 24 + 23 + 21 = 8192 + 2048 + 1024 + 512 + 256 + 32 + 16 + 8 + 2 = 12090。(也可利用十六进制位权:2×16³ + 15×16² + 3×16¹ + 10×16⁰ = 12090。)
Common mistake: mixing up nibble boundaries; always pad to 4 bits per hex digit. For decimal, remember that positional values double moving left.
常见错误:混淆四位边界;每个十六进制位一定要补足4位。十进制转换时务必记得向左每一位权值加倍。
2. Logic Gates and Boolean Algebra | 逻辑门与布尔代数
Typical question: Construct a truth table for the logic expression Q = (A NAND B) OR (NOT C). Then draw the corresponding logic circuit.
典型题目:构造逻辑表达式 Q = (A NAND B) OR (NOT C) 的真值表,并画出对应的逻辑电路图。
Solution: There are three inputs, so 2³ = 8 rows. Compute intermediate columns: X = A NAND B, Y = NOT C, then Q = X OR Y. NAND is false only when both inputs are true.
解答:共三个输入,真值表需要 8 行。计算中间列:X = A NAND B,Y = NOT C,然后 Q = X OR Y。NAND 只有在两个输入都为真时才为假。
| A | B | C | A NAND B | NOT C | Q |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 1 |
| 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 | 0 | 0 |
The circuit diagram uses a NAND gate on A and B, a NOT gate on C, and an OR gate combining their outputs to produce Q. Hand-drawn circuits should label all inputs and outputs clearly.
电路图使用一个 NAND 门处理 A 和 B,一个 NOT 门处理 C,再用 OR 门将二者输出相或得到 Q。手绘电路时应清晰标注所有输入输出。
3. Pseudocode and Algorithm Tracing | 伪代码与算法追踪
Typical question: Study the following pseudocode. Determine the output when the input is N = 6.
典型题目:阅读以下伪代码,给出输入 N = 6 时的输出。
X = 1
Y = 0
while X <= N do
Y = Y + X
X = X + 2
endwhile
print Y
Solution: Initialise X = 1, Y = 0. Loop condition X <= 6. Iteration 1: Y = 0 + 1 = 1, X becomes 3. Iteration 2: Y = 1 + 3 = 4, X becomes 5. Iteration 3: Y = 4 + 5 = 9, X becomes 7. Now 7 > 6, loop ends. Output Y = 9. The algorithm sums odd numbers 1 + 3 + 5.
解答:初始化 X = 1,Y = 0。循环条件 X ≤ 6。第1次迭代:Y = 0 + 1 = 1,X 变为 3。第2次:Y = 1 + 3 = 4,X 变为 5。第3次:Y = 4 + 5 = 9,X 变为 7。此时 7 > 6,循环终止。输出 Y = 9。算法计算了奇数 1 + 3 + 5 的和。
Tracing tables help organise variable values and prevent off-by-one errors.
使用追踪表格记录变量变化能有效避免边界错误。
4. Searching and Sorting Algorithms | 查找与排序算法
Typical question: The array [8, 3, 6, 4, 9, 2] is sorted using bubble sort in ascending order. Show the state of the array after the first pass of the algorithm.
典型题目:对数组 [8, 3, 6, 4, 9, 2] 进行冒泡排序(升序)。写出第一次完整遍历后数组的状态。
Solution: In bubble sort, adjacent elements are compared and swapped if out of order. First pass: compare 8 and 3 → swap → [3, 8, 6, 4, 9, 2]; compare 8 and 6 → swap → [3, 6, 8, 4, 9, 2]; compare 8 and 4 → swap → [3, 6, 4, 8, 9, 2]; compare 8 and 9 → no swap; compare 9 and 2 → swap → [3, 6, 4, 8, 2, 9]. After first pass, the largest element 9 has bubbled to the end.
解答:冒泡排序比较相邻元素,逆序则交换。第一次遍历:比较8和3交换得 [3,8,6,4,9,2];比较8和6交换得 [3,6,8,4,9,2];比较8和4交换得 [3,6,4,8,9,2];比较8和9不交换;比较9和2交换得 [3,6,4,8,2,9]。第一次遍历后,最大元素 9 被移至末尾。
Understanding each pass is crucial for exam questions on tracing and efficiency. Bubble sort has O(n²) worst-case time complexity.
理解每次遍历对追踪题至关重要。冒泡排序最坏时间复杂度为 O(n²)。
5. Stack and Queue Operations | 栈与队列操作
Typical question: An empty stack undergoes these operations: push(5), push(12), pop(), push(7), push(3), pop(), push(1). List the remaining elements from bottom to top.
典型题目:一个空栈依次进行以下操作:push(5), push(12), pop(), push(7), push(3), pop(), push(1)。列出栈中从底到顶的剩余元素。
Solution: Stack follows LIFO. Start empty []. push(5) → [5]; push(12) → [5,12]; pop() removes 12 → [5]; push(7) → [5,7]; push(3) → [5,7,3]; pop() removes 3 → [5,7]; push(1) → [5,7,1]. Remaining elements bottom to top: 5, 7, 1.
解答:栈遵循后进先出。初始为空。push(5): [5]; push(12): [5,12]; pop() 移除 12 → [5]; push(7): [5,7]; push(3): [5,7,3]; pop() 移除 3 → [5,7]; push(1): [5,7,1]。从底到顶剩余元素为 5, 7, 1。
For queues (FIFO), similar tracing ensures correct front/rear pointers.
对于队列(先进先出),类似的追踪需注意正确维护队首和队尾指针。
6. Binary Trees and Traversals | 二叉树与遍历
Typical question: A binary search tree is built by inserting these values in order: 50, 30, 70, 20, 40, 60, 80. Perform a pre-order traversal and list the sequence of values visited.
典型题目:按顺序插入以下值构建二叉搜索树:50, 30, 70, 20, 40, 60, 80。进行前序遍历,列出访问序列。
Solution: First, build the BST: root 50, left child 30, right child 70. For 30: left 20, right 40. For 70: left 60, right 80. Pre-order (root, left, right): visit 50, then left subtree: 30, then its left 20, then its right 40; then right subtree: 70, left 60, right 80. Sequence: 50, 30, 20, 40, 70, 60, 80.
解答:先构建二叉搜索树:根50,左子30,右子70。30的左子20,右子40;70的左子60,右子80。前序遍历(根、左、右):访问50,然后左子树:30,其左20,其右40;再右子树:70,左60,右80。序列为 50, 30, 20, 40, 70, 60, 80。
In-order gives the sorted order, and post-order is used in deletion or expression trees.
中序遍历得到排序序列,后序遍历常用于删除操作或表达式树。
7. Computer Architecture: Fetch-Decode-Execute | 计算机体系结构:取指-译码-执行
Typical question: Describe the fetch stage of the fetch-decode-execute cycle, identifying the roles of the program counter (PC), memory address register (MAR), memory data register (MDR), and the instruction register (IR).
典型题目:描述取指-译码-执行周期中的取指阶段,说明程序计数器(PC)、内存地址寄存器(MAR)、内存数据寄存器(MDR)和指令寄存器(IR)的作用。
Solution: During fetch, the address in the PC is copied to the MAR. The PC is incremented to point to the next instruction. The control unit sends a read signal to memory; the instruction at the address in MAR is retrieved and placed in the MDR. The contents of the MDR are then copied to the IR for decoding. The PC usually increments by 1 (or by the instruction length).
解答:在取指阶段,PC 中的地址被复制到 MAR,然后 PC 自增以指向下一条指令。控制单元向内存发送读信号,MAR 中地址处的指令被取出并放入 MDR,接着 MDR 的内容复制到 IR 以供译码。PC 通常增加 1(或指令长度)。
Understanding each register prevents confusion in diagram labelling questions.
清晰理解各寄存器分工有助于应对图表标注类题目。
8. Networking and the TCP/IP Stack | 网络与TCP/IP协议栈
Typical question: Explain the function of the transport layer in the TCP/IP model, referring to port numbers and the difference between TCP and UDP.
典型题目:解释 TCP/IP 模型中传输层的功能,涉及端口号以及 TCP 和 UDP 的区别。
Solution: The transport layer provides end-to-end communication, using port numbers to distinguish between different applications on the same device. TCP offers connection-oriented, reliable delivery with error checking, acknowledgements and flow control; it guarantees ordered, error-free data. UDP is connectionless, faster but without guarantees; suitable for real-time applications like video streaming where occasional packet loss is acceptable. Both encapsulate data into segments/datagrams.
解答:传输层提供端到端通信,利用端口号区分同一设备上的不同应用程序。TCP 面向连接,提供可靠传输,具有差错校验、确认和流量控制,保证按序无错交付。UDP 无连接,速度更快但不可靠,适合视频流等允许偶发丢包的实时应用。二者都将数据封装为段或数据报。
Diagrams should show source and destination port numbers in the segment header.
示意图应标明段头部中的源端口号和目的端口号。
9. Object-Oriented Programming (OOP) Concepts | 面向对象编程(OOP)概念
Typical question: Using a real-world example, differentiate between a class and an object. Explain the principles of encapsulation and inheritance with reference to your example.
典型题目:结合实际例子,区分类与对象,并参照例子解释封装和继承的原则。
Solution: A class is a blueprint, e.g., a class 'Car' defines attributes like model, speed, and methods like accelerate(). An object is an instance: 'myCar = new Car("Sedan", 0)'. Encapsulation bundles data and methods, hiding internal state; e.g., the speed attribute is private and can only be modified via accelerate() or brake() methods, preventing invalid states. Inheritance allows a class 'ElectricCar' to extend 'Car', inheriting model and speed while adding batteryLevel, without rewriting common code.
解答:类是蓝图,例如“汽车”类定义了型号、速度等属性和 accelerate() 等方法。对象是实例:'myCar = new Car("Sedan", 0)'。封装将数据和方法绑定,隐藏内部状态;例如 speed 属性私有,只能通过 accelerate() 或 brake() 修改,避免非法状态。继承允许“电动汽车”类扩展“汽车”类,继承型号和速度,并添加 batteryLevel,无需重写通用代码。
Polymorphism is also frequently examined, allowing objects of different classes to be treated through a common interface.
多态性也常考,它允许通过统一接口处理不同类的对象。
10. Cybersecurity Threats and Mitigation | 网络安全威胁与防范
Typical question: Describe what a SQL injection attack is and suggest two methods developers can use to prevent it.
典型题目:描述什么是 SQL 注入攻击,并提出开发人员可采用的两种防范方法。
Solution: SQL injection occurs when user input is concatenated directly into SQL queries without validation, allowing an attacker to modify the query's logic. For example, inputting ' OR 1=1 --' could bypass login authentication. Prevention methods: (1) Use parameterised queries (prepared statements) which separate SQL code from data; (2) Input validation and sanitisation, such as escaping special characters or using whitelists. Additionally, applying least privilege to database accounts reduces impact.
解答:SQL 注入发生在将用户输入未经验证直接拼接到 SQL 查询中时,攻击者可篡改查询逻辑。例如输入 ' OR 1=1 --' 可绕过登录认证。防范方法:(1) 使用参数化查询(预编译语句),使代码与数据分离;(2) 输入验证与净化,如转义特殊字符或使用白名单。此外,为数据库账号分配最小权限可降低影响。
Always discuss both technical and procedural measures in security questions.
安全类题目应同时讨论技术措施和管理措施。
11. Finite State Machines (FSM) | 有限状态机
Typical question: Design an FSM that recognises strings over {a,b} ending with 'ab'. Draw the state transition diagram and give the state transition table.
典型题目:设计一个能识别字母表 {a,b} 上以 'ab' 结尾的字符串的有限状态机。画出状态转换图并给出状态转换表。
Solution: States: S0 (start/neither), S1 (last seen 'a'), S2 (accepting, last seen 'ab'). Transitions: From S0 on 'a' go to S1; on 'b' stay S0. From S1 on 'a' stay S1; on 'b' go to S2. From S2 on 'a' go to S1; on 'b' go to S0. This captures the pattern wherever it appears.
解答:状态:S0(初始,尚未匹配),S1(刚读入 'a'),S2(接受态,刚匹配 'ab')。转换规则:S0 读到 'a' 转至 S1,读到 'b' 留在 S0;S1 读到 'a' 留在 S1,读到 'b' 转至 S2;S2 读到 'a' 转至 S1,读到 'b' 转至 S0。无论 'ab' 出现在何处均可识别。
| Current State | Input a | Input b |
|---|---|---|
| S0 | S1 | S0 |
| S1 | S1 | S2 |
| S2 | S1 | S0 |
Mark the initial state with an incoming arrow and the accepting state with a double circle.
初始状态用无源箭头标出,接受态用双圈表示。
12. Computational Thinking and Flowcharts | 计算思维与流程图
Typical question: Draw a flowchart that reads positive integers until -1 is entered and outputs the average of the even numbers entered (excluding -1). If none, output 0.
典型题目:绘制流程图:不断读入正整数,直到输入 -1 结束,输出所有偶数的平均值(不含 -1)。若没有偶数则输出 0。
Solution: Flowchart needs decision symbols for checking evenness and termination. Maintain sum and count for evens. Structure: start, initialise sum=0, count=0. Input num. Decision: if num == -1, go to final calculation. Else if num % 2 == 0, then sum = sum + num, count = count + 1. Loop back for next input. After loop, if count > 0, output sum/count, else output 0. End.
解答:流程图需要判断框来检查偶数和终止条件。维护偶数和 sum 与计数 count。结构:开始,初始化 sum=0,count=0;输入 num;判断:若 num == -1 则转最终计算;否则若 num % 2 == 0 则 sum = sum + num,count = count + 1;循环读下一个数。循环结束后,若 count > 0 输出 sum/count,否则输出 0。结束。
Consistent use of standard flowchart symbols (oval for start/end, parallelogram for input/output, rectangle for process, diamond for decision) is required.
要求统一使用标准流程图符号:起止用椭圆形,输入输出用平行四边形,处理用矩形,判断用菱形。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导