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

Spring Midsem

Cs101 iitb

Uploaded by

gamerdeath923
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Spring Midsem

Cs101 iitb

Uploaded by

gamerdeath923
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CS101 Spring 2020

A Mid-semester Exam
11 questions, 12 pages, 50 marks
26th February 2020, 8:30 am to 10:30 am

Name: ___________________________ Roll number: _________________________

Lab No (L1/L2/L3): ___________ Group No.: _________ Department: _____________

You MUST write your final answer on this question paper itself. You can use spare pages on
the question paper for rough work. Answers must be written in p​ en (NOT pencil). Write your
roll number on all pages. Assume all codes have the “simplecpp” header file included.

(FOR GRADING PURPOSES ONLY)


Q# Marks Grading Verifying TA/Student Remarks
TA TA
1
2
3
4
5
6
7
8
9
10
11

Total: ​____________________

1
Roll number: _________________________

For Rough work

Page 2 of 12
Roll number: _________________________

Q1 (5 marks). ​The 52 cards in the playing deck are numbered from 1 to 52 as follows.
Group the cards by their suits. The order of suits is Spades, Clubs, Hearts and Diamonds.
Within a suit, order the cards in the increasing order of their value, Ace being counted as 1.
Then number the cards as per this ordering.

For example: 3 of Spades gets the value 3, while 7 of Hearts gets the value 33.

The following code takes as input an integer value representing a card in the given fashion.
Complete the code to print the card based on its value. Print the name for the face cards,
which are Jack, Queen, and King. For others, print their numerical value.

#include <simplecpp> Answers:

main_program { (i) _____________


int n;
cin >> n; (ii) _____________

switch (___(i)___) { (iii) _____________


case ___(ii)___:
cout << "King of "; break; (iv) _____________
case ___(iii)___:
cout << "Queen of "; break; (v) _____________
case ___(iv)___:
cout << "Jack of "; break; (vi) _____________
default:
cout << ___(v)___ << " of "; (vii) _____________
}
(viii) _____________
switch (___(vi)___) {
case ___(vii)___: (ix) _____________
cout << "Spades"; break;
case ___(viii)___: (x) _____________
cout << "Clubs"; break;
case ___(ix)___:
cout << "Hearts"; break;
case ___(x)___:
cout << "Diamonds";
}
}

Page 3 of 12
Roll number: _________________________

Q2 (4 marks)​. The following program accepts 5 numbers and their respective 5 choices
(​‘s’ or ​‘f’​). If the choice is ​‘s’​, then the program should print the sum of the digits of that
number; whereas if the choice is ​‘f’​, then the program should calculate the factorial of the
number and print the sum of the digits of the factorial obtained. The function ​‘fact’ calculates
the factorial while the function ​‘sum’ calculates the sum of the digits. Fill in the blanks to
complete the program.

int sum(int n){ Answers


int foo = 0;
while(n!=0) {
foo += _________(i)__________;
(i) ____________________
n = n / 10;
}
return foo;
}
(ii)_____________________
int fact(int n) {
int bar = 1;
while(n >= 1) {
bar = bar * n; (iii)____________________
n--;
}
return _______(ii)________;
} (iv)_____________________
main_program {
int n; char choice;
for (int i=1; i<=5; i++) {
cin >> n; cin >> choice;
switch(choice) {
case 'f':
n = _____(iii)_____;
case 's':
n = _____(iv)_____;
}
cout << n << “\n”;
}
}

Page 4 of 12
Roll number: _________________________

Q3 (3 marks). ​The following code reads 10 characters, one by one, and converts all
lowercase alphabet to uppercase. It doesn’t change other characters. Fill in the blanks to
complete the code.

Note: In the ASCII mapping, lowercase characters are mapped sequentially i.e. one after the
another. Similarly, for uppercase characters. Note that the difference between ASCII of
lowercase ‘a’ and uppercase ‘A’ is not 26.

main_program { Answers.
char c;
repeat(10) { (i) _____________________
cin >> c;
if (_____(i)_____ <= c &&
c <= _____(ii)_____) { (ii) _____________________
c += _____(iii)_____;
}
cout << c; (iii) _____________________
}
}

Q4 (4 marks).​ C++ has bitwise operations, which act on the binary representations of their
operands bit by bit. “&, |, ~” are bitwise AND, OR and NOT respectively. “​a >> b​” shifts the
bits of “​a​” to the right by “​b​” places.

Predict the output of the following instructions. The integer variable “​a​” is initialized to ​90.

Code Output

cout << (a >> 2);

cout << (a & 34);

cout << (a | 45);

cout << (a & (~42));

Page 5 of 12
Roll number: _________________________

Q5. (4 Marks) ​ Given below are four programs. In each case predict the output.

Program - i Program - ii

void incr(int &a) { void incr(int b) {


a++; int a = b;
} a++;
}
main_program {
int a = 10; main_program {
incr(a); int a = 10;
cout << a; incr(a);
} cout << a;
}

Program - iii Program - iv

int incr(int a) { int incr(int b) {


int b = a; int a = b;
b++; a ++;
return a; return a;
} }

main_program { main_program {
int a = 10; int a = 10;
a = incr(a); a = incr(a);
cout << a; cout << a;
} }

(i) _________________ (ii) _________________

(iii) ________________ (iv) ___________________

Page 6 of 12
Roll number: _________________________

Q6 (4 marks).​ The program accompanying the following diagram for drawing a tree is given
below. It uses a recursive approach for drawing such a tree. Fill in the blank spaces so that
the program when executed draws the following diagram.

#include <simplecpp> Answers.

void tree(int L, double trunkLength) { (i) _________________


if (L > 0) {
forward(____(i)____);
left(30);
(ii) ________________
tree(L-1, 0.8*trunkLength);
right(____(ii)____);

forward(0.4*trunkLength); (iii) ________________


right(____(iii)_____);
tree(L-1, 0.8*trunkLength);
left(30);
(iv) ________________
forward(-trunkLength);
}
}

main_program {
turtleSim();
left(____(iv)____);
tree(7, 70);
}

Page 7 of 12
Roll number: _________________________

Q7. (6 marks)​ Consider the functions f and g. What will be the value of “a”, “b” and “c” after
the two main_program segments (Program 1 and Program 2) are executed:

bool f(int* x, int* y){


*x -= 8;
int temp;
temp = *x;
*x = *y;
*y = temp;
return true;
}

bool g(int *y){


*y += 20;
if(*y > 100){
return false;
}
else return true;
}

Program Segment 1 Program Segment 2

main_program{ main_program{
int a = 5, b = 89; int a = 5, b = 89;
bool c = g(&b) || f(&a, &b); bool c = g(&b) && f(&a, &b);
} }

What would be the value of a, b and c? What would be the value of a, b and c?

a ________________________ a ________________________

b ________________________ b ________________________

c ________________________ c ________________________

Page 8 of 12
Roll number: _________________________

Q8 (6 marks).​ What is the output printed by the following program?

main_program{
int a=5, b=8, c=13;
int *p = &a, *q = &b, *r=&c;
int count = 2;
while(count >0){
p = q; q = r; r = p;
count--;
}
cout << *p << " "<< *q << " " << *r << endl;
cout << a << " " << b << " " << c << endl;

count = 2;
while(count > 0){
*p = *q; *q = *r; *r = *p;
count --;
}
cout << *p << " "<< *q << " " << *r << endl;
cout << a << " " << b << " " << c << endl;
}

Please provide your output here:

(i) First output line: ______ ______ ______

(ii) Second output line: ______ ______ ______

(iii) Third output line: ______ ______ ______

(iv) Fourth output line: ______ ______ ______

Page 9 of 12
Roll number: _________________________

Q9 (6 marks). ​Linear diophantine equation for a tuple <​a, b​> is “​ax + by = 1​” where “​a​”
and “​b​” are given positive integers, and the variables are “​x​” and “​y​”. A solution to this
equation is integer assignments to the variables, such that the equation is satisfied (note that
variables can take negative integer values).

For example, “​7x + 5y = 1​” has as a solution “​x = -2, y = 3​”. Note that some
equations may have no solutions, such as “​12x + 16y = 1​”.

A recursive way to find a solution, if one exists, is to do the following.


I. In the base case: If “​a​” is divisible by “​b​”:
A. If “​b​” is 1, then the solution is “​x = 0, y = 1​”.
B. Otherwise, there is no solution.
II. Otherwise, find the solutions “​x​1​, y​1​” for the tuple <​b, r​>, (​r​ is the remainder when
“​a​” is divided by “​b​”), if one exists. Then do the following:
A. x = y​1
B. y = x​1​ - q * y​1​, ​where “​q​” is the quotient when “​a​” is divided by “​b​”.

Complete the following code which implements this recursive algorithm.

#include <simplecpp> Answers.

bool diophantine(int a, int b, int &x, int &y){ (i)​ _____________


if (_____(i)_____) {
if (_____(ii)_____) {
x = 0; (ii)​ _____________
y = 1;
return true;
} else { (iii)​ _____________
return false;
}
} (iv)​ _____________

if (diophantine(_____(iii)_____)) {
int u = x; (v)​ _____________
int v = y;
int q = _____(iv)_____;
x = _____(v)_____; (vi)​ _____________
y = _____(vi)_____;
return true;
} else {
return false;
}
}

Page 10 of 12
Roll number: _________________________

Q10 (5 marks). ​The function ​eulerTotient​, implemented below, calculates the Euler’s
Totient function for a positive integer “​n​”, which it takes as its argument. The totient function
is defined as
1
ϕ(n) = n ∏ (1 − p)
p|n
where the product is over all primes that divide “​n​”. But the code has errors in exactly 5 lines,
some syntax errors, some logical. In the table, fill the line numbers which have errors, and
provide correct code for these lines. A line may have multiple errors.

​1 void isPrime(int n) {
2 int m = n-1;
3 for (int i=2; i<=m; i++) {
4 if (n % i == 2) return false;
5 }
6 return true;
7 }
8
9 int eulerTotient(n) {
10 int phi = n;
11 for (int i=2; i<=n; i++) {
12 if (n%i == 0 && isPrime(i)) {
13 phi = (phi / (i+1)) * (i-1);
14 }
15 }
16 return;
17 }

Line number Correct code

Page 11 of 12
Roll number: _________________________

Q11. (3 marks) The following recursive program reverses a sequence of integers. It first
takes the length of the sequence as input n. Then it takes n integer inputs. The output is the
reverse sequence (with spaces between the integers).

Example:
Sample input:
4
45 78 99 23

Sample Output:
23 99 78 45

#include <simplecpp>

void reverse(int n)
{
int x;
cin >> x; Answers:
if(n == 1){ // base case
_____ (i)______; (i) _____________________________
}
else{ // recursive part
______(ii)_______; (ii) _____________________________

______(iii)______;
} (iii) _____________________________
}

main_program{
int n;
cin >> n;
reverse(n);
}

Page 12 of 12

You might also like