0% found this document useful (0 votes)
94 views21 pages

PDS Long Test (Mid-Sem) 1

Uploaded by

Ak
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)
94 views21 pages

PDS Long Test (Mid-Sem) 1

Uploaded by

Ak
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/ 21

3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Home / My courses / Programming and Data Structures Theory(CS10003) Section 11-17 & Backlog- Autumn 2021 (First Year)

/ Long Test 1 on February 8, 2022 / Long Test 1 on February 8, 2022

Started on Tuesday, 8 February 2022, 9:00 AM


State Finished
Completed on Tuesday, 8 February 2022, 10:00 AM
Time taken 1 hour
Grade 12.00 out of 36.00 (33%)

Question 1
Correct

Mark 1.50 out of 1.50

Fill up the blank in the following code, so that it displays 12111212 in the output.

#include <stdio.h>

int main() {

int i, j, k;

for(i=1; i<5; i++) {

for (j=1; j<5; j++) {

for (k=1; k<5; k++) {

printf("%d", k);

if(k==2) break;

printf("%d", j);

if (j==1) break;

printf("%d", i);

if( i==2  ) break;

return 0;

The correct answer is: i==2

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 1/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 2
Incorrect

Mark 0.00 out of 1.50

Complete the blank statement in the following program, so that it produces output as follows,

ABCDE

BCDEF

CDEFG

DEFGH

EFGHI

You should write a single expression in the blank space.

#include <stdio.h>

int main() {

char box[5][5];

int i, j;

for(i='A'; i<'A'+5; i++) {

for(j=i; j<i+5; j++) {

box[i-'A'][j-i] = (char)((int)'A'  ;

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

for(j=0; j<5; j++){

printf("%c ",box[i][j]);

printf("\n");

return 0;

The correct answer is: box[i-'A'][j-i] = j

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 2/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 3
Correct

Mark 1.50 out of 1.50

Fill in the blank to complete the code to compute the area of a rectangle. The code should output: Area of Rectangle: 200.

#include <stdio.h>

struct Rectangle {

int length;

int breadth;

};

int main() {

int area;

struct Rectangle r = { 10, 5 };

r.length = 20;

r.breadth = 10;

area = area = r.length * r.breadth  ;

printf ("Area of Rectangle: %d", area);

return 0;

The correct answer is: r.length * r.breadth

Comment:

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 3/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 4
Incorrect

Mark 0.00 out of 1.50

Consider the following function sumMinorDiagElements which takes a 4X4 matrix as an argument and computes the sum of the elements in its minor diagonal
(“minor diagonal” of a square matrix A[4][4] is the set of elements lying on the diagonal line connecting the elements A[0][3] and A[3][0]).

In the main(), array A is initialized as shown and printf() is expected to produce 34 as the display output. However, one statement of the function is incomplete.
Complete the missing part of the statement.

#include <stdio.h>

int sumMinorDiagElements(int mat[][4]) {

int i, j, sum = 0;

for ( i = 0; i < 4; i++ ) {

for ( j = 0; j < 4; j++ ) {

if ( i+j==3  )

sum += mat[i][j];

return sum;

int main() {

int A[][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

printf("%d\n",sumMinorDiagElements(A));

return 0;

The correct answer is: (i + j) == 3

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 4/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 5
Correct

Mark 1.50 out of 1.50

Complete the following program by filling up the blank, so that the program produces the following output when executed: 10 11

#include<stdio.h>

int fun2(int a[1]) {

return a[0]++  ;

int fun1(int a[1]) {

return fun2(a);

int main() {

int a[1] = {10};

int out1, out2;

out1 = fun1(a);

out2 = fun2(a);

printf("%d %d", out1, out2);

return 0;

The correct answer is: a[0]++

Question 6
Incorrect

Mark 0.00 out of 1.50

Fill up the blank in the following program, so that it prints CS10002 in its output.

#include <stdio.h>

int main() {

char s[] = "Hello CS10002";

char d[14];

int i=0, j=0;

while( i<5  );

while(s[i]!='\0') {

d[j++] = s[i++];

d[j] = '\0';

printf("%s", d);

return 0;

The correct answer is: s[i++]!=' '

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 5/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 7
Incorrect

Mark 0.00 out of 1.50

Complete the following program by filling up the blank, so that it prints the following in the output.

A5 B4 C3 D2 E1

You can write a single expression in the blank space.

#include <stdio.h>

int main() {

char a[5][3];

char b[5] = {'A', 'B', 'C', 'D', 'E'};

int c[5] = {5, 4, 3, 2, 1};

int i;

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

a[i][0] = b[i];

a[i][1] = '0' + c[i];

a[i][2] = ' '  ;

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

printf("%s ",a[i]);

return 0;

The correct answer is: a[i][2] = '\0'

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 6/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 8
Incorrect

Mark 0.00 out of 1.50

The following C code removes all occurrences of the character ‘a’ from an input string.

For example: if the input is “Kharagpur”, the output will be “Khrgpur”


#include<stdio.h>

#include<string.h>

int main(){

char inpstr[50], outstr[50];

int i = 0, j = 0, len;

printf("\n\nEnter the string: ");

scanf("%s", inpstr);

while(inpstr[i] != '\0') {

if(inpstr[i] != 'a') printf("%s", inpstr[i])  ;

i++;

outstr[j] = '\0';

printf("\nOutput string after removing all 'a's: %s", outstr);

return 0;

The correct answer is: outstr[j++] = inpstr[i]

Question 9
Incorrect

Mark 0.00 out of 1.50

Let us consider a quarternary number system in which the base or radix is 4 and the symbols are 0,1,2,3.

An unsigned number in this quarternary number system is represented as: 1203.1203

What is its value in the decimal number system? 99.99 

The correct answer is: 99.38671875

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 7/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 10
Correct

Mark 1.50 out of 1.50

Complete the following program, so that it prints the following pattern in the output.

**+

**+*++

**+*++*+++

Fill the blank in the following program.

#include <stdio.h>

int main() {

int i, j, k;

for(i=1; i<5; i++){

for(j=0; j<i; j++) {

printf("*");

for( k=0;k<j;k++  ) {

printf("+");

printf("\n");

return 0;

The correct answer is: k=0; k<j; k++

Comment:

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 8/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 11
Incorrect

Mark 0.00 out of 1.50

Complete the following program by filling up the blank, so that the program produces output 720. Note that 720 = 1 x 2 x 3 x 4 x 5 x 6.

#include <stdio.h>

int fun(int j) {

static int i = 1;

if(j < 2) {

return ++i;

else return (j+1)*fun(j-1)  ;

int main() {

printf("%d", fun(5));

return 0;

The correct answer is: (++i)*fun(--j)

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 9/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 12
Incorrect

Mark 0.00 out of 1.50

Consider the following program that reads from the user two character strings str1, str2 each of size less than 20, and prints 1 if they are equal (case-wise),
otherwise it prints 0. However, a part of one statement is missing. Complete the missing part of the statement so that the program behaves as required.

#include <stdio.h>

int main() {

int i = 0, flag = 0;

char str1[21],str2[21];

scanf("%s%s", str1,str2);

while ( i<20  ) {

// Go on until the end of either of the strings

if (str1[i] != str2[i]) {

printf("%d\n",0);

return 0;

i++;

if (str1[i]=='\0' && str2[i]=='\0')

printf("%d\n",1);

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

return 0;

The correct answer is: str1[i] != '\0' && str2[i]!='\0'

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 10/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 13
Correct

Mark 1.50 out of 1.50

Complete the following program by filling up the blank so that it prints the following in the output: 18 29 22 21 34

#include <stdio.h>

void fun(int[], int[], int);

int main() {

int x[5] = {34, 21, 22, 29, 18}, y[5], i;

fun( y,x,5  );

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

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

printf("\n");

return 0;

void fun(int a[], int b[], int n) {

int i;

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

a[i]=b[4-i];

The correct answer is: y, x, 5

Comment:

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 11/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 14
Correct

Mark 1.50 out of 1.50

Consider the following C program.

#include <stdio.h>

void print_pattern(int i, int j) {

int k;

if(i == 0) {

return;

if(i%2 != 0) {

for(k=0; k<j; k++) {

printf("+");

printf("\n");

print_pattern(i-1, j);

printf("*");

int main() {

print_pattern( 5,5  );

return 0;

Complete the program by filling up the blank, so that it prints the following pattern in the output.

+++++

+++++

+++++

*****

The correct answer is: 5,5

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 12/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 15
Correct

Mark 1.50 out of 1.50

Consider the following program to print the factorial of a given integer read from the keyboard. Complete the code by filling up the blank, so that the factorial of
the number read as input is produced at the output. You are not allowed to use the comma operator.

#include <stdio.h>

int main() {

int n, i, fact = 1;

scanf("%d", &n);

for(i = n; i > 1; fact *= i--  );

printf("%d", fact);

The correct answer is: i--

Question 16
Incorrect

Mark 0.00 out of 1.50

Let us assume a computer system that uses 2’s complement representation of integers. Addition and subtraction are done using 2’s complement.

Consider that we are trying to do the following addition of two 32 bit numbers (with the MSB denoting the sign bit):

01111101001100110111110100111011

01111111010011110011110101000111

It is observed that the Carry Out from the MSB is 0.

What is the Carry In to the MSB? 0 

The correct answer is: 1

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 13/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 17
Correct

Mark 1.50 out of 1.50

A number is said to be perfect if it is the sum of all its possible factors (except itself!). For example, 6 has factors 1,2,3 and 1+2+3=6 and hence it is perfect.
Also 28=1+2+4+7+14 is perfect.

Fill up the missing part of the following program, so that it will display 1 or 0 depending on whether an input number is perfect or not.

#include <stdio.h>

int main() {

int i, n, sum = 0;

scanf("%d",&n);

for(i=1; i<n; i++) {

if ( n%i==0  ) sum += i;

printf("%d",(sum == n));

return 0;

The correct answer is: n % i == 0

Comment:

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 14/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 18
Incorrect

Mark 0.00 out of 1.50

Consider the following C program that reads from the keyboard an input array a[] of positive integer numbers less than 100. It calls a function func() to sort the
elements of array a[] and populate another array b[] with the sorted elements. Note that in the procedure, it does not change the elements of array a[]. Complete
the program so that it produces the expected output with the sorted integers in array b[].

#include <stdio.h>

void func(int a[], int b[], int n) {

int i, j, temp, last = -1;

for(i=0; i<n; i++) {

temp = 9999;

for(j=0; j<n; j++) {

if( a[j]!=0  ) {

temp = a[j];

b[i] = temp;

last = temp;

int main() {

int a[5] = {10, 8, 23, 12, 7};

int b[5] = {-1}, i;

func(a, b, 5);

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

printf("%d ", b[i]);

printf("\n");

return 0;

The correct answer is: a[j] < temp && a[j] > last

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 15/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 19
Incorrect

Mark 0.00 out of 1.50

In the following program, an array A is initialized in the main() to contain the values 1, 2, 3, 4, 5, 6, 7. Then the function call reverse(A,0,6) is made, and finally
when the contents of the array A is displayed, the reversed array 7, 6, 5, 4, 3, 2, 1 is expected to get displayed.

However, a part of one statement is missing. Complete the missing part of the statement, so that the program behaves as required.

#include <stdio.h>

void reverse(int array[], int leftIndex, int rightIndex) {

int temp;

if(leftIndex < rightIndex) {

temp = array[leftIndex];

array[leftIndex] = array[rightIndex];

array[rightIndex] = temp;

reverse(array, rightIndex,temp)  );

int main() {

int i, A[]={1,2,3,4,5,6,7};

reverse(A,0,6);

for(i=0;i<7;i++)

printf("%d",A[i]);

printf("\n");

return 0;

The correct answer is: leftIndex+1, rightIndex-1

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 16/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 20
Incorrect

Mark 0.00 out of 1.50

Consider the following C code.

#include<stdio.h>

#define ROWCOL 2

void add_columns (int matrix[100][100],int  {

int i, j;

for (i=0;i<ROWCOL;i++) {

int sum = 0;

for (j=0;j<ROWCOL;j++) sum=sum+x[i][j];

z[i]=sum;

return;

int main() {

int arr_a[ROWCOL][ROWCOL], arr_b[ROWCOL];

int i,j;

for (i=0;i<ROWCOL;i++)

for (j=0;j<ROWCOL;j++) scanf("%d",&arr_a[i][j]);

add_columns(arr_a, arr_b);

for (i=0;i<ROWCOL;i++) printf ("%d ",arr_b[i]);

return 0;

The program reads the elements of a 2D array. It prints the sum of the column elements for each row. For example, if the first row contains the values 10, 20
and the second row contains the values 30, 40, the output will be 30 70.

Fill the blank space so that the program prints the values correctly.

The correct answer is: (int x[][2], int z[])

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 17/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 21
Not answered

Marked out of 1.50

Consider the following C code.

#include<stdio.h>

void func_1();

int a;

int main() {

a=10;

func_1();

printf ("a=%d",a);

return 0;

void func_1() {

 ;

a=20;

return;

Fill up the blank in the code so that the program prints: a=10

The correct answer is: int a

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 18/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 22
Not answered

Marked out of 1.50

In the following program, the function recur()is invoked in the main() with arguments 1,2,3,4,5,6 and 7, and the returned values are expected to be displayed
as 10,10,20,30,50,80 and 130, respectively.

#include <stdio.h>

int recur(int data) {

int k;

k=(data>2)?  :10;

return k;

int main() {

int i;

for(i=1;i<8;i++)

printf("%d\n", recur(i));

return 0;

However, a part of one statement is missing. Write the missing part of the statement.

The correct answer is: (recur(data-1)+recur(data-2))

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 19/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 23
Not answered

Marked out of 1.50

Consider the following program that is intended to display a pyramid of digits (as shown below) for a given number (n<10) input by the user. For example, for
n=5, a pyramid of 5 lines should be displayed as follows:

232

34543

4567654

567898765

However, a part of one of the statement is missing. Complete the missing part so that the program behaves as required.

int main() {

int i,j,n;

scanf("%d",&n);

if (n<1) return 0;

for (i=1; i<=n; i++) {

for (j=i; j<n; j++) printf(" "); /* get the start point to print */

for (j=0;j<i;j++) printf(" %3d",i+j); /* print in increasing order */

for (  ) printf(" %3d",i+j); /*print in decreasing order */

printf("\n");

return 0;

The correct answer is: j=j-2;j>=0;j--

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 20/21
3/14/22, 7:08 PM Long Test 1 on February 8, 2022: Attempt review

Question 24
Not answered

Marked out of 1.50

Consider the following program which prints the addition of the individual column of a two dimensional array. Fill in the blank with
appropriate C statement so that the output of the program is 4 8 14

#include<stdio.h>

int main() {

int matrix[3][3]= {

{1,2,3},

{1,3,5},

{2,3,6}

};

int i,j,sum;

int rows=3,cols=3;

for (i=0;i<rows;i++){

sum=0;

for(j=0;j<cols;j++){

sum = sum +  ;

printf("%d ",sum);

return 0;

The correct answer is: matrix[j][i]

◄ Class Test 1 on Jan 11, 2022 (Tuesday)

Jump to...

kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 21/21

You might also like