CS 100 Spring 2018 Midterm V3
CS 100 Spring 2018 Midterm V3
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 the area outside the answer boxes for rough work. The last page is also empty.
⇒ 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.
⇒ This booklet contains Q1-Q5. Q6-Q9 are programming questions available on LMS after you submit
this booklet.
⇒ It is recommended to spend about 1 hour on the written part and 1.5 hours on the programming part.
Specific instructions:
1. Open book/notes, closed book/notes, help sheet: Closed Book / Closed Notes.
VERSION 3
Question 1 2 3 4 5 6 7 8 Total
Marks Obtained
1
Question 1 [10 marks]
i. Break
ii. 0brake
iii. _break
iv. brake
v. break
b. Answer true or false for each of the following. Write your answer on the
left side. [4]
double gr = 1.61803;
cout<<fixed<<setprecision(4)<<gr;
cout<<setw(7)<<setprecision(3)<<gr;
d. What are the values of v_1 and v_2 at the end of the following code? [2]
double pi1 = 22 / 7;
int pi2 = 3.14159;
double v_1 = pi2 / 2.0 + pi1 * pi2;
int v_2 = pow(pi1,2) * (pi2 % 2);
v_1 = v_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
6 int Price;
7 double inflation = 10%;
8
9 cout << "Enter original price: ";
10 cin >> Price;
11
12 cout >> "Enter number of years: ";
13 int yrs;
14
15 for (int i = 0; i < yrs; i++);
16 {
17 Price = Price * inflation;
18 }
19
20 cout<<"Price after << yrs << " yrs is " << Price;
21
22 return 0
23 }
3
Question 3 [10 marks]
b. What is the truth value of the following lines of code? Write your answer
on the left side. [3]
int a = 100; bool b = 2;
i. 0 < a < 10
ii. a = b - 1
iii. !b || a%10
c. Write a piece of code that asks the user to enter an integer between 1 and
6 (inclusive). If the user enters a number outside the range, or a number
that is not an integer, the code should repeat asking for a valid integer
until the user provides it. Assume that the user only enters numeric
values. [4]
Hint: you can use the floor function, which throws away the decimal part
of a number, to check whether the number entered was an integer or not:
floor(4.6) is 4
4
Question 4 [10 marks]
5
Question 5 [10 marks]
a. The code below takes the integer num from the user and computes two
integers: count1 and count2. What is the output of the code for each
given input? Can you explain what count1 and count2 represent?
[6]
cout << count1 << " " << count2 << endl;
1234
567
Explanation =
6
b. The code below calculates the sale price of an item. It has some logical
errors in the branching conditions. What is the output of this code for
each given input? [4]
double price;
bool on_sale;
double disc_rate = 0.30;
const int min_for_disc = 5000;
cout << "Enter original price and sale status (1 / 0): ";
cin >> price >> on_sale;
5000 1 (true)
5000 0 (false)
2500 0 (false)
20000 1 (true)