[go: up one dir, main page]

0% found this document useful (0 votes)
75 views7 pages

CS 100 Spring 2018 Midterm V3

The document is an exam paper for a computational problem solving course. It contains instructions that students must follow, such as not talking or copying others' work during the exam. The exam consists of 9 questions - 5 written questions worth a total of 50 marks, and 4 programming questions worth another 50 marks. Students are advised to spend about 1 hour on the written section and 1.5 hours on the programming problems.

Uploaded by

Basra Ajmal
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)
75 views7 pages

CS 100 Spring 2018 Midterm V3

The document is an exam paper for a computational problem solving course. It contains instructions that students must follow, such as not talking or copying others' work during the exam. The exam consists of 9 questions - 5 written questions worth a total of 50 marks, and 4 programming questions worth another 50 marks. Students are advised to spend about 1 hour on the written section and 1.5 hours on the programming problems.

Uploaded by

Basra Ajmal
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/ 7

Lahore University of Management Sciences

BS Programme

Name: Roll Number:


Course Title: Computational Problem Solving Semester: Spring
Course Code: CS 100 Academic Year: 2017-2018
Instructor: Zaid Saeed Khan Date: March 14, 2018
Exam: Midterm Time Allowed: 2.5 hours
Total Pages: 7 Total Marks: 100

DO NOT OPEN THIS EXAM UNTIL TOLD TO DO SO.

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.

2. Calculator usage: Not allowed.

3. Write in pen/pencil: Anything except red pen.

4. Any other instruction(s): Mobile phones are not allowed.

VERSION 3

Question 1 2 3 4 5 6 7 8 Total

Max Marks 10 10 10 10 10 15 15 20 100

Marks Obtained

1
Question 1 [10 marks]

a. Which of the following variable names are invalid?


(circle all the answers) [2]

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]

i. C++ is a machine language


ii. A .exe file is in binary
iii. A linker translates code from C++ to binary
iv. A program with syntax errors is not compiled

c. What will be the output of the following statements? [2]

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 =

Programmer joke: !false


It’s funny because it’s true…
Question 2 [10 marks]

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 }

Line (order not


Error Location Type Correction
important)

3 Main syntax main (small m)

22 0 syntax 0; (add semicolon)

3
Question 3 [10 marks]

a. Write a line of code representing the following expression. Assume the


variables p, e, x, and m are already defined. [3]

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]

a. What is the output of the following code? [3]

string str = "detartrated";


int len = str.length();
for (int i = 0; i < len/2; i++)
{
cout << str.substr(i,len-2*i) << endl;
++i;
}

b. Draw a flowchart for the code above. [7]

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]

int num, dig, count1 = 0, count2 = 0;

cout << "Enter a positive integer: ";


cin >> num;

while (num > 0)


{
dig = num % 10;
if (dig % 2)
{
count2++;
}
count1++;
num = num / 10;
}

cout << count1 << " " << count2 << endl;

num count1 count2

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;

if (price > min_for_disc) { disc_rate = 0.10; }

else if (price > 2 * min_for_disc) { disc_rate = 0.20; }

else if (price > 3 * min_for_disc) { disc_rate = 0.30; }

else if (price < min_for_disc) { disc_rate = 0.00; }

else if ( !on_sale) { disc_rate = 0.00; }

double sale_price = price * (1 - disc_rate);


cout << sale_price;

price on_sale sale_price

5000 1 (true)

5000 0 (false)

2500 0 (false)

20000 1 (true)

You might also like