Iitkgp Pds-2023a-Sol
Iitkgp Pds-2023a-Sol
Iitkgp Pds-2023a-Sol
KHARAGPUR
Stamp / Signature of the Invigilator
1. You must occupy your seat as per the Examination Schedule/Sitting Plan.
2. Do not keep mobile phones or any similar electronic gadgets with you even in the switched off mode.
3. Loose papers, class notes, books or any such materials must not be in your possession, even if they are irrelevant to the
subject you are taking examination.
4. Data book, codes, graph papers, relevant standard tables/charts or any other materials are allowed only when instructed
by the paper-setter.
5. Use of instrument box, pencil box and non-programmable calculator is allowed during the examination. However,
exchange of these items or any other papers (including question papers) is not permitted.
6. Write on the answer-script and do not tear off any page. For rough work, use last page(s) of the answer script and
white spaces around the questions. Report to the invigilator if the answer script has torn or distorted page(s).
7. It is your responsibility to ensure that you have signed the Attendance Sheet. Keep your Admit Card/Identity Card on the
desk for checking by the invigilator.
8. You may leave the examination hall for wash room or for drinking water for a very short period. Record your absence
from the Examination Hall in the register provided. Smoking and the consumption of any kind of beverages are strictly
prohibited inside the Examination Hall.
9. Do not leave the Examination Hall without submitting your answer script to the invigilator. In any case, you are not
allowed to take away the answer script with you. After the completion of the examination, do not leave the seat until
the invigilators collect all the answer scripts.
10. During the examination, either inside or outside the Examination Hall, gathering information from any kind of sources or
exchanging information with others or any such attempt will be treated as ‘unfair means’. Do not adopt unfair means and
do not indulge in unseemly behavior.
Violation of any of the above instructions may lead to severe punishment.
Marks Obtained
Marks obtained (in words) Signature of the Examiner Signature of the Scrutineer
The end of this question paper is marked by — END —.
Write the answers only in the designated boxes or above the blank lines.
#include <stdio.h>
int main ()
{
char c1 = ’A’, c2 = ’Z’; /* ASCII value of ’A’ is 65 */
char c3 = c2 - c1 + 45;
int i = (c1 < c3) && (c3 > ’\0’);
int j = (c3 != c2) || ((c3 - c2) > c1);
int k = (i != j) + (c3 > 100) - (c3 < 80);
printf("c3 = %c c3 = %d i = %d j = %d k = %d", c3, c3, i, j, k);
return 0;
}
c3 = F c3 = 70 i = 1 j = 1 k = -1
#include <stdio.h>
int main(){
int i, sum = 0,count=0;
printf("Numbers: ");
for(i = 1; !((i+1)%5==0 || (i+7)%6 == 1 ||
(i-27)%7==2); i = i*(i+3)/2, count++){
printf("%d ", i%17);
sum += i/12345678;
}
printf("\n");
printf("Count = %d\n",count);
printf("Sum = %d\n", sum);
return 0;
}
Numbers: 1 2 5 3 9 3 9
Count = 7
Sum = 29
1
3. Given an array of n integers and an integer k, the following code determines whether there are two
elements in the array such that their difference is k. If found, it prints their indices. Fill up the following
code so that it produces the correct result. Assume that 2 ≤ n ≤ 100. [5]
#include <stdio.h>
int main(){
int a[100], n, k, i, j, found = 0;
int main(){
int x1, y1, x2, y2, a, b, c;
printf("Enter coordinates of A and B: ");
b = a%b;
2
5. The following program populates the first n (1 ≤ n ≤ 100) prime numbers in an array, using the facts
that (i) 2 is the smallest prime and (ii) a number is prime if and only if it is not divisible by any prime
less than its square root. Every newly discovered prime, obtained this way, is put in the array. Fill in
the blanks. [10]
#include <stdio.h>
#include <math.h>
int main() {
int n, i=2, tag, j=0, number, _PRIME[100], limit;
if (!(_______tag________)){ // 1 mark
3
6. Fill in the one blank line given and write the output (five lines) of the program when input = 2. [5]
#include <stdio.h>
#include <math.h>
int main(){
float r, area, a = 10.0;
scanf ("%f", &r);
printf ("A = %f\n", a);
area = cArea(cArea(r));
printf ("A = %f\n", a);
printf ("Area is %f \n", area);
return 0;
}
A = 10.000000
r = 2.000000
r = 12.566360
A = 10.000000
Area is 496.099213
7. The school-book multiplication algorithm for two non-negative decimal integers a and b, each
possibly having multiple digits, can be written as:
Fill in the blanks of the following program to compute and print the product of two non-negative
integers following the above algorithm. The two non-negative integers are input by the user. [6]
4
// Each blank carries 1 mark
#include <stdio.h>
int main(){
// declare non-negative integer variables
b = b / 10; // update b
int main(){
double x, term, val;
int n;
printf("\nEnter the value of x: ");
scanf("%lf", &x);
term = term * x / n;
val += term; n = n + 1;
}
printf("\nexp(%lf): %lf\n", x, val);
return 0;
}
5
9. Write the output of the following code. [3 + 2]
#include <stdio.h>
int server = 1, taskid = 1;
void serve (int num_tasks){
int taskid = 1;
for (int i = 0; i < num_tasks; i++) {
printf("Task %d - Server %d \n", taskid, server);
server++; taskid++;
if (server > 3) server = 1;
}
}
int main(){
serve(2);
serve(3);
}
Task 1 - Server 1
Task 2 - Server 2
Task 1 - Server 3
Task 2 - Server 1
Task 3 - Server 2
Suppose the serve() function in the above code prints the tasks executed on different servers.
Suggest a change in only one line of the code so that the serve() function prints the total number
of tasks executed till that time. Write that line of code before the change and the same after the change.
10. Consider the following function which calculates the mode of a given integer array (a[]), passed as
an argument along with the size (n). [Mode is an element that appears the maximum number of times.
Ties are broken arbitrarily.] Fill in the blanks to complete the code. [5]
6
for (i = 0; i < n; ++i) {
___________________;
if (___________________)
++count;
}
if (count > maxCount) {
___________________;
___________________;
}
}
return maxValue;
}
Blank 1: maxCount=0;
Blank 2: count = 0;
Blank 3: a[j] == a[i]
Blank 4: maxCount = count;
Blank 5: maxValue = a[i];
11. Determine the single-precision floating-point representation from the given normalized number
−1.111100011001 × 256 . [2]
#include <stdio.h>
int main() {
int a=5, b=10, c=-6, d;
d = a--/2.0 == 2.5 && b/5.0 != 0.0 && c/-3.0 && ++a/2.0 == 2;
a = b+2 == -2*c != d+a+c;
printf("d = %d, a = %d\n", d, a);
return 0;
}
d = 0, a = 1
7
13. Print the output of the following program for the input x = 78.467523. [3]
#include <stdio.h>
int main (){
double x, y;
long int a;
scanf("%lf", &x);
y = x - (int)x;
a = (int)x;
if (y >= 0.5) ++a;
printf("x = %0.2lf, y = %0.2lf, a = %ld", x, y, a);
return 0;
}
x = 78.47, y = 0.47, a = 78
#include <stdio.h>
int main() {
int j = (printf("TEN = "), 10);
printf("%d", j);
return 0;
}
TEN = 10
— END —
Rough work