Written Quizzes Solutions Spring
Written Quizzes Solutions Spring
Roll Number
Division / Group
Name
• Please write your answers neatly with pen, in the space provided.
• Each question carries 3 marks. Every question has 3 sub-parts, of 1 mark each.
• You can use the empty space in the margins and at the bottom of the page for rough work.
• Please avoid asking for clarifications, unless you think there is a mistake in the question itself.
1. Write down the output printed by the program in the space provided next to it.
int a = 5; int b = 7;
int c = 2; int d = 9;
int e = a * b % c + d;
printf("e=%d\n", e); //blank (a)
(a) e=
int i = a++ + ++b - c--;
printf("i=%d\n", i); //blank (b) (b) i=
//blank (c)
printf("f1=%.1f f2=%.1f\n", f1, f2);
2. Write down the output printed by the program in the space provided next to it.
while(n1 = n2) {
n1++; n2--;
count++;
}
//blank (a)
printf("count=%d\n", count);
(a) count=
while(count-- > 0) { (b) n1=
--count; n1++;
} (c) n2=
//blank (b)
printf("n1=%d\n", n1);
while(n2 < 0) {
n2++;
}
//blank (c)
printf("n2=%d\n", n2);
3. Consider the following badly indented program.
if(a >= 5)
if(b < 6) (a) What does the program print when a=5, b=7, c=9?
printf("AAA ");
else (b) What does the program print when a=4, b=4, c=4?
printf("BBB ");
else if(c == 9)
(c) Can the program print “AAA CCC ZZZ” for some values of
printf("CCC ");
a, b, c? Answer Yes or No.
printf("ZZZ\n");
4. Consider the following incorrect program to compute the average of a set of positive numbers.
The program accepts inputs until the sentinel value -1 is entered, and then prints out the average
of the entered numbers. The program has several bugs. Describe any three valid bugs in the space
provided next to the program.
int counter;
int number;
int total;
float average;
(a) Bug:
printf("Enter number, -1 to end: ");
scanf("%d", number);
int N;
printf("Enter number: ");
scanf("%d", &N); (a) What does the program print when N = 52?
int x = 0;
int y = 1; (b) What does the program print when N =
352?
while( y < N) {
y = y*2; (c) What is the program computing? Write
x++; down the output printed by the program as
} a mathematical function of the input N.
printf("%d\n", x);
6. Consider the following program which takes an input a positive integer N, and prints a N X N
matrix whose diagonal elements (on both diagonals) are 1, and the remaining elements are 0. For
example, the output of the program for N = 5 and N = 6 are shown in the table below, on the right
side, for your reference. Fill in the blanks with suitable code to complete the program. Write your
answer in the space provided in the table below.
int N; //N = 6
//N = 5
printf("Enter number: "); 100001
10001
scanf("%d", &N); 010010
01010
001100
00100
int i = 0; 001100
01010
int j = 0; 010010
10001
100001
while(i < N) {
j = 0; (a) What is A?
while(j < N) {
if( A ) //fill A
printf("1");
(b) What is B?
else printf("0");
B; //fill B
}
C1; //fill C1 (c) What are the two statements C1 and C2?
C2; //fill C2 You can write them in any order.
}
7. Consider the following program to compute the largest odd number in an input of 10 different
positive numbers. Fill in the blanks with suitable code to complete the program.
8. Shown below is a program to compute the integer quotient and remainder when positive integer p
is divided by positive integer q. The program performs division by repeated subtraction. Complete
the code such that the variable quot and rem hold the integer quotient and remainder respectively.
int p, q;
printf("Enter two numbers: "); (a) What is initial value A?
scanf("%d %d", &p, &q);
while( B ) {
rem = rem - q; (c) What is C?
C;
}
printf("%d %d\n", quot, rem);
9. Consider the following program that takes two numbers as input, and computes whether the two
numbers are co-prime or not. Recall that two numbers are co-prime if their only common factor
is 1. Fill in the blanks with suitable code to complete the program.
int p, q;
printf("Enter two numbers: ");
scanf("%d %d", &p, &q);
int num = 1;
(a) What is A?
int end = (p < q) ? p : q;
int r = 1;
if( C ) printf("coprime\n");
else printf("not coprime\n");
10. Consider the following program that computes the number of 7s in the digits of a given number.
Fill in the blanks with suitable code to complete the program.
int num;
printf("Enter number: ");
scanf("%d", &num);
(a) What is A?
int count = 0; int digit;
1.
(a) e=10
(b) i=11
(c) 49.0 49.5
2.
(a) count=10
(b) n1=5
(c) n2=0
3.
(a) BBB ZZZ
(b) ZZZ
(c) No. (Reason not required, but here it is: AAA and CCC are in if and else clauses, and they can
never both be printed together.)
5.
(a) 6
(b) 9
(c) ceil (log2 (N) ), can be written with the ceil math symbol too.
6.
(a) j == i || j == (N-i-1)
(b) j++
(c) printf("\n")
i++
7.
(a) num % 2 == 1 && num > maxodd
(b) maxodd = num
(c) i++
8.
(a) p
(b) rem >= q
(c) quot++
9.
(a) p % num == 0 && q % num == 0
(b) num++
(c) r == 1
10.
(a) two possible answers: num != 0 (or) num > 0
(b) num % 10
(c) num / 10
https://moodle.iitb.ac.in/pluginfile.php/377081/mod_resource/content/1/quiz1-key.txt 1/1
CS 101 Spring 2023
Roll Number
Division / Group
Name
int main(void) {
int n; (c) What does the program
printf("Enter number greater than 3: "); do? Briefly describe what
scanf("%d", &n); the program prints for dif-
printf("%d\n", mystery(n, 2)); ferent values of N.
}
2. Consider the program shown below. State the output of the program for the various values of the
input numbers given below.
void f1(int x) {
x++;
printf("value printed in f1 = %d\n", x); (a) value printed in
} f1 =
void f2(void) {
x++; (b) value printed in
printf("value printed in f2 = %d\n", x); f2 =
}
int main(void) {
(c) value printed in
int y = 5;
main =
x = 10;
f1(y);
f2();
x += 10;
printf("value printed in main = %d\n", x);
}
4. Consider the program shown below. State the output of the program for each pair of input values
given below.
int main(void) {
int n1, n2;
printf("Enter two numbers: ");
scanf("%d %d", &n1, &n2); (a) Input = 5 10
Output =
int i = n1; int sum = 0;
6. The program shown below implements the recursive Euclid’s algorithm to compute the GCD of
two numbers. When this program is given the input of 33 81, it prints out 6 lines of output.
Write down the six lines of output, one line in each sub-part, in the space next to the program.
(c)
int main(void) {
int x, y; (d)
printf("Enter two numbers: ");
scanf("%d %d", &x, &y); (e)
printf("%d\n", gcd(x, y)); (f)
}
7. The program below takes a positive number n as input and prints a triangle containing the star
(“*”) symbol in n rows. The first row has one star symbol in the center, the second row has 3 stars
in the center, and so on. The last row has only stars and no spaces. Sample output for n = 3 and
n = 4 is shown next to the program. Complete the program by filling in the expressions A, B, and
C. Write your answer in the space next to the program. You must not declare any new variables or
add any new statements to the program.
//n = 4
int main(void) { //n = 3 *
int n; * ***
printf("Enter a number: "); *** *****
scanf("%d", &n); ***** *******
int x, y;
for(x = 1; x <= A; x++) { (a) What is A?
for(y = 1; y <= B; y++)
printf(" ");
for(y = 1; y <= C; y++) (b) What is B?
printf("*");
printf("\n");
} (c) What is C?
}
8. Consider the prime factorization of a number, where the number is expressed as a product of
primes. The program shown below prints the number of times a given prime factor appears in
the prime factorization of an input number. For example, 360 = 23 ∗ 32 ∗ 5, so we say that the
prime factor 2 appears 3 times in the prime factorization of 360. If the program below is given 360
and 2 as input, it prints 3 as output. Complete the program below by filling in suitable expressions
for A, B, and C. You must not declare any new variables or add any new statements to the program.
int main(void) {
int main(void) {
srand(time(NULL)); (a) What is A?
int die6, die8;
do {
(b) What is B?
die6 = A;
die8 = B;
printf("%d %d\n", die6, die8);
} while(C); (c) What is C?
printf("Game done\n");
}
10. Shown below is code to compute and print all Fibonacci numbers between 1 and some number
n > 2 using iteration. The program prints n lines, with line i containing the number i and the i-th
Fibonacci number. Complete the program below by filling in suitable code for A, B, and C. You
must not declare any new variables in the program.
int main(void) {
int n;
printf("Enter number greater than 2: ");
scanf("%d", &n);
(a) What is expression A?
printf("1 1\n2 1\n"); //first two lines
1
(a) 1
(b) 0
(c) program prints 1 if N is prime, 0 otherwise
2.
(a) 2 1 3
(b) 7 5 9 21
(c) 8 6 4 9 2 29
(If students missed the print statement of the individual digits but only wrote the print
statement for the sum correctly, 2 out of 3 marks may be given.)
3.
(a) value printed in f1 = 6
(b) value printed in f2 = 11
(c) value printed in main = 21
4.
(a) 16
(b) 24
(c) 19
5.
(a) 10000
(b) 11011
(c) program prints N in binary
6.
(a) 33 81
(b) 81 33
(c) 33 15
(d) 15 3
(e) 3 0
(f) 3
7.
(a) n
(b) n-x
(c) 2*x-1
8.
(a) num
(b) x / f
(c) y+1
9.
(a) 1 + rand() % 6
(b) 2 + rand() % 8
(c) die6 != die8
10.
https://moodle.iitb.ac.in/pluginfile.php/381286/mod_resource/content/1/quiz2-key.txt 1/1
CS 101 Spring 2023
Roll Number
Division / Group
Name
• Please write your answers neatly with pen, in the space provided.
• Each question carries 5 marks. For questions with 5 sub-parts, each sub-part carries 1 mark. For
questions with 3 sub-parts, one of the sub-parts will carry 1 mark and the other two will carry 2
marks each.
• You can use the empty space in the margins and on the last page for rough work.
int i = 0, j = 0, k = 0;
while(i < SIZE && j < SIZE) {
if(a[i] <= b[j])
c[k] = a[i];
else
c[k] = b[j];
while(i<SIZE && a[i] == c[k]) i++;
while(j<SIZE && b[j] == c[k]) j++;
k++;
}
(a) The values in array a are 1, 2, 2, 4, 7, 9, 9, 11, 11, 20 and the values in
array b are 1, 1, 2, 3, 5, 9, 12, 12, 15, 15. What are the values in array c?
(b) The values in array a are 11, 21, 21, 43, 53, 61, 77, 77, 92, 92 and the
values in array b are 10, 18, 25, 35, 56, 93, 120, 120, 150, 150. What
are the values in array c?
(c) What is the program doing? How is the program computing array c from arrays a and b?
2. What is the output of the following program?
int x = 10;
int y[5] = {1, 2, 3, 4, 5};
(*py) += 2;
*px = *px + *py;
printf("star px = %d\n", *px);
int w = *px - 2 * z;
printf("w = %d\n", w);
py += w;
printf("star py = %d\n", *py);
(a) star px =
(b) z =
(c) w =
(d) star py =
(e) Values printed from array y
while(*x != *y) {
*z = *x;
x++;
z++;
}
*z = ’\0’;
}
void mysteryB(const char *x, const char *y, char *z) {
while(*x != *y) {
*z = *x; x++; z++;
}
while(*x == *y) {
x++; y++;
}
while(*x != ’\0’) {
*z = *x; x++; z++;
}
*z = ’\0’;
}
int count = 0;
while(*x == *y) {
count++;
x++; y++;
}
return count;
}
int main(void) {
char *string1 = "hellocs101world";
char *string2 = "101";
char *string3 = "hello";
char stringA[50];
char stringB[50];
1 2 3 4 5
2 1 7 6 5
3 6 4 3 1
4 5 9 1 2
5 2 7 6 3
Suppose the function sortByCol is asked to sort this array by column index 2 (i.e., third col-
umn). The third column has values 3, 7, 4, 9, 7 in the original array. The function must then
shuffle the rows of this 2D array in such a way that the values in the third column are in ascending
order. The sorted array, after being sorted on column index 2 is shown below.
1 2 3 4 5
3 6 4 3 1
2 1 7 6 5
5 2 7 6 3
4 5 9 1 2
The function sortByCol calls the function swap to swap rows of the 2D array during the sorting
process. The function swap takes two rows (1D arrays) of the 2D array as arguments, and swaps
them using the help of a temporary array. Note that we must swap entire rows during the sorting
process, so that the elements of the same row stay together throughout the sorting. Complete code
below for the function sortByCol and the function swap which it calls, by filling in the blanks.
Write your answer in the space provided after the code on the next page.
(b)
(c)
5. Complete the code shown below for the insertion sort algorithm to sort an array x of SIZE integers
in ascending order, where SIZE is suitably defined. Insertion sort works as follows. The algorithm
considers each element of the array one by one, starting from the second element, and places it in
the correct sorted position with respect to all the elements before it. Suppose we are considering
array element at index i. Then we look backwards over all the elements of the array before this
element. Any elements larger than the i-th element are moved forward in the array, and i-th
element is then put into its correct sorted place. Below is the output shown from running the
completed insertion sort program, where we show how an array of SIZE 5 is sorted by going over
elements at index i = 1, 2, 3, 4 and placing each of them in the correct sorted position
with respect to the elements before it.
Unsorted array 77 68 59 93 8
i=1, inserted item 68, array 68 77 59 93 8
i=2, inserted item 59, array 59 68 77 93 8
i=3, inserted item 93, array 59 68 77 93 8
i=4, inserted item 8, array 8 59 68 77 93
Sorted array 8 59 68 77 93
(a)
(b)
(c)
6. Complete the program shown below by filling in the blanks. The program finds the longest subse-
quence of increasing elements in the array. It calculates the length of the longest such subsequence,
as well as its start and end index values. If there are many such longest subsequences of the same
length, the program finds the first of them. For example, consider the array below.
49 87 15 48 30 51 55 4 74 32
if(___blank (a)___) {
end = ___blank (b)___;
len = len + 1;
}
else {
if(len > maxlen) {
maxlen = len;
maxlen_start = start;
maxlen_end = end-1;
}
start = ___blank (c)___;
end = start + 1;
len = 1;
}
}
(a)
(b)
(c)
ROUGH PAGE
5/22/23, 2:10 PM https://moodle.iitb.ac.in/pluginfile.php/383400/mod_resource/content/1/quiz3-key.txt
(a) 1 2 3 4 5 7 9 11 12 15 20
(c) Merge both arrays in sorted order / take a union over sorted array while removing duplicates /
keeping only unique elements
NOTE: both aspects (merging, removing duplicates) are important. No partial marks if only one of
the two is written.
2.
(a) star px = 13
(b) z = 5
(c) w = 3
(d) star py = 4
(e) 3 2 3 4 5
(a) hellocs
(b) hellocsworld
(c) 5
NOTE: x and y can be swapped in the above answers also. We are basically looking for two one-
dimensional arrays named x and y
(c) item
OR
https://moodle.iitb.ac.in/pluginfile.php/383400/mod_resource/content/1/quiz3-key.txt 1/2
5/22/23, 2:10 PM https://moodle.iitb.ac.in/pluginfile.php/383400/mod_resource/content/1/quiz3-key.txt
NOTE: above two expressions are equivalent because len = end-start, and this equivalence can be
applied in all answers
(b) end+1
(c) end
https://moodle.iitb.ac.in/pluginfile.php/383400/mod_resource/content/1/quiz3-key.txt 2/2
CS 101 Spring 2023
Roll Number
Division / Group
Name
• Please write your answers neatly with pen, in the space provided.
• Each question carries 3 marks. All questions have 3 sub-parts of 1 mark each.
• You can use the last page or empty space in the margins for rough work.
struct node {
int data;
struct node *prev;
struct node *next;
};
Note that the pointer to the previous node in the first node, and the pointer to the next node in the
last node of the linked list are NULL. Suppose startPtr is a pointer to the first node of the
list, and newPtr is a pointer to a newly allocated linked list node that we wish to insert into the
list. Fill in the blanks in the code snippets shown below in order to update the various “links” and
insert the new node in the specified position.
(a) Insert the new node at the start of the list, before the current first node, the pointer to which
is stored in startPtr.
newPtr->prev = NULL;
startPtr->prev = ________________________;
newPtr->next = ________________________;
startPtr = newPtr;
(b) Insert the new node before a node whose pointer is stored in currPtr. You may assume
that the node pointed to by currPtr is not the first node in the linked list.
currPtr->prev->next = newPtr;
newPtr->prev = _____________________;
_______________ = newPtr;
newPtr->next = currPtr;
(c) Insert the new node after a node whose pointer is stored in currPtr. You may assume that
the node pointed to by currPtr is not the last node in the linked list.
__________________ = newPtr;
newPtr->next = _______________________;
currPtr->next = newPtr;
newPtr->prev = currPtr;
int main(void) {
int x = 20, y = 0;
for(;;) {
x += 2;
if(x < 25) continue;
y++;
if(x > 30) break; (a) y =
}
printf("y = %d\n", y);
int z = 0; (b) z =
while(x) {
x /= 2;
z++; (c) x =
}
printf("z = %d\n", z);
do {
z = x++;
} while(x < y - z);
printf("x = %d\n", x);
}
4. What is output of the program shown below?
#define SIZE 5
int mysteryA(int *x, int *y) {
return *x + *y;
}
int mysteryB(int *x, int *y) {
if(x != y) {
int z = *(x+1) - *x;
int k = mysteryB(x+1, y);
return (z > k ? z : k); (a) mysteryA=
}
else return 0;
} (b) mysteryB=
int mysteryC(int *x, int n) {
int k = 0;
while(n) {
(c) mysteryC=
k += *(x++); n--;
}
return k;
}
int main(void) {
int x[SIZE] = {11, 24, 36, 42, 50};
printf("mysteryA=%d\n", mysteryA(x, x+4));
printf("mysteryB=%d\n", mysteryB(x, x+4));
printf("mysteryC=%d\n", mysteryC(x, SIZE));
}
5. For each of the code snippets shown below, indicate if the code will throw an error, or if it is
correct, what its output will be. Write your answer next to the code.
6. Consider the following function that uses the bubble sort algorithm to sort an array of strings in
alphabetical order. The function takes a 2-D array of characters as an argument, where the array
contains SIZE number of strings, each containing up to LEN characters (including the terminating
null character). Complete the code shown below to correctly perform the sorting. You must use
the following functions from the string library in your solution. The function strcmp(const
char *s1, const char *s2 returns value of 0, greater than 0, or less than 0 based on
whether s1 is equal to, greater than, or less than s2. The function strcpy(char *s1, const
char *s2 copies s2 into s1.
#define SIZE 10
#define LEN 20 (a) What is A, the argument to
void bubblesort( A ) { bubble sort?
char tmp[LEN];
for(int i=0; i<SIZE; i++){
for(int j=0; j <SIZE-1; j++){
if( B ){ (b) What is B, the condi-
C1; C2; C3; tion that is checked before
} swapping?
}
}
} (c) What are statements C1,
int main(void) { C2, C3 to perform the
char x[SIZE][LEN] = ... //initialization swap operation?
bubblesort(x);
for(int i = 0; i < SIZE; i++)
printf("%s\n", x[i]);
}
7. What is the output of the following program?
int x[3];
void f1(void) {
static int x[3] = {4,5,6};
for(int i=0; i<3; i++) ++x[i];
printf("f1 %d\n", x[0]+x[1]+x[2]);
}
(a) f1
void f2(int y[]) {
for(int i=0; i<3; i++) ++y[i];
printf("f2 %d\n", x[0]+x[1]+x[2]);
} (b) f2
void f3(void) {
for(int i=0; i<3; i++) x[i] += x[0];
} (c) main
int main(void) {
x[0] = 1; x[1] = 2; x[2] = 3;
f1();
f2(x);
f3();
printf("main %d\n", x[0]+x[1]+x[2]);
}
8. Consider the code snippet shown below. Assume NUMBER is defined to be 100.
int array[NUMBER];
for(int i=0; i<NUMBER; i++) (a) What is the value of array[67]?
array[i] = i;
for(int i=2; i<NUMBER; i++) {
if(array[i] != 0) { (b) What is the value of array[76]?
for(int j=2; j<NUMBER; j++) {
if(array[i]*j >= NUMBER)
break; (c) What is the program calculating? How
array[array[i]*j] = 0; would you describe the values in the array
} after the code completes?
}
}
9. Complete the program shown below, which implements a recursive version of the binary search
algorithm. The arguments to the recursive function bs are the key, and pointers to the start and
end array elements over which define the sub-array over which the search is being performed. The
function returns a pointer to the search key array element if the key is found, and NULL otherwise.
You cannot add any new variables or statements to the code, but must only fill in A, B, C with
suitable values.
#define SIZE 10
int *bs(int k, int *low, int *high)
{
if(low <= high) {
int *middle = low+(high-low)/2;
if (A)
return middle;
else if (B) (a) What is A?
return bs(k,low,middle-1);
else
return C; (b) What is B?
}
else return NULL;
}
(c) What is C?
int main(void)
{
int x[SIZE]; int key;
//array and key suitably initialized
int *result = bs(key, &x[0], &x[SIZE - 1]);
if (result != NULL)
printf("\nfound key at %p\n", result);
else
printf("\n%d not found\n", key);
}
10. Consider the program shown below that implements a linked list in C++. New nodes are always
added at the start of the list. The linked list class also has a function to reverse the list, which has
some lines of code missing. Fill in the missing code so that the program works correctly. The
example print statements in the main function will help you understand what the reverse function
must accomplish. Hint: the function flips the direction of all the “links” in the list, and moves
the start pointer to the last node. Note that you must only complete the missing pieces of code
indicated as A, B, C in the code of the function reverse, and you must not declare any new
variables or add any new statements to the program.
#include <iostream>
using namespace::std;
void LinkedList::reverse(void) {
class Node { if(start == NULL) return;
public: Node *prev = NULL;
int data; Node *curr = start;
Node *next;
}; while(curr != NULL) {
Node *tmp = curr->next;
class LinkedList { curr->next = prev;
private: prev = A;
Node *start; curr = B;
public: }
void init(void) { start = C;
start = NULL; }
}
void print(void) { int main(void) {
Node *curr = start; LinkedList mylist;
while(curr != NULL) { mylist.init();
cout << curr->data << "-->"; mylist.add(1);
curr = curr->next; mylist.add(2);
} mylist.add(3);
cout << "NULL" << endl; mylist.print();
} //prints 3-->2-->1-->NULL
void add(int value) {
Node *node = new Node(); mylist.reverse();
node->data = value; mylist.print();
node->next = start; //prints 1-->2-->3-->NULL
start = node; }
}
void reverse(void);
};
(a) What is A?
(b) What is B?
(c) What is C?
ROUGH WORK
6/17/23, 4:17 PM moodle.iitb.ac.in/pluginfile.php/384572/mod_resource/content/1/quiz4-key.txt
1.
(a) StringA = hellocs101class
(b) StringB = cs101world
(c) StringC = (null) OR \0 any other output indicating a null /invalid value is printed. The
behavior of printf when printing a null value is undefined.
2.
(a)
startPtr->prev = ____newPtr___;
newPtr->next = ___startPtr___;
(b)
newPtr->prev = ___currPtr->prev___;
___currPtr->prev___ = newPtr;
(c)
___currPtr->next->prev__ = newPtr;
newPtr->next = ___currPtr->next___;
3.
(a) y = 4
(b) z = 6
(c) x = 3
4.
(a) mysteryA=61
(b) mysteryB=13
(c) mysteryC=163
5.
(a) Error
(b) Prints 5
(c) Prints 5
6.
(a) char x[][LEN] or char x[SIZE][LEN]
(b) strcmp(x[j], x[j+1])>0 (can be >= also)
(c) (swap x[j] and x[j+1] using tmp)
strcpy(tmp, x[j]);
strcpy(x[j], x[j+1]);
strcpy(x[j+1], tmp);
NOTE: name of the array argument can be anything above, not just x
7.
(a) f1 18
(b) f2 9
(c) main 19
8.
(a) 67
(b) 0
(c) array[i] = i if i is prime, 0 otherwise. (The code is basically implementing Sieve of
Eratosthenes.)
9.
(a) k == *middle
(b) k < *middle
(c) bs(k,middle+1,high)
10.
(a) curr
(b) tmp
(c) prev
https://moodle.iitb.ac.in/pluginfile.php/384572/mod_resource/content/1/quiz4-key.txt 1/1