COS1511
EXAM PACK
+27 81 278 3372
, Oct/Nov 2021
75 Marks
This examination question paper consists of 9 pages.
INSTRUCTIONS:
Use word Document to answer the questions
Convert to PDF before submitting
Marks are awarded for part of an answer, so do whatever you are able to in each question.
This exam is IRIS invigilated. Please make sure you use IRIS.
ALL THE BEST!
[TURN OVER]
, 2 COS1511
Oct/Nov 2021
QUESTION 1 6 MARKS
1.1 Consider the C++ code segment below. What value will newvalhave after this code has been
executed? (2)
int var1 = 4;
int var2 = 10;
int newval = 0;
if (var1 * 2 >= var2)
newval = 5 + 2 * var2;
else if (var1 < var2)
newval = var2 - var1 * 2;
else
newval = var1;
1.2 Suppose the input value for a is 5. What is the value of a after the following C++ code has
been executed? (2)
int a;
cin >> a;
switch (a)
{
case 1: a += 3;
case 3: a = a * 3; break;
case 5: a = ++a + 10;
case 6: a /= 2;
default: a = a + 1;
}
1.3 Consider the following C++ code segment: (2)
int findValue(int numberP)
{
int count = 0;
int value = 20;
while (count < numberP)
{
value += count;
count ++;
}
return value;
}
What will be the output of the following statement executed in the main function?
cout << findValue(3);
[TURN OVER]
EXAM PACK
+27 81 278 3372
, Oct/Nov 2021
75 Marks
This examination question paper consists of 9 pages.
INSTRUCTIONS:
Use word Document to answer the questions
Convert to PDF before submitting
Marks are awarded for part of an answer, so do whatever you are able to in each question.
This exam is IRIS invigilated. Please make sure you use IRIS.
ALL THE BEST!
[TURN OVER]
, 2 COS1511
Oct/Nov 2021
QUESTION 1 6 MARKS
1.1 Consider the C++ code segment below. What value will newvalhave after this code has been
executed? (2)
int var1 = 4;
int var2 = 10;
int newval = 0;
if (var1 * 2 >= var2)
newval = 5 + 2 * var2;
else if (var1 < var2)
newval = var2 - var1 * 2;
else
newval = var1;
1.2 Suppose the input value for a is 5. What is the value of a after the following C++ code has
been executed? (2)
int a;
cin >> a;
switch (a)
{
case 1: a += 3;
case 3: a = a * 3; break;
case 5: a = ++a + 10;
case 6: a /= 2;
default: a = a + 1;
}
1.3 Consider the following C++ code segment: (2)
int findValue(int numberP)
{
int count = 0;
int value = 20;
while (count < numberP)
{
value += count;
count ++;
}
return value;
}
What will be the output of the following statement executed in the main function?
cout << findValue(3);
[TURN OVER]