Chapter 1.2: Computation and Computational Thinking

Chapter 1.2: Computation and Computational Thinking · Study Questions
ComputationAbstractionHistory of computersTuring machineLimitsDeclarative and imperativeEuclid and BabylonAlgorithmCondition and loop
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Programming and Computation

Question 1: Building Blocks of Computation

Computation is built on mathematics and logic. Which of the following Java lines contains a decision (logic)?

C: if chooses a different path depending on whether the condition is true or false; this is the logic building block. A and D are mathematical (arithmetic) operations, B only produces output.

Multiple Choice · Computer Science

Question 2: The Metro Map

A metro map does not show streets, buildings or real distances; it shows only the stops and the lines. Which computer science concept is this an example of?

A: Abstraction hides the unnecessary details of a system and keeps its useful essence. Decomposition splits a large job into smaller subtasks; computability is about whether a problem can be solved by an algorithm.

Multiple Choice · History of Computers

Question 3: Who Did What?

Which of the following pairings is correct?

B: In 1843, in “Note G”, Lovelace gave a step-by-step method for Bernoulli numbers. Fortran was developed by John Backus and IBM (1957); ENIAC used decimal numbers and was programmed with cables, and the stored program is an EDVAC design; the ABC was built by Atanasoff and Berry.

Multiple Choice · History of Computers

Question 4: Stored Program

Which is a feature of a stored-program computer?

D: This is the idea of EDVAC and the von Neumann architecture: the program is numbers in memory, and loading a new one is enough. A is ENIAC, B is fixed-program machines (Pascaline, ABC), C is a feature of the mechanical era.

Step-by-Step Trace · Turing Machines

Question 5: A Turing Machine That Adds One

The rules have the form (state, symbol read) → (write, move, new state). The head starts at the rightmost digit. What is on the tape when the machine halts?

□10111□state: q0

Start: state q0, the head is on the rightmost digit (cell 4). The tape holds 10111 = 23.

□10110□state: q0

Read 1 in cell 4 → rule 1: wrote 0 (the carry continues), the head moved left.

□10100□state: q0

Read 1 in cell 3 → rule 1: wrote 0 (the carry continues), the head moved left.

□10000□state: q0

Read 1 in cell 2 → rule 1: wrote 0 (the carry continues), the head moved left.

□11000□state: halt

Read 0 in cell 1 → rule 2: wrote 1, the machine halted. The tape holds 11000 = 24: the machine added 1 to the number (4 rules applied).

(q0, 1) → write 0, move left, q0
(q0, 0) → write 1, halt
(q0, □) → write 1, halt

Output Question · Turing Machines

Question 6: What Does It Compute?

The head starts at the leftmost cell and moves right on every rule without changing the tape. For the tape 10110, in which state does the machine halt and how many steps does it run? What does the machine compute?

1  (q0, 0) → write 0, move right, q0
2  (q0, 1) → write 1, move right, q1
3  (q1, 0) → write 0, move right, q1
4  (q1, 1) → write 1, move right, q0
5  (q0, □) → halt, “even”
6  (q1, □) → halt, “odd”
Show answer

States: q0 →(1) q1 →(0) q1 →(1) q0 →(1) q1 →(0) q1 → □ is read → rule 6: halts with “odd”.

5 read steps + halting = 6 steps. q0 means “an even number of 1s so far”, q1 means “an odd number of 1s”; the machine finds whether the number of 1s on the tape is odd or even (10110 has three 1s).

Multiple Choice · Turing Machines

Question 7: The Halting Problem

Which statement about Turing's halting problem is correct?

A: In 1936 Turing proved that no such algorithm can exist; the obstacle is not speed or memory but a logical limit (B is wrong). Java is Turing-complete and a compiler cannot find infinite loops in general (C is wrong); the Turing machine's tape is already infinite (D is wrong).

Output Question · Limits of Computers

Question 8: Limits in Java

What does the program print?

int big = Integer.MAX_VALUE;
System.out.println(big + 1);
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(-7 / 2);
System.out.println(0.1 * 3);
System.out.println(1e308 * 10);
Show answer
-2147483648
3
3.5
-3
0.30000000000000004
Infinity

int is 32 bits; adding 1 to the largest value wraps around to the smallest. The quotient of two ints is an int and is truncated toward zero: 7 / 2 = 3, −7 / 2 = −3. If one operand is a double, real division is performed.

0.1 cannot be written exactly in binary. The largest double is ≈ 1.8·10³⁰⁸; beyond that the result is Infinity; no exception is thrown.

Multiple Choice · Kinds of Knowledge

Question 9: Imperative Knowledge

Which of the following is imperative (how-to) knowledge?

C: It gives a recipe that says what the steps are and when to stop: Euclid's algorithm. A, B and D define what something is (declarative); they let you check a candidate but do not tell how to find the result.

Step-by-Step Trace · Kinds of Knowledge

Question 10: Euclid: GCD(270, 192)

What does the program print? How many times is the remainder computed?

a270b192r78

270 = 1 · 192 + 78 → the first remainder r = 78.

a192b78r36

r ≠ 0 → shift: a ← b, b ← r. New remainder 192 % 78 = 36 (192 = 2 · 78 + 36).

a78b36r6

r ≠ 0 → shift: a ← b, b ← r. New remainder 78 % 36 = 6 (78 = 2 · 36 + 6).

a36b6r0

r ≠ 0 → shift: a ← b, b ← r. New remainder 36 % 6 = 0 (36 = 6 · 6 + 0).

a36b6r0

r = 0, the loop ends. The last divisor b = 6. 4 divisions were done; the numbers got smaller but the GCD never changed.

int a = 270, b = 192;
int r = a % b;
while (r != 0) {
    a = b;
    b = r;
    r = a % b;
}
System.out.println("GCD = " + b);

Output Question · Kinds of Knowledge

Question 11: Babylonian Method: √10

What does the program print? (Math.sqrt(10) = 3.1622776601683795)

double x = 10, y = 3;
for (int n = 1; n <= 3; n++) {
    y = (y + x / y) / 2;
    System.out.println(n + ": " + y);
}
Show answer
1: 3.166666666666667
2: 3.162280701754386
3: 3.162277660169842

y₁ = (3 + 10/3) / 2 = 19/6; y₂ = (y₁ + 10/y₁) / 2; y₃ agrees with the true root to about 12 digits. In each round the number of correct digits roughly doubles.

The result is still only an approximation: the third estimate differs from Math.sqrt in the last digits.

Multiple Choice · Algorithm and Problem

Question 12: Which Is Not an Algorithm?

Which of the following instructions cannot be an algorithm?

B: It never ends; this violates the finiteness property of algorithms. The others are clear, ordered and finite steps (C stops when the list ends).

Coding Question · Algorithm and Problem

Question 13: Weighted Average

Course grade = midterm × 0.4 + final × 0.6. Identify the input and output of the problem, and write its pseudocode and the Java program. What is the output for a midterm of 50 and a final of 80?

Show answer

Input: two integers (midterm, final). Output: one decimal number. Pseudocode: read the midterm → read the final → avg ← midterm · 0.4 + final · 0.6 → print avg.

Scanner scanner = new Scanner(System.in);
int midterm = scanner.nextInt();
int finalExam = scanner.nextInt();
double average = midterm * 0.4 + finalExam * 0.6;
System.out.println("Average: " + average);

50, 80 → 20 + 48 = Average: 68.0. The sample input–output pair (50, 80 → 68.0) is also used to test the program.

Multiple Choice · Algorithmic Operations

Question 14: do-while versus while

What does the code print?

int k = 5;
do {
    System.out.print(k + " ");
    k++;
} while (k < 5);

D: do-while runs the body first and tests the condition afterwards: 5 is printed, k becomes 6, 6 < 5 is false → the loop ends. With the same condition, while (k < 5) would never run the body (A). It is the Java counterpart of repeat … until in pseudocode.

Step-by-Step Trace · Algorithmic Operations

Question 15: Largest in a Negative List

What does the program print? How many times is largest updated?

-40-91-22-73

largest starts with the first element: −4 (green).

-40-91-22-73

i = 1: -9 > -4 is false, no change.

-40-91-22-73

i = 2: -2 > -4 is true → largest = -2.

-40-91-22-73

i = 3: -7 > -2 is false, no change.

-40-91-22-73

i = 4 < 4 is false, the loop ends: −2. If largest had started at 0, no element is greater than 0, so 0 would have been printed by mistake.

int[] list = {-4, -9, -2, -7};
int largest = list[0];
for (int i = 1; i < list.length; i++) {
    if (list[i] > largest) {
        largest = list[i];
    }
}
System.out.println("Largest: " + largest);

Spot the Bug · Algorithmic Operations

Question 16: Two Bugs

The program was written to print the sum of the numbers from 1 to 5 (15). Find the two bugs; which one does the compiler catch?

int total;
int i = 1;
while (i <= 5) {
    total = total + i;
}
System.out.println(total);
Show answer

(1) total is used before it is assigned a value: compile error variable total might not have been initialized (lines 4 and 6). Fix: int total = 0;

(2) In the body, i never changes: i ≤ 5 is always true, so this is an infinite loop. The compiler does not catch this; add i = i + 1; to the body. With both fixed, the output is 15.

Output Question · Algorithmic Operations

Question 17: Compound Conditions

What does the program print? What if y = 200?

int x = 120, y = 150;
if (x > 100 && y == 200) {
    x = x - 100;
} else if (x > 100 || y == 200) {
    x = x + 100;
} else {
    x = 0;
}
System.out.println("x = " + x);
Show answer
x = 220

120 > 100 is true but 150 == 200 is false → && is false. In the second condition one true side is enough for || → x = 220. The third branch is skipped.

If y were 200, the first condition would be true and x = 20 would be printed; the else if branches would never be tested.

The End

Chapter 1.2: Computation and Computational Thinking: Study Questions
Lecture slides: Chapter 1.2: Computation and Computational Thinking · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.