CS U111 · Mid-sem prep · Breadth

Lectures 1–10, the breadth checklist

For each lecture: the facts and terms worth being able to say out loud, a link to the page that teaches each one, and two or three multiple-choice self-tests. All of it comes from the lecture decks themselves.

Start here · what the instructors announced

The syllabus, as posted: "Lectures: Until recursion, except slide number 21 in the recursion deck. Labsheets: Until labsheet 5a. Conceptual and breadth coverage is expected for the lecture portion, and depth coverage is expected for the labsheets portion."

So this page is the lecture half: breadth, which mostly means multiple-choice questions. Lecture 10 is recursion, and its slide 21 (Recursion on Arrays, the sumArrayHelper example) is excluded. The depth half is labs 1–5a: Lab 4, Lab 5a and the write-a-program routine.

Lectures 1–4 are where nobody has an advantage. The material on computational thinking, input → process → output, flowcharts and pseudocode is this course's own. A JEE background doesn't cover it, and none of the past papers on the site asked about it, because they come from the old course. That's why those four lectures get the fullest treatment below. Reading them once carefully is enough.

The exam format: follow it exactly

From the instructors' slides, Important Instructions for the Mid-Semester Examination. They warn that "failure to follow the instructions will lead to non-evaluation":

How to use it (about 60–75 minutes in total). For each lecture, read the checklist and tick a box only if you could explain that point to a friend without looking. Then do the self-tests before opening them. Only open the linked page for the points you couldn't tick. If time is short, do lectures 1–4 and 10 first: those are the parts with no other page to rely on.

Lecture 1 · Computation & computational thinking

Deck: 20 slides · reading Beecher ch. 2–3 · no other page on this site covers this lecture, so everything you need is below

Check yourself (1 of 3)

In the IPOS model of an online food order, "order history and delivery records" belong to which part?

  1. Input
  2. Process
  3. Output
  4. Storage

(d) Storage. They're kept for later, not shown to the user right now. The slide lists restaurant, items and address as input; search, estimate, assign rider and payment as process; and confirmation and ETA as output.

Check yourself (2 of 3)

Spam detection in Gmail is the slide's everyday example of which pillar?

  1. Decomposition
  2. Pattern recognition
  3. Abstraction
  4. Algorithm design

(b) Pattern recognition. Spam shares recognisable features. Keep the slide's pairs straight: festival → decomposition, Google Maps → abstraction (it shows roads and hides everything else), recipe → algorithm design.

Check yourself (3 of 3)

Which statement matches the course's view of where programming sits?

  1. Programming is the first step: write code, then understand the problem
  2. Programming is one step in a pipeline that starts with turning a real-world problem into a computational one
  3. With AI, the algorithm step is no longer needed
  4. Software and algorithms are the same thing

(b). The pipeline is real-world problem → computational problem → algorithm → program → software. In the course's words, "programming is a means of expressing computational solutions, not an end in itself."

Lecture 2 · From algorithms to C programs

Deck: 29 slides · taught on this site in the operators & I/O lesson and the Mac setup guide (compile-run loop)

Check yourself (1 of 3)

The slide's end-of-lecture activity, with int a = 10, b = 3;. What do printf("%d\n", a / b); printf("%d\n", a % b); printf("%.2f\n", (float)a / b); print?

  1. 3.33, 1, 3.33
  2. 3, 1, 3.33
  3. 3, 0, 3.00
  4. 3, 1, 3.00

(b) 3, 1, 3.33 (compiled to check). (float)a converts a to 10.0 before the division, so the fraction is kept. Compare (float)(a / b), which divides first and gives 3.00.

Check yourself (2 of 3)

A program compiles cleanly, then stops partway through with a division by zero. According to the lecture, that is a…

  1. syntax error
  2. compilation error
  3. runtime error
  4. logic error caught by the compiler

(c) runtime error. It compiles and fails during execution. A missing semicolon is the slide's syntax-error example, and an undeclared variable is its compilation-error example.

Check yourself (3 of 3)

Which command produces an executable called hello from hello.c?

  1. ./hello.c
  2. gcc hello.c -o hello
  3. nano hello
  4. gcc hello -o hello.c

(b). -o names the output file. Then ./hello runs it, and nano just edits a file. Option (d) has the names backwards and would try to overwrite your source file.

Lecture 3 · Bits, bytes & number systems

Deck: 28 slides · reading Hanly & Koffman ch. 1 · the conversions are drilled on the number systems drill (methods 1–5)

Check yourself (1 of 3)

What is −5 in 8-bit two's complement?

  1. 10000101
  2. 11111010
  3. 11111011
  4. 00000101

(c) 11111011. 5 = 00000101. Flipping the bits gives 11111010, and adding 1 gives 11111011. Option (b) is the one's complement, which is the trap. (a) is "sign bit + magnitude", a scheme the lecture doesn't use.

Check yourself (2 of 3)

The slide's activity: an unsigned char holds 250. What does it show after adding 10?

  1. 260
  2. 4
  3. 255
  4. a compile error

(b) 4 (compiled to check). It can hold only 0–255, so 260 wraps around to 260 − 256 = 4. C doesn't stop you or warn you: "overflow wraps silently."

Check yourself (3 of 3)

The other activity questions: what is 200 in binary, and what is 11001 in decimal?

  1. 11001000 and 25
  2. 11000100 and 25
  3. 11001000 and 19
  4. 10011000 and 26

(a). 200 = 128 + 64 + 8 = 11001000, and 11001 = 16 + 8 + 1 = 25. From slide 16, for practice: 11010 = 26, 100101 = 37, 37 = 100101, 58 = 111010.

Lecture 4 · Flowcharts & algorithmic thinking

Deck: 15 slides · reading Beecher ch. 2–3 · no other page covers flowcharts or pseudocode, so the figures below are the reference

Start sum ← sum + x x > 0 ? Input x Oval start / end Rectangle process (one step) Diamond decision (yes / no) Parallelogram input / output
The four symbols from Lecture 4, slide 6. Only the diamond has two exits.
Start Input year year % 4 == 0 ? No Not a leap year Yes year % 100 == 0 ? No Leap year Yes year % 400 == 0 ? No Not a leap year Yes Leap year End
The leap-year flowchart from slide 9. "Yes" carries on down, and "No" exits to the right. Green parallelograms are outputs. Check: 1900 → Yes, Yes, No → not a leap year. 2000 → Yes at all three → leap year.

The same algorithm in the slide's pseudocode. The slide merges the first two diamonds into one compound condition:

BEGIN
  INPUT year
  IF year % 4 == 0 AND year % 100 != 0 THEN
    OUTPUT "Leap Year"
  ELSE IF year % 400 == 0 THEN
    OUTPUT "Leap Year"
  ELSE
    OUTPUT "Not a Leap Year"
  END IF
END
Check yourself (1 of 3)

In a flowchart, "Read the student's marks" goes in which symbol?

  1. Rectangle
  2. Diamond
  3. Parallelogram
  4. Oval

(c) Parallelogram. Reading a value in and displaying one out are both input/output. A calculation such as "compute the average" is a rectangle.

Check yourself (2 of 3)

The grade activity has five grades: A ≥ 90, B 80–89, C 70–79, D 60–69, F < 60. What is the fewest decision diamonds needed?

  1. 3
  2. 4
  3. 5
  4. 9

(b) 4. Ask ≥ 90?, ≥ 80?, ≥ 70?, ≥ 60? in order. The last "No" is F, with no question needed. In general, n outcomes need n − 1 questions. In C this is the else if ladder in Lecture 5, where F is the final else.

Check yourself (3 of 3)

"Keep asking until the instructions say stop, and if they never do, go on forever." Which algorithm property does this break?

  1. Finite
  2. Precise
  3. Effective
  4. Has output

(a) Finite. An algorithm must terminate after a finite number of steps. "Precise" means every step is unambiguous, and "effective" means every step can actually be done (for example, "list every real number" can't be).

Lecture 5 · Conditional statements in C

Deck: 22 slides · taught in depth on the operators & selection lesson and notes (Labs 1–3 depth)

Check yourself (1 of 3)

With int a = -1, b = 5;, what does this print?

if (a > 0)
    if (b > 0)
        printf("both positive\n");
else
    printf("a is not positive\n");
  1. a is not positive
  2. both positive
  3. nothing
  4. compile error

(c) nothing (compiled to check, and clang warns "add explicit braces to avoid dangling else"). The else belongs to if (b > 0). Since a > 0 is false, the whole inner if-else is skipped.

Check yourself (2 of 3)

Which is the best tool for "price depends on whether age < 12, age ≥ 65, or otherwise"?

  1. switch (age) with three cases
  2. an if / else if / else ladder
  3. three separate switches
  4. a do-while

(b). These are ranges, and switch matches only exact values. That's the Lecture 5 rule: if-else for ranges and compound conditions, switch for discrete values.

Check yourself (3 of 3)

P || Q is false when…

  1. P is false
  2. exactly one of P and Q is false
  3. both P and Q are false
  4. both P and Q are true

(c). Only one row of the truth table makes || false: the one where both are false.

Lecture 6 · Repetition: loops in C

Deck: 23 slides · taught on the loops lesson and notes, with Lab 4 worked (depth) and the loops drill

Check yourself (1 of 3)

With int i = 10;, what does do { printf("%d\n", i); } while (i <= 5); print?

  1. nothing
  2. 10, once
  3. 10 forever
  4. 5 6 7 8 9 10

(b) 10, once. The body runs before the first check, and then 10 <= 5 is false. The same code as a while loop prints nothing. (It isn't an infinite loop, because the condition is false on the first check.)

Check yourself (2 of 3)

How many times does printf("*") run in for (r = 1; r <= 4; r++) for (c = 1; c <= r; c++) printf("*");?

  1. 4
  2. 8
  3. 10
  4. 16

(c) 10, which is 1 + 2 + 3 + 4, because the inner loop's limit depends on r. That's the staircase pattern, a past-paper favourite. Only when the inner limit is fixed is the count rows × columns.

Check yourself (3 of 3)

Which statement about continue is true?

  1. It exits the loop immediately
  2. It skips the rest of the current iteration, and the loop goes on
  3. It restarts the loop from its first iteration
  4. It only works inside switch

(b). (a) describes break. In a for loop, continue still runs the update before re-checking the condition.

Lecture 7 · Functions in C

Deck: 21 slides · taught on the functions lesson and notes · Lab 5a (depth): every problem worked

Check yourself (1 of 3)

What does main print?

void modifyValue(int x) { x = 100; }
int main(void) { int a = 10; modifyValue(a); printf("%d\n", a); }
  1. 100
  2. 10
  3. 0
  4. garbage

(b) 10. x is a separate copy, and changing the copy never touches a. That's pass by value, the photocopy analogy.

Check yourself (2 of 3)

Which line is a function declaration (prototype)?

  1. int add(int a, int b) { return a + b; }
  2. int add(int a, int b);
  3. add(5, 3);
  4. int r = add(5, 3);

(b). It has a semicolon and no body. (a) is the definition, and (c) and (d) are calls.

Check yourself (3 of 3)

A global int count = 5; exists, and inside f() you write int count = 0; count++;. After f() returns, what is the global count?

  1. 6
  2. 1
  3. 5
  4. 0

(c) 5. The local count shadows the global, so count++ changes only the local, which is destroyed when f returns. This is the "variable shadowing" danger from slide 18.

Lecture 8 · Arrays in C

Deck: 30 slides · taught on the arrays lesson and notes and the arrays ladder (this lecture is breadth only: Lab 6 is not in the mid-sem)

Check yourself (1 of 3)

Binary search for 85 in the sorted array {68, 72, 77, 85, 91}. Which index is checked first?

  1. 0
  2. 2
  3. 3
  4. 4

(b) 2, because mid = 0 + (4 − 0) / 2 = 2, which holds 77. Since 77 < 85, the search moves right: left = 3, mid = 3 holds 85, found. That's 2 steps.

Check yourself (2 of 3)

int marks[5] = {72, 85, 91, 68, 77}; printf("%d", marks[5]);: what happens?

  1. a compile error: index out of range
  2. it prints 77
  3. undefined behaviour: it compiles, and prints whatever is in that memory
  4. it prints 0

(c). C does no bounds checking. (clang does warn for a constant index like this one, but it still compiles.) The valid indices are 0–4.

Check yourself (3 of 3)

int grid[3][4]; has how many elements, and what does grid[1][2] mean?

  1. 7 elements, column 1 row 2
  2. 12 elements, row 1 column 2
  3. 12 elements, row 2 column 1
  4. 20 elements, row 1 column 2

(b). 3 × 4 = 12, and the row index always comes first.

Lecture 9 · Strings in C

Deck: 30 slides · taught on the strings lesson (input widget) and notes (four slide corrections) · breadth only

Check yourself (1 of 3)

The user types Ada Lovelace. What does char name[20]; scanf("%s", name); printf("[%s]", name); print?

  1. [Ada Lovelace]
  2. [Ada]
  3. [Lovelace]
  4. [A]

(b) [Ada]. %s stops at the first whitespace. %[^\n] would capture the whole line.

Check yourself (2 of 3)

char g[] = "Hi";: what are sizeof(g) and strlen(g)?

  1. 2 and 2
  2. 3 and 2
  3. 3 and 3
  4. 2 and 3

(b) 3 and 2 (compiled to check). sizeof counts the '\0' box. strlen counts only the characters before it.

Lecture 10 · Recursion in C

Deck: 23 slides · reading Hanly & Koffman ch. 9 · taught on the recursion lesson and notes · slide 21 (Recursion on Arrays) is excluded from the mid-sem

Check yourself (1 of 3)

The slide's in-class activity: what does mystery(5) return?

int mystery(int n) { if (n <= 1) return n; return mystery(n-1) + mystery(n-2); }
  1. 8
  2. 5
  3. 3
  4. it never stops

(b) 5 (compiled to check). It's Fibonacci: m(0)=0, m(1)=1, m(2)=1, m(3)=2, m(4)=3, m(5)=5. The base case is n <= 1, and both calls make n smaller, so it terminates.

Check yourself (2 of 3)

What does confusingPrint(2) print?

void confusingPrint(int n) {
    if (n == 0) { return; }
    printf("W%d ", n);
    confusingPrint(n - 1);
    printf("U%d ", n);
}
  1. W2 U2 W1 U1
  2. W2 W1 U1 U2
  3. W2 W1 U2 U1
  4. W1 W2 U2 U1

(b) W2 W1 U1 U2. Before the call it prints in descending order, while winding. After the call, it prints in reverse as the frames unwind, innermost first.

Check yourself (3 of 3)

Which function is tail-recursive?

  1. return n * fact(n - 1);
  2. return fib(n - 1) + fib(n - 2);
  3. return tailFact(n - 1, n * acc);
  4. return 1 + count(n / 10);

(c). Nothing is left to do after the call returns, because the multiplication happened before the call. In (a), (b) and (d), a multiply or add is still waiting.

Slide corrections (checked with clang)

Where a slide says something that isn't quite right, here is the correct version, with the evidence. In the exam, give the correct answer. If an MCQ seems to hinge on one of these, write the assumption you made in Part II.

SlideSaysActually
L5 s15 (= vs ==)"The result of an assignment is always true"The value of an assignment is the value assigned. if (x = 5) is true because 5 is nonzero, but if (x = 0) is false (compiled: takes the else). The slide's real point, that you assigned when you meant to compare, still holds.
L10 s17 (mutual recursion)"isEven(3) calls isOdd(2), which calls isEven(1), which calls isOdd(0) — true."isOdd(0) returns false, and that false travels back up, so isEven(3) = false (0), which is right because 3 is odd. Compiled: isEven(3) = 0 and isOdd(3) = 1. Don't read the slide's "— true" as the answer.
L3 s5 (sizes)kB = 210 bytesThat's the course's convention, so use 1024 in this exam. Strictly, the standard (SI) kB is 1000 bytes, and 1024 bytes is a kibibyte (KiB).
L3 s24 (type sizes)char: −128 to 127True on her Mac (compiled: CHAR_MIN = −128), but whether plain char is signed is up to the compiler. signed char is −128…127 and unsigned char is 0…255 everywhere.
L8 s6 (out of bounds)"…without ever crashing"It's undefined behaviour, which can crash, print garbage, or seem to work. (Details in the arrays notes.)
L8 s21, s23 (sorting)"sorted after 3 passes" / "sorted after 3 swaps"The array's contents are sorted after 3, but the loops as written run 4 bubble passes and 4 selection swaps. The 4th selection swap swaps 85 with itself. If an MCQ asks how many times the code swaps, count what the code does.
L9 (four slides)—The trailing bytes of char g[6] = "Hi" are 0, not garbage. i <= strlen prints the '\0' and then stops. %s and %[^\n] need a width to be safe. strcmp returns a sign, not just 0. All four are explained in the strings notes.

Also checked and correct as printed: L2's activity (3, 1, 3.33), L3's 12 − 5 two's-complement sum, 89 ↔ 01011001 ↔ 0x59, the overflow 255 + 1 → 0, L5's dangling else and fall-through outputs, L6's continue output and do-while, L7's clang errors for an undeclared function and a nested definition, L8's sum 393 and average 78.60, and L10's factorial(3) = 6, computePower(2,5) = 32, fib(4) = 3 with call counts fib(2)×2 and fib(1)×3, tailFactorial(3,1) = 6 and digitSum(123) = 6.

Don't do this tonight

Don't read the textbook chapters cover to cover, and don't hunt for "C MCQ" sets online. They're full of pointer and printf-trivia questions this course hasn't reached. Breadth here means the decks, and this page is the decks. Spend any spare time on the depth half: Lab 5a and one past-paper program.