Spring Midsem
Spring Midsem
A Mid-semester Exam
11 questions, 12 pages, 50 marks
26th February 2020, 8:30 am to 10:30 am
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.
Total: ____________________
1
Roll number: _________________________
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.
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.
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
Page 5 of 12
Roll number: _________________________
Q5. (4 Marks) Given below are four programs. In each case predict the output.
Program - i Program - ii
main_program { main_program {
int a = 10; int a = 10;
a = incr(a); a = incr(a);
cout << a; cout << a;
} }
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.
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:
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: _________________________
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;
}
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”.
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 }
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