0% found this document useful (0 votes)
37 views8 pages

COS1511 January Examination 2023

Uploaded by

rjcronje28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views8 pages

COS1511 January Examination 2023

Uploaded by

rjcronje28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Jan/Feb 2022

70 Marks

This paper consists of 7 pages out of 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.

ALL THE BEST

[TURN OVER]

Open Rubric
COS1511
Jan/Feb 2023
Choose the correct option and write only the number

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

2.What is the value of xafter the following statements? (2)

int x, y, z; y = 10;
z = 3;
x = y * z - 3;

1. 0
2. 3
3. 10
4. 27

3.What is the value of xafter the following statements? (2)

int x;
x = x + 15;

1. 0
2. 14
3. 15
4. not defined

4.What is the output of the following code? (2)

cout << "Where is the \\" << endl;

1. Where is the \
2.Where is the
3.Nothing, it is a syntax error
4.Where is the \ endl

5.What is the value of xafter the following statements? (2)

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)

float tax; float total; [TURN OVER]

cout << "Enter the cost of the item\n"; cin >> total;
if ( total >= 5.0)
COS1511
Jan/Feb 2023
{

tax = 0.14;
cout << total + (total * tax) << endl;
}

else
{

cout << total << endl;


}
1. 5
2. 5.7
3. 11
4. 11.4

7.Given the following code fragment and the input value of 3.0, what output is generated? (2)

float tax; float total;


cout << "Enter the cost of the item\n"; cin >> total;
if (total > 3.0)
{

}
else
{

tax = 0.10;
cout << total + (total * tax) << endl;

1. 3.0
2. 2.0
3. 3.3
4. 2.3

8.If xhas the value of 3, yhas the value of -2, and wis 10, is the following condition true or false? (2)

if( x < 2 && w < y)

1. true && true


true

2. false && false


false

3. false && false


true

4. true && false


false
[TURN OVER]
9.Given the following code fragment, and an input value of 8, what is the output that is generated? (2)

int x;
COS1511
Jan/Feb 2023

cout << "Enter a value\n"; cin >>


x;
if (x = 0)
{

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 [29]

Question 2a (6)

State what output, if any, results from each of the following statement by first working it out on paper
and then including it as a program. Submit a completed table as below:

CODE OUTPUT
Example for (int i = 0; i < 10; i++) 0123456789
cout << i;
cout << endl;

a. for (int i = 1; i <= 1; i++)


cout << “*”;
cout << endl;
b. for (int i = 2; i >= 2; i++)
cout << “*”;
cout << endl;

c. for (int i = 2; i >= 2; i++)


cout << “*”;
cout << endl;
[TURN OVER]

d. for (int = 12; i >= 9; i--)


cout << “*”;
COS1511
Jan/Feb 2023

cout << endl;

e. for (int i = 0; i <= 5; i++)


cout << “*”;
cout << endl;

f. for (int i = 1; i <= 5; i++)


cout << “*”;
i = i + 1;
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.

Question 2c (8)

The program given below gives the output below. Fill in the missing values and write only the answer.
The output:

Please enter 10 integers, positive, negative, or zeros.


The numbers you entered are:
2
7
-4
-3
0
7
4
0
-9
-4

There are 6 evens, which includes 2 zeros.


The number of odd numbers is: 4

#include <iostream>
using namespace std;

const int LIMIT = 2d.1-----

int main ()
{
2d.2----- counter;
int number;

int zeros = 0; [TURN OVER]


int odds = 0; int
evens = 0;
COS1511
Jan/Feb 2023

cout << "Please enter " << LIMIT << " integers, "
<< "positive, negative, or zeros." << endl;

cout << "The numbers you entered are:" << endl;


for (int counter = 1; counter <= LIMIT; counter++)
{
cin 2d.3----- number;
2d.4----- (number % 2)
{
case 0:
evens++;
if (number == 0)
zeros++;
2d.5-----;
case 1:

2d.6-----:
2d.7-----;
}
}
cout << endl;

cout << "There are " << 2d.8----- << " evens, "
<< "which includes " << zeros << " zeros."
<< endl;
cout << "The number of odd numbers is: " << odds
<< endl;

return 0;
}

QUESTION 3 [21]

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:

• if the number of rooms booked is at least 10, the discount is 10%;


• if the number of rooms booked is at least 20, the discount is 20%;
• if the number of rooms booked is greater or equal to 30, the discount is 30%;
• In addition, if rooms are booked for at least three days, there is an additional 5%
discount.
Write a program that prompts the user to enter the cost of renting one room, the number of rooms booked,
the number of days the rooms are booked and these laws tax (as a percent).

Display the output as follows:


[TURN OVER]
Please enter the following:
cost per room: 1000 sales tax
per room: 10
COS1511
Jan/Feb 2023
the number of rooms: 35 number of
days: 2

The total cost for one room is R1000 The


discount per room is 30%
The number of rooms booked: 35
The total cost of the rooms are R: 70000
The sales tax paid is : 10%
The total cost per booking is R77000

Question 3b (10)

Four experiments are performed, each consisting of five test results. The results for each experiment are
given in the following list.

1st experiment results: 23.2 31 16.9 27 25.4


2nd experiment results: 34.8 45.2 27.9 36.8 33.4
3rd experiment results: 19.4 16.8 10.2 20.8 18.9
4th experiment results: 36.9 39 49.2 45.1 42.7
Complete the program below using a nested loop to compute and display the average of the test results
for each experiment. Display the average with a precision of two digits after the decimal point.

#include <iostream>
using namespace std;
int main()
{
3b. 1-----result, average;

for (int exp = 0; 3b.2-----)


{
3b.3----- //initialize total
cout << "Please enter results for experiment no " << exp + 1
<< ": " << endl;

for (int i = 0; 3b.4-----)


{
cout << "Result no " << i + 1 << ": ";
cin >> 3b.5-----;
3b.6-----;//calculate total
}
3b.7----- //calculate average
//Settings to Display the average with a precision of two digits
after the decimal point
3b.8-----;
3b.9-----;
cout << "Average for experiment no " << exp + 1 << ": "
<< 3b.10----- << endl << endl; //display average
} [TURN OVER]
return 0;
}
COS1511
Jan/Feb 2023

Question 3c (10)

In this program, you have to make use of the switch statement.

The average life expectancy (in hours) of a lightbulb based on the bulb’s wattage is listed in the table
below:

Watts Life expectancy


(hours)
25 25000
40 1000
60 1000
75 750
100 750

Write a program that when given a bulb’s wattage, displays the average life expectancy.

©
UNISA
2023

[TURN OVER]

You might also like