PDS Long Test (Mid-Sem) 1
PDS Long Test (Mid-Sem) 1
Home / My courses / Programming and Data Structures Theory(CS10003) Section 11-17 & Backlog- Autumn 2021 (First Year)
Question 1
Correct
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;
printf("%d", k);
if(k==2) break;
printf("%d", j);
if (j==1) break;
printf("%d", i);
return 0;
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
Complete the blank statement in the following program, so that it produces output as follows,
ABCDE
BCDEF
CDEFG
DEFGH
EFGHI
#include <stdio.h>
int main() {
char box[5][5];
int i, j;
box[i-'A'][j-i] = (char)((int)'A' ;
printf("%c ",box[i][j]);
printf("\n");
return 0;
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
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;
r.length = 20;
r.breadth = 10;
return 0;
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
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 i, j, sum = 0;
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;
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
Complete the following program by filling up the blank, so that the program produces the following output when executed: 10 11
#include<stdio.h>
return a[0]++ ;
return fun2(a);
int main() {
out1 = fun1(a);
out2 = fun2(a);
return 0;
Question 6
Incorrect
Fill up the blank in the following program, so that it prints CS10002 in its output.
#include <stdio.h>
int main() {
char d[14];
while( i<5 );
while(s[i]!='\0') {
d[j++] = s[i++];
d[j] = '\0';
printf("%s", d);
return 0;
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
Complete the following program by filling up the blank, so that it prints the following in the output.
A5 B4 C3 D2 E1
#include <stdio.h>
int main() {
char a[5][3];
int i;
a[i][0] = b[i];
printf("%s ",a[i]);
return 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
The following C code removes all occurrences of the character ‘a’ from an input string.
#include<string.h>
int main(){
int i = 0, j = 0, len;
scanf("%s", inpstr);
while(inpstr[i] != '\0') {
i++;
outstr[j] = '\0';
return 0;
Question 9
Incorrect
Let us consider a quarternary number system in which the base or radix is 4 and the symbols are 0,1,2,3.
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
Complete the following program, so that it prints the following pattern in the output.
**+
**+*++
**+*++*+++
#include <stdio.h>
int main() {
int i, j, k;
printf("*");
for( k=0;k<j;k++ ) {
printf("+");
printf("\n");
return 0;
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
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;
int main() {
printf("%d", fun(5));
return 0;
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
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 ) {
if (str1[i] != str2[i]) {
printf("%d\n",0);
return 0;
i++;
printf("%d\n",1);
else printf("%d\n",0);
return 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
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>
int main() {
fun( y,x,5 );
printf("%d ",y[i]);
printf("\n");
return 0;
int i;
a[i]=b[4-i];
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
#include <stdio.h>
int k;
if(i == 0) {
return;
if(i%2 != 0) {
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.
+++++
+++++
+++++
*****
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
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);
printf("%d", fact);
Question 16
Incorrect
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
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
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);
if ( n%i==0 ) sum += i;
printf("%d",(sum == n));
return 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
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>
temp = 9999;
if( a[j]!=0 ) {
temp = a[j];
b[i] = temp;
last = temp;
int main() {
func(a, b, 5);
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
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>
int temp;
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;
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
#include<stdio.h>
#define ROWCOL 2
int i, j;
for (i=0;i<ROWCOL;i++) {
int sum = 0;
z[i]=sum;
return;
int main() {
int i,j;
for (i=0;i<ROWCOL;i++)
add_columns(arr_a, arr_b);
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.
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
#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
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
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 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.
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
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 (j=i; j<n; j++) printf(" "); /* get the start point to print */
printf("\n");
return 0;
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
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;
Jump to...
kgpmoodlenew.iitkgp.ac.in/moodle/mod/quiz/review.php?attempt=137020&cmid=2317 21/21