Download - CS101 Autumn 2019 End-semester Exam
★ CS101 Autumn 2019 End-semester Exam
13 questions, 14 pages, 40 marks
13th November 2019, 2:00 pm to 5:00 pm
Name: _________________________________ Roll number: _____________________________ Lab No (L1/L2/L3): ___________ Group No.: ______________ Department: _________________
Write your answers directly on this answer paper in the spaces provided. You can use spare pages on the
question paper for rough work. Answers must be written in pen (not pencil). Write your roll number on all
pages. Please make sure that your handwriting is readable.
(FOR GRADING PURPOSES ONLY) Q# Max.
marks Obtained
marks Grading
TA Verifying
TA TA/Student remarks
1 2
2 3
3 4
4 2
5 3
6 3
7 2
8 2
9 4
10 4
11 4
12 4
13 3
Total: ____________________
1
★ Roll number: ________________________________
For rough work
2
★ Roll number: ________________________________
Q1. (2 marks) What will be the values of variables a and b after the following code fragment is
executed?
int a=0, b=0;
for(int i=0; i<3; i++) {
for(int j=i; j<3; j++) {
b = b + i;
}
i++;
a = b + i;
}
Answer:
(i) a = __________________
(ii) b = __________________
Q2. (3 marks) Fill in the number representations of the numbers in the blank cells. Assume all numbers
are stored using 8-bits and two’s complement form. For binary representations of fractions, assume the
following format: MSB bit: sign of mantissa, 4 bits of mantissa followed by 1 bit of sign of exponent and
2 bits of exponent.
Decimal Hexa-decimal Binary
23
0xEA
3.5
3
★ Roll number: ________________________________
Q3. (4 marks) What are the outputs corresponding to the cout statements in the code listed below?
#include<iostream>
int func(int& a, int b) {
a = a + b;
b = a;
return b;
}
int main () {
int a[5] = {5, 4, 3, 2, 1};
int *b;
b = a; Answers:
cout << *b << endl; (i) _________________________
a[1] = *(&a[0]);
cout << a[1] << endl; (ii) _________________________
b = b + 2;
cout << *b << endl; (iii) _________________________
*b = *a + 2;
cout << *b << endl; (iv) _________________________
int c = 8, d = 5;
d = func(c, d) - d;
c = c - d;
cout << c << endl; (v) _________________________
cout << d << endl; (vi) _________________________
char e[]= "END-SEM-EXAM-2019-CS101";
char *f = e;
cout << f + (f[1] - f[0] + 4)<<endl; (vii) ________________________________
cout << &e[f - &e[1] + 9]<<endl; (viii) ________________________________
return 1;
}
4
★ Roll number: ________________________________
Q4. (2 marks) What is the minimum number of swap operations needed to sort the following sequence of numbers in ascending order using Selection Sort?
Sequence Minimum number of swap operations between elements of the sequence
(i) 5 4 3 2 1 6
(i) 1 2 3 5 4 6
(i) 6 2 3 4 1 5
(i) 2 3 4 6 1 5
Q5. (3 marks) Consider the function sum that attempts to add the elements of an array a. The array a
and the total number of elements of the array are passed as arguments to this function.
int sum(int a[], int len) {
unsigned int i;
int result = 0;
for (i = 0; i <= len-1; i++)
result += a[i];
return result;
}
Assume the above function sum is called from the main program as follows:
int result = sum(A, n); // n is the number of elements in the array A
What will be the return value of the function sum for the following cases?
If the program executes correctly, then state its value. If not, output any possible errors (logical or
syntactical), stating reasons.
Cases Outputs
int A[3]={10, 20, 30};
int n = 3; (i) ___________________________________________
int A[3]={10, 20, 30};
int n = 0;
(ii) ___________________________________________
int b=3;
int *A = &b;
int n = 1;
(iii) ___________________________________________
5
★ Roll number: ________________________________
Q6. (3 marks) Consider the following program, in which A, B, and C are placeholders.
int main()
{
int x=A, y=B, z=C; if ( !(x ^ y) )
cout << "1";
else
cout << "0";
if ( !((x | z) & ~(x & z)) )
cout << "1";
else
cout << "0";
if ( ((y & ~z) | (~y & z)) )
cout << "1";
else
cout << "0";
return 0;
}
What will be printed in the following cases, for different values used for A, B, and C?
(i) A = 3, B = 5, C = 7 (i) ________________________________
(ii) A = 5, B = 5, C = 5 (ii) ________________________________
Q7. (2 marks) Which of the following four marked lines have the correct C++ syntax? (Write Yes for
correct syntax and No for incorrect syntax.)
struct T {int a[1]; int* b;};
int main() {
T a, *b;
b = a; // line 1 (*b)->a = a.a; // line 2 *(a.a) = 1234; // line 3 a.b = a.a; // line 4
return 1;
}
Answer:
1.
2.
3.
4.
6
★ Roll number: ________________________________
Q8. (2 marks) R1D1 and R2D2 are programmers of the CS101 class. They have written a function that calculates the multiplication table of a number n up to 10 and returns the sum of all the calculated multiples. E.g., if we consider n as 8, then the program should calculate and print the sum (8*1 + 8*2 + ... + 8*10) = (8 + 16 + ... + 80) = 440
The functions of R1D1 and R2D2 are as follows:
int R2D2(int n, int i) {
if(i <= 9)
n = n*i + R2D2(n, i+1);
return n;
}
int R1D1(int n, int i) {
if(i != 0){
n = R1D1(n, i-1) + n*i;
return n;
}
else
return n*i;
}
int main() {
int n;
cout << "Enter a number " << endl;
cin >> n;
cout << R2D2(n, 1) << endl;
cout << R1D1(n, 10) << endl;
return 0;
}
Choose the correct option from the following:
(A) Both the functions will compute the sum as mentioned in the question.
(B) Only R1D1’s function will work correctly.
(C) Only R2D2’s function will work correctly.
(D) None of the functions will work correctly.
Answer:
7
★ Roll number: ________________________________
Q9. (4 marks) Using STL classes, manage the names and grades of courses taken by the students.
Assume grades have values 0 to 10. The number of courses each student has registered for and the
number of students itself is not known apriori. Complete the following functions and the main function.
Note: For each student, store name and grades for each registered course.
// add student name and her/his grades to the store.
void addstudentinfo( _____________________________ ) {
}
// print all student records in the format: student name, average grade
void printstats( ___________________________ ) {
}
8
★ Roll number: ________________________________
int main() {
int i, n;
// declare variable for storing records
map _____________________________ *coursedb;
// allocate memory for map
coursedb = ___________________________________ ;
cin >> n ; // number of students
// collect and add information of all students one by one
for(i=0; i<n; i++)
addstudentinfo( ________________________ );
printstats( __________________________ );
return 1;
}
Input format
<n> // number of students
name1 <k> <g 1 > <g 2 > … <g k > // name followed by k, the number of grades
// and then the grades
. // for all n students
.
.
Sample input Sample output
5
Student1 3 7 8 9
Student2 2 2 10
Student3 4 5 6 7 8
Student4 1 10
Student5 5 8 8 8 8 8
Student1 8
Student2 6
Student3 6.5
Student4 10
Student5 8
9
★ Roll number: ________________________________
Q10. (4 marks) Listed below is an incomplete code snippet that collects information about persons and
their friends. The example collects information of 3 persons (0, 1 and 2). Person 1 is friend of Person 0,
and Persons 0 and 2 are friends of Person 1. Fill in the blanks to complete the program.
Note: push_back is the member function of the vector STL class to add elements.
struct person {
string name, petname;
vector <_________________________> friends; }; // blank1
int main() {
person people[3]; // array to store 3 persons // get basic information (name and petname) of each person
for (int i=0; i<3; i++) {
cin >> _____________________________; // name , blank2
cin >> _____________________________; // petname , blank3
}
// setup friends of Person 0 and Person 1 // Friend of Person 0 is 1, and Friends of 1 are 0 and 2
________________________________________________; // blank4
________________________________________________; // blank5
________________________________________________; // blank6
// print friends of Person 1. first find the number of
// friends using a function call
size_t s = ___________________________________; // blank7
// print names of friends (of Person 1)
for (int i=0; i<s; i++)
cout << ______________________________________ <<endl; // blank8
// change pet name of Person 1
cin >> _______________________________________________; // blank9
// print updated pet name of Person 1 via friend information of Person
0
cout << ______________________________________________; // blank10
return 1; }
10
★ Roll number: ________________________________
Q11. (4 marks) Write a function to find the kth smallest number in an array. Input to the function is an
array of non-negative integers, the number of elements in the array, and the value of k. No STL classes to be used. Duplicate numbers, if any, are treated as separate numbers.
// Inputs are the array, the length of the array, and the value of k.
// Return the kth smallest element. Assume 0<k<=len.
int ksmallest ( __________________, int len, int k ) {
// declare temporary array to store k elements
int* kset = ____________________________ ;
// set all elements of kset to -1
// consider elements of array sequentially and determine if each is part of
// the kth smallest set. No sorting or updates of input array allowed.
for (int i=0; i<len; i++) {
}
return ______________________________;
}
11
★ Roll number: ________________________________
Q12. (4 marks) The struct Point defines a point on a 2D plane and class Shape uses a set of points to
represent a shape on the plane. Complete the definition of the Shape class with the required
constructors, destructor, and operator overloading functions. Usage is specified in the main function.
struct Point { int x; int y; };
class Shape {
int npoints; // number of points
Point* points; // array of points
public:
// default constructor
Shape() { npoints = 0; points = NULL; }
12
★ Roll number: ________________________________
// definition of class Shape continues
};
int main() {
Point p1 = {0, 0}, p2 = {5, 0}, p3 = {5, 5};
Shape s1, *s2, s3;
((s1 + p1) + p2) + p3; // + adds a point to the calling Shape object
s2 = new Shape();
s3 = *s2 = s1; // = is the assignment operator
delete s2;
return 0; }
13
★ Roll number: ________________________________
Q13. (3 marks) A binary tree---a node with at most two children, is represented using struct bt.
Complete the function which finds the depth and the number of leaf nodes of the binary tree. A node
without any children is a leaf node. Depth of a node is the number of edges from the root node. The root
is at depth 0. Depth of the tree is the maximum depth amongst all nodes.
// struct storing value and pointers to children of a node
struct bt { int value; bt *left, *right; };
// function to estimate the depth of the tree and the number of leaf nodes
int tree_stats(bt* node, ________________ leafcount) {
return ___________________________;
}
int main() {
// assume this function builds a tree and returns pointer to root of tree
struct bt *root = construct_tree();
int leafcount = 0, depth = 0;
depth = tree_stats(______________________, ____________________);
cout << depth << leafcount;
return 0; }
14