100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

Question and Answers (EXAM PACK) | COS1511 Programming I

Rating
-
Sold
4
Pages
212
Grade
A+
Uploaded on
22-10-2022
Written in
2022/2023

Question and Answers (EXAM PACK) | COS1511 Programming I | Answers to Jan 2022, Nov 2018, Nov 2021, Nov 2016, June 2016.

Institution
Course











Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
October 22, 2022
Number of pages
212
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Content preview

OCT/NOV 2022




75




This examination question paper consists of 9 pages out of 75 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 COS1511
OCT/NOV 2022

QUESTION 1 6 MARKS

1.1 Consider the C++ code segment below. What value will newval have 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]

, 3 COS1511
OCT/NOV 2022

QUESTION 2 4 MARKS

In Questions 2.1 and 2.2 you have to write down what the purpose of the segment of code is. Look at
the following example before answering the questions:

int a,b,c;
cin >> a >> b >> c;
cout << c + b + a;

The purpose of the above code segment is to input three integer values and display their sum.

Now answer questions 2.1 and 2.2 below:

2.1 Assume that s and n have been declared as integers. Explain in words what the purpose of
the following segment of code is: (2)

int s = 0;
int n = 0;
while (n <= 5)
{
s = s + n;
n++;
}

2.2 Explain the purpose of the following segment of code: (2)

int numbers[ ] = {11, 0, 15, 0, 16, 23};
int c = 0;
for (int i = 0; i <= 5; i++)
if (numbers[i] != 0)
c += 1;

QUESTION 3 8 MARKS

Consider the following C++ code segment below.

1 int result(int valueP)
2 {
3 int a = 2;
4 int count = 0;
5 while (count < valueP)
6 {
7 a += count + a / 2;
8 count += 2;
9 }
10 return a;
11 }

Demonstrate the execution and output of the program by drawing a variable diagram that traces each
line of code if the value of value P is 6. You are required to draw a variable diagram to illustrate
what the code does.

[TURN OVER]

, 4 COS1511
OCT/NOV 2022

QUESTION 4 8 MARKS

Karlton Learning wants a program that displays the amount of money a company owes for a seminar.
The fee per person is based on the number of people the company registers. For example, if the
company registers 7 people then the amount owed is R 560.00. If the user enters a number that is less
than or equal to 0, the program should display an appropriate error message.

Number of registrants Fee per person
1 through 4 R100.00
5 through 10 R 80.00
11 or more R 60.00

Complete the program below.

#include <iostream>
using namespace std;
int main()
{
// Question 4.1 (2)
// Declare a variable to hold the number of registrants and a
// variable to hold the amount a company owes for a seminar.

// Question 4.2 (2)
// Write statements to input the number of registrants. If the
// user enters a number that is less than or equal to 0, the
// program should display an appropriate error message.

// Question 4.3 (4)
// Write statements to calculate and display the amount of money
// a company owes for a seminar.

return 0;
}

QUESTION 5 5 MARKS

In order to plan road maintenance, the department of Road Works request that road usage must be
determined. This is done by counting the number of vehicles using the road with two wheels, four
wheels and more than four wheels respectively. Write down ONLY the necessary switch statement
to count the number of vehicles with two wheels, four wheels and more than four wheels. Do NOT write
a complete program.

Use the following variables:

int nrWheels; // number of wheels of a vehicle using the road

int countTwo, countFour, countMore; // the counters for the
// number of vehicles with
// two, four and more than
// four wheels respectively



[TURN OVER]
$3.04
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
robyn9745

Get to know the seller

Seller avatar
robyn9745 DK
Follow You need to be logged in order to follow users or courses
Sold
9
Member since
3 year
Number of followers
9
Documents
14
Last sold
1 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions