COS1511 January Examination 2023
COS1511 January Examination 2023
COS1511 January Examination 2023
70 Marks
INSTRUCTIONS:
1. Answer all the questions on a Word document/by hand and convert to PDF.
2. Number your answers and label your rough work clearly.
4. Marks are awarded for part of an answer, so do whatever you are able to in each question.
[TURN OVER]
2
Question 1 [20]
1.What is the output of the following code fragment? (2)
int x = 0;
while( x < 8)
cout << x << " ";
x ++;
cout << x << endl;
1. 0
2. infinite loop
3. 1 2 3 4 5 6 7 8
4. 0 1 2 3 4 5 6 7 8
int x, y, z; y = 10;
z = 3;
x = y * z - 3;
1. 0
2. 3
3. 10
4. 27
int x;
x = x + 15;
1. 0
2. 14
3. 15
4. not defined
1. Where is the \
2.Where is the
3.Nothing, it is a syntax error
4.Where is the \ endl
int x;
x = 19 % 5;
1. 4.0
2. 4
3. 3.8
4. 8
6.Given the following code fragment and the input value of 5, what output is generated? (2)
[TURN OVER]
float tax; float total;
cout << "Enter the cost of the item\n"; cin >> total;
3
7.Given the following code fragment and the input value of 3.0, what output is generated? (2)
}
else
{
tax = 0.10;
cout << total + (total * tax) << endl;
1. 3.0
2. 2.0
3. 3.3
4. 2.3
8.If x has the value of 3, y has the value of -2, and w is 10, is the following condition true or false? (2)
9.Given the following code fragment, and an input value of 8, what is the output that is generated? (2)
int x;
[TURN OVER]
cout << "Enter a value\n"; cin >>
x;
if (x = 0)
4
{
cout << "x has no value\n";
}
else
{
cout << "x has a value\n";
}
1. x has no value
2. x is 8
3. x has a value
4. unable to determine
10.Which of the following data types cannot be used in a switch controlling expression? (2)
1. int
2. enum
3. array
4. char
QUESTION 2 [19]
Question 2a (6)
State what output, if any, results from each of the following statements. Write only the solution.
CODE OUTPUT
Example for (int i = 0; i < 10; i++) 0123456789
cout << i;
cout << endl;
Question 2b (5)
Suppose we want to find a student that qualifies for an internship. For each student, we input the name, the
age of student and the final mark obtained for the examination in a while loop. To qualify, the student
should be younger than 30 with a final mark of more than 65%. Read in values until a suitable candidate is
found. Display appropriate messages, whether successful or not. The variable names are name, age and
finalMark respectively. Complete the while loop below. You only have to write down the completed
while loop.
string name
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Enter final mark for exam: ";
cin >> finalMark;
Question 2c (8)
The program given below gives the output below. Fill in the missing code and write down only the answer for each of the
questions 2.c.1 to 2.c.8. Remember to number your answers.
The output:
#include <iostream>
using namespace std;
int main ()
{
2c.2----- counter;
int number;
[TURN OVER]
6
int zeros = 0;
int odds = 0; int
evens = 0;
cout << "Please enter " << LIMIT << " integers, "
<< "positive, negative, or zeros." << endl;
2c.6-----:
2c.7-----;
}
}
cout << endl;
cout << "There are " << 2c.8----- << " evens, "
<< "which includes " << zeros << " zeros."
<< endl;
cout << "The number of odd numbers is: " << odds
<< endl;
return 0;
}
QUESTION 3 [31]
In this question, we describe the problem and then you have to decide yourself how you are going to tackle it.
Question 3a (11)
The cost of renting a room at a hotel is R900 per night. For special occasions, such as a wedding or
conference, the hotel offers a special discount as follows:
Question 3b (10)
Four experiments are performed, each consisting of five test results. The results for each experiment are
given in the following list.
#include <iostream>
using namespace std;
int main()
{
3b. 1-----result, average;
cout << "Average for experiment no " << exp + 1 << ": "
<< 3b.10----- << endl << endl; //display average
}
return 0;
}
Question 3c (10)
The average life expectancy (in hours) of a lightbulb based on the bulb’s wattage is listed in the table
below:
Write a program that when given a bulb’s wattage, displays the average life expectancy.
©
UNISA
2023
[TURN OVER]