0% found this document useful (0 votes)
56 views

Mid+ CS100F17V1

The document provides the cover page for a midterm exam, outlining instructions that students must follow such as not opening the exam until instructed, keeping their eyes on their own paper, only writing answers in the given areas, and not talking or copying from others during the exam. It specifies the exam

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)
56 views

Mid+ CS100F17V1

The document provides the cover page for a midterm exam, outlining instructions that students must follow such as not opening the exam until instructed, keeping their eyes on their own paper, only writing answers in the given areas, and not talking or copying from others during the exam. It specifies the exam

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/ 13

Lahore University of Management Sciences

BS Programme

Name: Section: Roll Number :


Course Title: Computational Problem Solving Semester: Fall
Course Code: CS 100 Academic Year: 2017-2018
Instructor: Dr. Shafay, Dr. Rizwan, Zaid Date: October 20, 2017
Exam: Midterm Time Allowed: 100 Minutes
Total Pages: 12 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 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.

2. Calculator usage: Not allowed.

3. Write in pen/pencil: Any. No red pen.

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

Question 1 2 3 4 5 6 7 8 9 Total

Max Marks 10 10 10 10 10 10 10 10 20 100

Marks Obtained

1
Question 1 [10 marks]

a. Which of the following cannot be used as a variable name?


(circle the answer) [1]
i. 1Price
ii. Price1
iii. price1
iv. _price1
v. Price_1

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 =

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);

c = d = e =

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


double p = 3.14159;


cout<<setw(8)<<p;

cout<<setw(8)<<setprecision(4)<<p;

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);

x = y =
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 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 }

Line (order not


Error Location Type Correction
important)

3 Main syntax main (small m)

16 0 syntax 0; (add semicolon)

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 𝑎

b. Write a complete program for distance conversion from km to miles. The


program asks the user to enter distance in km and displays the distance in
miles. Assume that the user provides valid input (i.e. you do not need to
check for incorrect input). (Hint: 1 km = 0.62 mile) [3]

#include












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]

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.

Programmer joke: !false


It’s funny because it’s true…

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]

int num, temp, rem, b = 0, place = 1;

cout << "Enter an integer: ";


cin >> num;
temp = num;

while(temp > 0)
{
rem = temp % 2;

b = b + (rem * place);

temp = temp / 2;

place = place * 10;


}

cout << b;

num b

Explanation =

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;

if (salary > min_taxable) { tax_rate = 0.05; }

else if (salary > 2 * min_taxable) { tax_rate = 0.10; }

else if (salary > 3 * min_taxable) { tax_rate = 0.15; }

else if (salary < min_taxable) { tax_rate = 0.00; }

else if ( tax_free ) { tax_rate = 0.00; }

tax = salary * tax_rate;


cout << tax;

salary tax_free tax

1000 1 (true)

1000 0 (false)

500 0 (false)

4000 1 (true)

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, b, c, d, e;

a = b = c = d = 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++;
}

Variable Value Purpose/Function

9
e

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).

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).

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).

12
13

You might also like