COS1512
EXAM PACK
,COS1512 Assessment 2 2026
Introduction to Programming II
EXAM ANSWERS
QUESTION 1 [12]
1.1
The number used to refer to a particular element of an array is called its index
(subscript).
1.2
a) False.
An array stores elements of the same data type.
b) False.
An array index must be an integral type, such as int; it cannot be double.
c) True.
It is an error to provide more initializers than the declared number of array elements.
d) False.
Arrays are passed to functions by reference/address, not by value in the ordinary
array-parameter syntax.
e) False.
A function cannot directly return an array type. It can return a pointer/reference to an
array or use another mechanism such as a class/container.
1.3
The code has several errors:
, prod_name is an array, so cin >> prod_name is incorrect; an individual element
such as prod_name[i] must be used.
The loop condition i < 4 accesses four elements even though the arrays contain
only three elements (0–2).
The for loop has no braces, so only the cout statement belongs to the loop.
The program checks only price[0] rather than the price corresponding to each
product.
The comment says products costing more than R200, but the condition uses
500.0.
Input should be performed for each product.
Corrected code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string prod_name[3];
double price[3];
for (int i = 0; i < 3; i++)
{
cout << "Enter product name: ";
cin >> prod_name[i];
cout << "Enter product price: ";
cin >> price[i];
if (price[i] > 200.0)
cout << prod_name[i] << " is expensive!\n";
, }
return 0;
}
QUESTION 2 [20]
A suitable OOP solution using a 1D array, class and function overloading is:
#include <iostream>
using namespace std;
class StudentScores
{
private:
double scores[100];
int numberOfStudents;
public:
StudentScores(int n)
{
numberOfStudents = n;
}
void inputScores()
{
for (int i = 0; i < numberOfStudents; i++)
{
cout << "Enter score for student " << i + 1 << ": ";
cin >> scores[i];
}
EXAM PACK
,COS1512 Assessment 2 2026
Introduction to Programming II
EXAM ANSWERS
QUESTION 1 [12]
1.1
The number used to refer to a particular element of an array is called its index
(subscript).
1.2
a) False.
An array stores elements of the same data type.
b) False.
An array index must be an integral type, such as int; it cannot be double.
c) True.
It is an error to provide more initializers than the declared number of array elements.
d) False.
Arrays are passed to functions by reference/address, not by value in the ordinary
array-parameter syntax.
e) False.
A function cannot directly return an array type. It can return a pointer/reference to an
array or use another mechanism such as a class/container.
1.3
The code has several errors:
, prod_name is an array, so cin >> prod_name is incorrect; an individual element
such as prod_name[i] must be used.
The loop condition i < 4 accesses four elements even though the arrays contain
only three elements (0–2).
The for loop has no braces, so only the cout statement belongs to the loop.
The program checks only price[0] rather than the price corresponding to each
product.
The comment says products costing more than R200, but the condition uses
500.0.
Input should be performed for each product.
Corrected code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string prod_name[3];
double price[3];
for (int i = 0; i < 3; i++)
{
cout << "Enter product name: ";
cin >> prod_name[i];
cout << "Enter product price: ";
cin >> price[i];
if (price[i] > 200.0)
cout << prod_name[i] << " is expensive!\n";
, }
return 0;
}
QUESTION 2 [20]
A suitable OOP solution using a 1D array, class and function overloading is:
#include <iostream>
using namespace std;
class StudentScores
{
private:
double scores[100];
int numberOfStudents;
public:
StudentScores(int n)
{
numberOfStudents = n;
}
void inputScores()
{
for (int i = 0; i < numberOfStudents; i++)
{
cout << "Enter score for student " << i + 1 << ": ";
cin >> scores[i];
}