]]
⋆
COS1512: Introduction to Programming II
OCT/NOV Examination 2026 — Comprehensive Revision Guide
(Covers Past Papers: 2023, 2024 & 2025)
⋆ ⋄ ⋆ ⋄ ⋆ ⋄ ⋆ ⋄ ⋆
[ Computer Science – Object-Oriented Programming (C++) [
_ Exam Revision Guide
COS1512
Module Code:
Introduction to Programming II
Module Name:
OCT/NOV 2023, 2024 & 2025
Papers Covered:
OCT/NOV Examination 2026
Prepared For:
100 marks (75 minutes)
Total Marks:
Savitch, Problem Solving with C++
Textbook:
Use this guide to revise thoroughly. Focus on understanding, not memorisation.
Exam Revision Notes | COS1512 | 2023–2025
, COS1512 | Exam Revision OCT/NOV Examination 2026
Section 1: Assertions and Overloaded Functions [10 marks]
1.1 [3 marks]
Question: What is the purpose of the assert statement in C++? Write a short code
fragment that uses assert to validate that a variable age is greater than zero before
processing it.
Answer:
Key Concept
The assert macro is a debugging aid that tests a condition. If the condition evaluates
to false (zero), the program prints a diagnostic message and calls abort(), terminat-
ing execution immediately. It is declared in <cassert>.
The assert statement is used during development to catch logical errors early by enforcing
pre-conditions, post-conditions, and invariants. It should NOT be used for user-input valida-
tion in production code; use if-statements or exceptions instead.
1 # include < iostream >
2 # include < cassert >
3 using namespace std ;
4
5 int main () {
6 int age ;
7 cout << " Enter age : " ;
8 cin >> age ;
9 assert ( age > 0) ; // terminates if age <= 0
10 cout << " Valid age : " << age << endl ;
11 return 0;
12 }
⋆ Exam Tip
Always include #include <cassert>. In the exam, remember that assert aborts the
program when the condition is false – it does NOT throw an exception or display a
user-friendly error.
Page 2 of 31
, COS1512 | Exam Revision OCT/NOV Examination 2026
1.2 [4 marks]
Question: Explain the concept of function overloading in C++. Write two over-
loaded versions of a function named area: one that calculates the area of a circle (given a
double radius) and one that calculates the area of a rectangle (given two double parame-
ters, width and height).
Answer:
Function overloading allows two or more functions to share the same name as long as their
parameter lists differ in number, type, or order of parameters. The compiler selects the correct
version based on the argument types at the call site (this is resolved at compile time – known
as static binding).
1 # include < iostream >
2 # include < cmath > // for M_PI
3 using namespace std ;
4
5 // Overload 1: area of a circle
6 double area ( double radius ) {
7 return M_PI * radius * radius ;
8 }
9
10 // Overload 2: area of a rectangle
11 double area ( double width , double height ) {
12 return width * height ;
13 }
14
15 int main () {
16 cout << " Circle area : " << area (5.0) << endl ;
17 cout << " Rectangle area : " << area (4.0 , 6.0) << endl ;
18 return 0;
19 }
The return type alone is not sufficient to distinguish overloaded functions – the parameter
lists must differ.
Page 3 of 31
,COS1512 | Exam Revision OCT/NOV Examination 2026
1.3 [3 marks]
Question: Study the following code and predict the output. Explain what happens at each
function call.
include <iostream> using namespace std; void display(int x) cout « "int: "
« x « endl; void display(double x) cout « "double: " « x « endl; void
display(char x) cout « "char: " « x « endl; int main() display(10);
display(3.14); display(’A’); return 0;
Answer:
• display(10) – the literal 10 is an int, so the int overload is called. Output: int: 10
• display(3.14) – the literal 3.14 is a double, so the double overload is called. Output:
double: 3.14
• display(’A’) – the literal ’A’ is a char, so the char overload is called. Output: char:
A
. Watch Out
If you call display(3.14f) (a float literal), the compiler must convert it to either
double or int; it would pick double because float-to-double is a promotion, not a
conversion.
Page 4 of 31
, COS1512 | Exam Revision OCT/NOV Examination 2026
Section 2: File I/O Streams [10 marks]
2.1 [5 marks]
Question: Write a complete C++ program that reads integers from a text file named
numbers.txt and writes only the even numbers to a new file named evens.txt. The
program should display an error message if either file cannot be opened.
Answer:
Key Concept
File streams use ifstream (input), ofstream (output), or fstream (both), all declared
in <fstream>. Always check whether the file opened successfully before reading/writ-
ing.
1 # include < iostream >
2 # include < fstream >
3 using namespace std ;
4
5 int main () {
6 ifstream inFile ( " numbers . txt " ) ;
7 if (! inFile ) {
8 cerr << " Error : cannot open numbers . txt " << endl ;
9 return 1;
10 }
11
12 ofstream outFile ( " evens . txt " ) ;
13 if (! outFile ) {
14 cerr << " Error : cannot create evens . txt " << endl ;
15 return 1;
16 }
17
18 int num ;
19 while ( inFile >> num ) {
20 if ( num % 2 == 0) {
21 outFile << num << endl ;
22 }
23 }
Page 5 of 31
, COS1512 | Exam Revision OCT/NOV Examination 2026
24
25 inFile . close () ;
26 outFile . close () ;
27 cout << " Done . Even numbers written to evens . txt " << endl ;
28 return 0;
29 }
2.2 [5 marks]
Question: Explain the difference between text files and binary (data) files in C++.
Give one situation where each type would be preferred.
Answer:
Table 1: Text Files vs Binary Files
Aspect Text File Binary (Data) File
Storage Data stored as human-readable Data stored as raw bytes in mem-
characters (ASCII/UTF-8). ory representation.
Readability Can be opened and read in any Not human-readable; requires the
text editor. correct program.
Size Generally larger because numbers Smaller; integers stored in 4 bytes
are stored as digit characters. not as digit strings.
Speed Slower due to character conversion. Faster; data copied directly
to/from memory.
Usage in C++ ifstream/ofstream with « and ». fstream with read() and
write().
• Use a text file when storing configuration data, CSV data, or any information a user
might need to read or edit directly (e.g., a student marks file opened in Excel).
• Use a binary file when storing large datasets such as images, audio, or when speed and
storage efficiency matter (e.g., saving all elements of a large struct array at once using
write()).
Page 6 of 31