Sequence Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Sequence.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Executing a series of instructions one after another in a fixed, specific order. In sequential execution, each instruction must complete before the next one begins, and the order in which statements appear determines the order in which they run.

Do this, then that, then the next thingβ€”order matters and each step must finish before the next.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Each step must complete fully before the next step begins; the order determines the result.

Common stuck point: Some steps can't be reordered; others can. Understanding dependencies is key.

Sense of Study hint: When writing sequential code, think about which steps depend on results from earlier steps. If step B uses a value computed in step A, then A must come first. Trace through your sequence line by line to verify the order produces the correct result.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

easy
What is the output of this sequence of instructions? SET x = 5. SET y = 3. SET z = x + y. OUTPUT z.

Answer

88

First step

1
Step 1: x is assigned 5.

Full solution

  1. 2
    Step 2: y is assigned 3.
  2. 3
    Step 3: z = x + y = 5 + 3 = 8. Output: 8.
A sequence executes instructions one after another in order. The order matters β€” changing it would change the result.

Example 2

easy
Rearrange these steps into the correct sequence for logging into a website: (A) Click 'Login'. (B) Enter password. (C) Open browser. (D) Navigate to website. (E) Enter username.

Example 3

easy
Two sequences differ only by order. Sequence A: SET x = 2. SET y = x. SET x = 9. OUTPUT y. Sequence B: SET x = 2. SET x = 9. SET y = x. OUTPUT y. What does each print?

Example 4

easy
Trace: SET x = 1. SET x = x + 1. SET x = x + 1. SET x = x + 1. OUTPUT x.

Example 5

medium
Trace this swap attempt: SET a = 7. SET b = 2. SET a = b. SET b = a. OUTPUT a, b. Why doesn't this swap?

Example 6

medium
Trace: SET total = 0. SET total = total + 5. SET total = total + 10. SET total = total + 3. OUTPUT total.

Example 7

medium
Trace: SET a = 2. SET b = 3. SET c = a. SET a = b. SET b = c. OUTPUT a, b.

Example 8

hard
Trace: SET a = 3. SET b = a + 2. SET a = b * 2. SET b = a - b. OUTPUT a, b.

Example 9

hard
Trace: SET x = 8. SET y = 5. SET x = x + y. SET y = x - y. SET x = x - y. OUTPUT x, y.

Example 10

hard
Trace: SET a = 1. SET b = 2. SET c = 3. SET a = b + c. SET b = a + c. SET c = a + b. OUTPUT a, b, c.

Example 11

challenge
Six instructions with dependency arrows: A; B depends on A; C depends on A; D depends on B and C; E depends on C; F depends on D and E. Each step takes 1 unit of time. (a) Give one valid sequential order. (b) If you can run independent steps in parallel, what is the minimum total time?

Example 12

easy
Show step-by-step output for: n=2n=2; n=nβˆ—3n=n*3; n=n+1n=n+1; OUTPUT nn.

Example 13

medium
A recipe lists: (1) bake 30 min, (2) preheat oven, (3) mix batter. Why is order (2),(3),(1) correct rather than (1),(2),(3)?

Example 14

medium
A program reads two numbers, prints their sum, then their product. Write the sequence of 4 steps in plain pseudocode order.

Example 15

hard
A deploy script runs: build, run tests, push to prod. A junior reorders to: push, build, test. List 2 concrete failure modes.

Example 16

challenge
Given 4 statements S1..S4S_1..S_4 where S3S_3 depends on S1S_1 and S4S_4 depends on S2S_2, how many valid total orderings exist?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
What is the output? SET a = 10. SET b = a * 2. SET a = 0. OUTPUT b.

Example 2

easy
Trace this pseudocode: SET x = 2. SET y = x + 4. SET x = y * 2. OUTPUT x, y.

Example 3

easy
Trace in order: x=2x=2; y=x+3y=x+3; z=yΓ—2z=y\times2. What is zz?

Example 4

easy
Which runs first in 'print A; print B; print C'?

Example 5

easy
Trace: a=1a=1; a=a+1a=a+1; a=a+1a=a+1. Final aa?

Example 6

easy
Will this print the right area? 'print area; area = w * h'. Yes or no?

Example 7

easy
Reorder to be correct: (1) drink coffee, (2) pour coffee, (3) brew coffee. Give the order.

Example 8

easy
Trace: ss = 'ab'; ss = ss + 'c'; ss = ss + 'd'. Final ss?

Example 9

easy
In a sequence of 3 statements with no loops or ifs, how many times does the middle statement run?

Example 10

easy
Does swapping the order of 'x=5x=5' and 'print x' change the output? Yes or no.

Example 11

medium
Trace: a=3a=3; b=ab=a; a=10a=10; print bb. What prints?

Example 12

medium
Trace: x=4x=4; y=xβˆ—xy=x*x; x=1x=1; print yy. What prints?

Example 13

medium
Order these so the program works: (1) compute total = price * qty, (2) read qty, (3) read price. Give the correct order.

Example 14

medium
Trace: n=5n=5; n=nβˆ—2n=n*2; n=nβˆ’3n=n-3; n=n/7n=n/7. Final nn?

Example 15

medium
Two statements: 'open file' and 'read file'. Why must open come first, and what happens if reversed?

Example 16

medium
Trace: list=[]list=[]; append 1; append 2; remove first; append 3. What is the final list?

Example 17

medium
A setup runs: connect DB; load config; start server. A bug puts 'start server' first. Why does it fail?

Example 18

medium
Trace: a=5a=5; b=2b=2; a=aβˆ—ba=a*b; b=a+bb=a+b. Final aa and bb?

Example 19

medium
Trace: x=8x=8; y=3y=3; temp=xtemp=x; x=yx=y; y=tempy=temp. Final xx and yy?

Example 20

challenge
Trace: a=1,b=2a=1,b=2; a=a+ba=a+b; b=aβˆ’bb=a-b; a=aβˆ’ba=a-b. Final aa and bb?

Example 21

challenge
Trace: x=2x=2; x=x+xx=x+x; x=xβˆ—xx=x*x; x=xβˆ’xx=x-x. Final xx?

Example 22

challenge
Lines: (1) total=0, (2) total=total+price, (3) read price. A developer wrote them as 1,2,3. Why is total wrong, and what is the fix?

Example 23

easy
Trace the pseudocode: SET p = 4. SET q = 7. SET p = p + q. OUTPUT p.

Example 24

easy
What is the output? SET n = 3. SET n = n * n. SET n = n + 1. OUTPUT n.

Example 25

easy
Order these steps for making a sandwich: (A) Put filling between the slices. (B) Take out two slices of bread. (C) Open the bread bag. (D) Eat the sandwich.

Example 26

easy
What is the output? SET a = 5. SET b = 2. SET c = a - b. SET a = c. OUTPUT a.

Example 27

medium
Using a temporary variable, write a correct swap of a and b assuming SET a = 7, SET b = 2 at the start. Then give the output of (a, b).

Example 28

medium
Trace: SET x = 4. SET y = x + 2. SET z = y * x. SET x = z - y. OUTPUT x.

Example 29

medium
Two students wrote the same recipe steps in different orders. Student A: (1) preheat oven, (2) mix batter, (3) pour into pan, (4) bake. Student B: (1) mix batter, (2) pour into pan, (3) preheat oven, (4) bake. Which version is more efficient and why?

Example 30

medium
Trace: SET x = 6. SET y = 4. SET x = x * y. SET y = x / y. OUTPUT x, y.

Example 31

medium
What gets printed? SET name = "Ana". SET greeting = "Hi, " + name. SET name = "Ben". OUTPUT greeting.

Example 32

hard
A program has the steps: (1) read input, (2) validate input, (3) compute result, (4) display result. A teammate suggests moving validation after computation to save a step on valid inputs. Why is that a bad sequence?

Example 33

hard
Trace and find the bug. Intent: convert price from dollars to cents, then add 5% tax. SET price = 12. SET tax = price * 0.05. SET price = price * 100. SET total = price + tax. OUTPUT total. Is this correct?

Example 34

hard
Reorder these 5 build steps to minimize wall-clock time. (A) Compile source: 60s. (B) Download dependencies: 90s. (C) Run unit tests: 30s, needs compiled code. (D) Lint source: 20s. (E) Package artifact: 10s, needs compiled code and passed tests. Assume D and B can run in parallel with A. List a valid sequential order; then state which two steps you'd run in parallel for the speedup.

Example 35

easy
Trace: x=7x=7; y=xβˆ’2y=x-2; z=y+1z=y+1. What is zz?

Example 36

easy
Trace: a=4a=4; b=a+ab=a+a; c=b+ac=b+a. What is cc?

Example 37

easy
Order the steps to send an email: (1) Click Send, (2) Type message, (3) Open email app, (4) Enter recipient. Give the correct order.

Example 38

easy
Trace: p=3p=3; q=pq=p; p=p+10p=p+10. What is qq?

Example 39

easy
Trace: ss = ''; s=s+β€²gβ€²s = s + 'g'; s=s+β€²oβ€²s = s + 'o'. What is ss?

Example 40

medium
Trace: x=10x=10; y=x/2y=x/2; x=xβˆ’yx=x-y; OUTPUT xx.

Example 41

medium
Order so that account balance prints correctly: (1) print balance, (2) deposit = 50, (3) balance = balance + deposit, (4) balance = 100. Give the order.

Example 42

medium
Trace: a=6a=6; b=4b=4; a=a+ba=a+b; b=aβˆ’bb=a-b; OUTPUT aa, bb.

Example 43

medium
Trace: m=12m=12; m=m%5m=m\%5; m=mβˆ—4m=m*4; OUTPUT mm.

Example 44

medium
Trace: L=[1,2]L=[1,2]; LL.append(3); LL.append(LL[0]); OUTPUT LL.

Example 45

medium
Trace: a=2a=2; b=aβˆ—ab=a*a; c=bβˆ—ac=b*a; d=c+b+ad=c+b+a; OUTPUT dd.

Example 46

medium
Trace: x=5x=5; y=10y=10; swap (no temp): x=x+yx=x+y; y=xβˆ’yy=x-y; x=xβˆ’yx=x-y. Final x,yx,y?

Example 47

medium
Trace: count=0count=0; count=count+1count=count+1; count=countβˆ—2count=count*2; count=count+1count=count+1; OUTPUT countcount.

Example 48

medium
If you reorder `open file; read file; close file` to `read; open; close`, what happens at runtime?

Example 49

hard
Trace: a=1a=1; b=2b=2; c=3c=3; a=ba=b; b=cb=c; c=ac=a. Final a,b,ca,b,c?

Example 50

hard
A statement using zz appears at line 3; zz is assigned at line 7. Will the program work as written? Why or why not?

Example 51

hard
Trace: x=2x=2; y=xy=x; x=x+1x=x+1; z=x+yz=x+y; y=zβˆ’xy=z-x; OUTPUT x,y,zx,y,z.

Example 52

challenge
Trace: a=1a=1; b=ab=a; a=a+ba=a+b; b=a+bb=a+b; a=a+ba=a+b; OUTPUT aa.

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithm