0% found this document useful (0 votes)
22 views33 pages

Written Quizzes Solutions Spring

Cs101

Uploaded by

gamerdeath923
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)
22 views33 pages

Written Quizzes Solutions Spring

Cs101

Uploaded by

gamerdeath923
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/ 33

CS 101 Spring 2023

Quiz 1 (March 25th, 2023)


10 questions, 30 marks, 15% weightage

Roll Number
Division / Group
Name

Question no. Marks obtained Remarks or cribs


1
2
3
4
5
6
7
8
9
10
TOTAL

Please read the following instructions carefully before you start.


• You must write your answers in this question booklet itself and turn it in.

• 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=

int f = d * i / 2; (c) f1= f2=


float f1 = (float) f;
float f2 = (float) d * i / 2;

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

int n1 = 5; int n2 = 10;


int count = 0;

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

while (number != -1) { (b) Bug:


total = total + number;
printf("Enter number, -1 to end: ");
counter++;
scanf("%d", &number);
(c) Bug:
}

average = ( float ) total / counter;


printf("Average is %.2f\n", average);
5. Consider the following program.

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.

int i = 0; int maxodd = 0;


int num;

while( i < 10) {


printf("Enter positive number: "); (a) What is A?
scanf("%d", &num);

if( A ) { //fill A (b) What is B?


B; //fill B
}
C; //fill C
(c) What is C?
}

if(maxodd == 0) printf("none exists\n");


else printf("%d\n", maxodd);

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

int quot = 0; int rem = A; (b) What is B?

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;

while(num <= end) { (b) What is B?


if( A ) //fill A
r = num;
B; //fill B (c) What is C?
}

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;

while ( A ) { //fill A (b) What is B?


digit = B; //fill B
num = C; //fill C
if(digit == 7) count++; (c) What is C?
}
printf("%d\n", count);
5/22/23, 2:06 PM https://moodle.iitb.ac.in/pluginfile.php/377081/mod_resource/content/1/quiz1-key.txt

10 questions, each has 3 sub-parts.

Each sub-part carries 1 mark, and is graded independently.

No partial (0.5) marks if the answer for a sub-part is partly correct.

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

4. Any 3 from the following:


(a) total not initialized
(b) counter not initialized
(c) scanf syntax is wrong, & missing before number
(d) no check for division by zero when no numbers are entered

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

Quiz 2 (April 21st, 2023)


10 questions, 30 marks, 15% weightage

Roll Number
Division / Group
Name

Q. No. Marks Graded By Verified By Student Remarks / Cribs


1
2
3
4
5
6
7
8
9
10
TOTAL

Please read the following instructions carefully before you start.


• You must write your answers in this question booklet itself and turn it in.
• Please write your answers neatly with pen, in the space provided.
• Each question carries 3 marks. Questions have 3 sub-parts of 1 mark each, or 6 sub-parts of 0.5
marks each.
• You can use the empty space in the margins and at the bottom of the page for rough work.
• No clarifications will be provided on any question.
1. Consider the program shown below. State the output of the program for the various values of the
input numbers given below. Also describe what the program is computing.

int mystery(int n, int i) { (a) Input = 11


if(i == n-1) Output =
return 1;
else if(n % i == 0)
return 0;
(b) Input = 49
else return mystery(n, i+1);
Output =
}

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.

int mystery(int num) {


if(num < 10) {
printf("%d ", num);
return num;
} (a) Input = 21
else { Output =
int x = num % 10;
int y = mystery(num/10);
printf("%d ", x); (b) Input = 759
return x + y; Output =
}
}
(c) Input = 86492
int main(void) { Output =
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("%d\n", mystery(num));
}
3. What is output of the program shown below? Complete the print statements shown next to the
program.

int x; //global variable

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;

while(i <= n2) { (b) Input = 2 22


i++; Output =
if(i % 10 == 0)
break;
if(i % 2 == 0) (c) Input = 18 51
continue; Output =
sum += i;
}
printf("%d\n", sum);
}
5. Consider the program shown below. State the output of the program for each input given below.
Also describe what the program is printing.

void mystery_print(int num) {


int x = 1; int y = 1;
while(x*2 <= num) {
x *= 2; y++;
}
(a) Input = 16
for(int i = 0; i < y; i++) {
Output =
if(num >= x) {
printf("1"); num -= x;
}
else printf("0"); (b) Input = 27
x /= 2; Output =
}
printf("\n");
} (c) Given a positive integer number N as input,
what does the program print?
int main (void) {
int num;
printf("Enter a number: ");
scanf("%d", &num);
mystery_print(num);
}

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.

int gcd(int x, int y) { Input = 33 81


printf("%d %d\n", x, y); Write down the 6 lines of output
if(y == 0) in the order it is printed.
return x;
else (a)
return gcd(y, x % y);
} (b)

(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 num; int f; (a) What is A?


printf("Enter number and prime factor: ");
scanf("%d %d", &num, &f);
(b) What is B?
int x = A; int y = 0;
while(x % f == 0) {
x = B;
y = C; (c) What is C?
}
printf("%d\n", y);
}
9. Consider the following die-rolling program. The program simulates two dice, one with 6 faces
(which rolls a number between 1 and 6), and one with 8 faces (which rolls a number between 2
and 9). The program continues to roll the dice (and print the result) until both dice roll the same
number. The random number generator is seeded by the system time at the start of the program.
Complete the program by filling in suitable code for expressions A, B, and C shown below. You
must not declare any new variables or add any new statements to the program.

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

unsigned long long int prev_sum = 1;


(b) What is statement B?
unsigned long long int prev_prev_sum = 1;

for(int i = 3; i<= n; i++) {


unsigned long long int fib = A; (c) What is statement C?
printf("%d %llu\n", i, fib);
B;
C;
}
}
5/22/23, 2:08 PM https://moodle.iitb.ac.in/pluginfile.php/381286/mod_resource/content/1/quiz2-key.txt

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.

(a) prev_sum + prev_prev_sum


(b) prev_prev_sum = prev_sum
(c) prev_sum = fib

https://moodle.iitb.ac.in/pluginfile.php/381286/mod_resource/content/1/quiz2-key.txt 1/1
CS 101 Spring 2023

Quiz 3 (May 20, 2023)


6 questions, 30 marks, 15% weightage

Roll Number
Division / Group
Name

Q. No. Marks Graded By Verified By Student Remarks / Cribs


1
2
3
4
5
6
TOTAL

Please read the following instructions carefully before you start.


• You must write your answers in this question booklet itself and turn it in.

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

• No clarifications will be provided on any question.


1. Consider a part of a program shown below. Before the code shown below is executed, the program
declares three arrays as follows: int a[SIZE]; int b[SIZE]; int c[2*SIZE]; where
SIZE is defined to be 10. Arrays a and b are initialized with some values as described later,
whereas all elements of array c are initialized to 0. After initializing the arrays, the code shown
below is executed to compute the values of array c. Note that all values of array c may not be
populated by the code below, and the last few values may be left at the initial value of 0. For
different values of arrays a and b shown below, calculate what array c will contain at the end of
the code execution. You can list only the non-zero elements of c that are set by the code below.
Also, describe the general idea of what the code is doing.

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++;
}

while(i < SIZE) {


c[k] = a[i];
while(i<SIZE && a[i] == c[k]) i++;
k++;
}

while(j < SIZE) {


c[k] = b[j];
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};

int *px = &x;


int *py = y;

(*py) += 2;
*px = *px + *py;
printf("star px = %d\n", *px);

int z = *(py + 4);


printf("z = %d\n", z);

int w = *px - 2 * z;
printf("w = %d\n", w);

py += w;
printf("star py = %d\n", *py);

for(int i=0; i<5; i++)


printf("%d ", y[i]);
printf("\n");

Write the output by completing the statements shown below.

(a) star px =
(b) z =
(c) w =
(d) star py =
(e) Values printed from array y

3. What is the output of the following program?

void mysteryA(const char *x, const char *y, char *z) {

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 mysteryC(const char *x, const char *y) {

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

mysteryA(string1, string2, stringA);


printf("%s\n", stringA);

mysteryB(string1, string2, stringB);


printf("%s\n", stringB);

int partc = mysteryC(string1, string3);


printf("%d\n", partc);
}
(a) What is printed for stringA ?

(b) What is printed for stringB ?

(c) What is printed for variable partc?


4. Complete the program given below by filling in the blanks shown in the code. The program has
two functions. The function sortByCol takes two arguments: a two-dimensional array x and a
column index col. The array x has SIZE rows and SIZE columns, where SIZE is defined suitably
in the program. This function sorts the rows of this 2D array in ascending order using bubble sort,
where the ordering is based on the values in the specified column index col.
To give an example, suppose this function is given the following array as input (SIZE = 5):

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.

void swap(___blank (a)___) {


int temp[SIZE];
for(int i=0; i<SIZE; i++) {
temp[i] = x[i];
x[i] = y[i];
y[i] = temp[i];
}
}
void sortByCol (int x[][SIZE], int col) {
for(int pass=1; pass<SIZE; pass++) {
for(int row=0; row<SIZE-1; row++) {
if(___blank (b)___)
swap(___blank(c)___);
}
}
}
(a)

(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

Complete the following code by filling in the blanks suitably.

for(int i=1; i<SIZE; i++) {

int item = x[i];


int j = i;

while(j > 0 && ___blank(a)___) {


___blank (b)___;
j--;
}
x[j] = ___blank (c)___;
}

(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

The subsequences 49 87 and 15 48 and 30 51 55 and 4 74 are examples of sub-sequences


where the array elements are in increasing order. That is, each element of the subsequence is
greater than the previous element. Of these, 30 51 55 is the longest subsequence, of length 3,
starting at index 4 and ending at index 6. Complete the program below by filling the blanks, such
that the program correctly calculates the maximum length of the longest increasing subsequence
maxlen, along with its start and end index values maxlen start and maxlen end, by the
end of the program. Assume that an array x of SIZE integers is suitably initialized before this
code runs. You cannot declare any new variables or add any extra statements.

int x[SIZE]; //assume array is initialized suitably

int start = 0; int end = 1;


int len = 1; int maxlen = -1;
int maxlen_start = -1; int maxlen_end = -1;

while(start < SIZE && end < SIZE) {

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

1. (2 marks, 2 marks, 1 mark)

(a) 1 2 3 4 5 7 9 11 12 15 20

(b) 10 11 18 21 25 35 43 53 56 61 77 92 93 120 150

(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

3. (2 marks, 2 marks, 1 mark)

(a) hellocs

(b) hellocsworld

(c) 5

4. (1 mark, 2 marks, 2 marks)

(a) int x[], int y[]


OR
int x[5], int y[5]
OR
int x[SIZE], y[SIZE]
OR
int *x, int *y

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

(b) x[row][col] > x[row+1][col]

NOTE: it can be >= also

(c) x[row], x[row+1]


OR
x[row+1], x[row]

5. (2 marks, 2 marks, 1 mark)

(a) x[j-1] > item

NOTE: can be >= also

(b) x[j] = x[j-1]

(c) item

6. (2 marks, 2 marks, 1 mark)

(a) x[end] > x[end-1]

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

x[start+len] > x[start + len -1]

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

Quiz 4 (June 14th, 2023)


10 questions, 30 marks, 15% weightage

Roll Number
Division / Group
Name

Q. No. Marks Graded By Verified By Student Remarks / Cribs


1
2
3
4
5
6
7
8
9
10
TOTAL

Please read the following instructions carefully before you start.


• You must write your answers in this question booklet itself and turn it in.

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

• No clarifications will be provided on any question.


1. What is the output of the program shown below? Recall that the string library function strlen
returns the length of the string.

void mystery1(char *x, char *y, char *z) {


unsigned int lenx = strlen(x);
unsigned int leny = strlen(y);
for(unsigned int i=0; i< lenx-leny; i++) {
*z = *x; x++; z++;
}
for(unsigned int i=0; i<leny; i++) {
*z = *y; y++; z++;
}
*z = ’\0’;
}
char *mystery2(char *x, char *y) {
(a) StringA =
char *z = x;
while(*z != ’\0’ && *z != *y) z++;
bool b = true; int i = 0;
for( ; z[i] != ’\0’ && y[i] != ’\0’; i++) (b) StringB =
if(z[i] != y[i]) b = false;
if(b) return z;
else return ’\0’; (c) StringC =
}
int main(void) {
char *string1 = "hellocs101world";
char *string2 = "class";
char *string3 = "cs101";
char stringA[50];
mystery1(string1, string2, stringA);
printf("StringA = %s\n", stringA);
char *stringB = mystery2(string1, string3);
printf("StringB = %s\n", stringB);
char *stringC = mystery2(string1, string2);
printf("StringC = %s\n", stringC);
}
2. In a doubly linked list, nodes maintain pointers to the nodes before and after, as shown below.

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;

3. What is the output of the following program?

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.

(a) int i = 10;


const int *iPtr = &i;
*iPtr = 5;
printf("%d\n", *iPtr);
(b) int i = 10;
const int *iPtr = &i;
i = 5;
printf("%d\n", *iPtr);
(c) int i = 10;
int * const iPtr = &i;
*iPtr = 5;
printf("%d\n", *iPtr);

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

You might also like