Nexus Learn

Lecture 19: Midterm Review

Lecture Slides CS-307 Programming Fundamentals 7 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

The Mid-Term at a Glance

Week 8 · 30% of the course (24 of 80 marks)

  • Covers
  • Lectures 1–19
  • Practicals 1–13
  • Basics · decisions · loops · arrays
  • Bring
  • Pen, pencil (for flowcharts and trace tables), university card. No phones or calculators unless the date sheet says so.

Mid-Term

Programming Fundamentals · Lecture 19

Slide 3

Today's Plan

50 minutes · 12 parts

Mid-term at a glance

3 min

Learning outcomes

2 min

The big picture

4 min

Review: basics & operators

6 min

Review: decisions

5 min

Review: loops

6 min

Review: arrays

4 min

Sample paper: MCQs

5 min

Sample paper: trace & predict

6 min

Sample paper: bug hunt

3 min

Sample paper: long question

5 min

Exam tips & homework

1 min

Programming Fundamentals · Lecture 19

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Recall and connect the main ideas of Lectures 1–18 (from data types to arrays)

SLO 19.1 → CLO4 · C2

Trace code, predict output and find bugs in mid-term style questions

SLO 19.2 → CLO4 · C4

SLO 19.3 → CLO1 · C3

Plan and write a complete program for an exam problem using top-down design

  • CLO1 Identify the necessary properties of good problem-solving techniques.
  • CLO4 Describe the syntax and semantics of the C++ programming language.

Programming Fundamentals · Lecture 19

Slide 5

The Big Picture: Four Units

Each unit builds on the one before it

Unit 1 · L1–L7

Unit 2 · L8–L10

Unit 3 · L11–L16

Unit 4 · L17–L18

Basics

Decisions

Loops

Arrays

computer, compiler, variables, types, operators, problem solving

if, if-else, else-if ladder, switch, ?:

while, do-while, for, break/continue, nested, patterns

declare, memory, traverse, search, max/min, count

A typical long question = input (Unit 1) → validate with a loop (3) → decide (2) → store and scan an array (4) → output.

Big Picture

Programming Fundamentals · Lecture 19

Slide 6

Review: Basics & Operators (L1–L7)

The traps that cost marks every year

Review · Basics

Programming Fundamentals · Lecture 19

Slide 7

Review: Decisions (L8–L10)

Choose the right structure, then watch the order and the braces

  • Traps
  • if (x > 0); ← stray ;
  • no braces → only 1 statement
  • missing break → fall-through
  • ladder order: first true wins

Ladder order

  • if (m >= 85) g = 'A';
  • else if (m >= 70) g = 'B'; // reached only if m < 85
  • else g = 'F';
  • switch rules
  • int or char only
  • case needs a constant

Review · Decisions

Programming Fundamentals · Lecture 19

Slide 8

Review: Loops (L11–L16)

Start · condition · update — and know how many times the body runs

  • Count the rounds
  • for (i = 1; i <= 10; i += 3) → 1 4 7 10
  • outer 3 × inner 4 = 12
  • Traps
  • no update → never stops
  • i <= n vs i < n
  • for (…); ← empty body

Review · Loops

Programming Fundamentals · Lecture 19

Slide 9

Review: Arrays (L17–L18)

One name, many values, index 0 to size − 1

Key code

  • const int MAX = 30;
  • int a[MAX] = {}; // all 0
  • for (int i = 0; i < n; i++) // traverse
  • total += a[i];
  • int pos = 0; // max position
  • for (int i = 1; i < n; i++)
  • if (a[i] > a[pos]) pos = i;
  • Traps
  • Loop to i <= n · max starting at 0 · 'Not found' printed inside the loop · b = a; to copy · integer average

Review · Arrays

Programming Fundamentals · Lecture 19

Slide 10

Sample Paper — Part A: MCQs (6 marks)

1 minute each · circle one answer

1. 17 % 5 * 2 + 7 / 2 is:

2. int x = 5; int y = x++ + 2; y and x are:

A) 7 B) 7.5 C) 5 D) 8

A) 8, 6 B) 7, 6 C) 7, 5 D) 8, 5

3. Which loop always runs its body at least once?

4. switch (2) — case 1 Tea, case 2 Coffee, case 3 Juice, default Water — no break. Output?

A) while B) for C) do-while D) none

A) Coffee B) Tea Coffee C) Coffee Juice Water D) Water

5. int a[6] = {1, 2}; a[4] is:

6. 'undefined reference to area(double)' comes from the:

A) 0 B) 2 C) garbage D) error

A) preprocessor B) compiler C) linker D) loader

Sample Paper · Part A

Programming Fundamentals · Lecture 19

Slide 11

Part B — Q1 Trace (3) and Q2 Predict (3)

Show a trace table — marks are given for the working

Q1 q_trace.cpp (main part)

Q2 q_predict.cpp (main part)

  • int a[5] = {4, 9, 2, 7, 5};
  • int s = 0;
  • for (int i = 0; i < 5; i++) {
  • if (a[i] % 2 == 1)
  • s += a[i];
  • else
  • a[i] = 0;
  • }
  • // print s, then a[0..4]
  • int x = 10, steps = 0;
  • while (x > 1) {
  • if (x % 2 == 0) x = x / 2;
  • else x = 3 * x + 1;
  • steps++;
  • }
  • // print steps
  • for (int i = 1; i <= 3; i++) {
  • for (int j = 1; j <= i; j++)
  • cout << i * j << " ";
  • cout << endl;
  • }

Q1 answer

Q2 answer (/ = new line)

  • s = 21
  • 0 9 0 7 5
  • Steps: 6
  • 1 / 2 4 / 3 6 9

Sample Paper · Part B

Programming Fundamentals · Lecture 19

Slide 12

Part B — Q3 Find and Fix Three Errors (3)

Two stop the build, one gives a wrong answer

q_bug.cpp (main part)

  • int marks[4] = {70, 85, 64, 91}
  • int total = 0;
  • for (int i = 0; i < 4; i++);
  • total += marks[i];
  • cout << "Average: " << total / 4 << endl;
  • After fixing
  • for (int i = 0; i < 4; i++)
  • total += marks[i];
  • total / 4.0 → Average: 77.5

g++ says

  • error: expected ',' or ';' before 'int'
  • error: 'i' was not declared in this scope

Sample Paper · Part B

Programming Fundamentals · Lecture 19

Slide 13

Part C — Long Question (9): Plan First

Water bill for n houses: units ≤ 100 Rs. 5, ≤ 200 Rs. 8, above Rs. 12 per unit (flat)

1 Input

2 Process

3 Output

  • n (1–20, validated)
  • units[i] ≥ 0 for each house
  • bill[i] by slab ladder
  • total += bill[i]
  • top = position of max
  • table of house, units, bill
  • total and highest bill

Sample Paper · Part C

Programming Fundamentals · Lecture 19

Slide 14

Part C — Model Answer

Test: 4 houses · 80, 150, 230, 100 units (−5 rejected)

water_bill.cpp (main part)

Output

  • for (int i = 0; i < n; i++) {
  • do {
  • cout << "Units for house " << i + 1 << ": ";
  • cin >> units[i];
  • } while (units[i] < 0);
  • }
  • int total = 0, top = 0;
  • for (int i = 0; i < n; i++) {
  • if (units[i] <= 100) bill[i] = units[i] * 5;
  • else if (units[i] <= 200) bill[i] = units[i] * 8;
  • else bill[i] = units[i] * 12;
  • total += bill[i];
  • if (bill[i] > bill[top]) top = i;
  • }
  • House Units Bill
  • 1 80 400
  • 2 150 1200
  • 3 230 2760
  • 4 100 500
  • Total: Rs. 4860
  • Highest bill: house 3
  • (Rs. 2760)
  • Boundary
  • 100 units → first slab (≤ 100): Rs. 500, not 800.

Sample Paper · Part C

Programming Fundamentals · Lecture 19

Slide 15

Exam Tips

How to collect every mark you know

  • Before writing code
  • Read the question twice; underline input and output.
  • Write the plan as 3–5 comment lines.
  • Choose the loop: known count → for; validation → do-while; stop value → while.
  • Start values: sum 0, product 1, max = first element.
  • While answering
  • Trace / predict: draw a trace table — working earns marks.
  • Bug questions: say the line, the kind of error and the fix.
  • Check one boundary value (e.g. 100 units, marks 50).
  • Leave 3 minutes to check ; { } and i < n.

Programming Fundamentals · Lecture 19

Slide 16

Homework

Attempt the full sample paper again from the notes without looking at the answers; then mark it yourself.

From the question bank, do 10 MCQs and 3 short questions from Weeks 4–7.

Write the water-bill program for progressive slabs (first 100 units Rs. 5, next 100 Rs. 8, above Rs. 12).

Revise Practicals 6–13: re-run Task 3 of each from your lab book.

Next lecture: Mid-term examination (Week 8) — then Sorting a List (Lecture 20)

← All CS-307 material