Professional Documents
Culture Documents
BS Programme
The instructions below must be followed strictly. Failure to do so can result in serious grade loss.
⇒ Do not talk to anyone once the exam begins.
⇒ Keep your eyes on your own paper.
⇒ Read all questions very carefully before answering them. All questions are compulsory.
⇒ Only answers written within the given area will be marked.
⇒ You may use page 5 and the sides of other pages for rough work.
⇒ Put your pens down immediately when you are asked to. Marks may be deducted if you keep writing.
⇒ There are multiple versions of the exam. Do not try to copy from others. Cheating cases will be
severely dealt with.
⇒ You are not required to include libraries or define the main function except in questions where you are
asked to write a complete program.
Specific instructions:
1. Open book/notes, closed book/notes, help sheet: Closed Book / Closed Notes.
Question 1 2 3 4 5 6 7 8 9 Total
Marks Obtained
1
Question 1 [10 marks]
b. What is the value of Total at the end of the following code? [2]
int price = 10;
int items = 2;
int Items = 3;
int Price = price + 5;
Items = items;
int Total = Price * items;
Total = 30 [2]
c. What are the values of c, d, and e at the end of the following code? [3]
string a = "CS";
string b = "100";
string c = a + b;
int d = a.length() + b.length();
string e = b.substr(1,2);
3 . 1 4 1 5 9
cout<<setw(8)<<p;
[1]
cout<<setw(8)<<setprecision(4)<<p;
3 . 1 4 2
[1]
e. What are the values of x and y at the end of the following code? [2]
double a = 22 / 7;
int b = 2.717;
double x = -a * b + a / 2.0;
int y = pow(2,a) * (a % 2);
The code below contains 7 errors in total. Find each error, identify whether it
is a syntax (compile time) or logical (runtime) error, and mention how the
error can be fixed. Two of the errors are already done for you as examples.
1 #include <iostream>
2 using namespace std;
3 int Main()
4 {
5 const int days = 7;
6 int daily;
7 double raise = 5%;
8
9 if (daily < 0);
10 daily = 0;
11
12 days = 365;
13 double new_daily = daily * (1 + raise);
14 cout << "New yearly salary:' << new_daily * days;
15
16 return 0
17 }
3
Question 3 [10 marks]
a. Write a line of code for computing the following expression. Assume the
variables are already defined. [3]
! !
𝑧 = 𝑥 + 𝑦 − 2𝑥𝑦 cos 𝑎
z = sqrt(x*x + y*y – 2*x*y*cos(a))
return 0;
}
Note:
• Using data type float or double for km and miles [1]
• Syntax of cout and cin [1]
• iostream, namespace, main, etc. [1]
c. Write a piece of code that initializes two character variables (to any values
of your choice), swaps their values, and then displays them. [4]
char ch1 = 'a'; char ch2 = 'b';
ch1 = ch2;
ch2 = temp;
4
Question 4 [10 marks]
The code below identifies whether a given number is prime or not. Draw the
flowchart for this code.
int n;
cout << "Enter an integer greater than 1: ";
cin >> n;
bool pr = true;
int i = 2;
while (i <= n / 2)
{
if (n % i == 0)
{
pr = false;
}
++i;
}
if (pr)
cout << "This is a prime number";
else
cout << "This is not a prime number";
Draw the flowchart on the next page. You may use this page for any rough
work for any question.
5
[Flowchart]
6
Question 5 [10 marks]
a. The code below takes the integer num from the user and computes
another integer b. What is the output of the code for each given input?
Can you explain what the code is doing to num? [6]
while(temp > 0)
{
rem = temp % 2;
b = b + (rem * place);
temp = temp / 2;
cout << b;
num b
100 [2]
4
101 [2]
5
7
b. The code below calculates the tax due on a person’s salary. It has
some logical errors in the branching conditions. What is the output of
this code for each given input? [4]
double salary;
bool tax_free;
double tax_rate = 0.10;
double tax;
const int min_taxable = 1000;
cout << "Enter salary and tax status (exempt or not): ";
cin >> salary >> tax_free;
8
Question 6 [10 marks]
The following code has no errors. What will be the value of variables a, b,
c, d, and e at the end of the code? Identify what each of these variables
are counting.
int a = 0, b = 0, c = 0, d = 0, e = 0;
string s = "Midterm Exam CS100";
for(int i = 0; i < s.length(); i++)
{
if(s.substr(i,1) == "a" || s.substr(i,1) == "A"
|| s.substr(i,1) == "e" || s.substr(i,1) == "E"
|| s.substr(i,1) == "i" || s.substr(i,1) == "I"
|| s.substr(i,1) == "o" || s.substr(i,1) == "O"
|| s.substr(i,1) == "u" || s.substr(i,1) == "U")
a++;
else if((s.substr(i,1) >= "a" && s.substr(i,1) <= "z")
|| (s.substr(i,1) >= "A" && s.substr(i,1) <= "Z"))
b++;
else if (s.substr(i,1) >= "0" && s.substr(i,1) <= "9")
c++;
else if (s.substr(i,1) == " ")
d++;
else
e++;
}
9
Question 7 [10 marks]
Write a piece of code that takes three different integers as input from the
user and prints two things:
a. The integer that is neither the biggest nor the smallest.
b. How many of the integers are odd.
Assume that the user provides valid input (i.e. you do not need to check for
incorrect input).
int odd = 0;
if (n1 % 2 == 1) { odd++; }
if (n2 % 2 == 1) { odd++; }
if (n3 % 2 == 1) { odd++; }
cout << odd << " of the numbers are odd " << endl;
Part a Part b
Used decision statements [1] Used decision statements [1]
Correct syntax of decision Correct syntax of decision
statements + major error in statements + major error in
conditions [2] conditions [2]
Correct syntax of decision Correct syntax of decision
statements + minor error in statements + minor error in
conditions [3] conditions [3]
Completely correct [4] Completely correct [4]
10
Question 8 [10 marks]
Write a piece of code that takes in two positive integers m and r from the
user and prints all the numbers less than r that are exact powers of m.
Assume that the user provides valid input (i.e. you do not need to check for
incorrect input).
int m, r;
int po = m;
po = pow(m,i);
11
Question 9 [20 marks]
Write a piece of code that takes in a time from the user: one number
between 1 and 12 for hours, another number between 0 and 59 for minutes,
and a string for AM or PM. It then tells the user whether the time he/she has
entered is before, after, or equal to your bedtime (8:00 PM). Assume that the
user provides valid input (i.e. you do not need to check for incorrect input).
int h, m;
string ampm;
else
12