Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

COS1512 Exam Revision OCT/NOV 2026 Questions & Answers Past Papers 2026 |Introduction to Programming II|

Rating
-
Sold
-
Pages
31
Grade
A+
Uploaded on
22-05-2026
Written in
2025/2026

This exam revision paper is more than just a set of questions and answers. It’s designed to help you understand how each answer is reached, so you’re not just memorising but actually learning the concepts behind them. The solutions are clear, accurate, and supported by reliable academic references. It also includes predicted questions that are likely to appear, giving you a practical sense of what to expect and how to approach them with confidence. Whether you’re revising last minute or using it to strengthen your understanding over time, it’s structured in a way that aligns with what examiners look for. The explanations are straightforward and focused, making it easier to follow and apply. If you take the time to work through it properly, achieving high grades is a realistic outcome.

Show more Read less
Institution
Course

Content preview


]]
‡ ⋆



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 ‡

Connected book

Written for

Institution
Course

Document information

Uploaded on
May 22, 2026
Number of pages
31
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$3.51
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
BeeNotes teachmetutor
Follow You need to be logged in order to follow users or courses
Sold
313
Member since
11 months
Number of followers
0
Documents
861
Last sold
6 days ago
BeeNotes

BeeNotes: Buzzing Brilliance for Your Studies Discover BeeNotes, where hard-working lecture notes fuel your academic success. Our clear, concise study materials simplify complex topics and help you ace exams. Join the hive and unlock your potential with BeeNotes today!

4.1

39 reviews

5
23
4
4
3
8
2
1
1
3

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions