COS1511 January Examination 2023

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Jan/Feb 2023

70 Marks

This paper consists of 8 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]
2

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 x after 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 x after 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)
[TURN OVER]
float tax; float total;

cout << "Enter the cost of the item\n"; cin >> total;
3

if ( total >= 5.0)


{
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 x has the value of 3, y has the value of -2, and w is 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

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;

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


cout << "*";
cout << endl;
b. for (int i = 1; i <= 10; i++)
cout << i << " ";
cout << endl;

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


cout << "*";
cout << endl;

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


cout << "*";
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; [TURN OVER]
5

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;

//while loop to find a suitable candidate


cout << name << " qualifies for the internship " << endl;

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:

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 = 2c.1-----

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;

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


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

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:

• 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. [TURN OVER]
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).
7

Display the output as follows:

Please enter the following:


cost per room: 1000 sales tax
per room: 10
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 [TURN OVER]
3b.8-----;
3b.9-----;
8

cout << "Average for experiment no " << exp + 1 << ": "
<< 3b.10----- << endl << endl; //display average
}
return 0;
}

Question 3c (10)

In this program, you have to use 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy