Bỏ qua nội dung

Phát triển Sáng tạo

AP Nguyên lý khoa học máy tínhChủ đề 1

Bài học video cho chủ đề này Mở trang video
6:10

Phát triển Sáng tạo

Đây là một chương trình nên in trung bình cộng của hai số. Nó chạy. Nó không bị sập. Nó in ra một đáp án — và đáp án sai. Cho nó bốn và sáu…

Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung

1.1

Hợp tác

Chương trình

Hiểu biết bền vững (CRD-1): Việc tích hợp nhiều góc nhìn khác nhau thông qua hợp tác giúp cải thiện các sáng kiến công nghệ khi chúng được phát triển.

Mục tiêu học tập CRD-1.A: Giải thích cách các sáng kiến công nghệ được cải thiện thông qua sự hợp tác. [Kỹ năng 1.C]

  • CRD-1.A.1 Một sáng kiến công nghệ bao gồm một chương trình như một phần thiết yếu trong chức năng của nó.
  • CRD-1.A.2 Một sáng kiến công nghệ có thể là vật lý (ví dụ: ô tô tự lái), phần mềm máy tính phi vật lý (ví dụ: phần mềm chỉnh sửa ảnh), hoặc khái niệm máy tính phi vật lý (ví dụ: thương mại điện tử).
  • CRD-1.A.3 Sự hợp tác hiệu quả tạo ra một sáng kiến công nghệ phản ánh sự đa dạng về tài năng và góc nhìn của những người đã thiết kế ra nó.
  • CRD-1.A.4 Sự hợp tác bao gồm nhiều góc nhìn khác nhau giúp tránh thiên kiến trong quá trình phát triển các sáng kiến công nghệ.
  • CRD-1.A.5 Tham vấn và giao tiếp với người dùng là những khía cạnh quan trọng trong quá trình phát triển các sáng kiến công nghệ.
  • CRD-1.A.6 Thông tin thu thập được từ người dùng tiềm năng có thể được sử dụng để hiểu mục đích của một chương trình dưới nhiều góc nhìn khác nhau và để phát triển một chương trình tích hợp đầy đủ các góc nhìn này.

Mục tiêu học tập CRD-1.B: Giải thích cách các sáng kiến công nghệ được phát triển bởi các nhóm người. [Kỹ năng 1.C]

  • CRD-1.B.1 Các công cụ trực tuyến hỗ trợ hợp tác bằng cách cho phép các lập trình viên chia sẻ và cung cấp phản hồi về ý tưởng và tài liệu.
  • CRD-1.B.2 Các mô hình phổ biến như lập trình cặp đôi tồn tại để thuận tiện cho sự hợp tác.

Mục tiêu học tập CRD-1.C: Chứng minh các kỹ năng giao tiếp cá nhân hiệu quả trong quá trình hợp tác. [Kỹ năng 1.C]

  • CRD-1.C.1 Các đội hợp tác hiệu quả thực hành các kỹ năng giao tiếp cá nhân, bao gồm nhưng không giới hạn ở:
    • giao tiếp
    • xây dựng đồng thuận
    • giải quyết xung đột
    • đàm phán

Nguồn: Mô tả Khóa học và Bài thi College Board AP

English
A jigsaw in progress: collaboration and modular design piece the solution together
A jigsaw in progress: collaboration and modular design piece the solution together

In 1999 NASA lost the Mars Climate Orbiter, a spacecraft that had cost well over a hundred million dollars. The code was not badly written. One team's software gave its numbers in US units, and another team's software read them as metric units. Each piece worked alone; the failure was between the people. This unit is about how programs are really made: by groups, for users, in repeated cycles, with errors found and fixed along the way.

Computing is a collaborative 协作 activity. Working in a team brings more perspectives 视角, catches more errors, and produces better programs than working alone. Good collaboration uses consensus building 达成共识, clear communication, and each member's strengths. Pair programming 结对编程 – two people at one computer, one typing and one reviewing – is a common practice. On the exam, you should be able to explain how collaboration improved a program (more ideas, fewer bugs 缺陷, wider testing 测试).

A computing innovation 计算创新 is anything new that has a program as an essential part. It can be physical (a self-driving car), software (a photo editor), or a concept (online shopping). When a question says "computing innovation", any of the three will do.

Why different people matter. People with different backgrounds notice different problems. A team that includes different perspectives is more likely to avoid bias 偏见, because someone in the room will ask "what about users like me?". Developers also talk to users 用户 directly: what users say helps the team understand what the program is for and who it must work for.

Skills and tools. Good teams practise communication, consensus building, conflict resolution 解决冲突 and negotiation 协商. Online tools such as shared documents and shared code stores let people give feedback from anywhere.

Worked example. Two students build a quiz app. Mei writes the scoring procedure while Tom reviews each line as she types; later Tom asks five classmates to try the app, and two of them find that a blank answer crashes it. How did collaboration help?

  1. Tom reviews the code as it is typed (pair programming), which can catch errors early.
  2. The classmates are users with different habits; their tests found a case neither programmer had thought of, the blank answer.
  3. Each benefit is named and tied to the program: "more testers found the blank-answer crash" earns credit where "teamwork is good" does not.

Worked example: a disagreement. Two teammates disagree: Sara wants a timer on each quiz question, and Li says a timer will worry nervous users. Which is the best next step?

  • Sara and Li vote and the loser gives way: quick, but it ignores the users.
  • Each builds their own version and they never share it: the team splits.
  • They ask some users, then agree a rule together, for example a timer that users can turn off.

The third is consensus building: the decision comes from evidence, and both people accept it.

Từ vựng Luyện tập
English Tiếng Việt
collaborative/kəˈlæbrətɪv/ Hợp tác
perspectives/pəˈspektɪvz/ Góc nhìn
consensus building/kənˈsensəs ˈbɪldɪŋ/ xây dựng sự đồng thuận
Pair programming/peə ˈprəʊɡræmɪŋ/ Lập trình cặp đôi
bug/bʌɡ/ lỗi
testing/ˈtestɪŋ/ kiểm thử
computing innovation/kəmˈpjuːtɪŋ ˌɪnəˈveɪʃn/ Đột phá tính toán
bias/ˈbaɪəs/ thiên kiến
user/ˈjuːzə/ người dùng
conflict resolution/ˈkɒnflɪkt ˌrezəˈluːʃn/ giải quyết xung đột
negotiation/nɪˌɡəʊʃɪˈeɪʃn/ thương lượng
1.2

Chức năng và Mục đích của Chương trình

Chương trình

Hiểu biết bền vững (CRD-2): Các nhà phát triển tạo ra và đổi mới bằng quy trình thiết kế lặp lại lấy người dùng làm trung tâm, bao gồm các chu kỳ triển khai/phản hồi, và để lại nhiều không gian cho việc thử nghiệm và chấp nhận rủi ro.

Mục tiêu học tập CRD-2.A: Mô tả mục đích của một sáng kiến công nghệ. [Kỹ năng 1.A]

  • CRD-2.A.1 Mục đích của các sáng kiến công nghệ là giải quyết các vấn đề hoặc theo đuổi sở thích thông qua sự sáng tạo.
  • CRD-2.A.2 Hiểu rõ mục đích của một sáng kiến công nghệ mang lại cho các nhà phát triển khả năng tốt hơn trong việc phát triển sáng kiến đó.

Mục tiêu học tập CRD-2.B: Giải thích cách một chương trình hoặc đoạn mã hoạt động. [Kỹ năng 4.A]

  • CRD-2.B.1 Chương trình là một tập hợp các câu lệnh chương trình thực hiện một nhiệm vụ cụ thể khi chạy trên máy tính. Một chương trình thường được gọi là phần mềm.
  • CRD-2.B.2 Đoạn mã là một tập hợp các câu lệnh chương trình là một phần của một chương trình.
  • CRD-2.B.3 Một chương trình cần hoạt động tốt cho nhiều loại dữ liệu đầu vào và tình huống khác nhau.
  • CRD-2.B.4 Hành vi của một chương trình là cách chương trình hoạt động trong quá trình thực thi và thường được mô tả thông qua cách người dùng tương tác với nó.
  • CRD-2.B.5 Một chương trình có thể được mô tả một cách tổng quát bằng những gì nó làm, hoặc chi tiết hơn bằng cả những gì chương trình làm và các câu lệnh trong chương trình thực hiện chức năng này như thế nào.

Mục tiêu học tập CRD-2.C: Xác định đầu vào của một chương trình. [Kỹ năng 3.A]

  • CRD-2.C.1 Đầu vào chương trình là dữ liệu được gửi đến máy tính để xử lý bởi một chương trình. Dữ liệu đầu vào có thể đến từ nhiều dạng khác nhau, chẳng hạn như xúc giác, âm thanh, hình ảnh hoặc văn bản.
  • CRD-2.C.2 Một sự kiện gắn liền với một hành động và cung cấp dữ liệu đầu vào cho một chương trình.
  • CRD-2.C.3 Sự kiện có thể được tạo ra khi một phím được nhấn, chuột được nhấp, chương trình được khởi chạy, hoặc bất kỳ hành động nào khác được xác định xảy ra ảnh hưởng đến luồng thực thi.
  • CRD-2.C.4 Dữ liệu đầu vào thường ảnh hưởng đến kết quả mà một chương trình tạo ra.
  • CRD-2.C.5 Trong lập trình dựa trên sự kiện, các câu lệnh chương trình được thực thi khi bị kích hoạt thay vì thông qua luồng điều khiển tuần tự.
  • CRD-2.C.6 Dữ liệu đầu vào có thể đến từ người dùng hoặc các chương trình khác.

Mục tiêu học tập CRD-2.D: Xác định đầu ra được tạo ra bởi một chương trình. [Kỹ năng 3.A]

  • CRD-2.D.1 Đầu ra chương trình là bất kỳ dữ liệu nào được gửi từ một chương trình đến một thiết bị. Kết quả đầu ra của chương trình có thể đến từ nhiều dạng khác nhau, chẳng hạn như xúc giác, âm thanh, hình ảnh hoặc văn bản.
  • CRD-2.D.2 Kết quả đầu ra của chương trình thường dựa trên dữ liệu đầu vào của chương trình hoặc trạng thái trước đó (ví dụ: các giá trị nội bộ).

Nguồn: Mô tả Khóa học và Bài thi College Board AP

English

Every program is written for a purpose 目的 – it solves a problem or pursues an interest. A program takes input 输入, processes it, and produces output 输出. Inputs can come from a user, a device, a file, or another program; outputs can be visual, audible, textual, or a signal to a device. Being able to state a program's purpose, and describe its inputs and outputs clearly, is a core skill (and part of the Create performance task).

Some words the exam uses exactly:

  • A program 程序 (also called software 软件) is a collection of statements that does a task when a computer runs it. A code segment 代码段 is one part of a program.
  • A program's behavior 行为 is how it acts while it runs, usually described by how a user interacts with it.
  • Input and output can each be tactile 触觉的 (touch, vibration), audio, visual or text.
  • Output usually depends on the input, and it can also depend on prior state: values the program already holds.

You can describe a program broadly (what it does), or in more detail (what it does and how its statements do it). A written response usually needs the second.

Every program decomposes into input, processing, and output
Every program decomposes into input, processing, and output
Every program follows the input-processing-output model
Every program follows the input-processing-output model

Worked example. A step-counter app reads movement from the phone's motion sensor, counts steps, and shows the day's total and a badge when the user reaches 10,000.

Ask Answer
Purpose: why it exists to help users stay active by tracking their walking
Function: what it does counts steps and compares the total with a goal
Input readings from the motion sensor
Output the step total on screen; a badge at 10,000

The badge also shows prior state at work: the same step of input gives a badge only if the stored total has just reached the goal.

A common mistake is to give the function as the purpose: "it counts steps" says what the program does, not the problem it solves.

Khám phá

Khám phá mô hình đầu vào → xử lý → đầu ra

Bước qua mô hình IPO. Mọi chương trình nhận một số đầu vào, thực hiện xử lý trên đó bằng cách tuân theo các hướng dẫn, sau đó tạo ra đầu ra — truy vết một ví dụ ứng dụng thời tiết dọc theo quy trình này.

Từ vựng Luyện tập
English Tiếng Việt
purpose/ˈpɜːpəs/ Mục đích
input/ˈɪnpʊt/ dữ liệu đầu vào
output/ˈaʊtpʊt/ dữ liệu đầu ra
program/ˈprəʊɡræm/ Chương trình
software/ˈsɒftweə/ Phần mềm
code segment/kəʊd ˈseɡmənt/ đoạn mã
behavior/bɪˈheɪvjə/ Hành vi
tactile/ˈtæktaɪl/ thuộc xúc giác
1.3

Thiết kế và Phát triển Chương trình

Chương trình

Hiểu biết bền vững (CRD-2): Các nhà phát triển tạo ra và đổi mới bằng quy trình thiết kế lặp lại lấy người dùng làm trung tâm, bao gồm các chu kỳ triển khai/phản hồi, và để lại nhiều không gian cho việc thử nghiệm và chấp nhận rủi ro.

Mục tiêu học tập CRD-2.E: Phát triển một chương trình sử dụng quy trình phát triển. [Kỹ năng 1.B]

  • CRD-2.E.1 Một quy trình phát triển có thể được sắp xếp có chủ đích, hoặc mang tính chất khám phá.
  • CRD-2.E.2 Có nhiều quy trình phát triển khác nhau. Các giai đoạn sau đây thường được sử dụng khi phát triển một chương trình:
    • nghiên cứu và phản tư
    • thiết kế
    • tạo mẫu
    • kiểm thử
  • CRD-2.E.3 Một quy trình phát triển lặp lại yêu cầu tinh chỉnh và sửa đổi dựa trên phản hồi, kiểm thử hoặc phản tư trong suốt quá trình. Điều này có thể đòi hỏi việc quay lại các giai đoạn trước đó của quy trình.
  • CRD-2.E.4 Một quy trình phát triển tăng dần là quy trình chia nhỏ vấn đề thành các phần nhỏ hơn và đảm bảo mỗi phần hoạt động đúng trước khi thêm vào tổng thể.

Mục tiêu học tập CRD-2.F: Thiết kế một chương trình và giao diện người dùng của nó. [Kỹ năng 1.B]

  • CRD-2.F.1 Việc thiết kế một chương trình bao gồm nghiên cứu để xác định các yêu cầu của nó.
  • CRD-2.F.2 Nghiên cứu trong một quy trình phát triển hữu ích để hiểu và xác định các ràng buộc của chương trình, cũng như những mối quan tâm và sở thích của những người sẽ sử dụng chương trình.
  • CRD-2.F.3 Một số cách thức thực hiện nghiên cứu bao gồm các phương pháp sau:
    • thu thập dữ liệu thông qua khảo sát
    • kiểm thử người dùng
    • phỏng vấn
    • quan sát trực tiếp
  • CRD-2.F.4 Các yêu cầu của chương trình mô tả cách một chương trình vận hành và có thể bao gồm mô tả về các tương tác người dùng mà chương trình phải cung cấp.
  • CRD-2.F.5-alt specification của một chương trình xác định các yêu cầu đối với chương trình đó.
  • CRD-2.F.6 Trong một quy trình phát triển, giai đoạn thiết kế phác thảo cách thức thực hiện một-alt specification của chương trình đã cho.
  • CRD-2.F.7 Giai đoạn thiết kế của một chương trình có thể bao gồm:
    • brainstorming (tạo ý tưởng)
    • lên kế hoạch và storyboarding
    • tổ chức chương trình thành các module và thành phần chức năng
    • tạo các biểu đồ đại diện cho bố cục giao diện người dùng
    • phát triển chiến lược kiểm thử cho chương trình

Mục tiêu học tập CRD-2.G: Mô tả mục đích của một đoạn mã hoặc chương trình bằng cách viết tài liệu. [Kỹ năng 4.A]

  • CRD-2.G.1 Tài liệu chương trình là một bản mô tả bằng văn bản về chức năng của một đoạn mã, sự kiện, thủ tục hoặc chương trình và cách nó được phát triển.
  • CRD-2.G.2 Nhận xét (comments) là một dạng tài liệu chương trình được viết vào trong chương trình để con người đọc và không ảnh hưởng đến cách chương trình chạy.
  • CRD-2.G.3 Các lập trình viên nên ghi chú tài liệu cho chương trình trong suốt quá trình phát triển.
  • CRD-2.G.4 Tài liệu chương trình giúp trong việc phát triển và duy trì các chương trình chính xác khi làm việc cá nhân hoặc trong môi trường lập trình hợp tác.
  • CRD-2.G.5 Không phải tất cả các môi trường lập trình đều hỗ trợ nhận xét, do đó có thể cần các phương pháp tài liệu khác.

Mục tiêu học tập CRD-2.H: Ghi nhận các đoạn mã được sử dụng từ các nguồn khác. [Kỹ năng 1.C]

  • CRD-2.H.1 Việc ghi nhận bất kỳ đoạn mã nào được phát triển theo cách hợp tác hoặc bởi một nguồn khác là rất quan trọng.
  • CRD-2.H.2 Việc ghi nhận một đoạn mã (hoặc các đoạn mã) được viết bởi người khác và được sử dụng trong một chương trình có thể nằm trong tài liệu chương trình. Phần ghi nhận nên bao gồm nguồn gốc hoặc tên tác giả ban đầu.

Nguồn: Mô tả Khóa học và Bài thi College Board AP

English
A programmer debugging at a multi-monitor workstation — iterative design and testing
A programmer debugging at a multi-monitor workstation — iterative design and testing

Programs are built through an iterative 迭代的 process, not in one straight line: investigate the problem and users, design (often with a diagram 图示 or written plan), implement 实现 in code, and test – then repeat. A large problem is broken into smaller pieces (decomposition 分解). Comments 注释 and clear naming document the design so others (and your future self) can understand it. Development is incremental 增量的 – build and test a small piece, then add the next.

Common phases are investigating and reflecting, designing, building a prototype 原型 (an early working model), and testing. The order can be planned in advance or more exploratory, and a team may go back to an earlier phase at any time.

Worked example: iterative or incremental? The two words sound alike, and the exam separates them.

What the team does Word Why
Testers say the text is too small, so the team returns to the design and changes it iterative feedback sends them back to revise
The team makes questions display, tests that, then adds scoring, tests that, then adds a timer incremental one piece at a time, each working before it joins the whole

A real project usually does both.

The stages of program development, with testing feeding back to fix and refine
The stages of program development, with testing feeding back to fix and refine
Software is built by an iterative, incremental development process
Software is built by an iterative, incremental development process

Investigating what users actually need

Before any code is written, the developer investigates the problem and the people who will use the program. Ways to do that:

  • surveys 调查问卷 sent to potential users, which collect data from many people quickly;
  • interviews 访谈 and direct observation 观察 of users doing the task by hand;
  • user testing 用户测试, where people try an early version while the team watches;
  • studying existing solutions to see what already works and what frustrates people.

The findings are turned into a design. Two artefacts do that: a program requirements 程序需求 list saying exactly what the program must do, and diagrams representing the layout of the user interface 用户界面 — sketches showing which controls appear where, and what each one does when used. Designing the interface on paper first is cheaper than discovering after coding that the buttons are in the wrong place.

A program's specification 规格说明 defines its requirements. The design phase then plans how to meet them. It may include brainstorming, a storyboard 故事板 (a sequence of sketches of what the user sees), dividing the program into modules, and planning how it will be tested.

Events, and programs that wait

Not every program runs straight through from top to bottom. An event 事件 is generated when a key is pressed, a mouse is clicked, a program is started, or any other defined action occurs — and an event can change the flow of execution 执行流程 by triggering code attached to that event, called an event handler 事件处理程序. In many graphical programs, events wait in a queue until the current handler finishes.

This is why a program with a graphical interface can appear to be doing nothing: it is waiting for the next event. The order in which those events arrive depends on user actions and other event sources, so the same program can run its blocks in a different order each time it is used.

This style is called event-driven programming 事件驱动编程: statements run when they are triggered, not one after another from the top. The event also supplies input data, such as which key was pressed.

Documentation and credit

Program documentation 程序文档 is a written description of what a code segment, event, procedure or program does and how it was developed. Comments are one form: they are written for people and do not change how the program runs. Not every programming environment allows comments, so documentation sometimes lives in a separate file.

Write documentation throughout development, not at the end: it helps you and your teammates keep the program correct.

Code from someone else must be acknowledged: say in the documentation where it came from, with the original author's name if you know it. This applies to code written with a partner too.

Khám phá

Lặp qua quy trình phát triển lặp lại

Phát triển mang tính lặp lại — bạn lặp lại các giai đoạn, cải thiện chương trình một chút ở mỗi lần. Bước quanh vòng lặp và nhận thấy nó quay về điểm xuất phát thay vì kết thúc sau một lần chạy.

Từ vựng Luyện tập
English Tiếng Việt
iterative/ˈɪtərətɪv/ Lặp lại
diagram/ˈdaɪəɡræm/ sơ đồ
implement/ˈɪmplɪmənt/ triển khai
decomposition/ˌdiːkɒmpəˈzɪʃn/ phân tách
Comments/ˈkɒments/ ghi chú
incremental/ˌɪŋkrɪˈmentl/ Tăng tiến
prototype/ˈprəʊtəʊtaɪp/ Bản mẫu thử nghiệm
surveys/ˈsɜːveɪz/ bảng khảo sát
interview/ˈɪntəvjuː/ phỏng vấn
observation/ɒbzəˈveɪʃn/ quan sát
user testing/ˈjuːzə ˈtestɪŋ/ kiểm thử với người dùng
program requirements/ˈprəʊɡræm rɪˈkwaɪəmənts/ yêu cầu chương trình
user interface/ˈjuːzə ˈɪntəfeɪs/ giao diện người dùng
specification/ˌspesɪfɪˈkeɪʃn/ đặc tả
storyboard/ˈstɔːribɔːd/ bảng phân cảnh
event/ɪˈvent/ sự kiện
flow of execution/fləʊ ɒv ˌeksɪˈkjuːʃn/ luồng thực thi
event handler/ɪˈvent ˈhændlə/ Trình xử lý sự kiện
event-driven programming/ɪˈvent ˈdrɪvən ˈprəʊɡræmɪŋ/ lập trình hướng sự kiện
program documentation/ˈprəʊɡræm ˌdɒkjuːmənˈteɪʃn/ tài liệu chương trình
1.4

Xác định và Sửa chữa Lỗi

Chương trình

Hiểu biết bền vững (CRD-2): Các nhà phát triển tạo ra và đổi mới bằng quy trình thiết kế lặp lại lấy người dùng làm trung tâm, bao gồm các chu kỳ triển khai/phản hồi, và để lại nhiều không gian cho việc thử nghiệm và chấp nhận rủi ro.

Mục tiêu học tập CRD-2.I: Đối với các lỗi trong thuật toán hoặc chương trình: a. Xác định lỗi. [Kỹ năng 4.C] b. Sửa lỗi. [Kỹ năng 4.C]

  • CRD-2.I.1 Một lỗi logic là một sai sót trong thuật toán hoặc chương trình khiến nó hoạt động không đúng hoặc bất ngờ.
  • CRD-2.I.2 Một lỗi cú pháp là một sai sót trong chương trình nơi các quy tắc của ngôn ngữ lập trình không được tuân thủ.
  • CRD-2.I.3 Một lỗi thời gian chạy là một sai sót trong chương trình xảy ra trong quá trình thực thi chương trình. Các ngôn ngữ lập trình xác định các lỗi thời gian chạy riêng của chúng.
  • CRD-2.I.4 Một lỗi tràn là một lỗi xảy ra khi máy tính cố gắng xử lý một số nằm ngoài phạm vi giá trị đã được xác định.
  • CRD-2.I.5 Các phương pháp hiệu quả sau đây để tìm và sửa lỗi:
    • các trường hợp kiểm thử
    • truy vết tay
    • trực quan hóa
    • trình gỡ lỗi
    • thêm câu lệnh xuất dữ liệu phụ

Mục tiêu học tập CRD-2.J: Xác định các đầu vào và các kết quả hoặc hành vi mong đợi tương ứng có thể được sử dụng để kiểm tra tính đúng đắn của một thuật toán hoặc chương trình. [Kỹ năng 4.C]

  • CRD-2.J.1 Trong quá trình phát triển, kiểm thử sử dụng các đầu vào đã xác định để đảm bảo rằng thuật toán hoặc chương trình đang tạo ra các kết quả mong đợi. Các lập trình viên sử dụng kết quả từ việc kiểm thử để sửa đổi thuật toán hoặc chương trình của họ.
  • CRD-2.J.2 Các đầu vào đã xác định dùng để kiểm thử chương trình nên minh họa các kết quả mong đợi khác nhau nằm ở mức hoặc ngay bên ngoài cùng cực (tối thiểu và tối đa) của dữ liệu đầu vào.
  • CRD-2.J.3 Các yêu cầu về chương trình là cần thiết để xác định các đầu vào đã xác định phù hợp cho việc kiểm thử.

Nguồn: Mô tả Khóa học và Bài thi College Board AP

English

A bug is an error in a program; debugging 调试 is finding and fixing it. The exam names four kinds:

  • a syntax error 语法错误 breaks the language's rules, so the program will not run;
  • a runtime error 运行时错误 crashes the program while it runs (e.g. dividing by zero);
  • a logic error 逻辑错误 lets it run but gives the wrong result;
  • an overflow error 溢出错误 happens when a number is outside the range the computer can handle.
A trace table records each variable's value as the program runs, to find bugs
A trace table records each variable's value as the program runs, to find bugs

Find bugs by test cases 测试用例 with different inputs, adding print statements 打印语句 to see values, and hand-tracing 手动跟踪 the code. A debugger 调试器 stops the program at a chosen line so you can look at each variable, and a visualization 可视化 such as a chart can make a wrong pattern easy to see. Choose the test inputs deliberately: they should demonstrate the different expected outcomes at or just beyond the extremes — the minimum and maximum values the program should accept, and a value just outside each of them. A program that works on ordinary data very often fails on an empty list, a zero, or a value one past the end of a range, so those are the inputs worth trying first. Fixing one bug at a time and re-testing is the reliable method.

Worked example: choosing test inputs. A club sign-up page should accept ages 13 to 18. The requirements tell you the correct result, so you can choose inputs and say what you expect:

A number line of ages with 13 to 18 marked as accepted; tests at 12 and 19 are rejected and tests at 13, 15 and 18 are accepted
Test both ends of the range, and one step past each end

Five tests are enough: 12, 13, 15, 18 and 19. If the code says age > 13, only the test with 13 shows the fault; age 15 passes either way.

Exam skill: be able to name the type of an error and describe a testing strategy that would catch it – a recurring multiple-choice and Create-task theme.

Three kinds of programming error: syntax, logic, and runtime
Three common kinds of programming error: syntax, logic, and runtime

Worked example. A program meant to print the average of two numbers instead runs avg = a + b / 2. Tracing the order of operations 运算顺序, / runs before +, so it computes $a+\tfrac{b}{2}$ rather than the average. Add parentheses to fix it: avg = (a + b) / 2. Testing with $a=4,\ b=6$ confirms the fix — the buggy line gives $4+3=7$, the corrected line gives $\tfrac{10}{2}=5$. Testing with known inputs is exactly how you find and confirm a logic error.

Worked example: name the error.

What happens Type Reason
DISPLAY(score has no closing bracket; nothing runs syntax a language rule is broken
list[0] stops the program with an error message run-time AP lists start at index 1, and this only fails while running
A discount of 10% is taken as 10 dollars logic the program runs and the answer is wrong
A 4-bit counter at 1111 has 1 added overflow 16 is outside the range

Written responses about your own program

Section II of the exam gives 60 minutes for two questions with four written prompts, all about the program you submitted for the Create task. You see your own code segments in your Personalized Project Reference, so answers must be about your program, not programs in general. Imagine this procedure is in yours:

PROCEDURE Average(numbers)
{
   sum ← 0
   FOR EACH n IN numbers
   {
      sum ← sum + n
   }
   RETURN(sum / LENGTH(numbers))
}

Past prompts have asked for the following.

  1. A modification that causes a logic error, and how behavior changes. "If sum ← 0 is changed to sum ← 1, the procedure still runs, but every total is one too large. Average([2, 4]) returns $3.5$ instead of $3$."
  2. A call the procedure accepts but handles wrongly. "Average([]): the empty list is accepted, but LENGTH(numbers) is 0, so the last line divides by zero and a run-time error stops the program." State the call, the wrong behavior and the reason: three separate things.
  3. Documentation for another programmer. "A comment above the procedure saying that numbers must not be empty. Another programmer who reuses Average then knows to check for an empty list before calling it."
  4. An unexpected input. Name one real input to your program and say what your program actually does next. If it rejects the input, say which line does that.

Common mistakes. Describing what a modification is without saying what changes when the program runs. Giving a call without specific argument values. Answering about a different procedure from the one the prompt names.

Khám phá

Vẽ đường truy vết logic trò chơi đoán số và chỉ ra một lỗi logic

Kéo và thả giá trị dự đoán để xem nhánh nào được thực thi. Một lỗi logic sẽ đưa cùng một giá trị dự đoán vào nh nhánh sai — chương trình vẫn chạy, nhưng trả về thông báo sai. Số bí mật ở đây là 50.

Từ vựng Luyện tập
English Tiếng Việt
debugging/ˈdiːbʌɡɪŋ/ gỡ lỗi
syntax error/ˈsɪntæks ˈerə/ lỗi cú pháp
runtime error/ˈrʌntaɪm ˈerə/ lỗi thời gian chạy
logic error/ˈlɒdʒɪk ˈerə/ lỗi logic
overflow error/ˌəʊvəˈfləʊ ˈerə/ lỗi tràn dữ liệu
test case/test keɪs/ trường hợp kiểm thử
print statement/prɪnt ˈsteɪtmənt/ câu lệnh in
hand-tracing/hænd ˈtreɪsɪŋ/ tự theo dõi thủ công
debugger/ˈdiːbʌɡə/ trình gỡ lỗi
visualization/ˌvɪʒuːəlaɪˈzeɪʃn/ visualization: trực quan hóa
order of operations/ˈɔːdə ɒv ˌɒpəˈreɪʃnz/ Thứ tự thực hiện phép tính
1.4

Luyện tập

English

Answer each before reading the answers below.

  1. A step-counter app counts steps and shows a daily total so users can meet a goal. State the program's purpose and one of its functions.
  2. Classify each error as syntax, logic, run-time or overflow: (a) a missing closing bracket; (b) an average computed as sum / (count + 1); (c) dividing by a variable that is zero while the program runs; (d) adding $1$ to the largest value a fixed number of bits can store.
  3. A procedure returns "pass" for scores of $60$ or more. Choose four test inputs and explain your choices.
  4. Give one benefit of collaboration when designing a program.
  5. Distinguish iterative development from incremental development.
  6. Give two reasons for writing comments and other program documentation.
1.4

Đáp án

English
  1. Purpose: help users meet a daily activity goal. Function: for example, it adds $1$ to the total each time a step is detected, or displays the total.
  2. (a) Syntax; (b) logic; (c) run-time; (d) overflow.
  3. $59$, $60$ and $61$ test the boundary, where errors are most likely; an extreme such as $0$ or $100$ checks the limits of valid input.
  4. Different perspectives help find errors and avoid bias, so the program works for more users (for example, pair programming catches mistakes earlier).
  5. Iterative development repeats design, test and refine cycles, revising the program after feedback. Incremental development builds and tests the program in small pieces, adding one working part at a time.
  6. It explains what code does and why, so others (and the author later) can understand, maintain and debug it; it also records sources of borrowed code.
1.4

Mẹo làm bài thi

English
  • Assessment includes multiple-choice questions and the Create component, with written responses on the AP exam. Explain your reasoning clearly, not just your result.
  • Know the benefits of collaboration and how diverse perspectives reduce bias in a program.
  • Use precise vocabulary (iterative, incremental, program requirements) when you describe a design process.
  • Test at and just beyond the extremes of the input range, and say the expected output for each test.
  • Give and take feedback 反馈 constructively; credit collaborators and sources.
  • Break a large problem into smaller modules that a team can build in parallel.
Từ vựng Luyện tập
English Tiếng Việt
feedback/ˈfiːdbæk/ phản hồi

Bài học tương tác về chủ đề này

Làm theo từng bước, kèm theo bài tập kiểm tra ngay lập tức.

Đề thi cũ

Nhiều chủ đề hơn trong AP Nguyên lý khoa học máy tính

Đăng nhập hoặc tạo tài khoản

IGCSE, A-Level & AP