Spring 2024 CS102 Soln
Spring 2024 CS102 Soln
1. How many times will PDS be printed? 3. What will be the output for the following C code snippet?
#if a == 0
int main() #define b 5
{ #else
int i = 2048; #define b 10
for (; i; i >>= 1) #endif
printf("PDS\n");
return 0; int main()
} {
printf("%d", b);
return 0;
}
(A) 11 (B) 12 (C) 22 (D) Infinite
(A) 0 (C) 10
2. What will be the output of the following code snippet? (B) 5 (D) Compilation error
1
5. Predict Output (D) 5 6 7 8 9 10 11 12 13 14
int main()
(A) 15 11 19 16 20 15 (C) 15 11 19 15 16 20
{
int j = 1, num1 = 4; (B) 15 11 19 15 20 16 (D) 15 11 19 20 15 16
{
void main()
num1++;
{
printf("%d\n", num1);
int a = 10, b=5;
}
float a = a+b;
printf("%f", a);
}
}
(A) 4 5 6 7 8 9 10 11 12 13
(B) 5 6 7 8 9 10 11 12 13 (A) 15.0000 (C) 10.5
(C) 4 5 6 7 8 9 10 11 12 13 14 (B) 10 (D) Compilations Error
2
11. Predict Output 14. Predict Output
void xyz(int a[], int b [], int c[]) 15. Consider the same recursive C function that takes two ar-
{ guments
int i, j, k;
i = j = k = O; unsigned int foo(unsigned int n, unsigned int r)
while ((i<n) && (j<m)) {
if (a[i] < b[j]) if (n > 0)
c[k++] = a[i++]; return (n % r + foo(n / r, r));
else else
c[k++] = b[j++]; return 0;
} }
Which of the following condition(s) hold(s) after the termi- What is the return value of the function foo when it is called
nation of the while loop? as foo(513, 2)?
(a) j < m, k = n + j − 1, and a[n − 1] < b[j] if i = n
(A) 9 (B) 8 (C) 5 (D) 2
(b) i < n, k = m + i − 1, and b[m − 1] <= a[i] if j = m
(A) only (a) 16. What is the return value of f(p,p), if the value of p is initial-
ized to 5 before the call? Note that the first parameter is
(B) only (b) passed by reference, whereas the second parameter is passed
(C) either (a) or (ii) but not both by value.
(D) neither (a) nor (b)
int f (int &x, int c) {
13. Predict Output c = c - 1;
if (c==0) return 1;
x = x + 1;
int main () {
return f(x,c) * x;
int sum = 0, maxsum = 0, i, n = 6;
}
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
(A) 3024 (B) 6561 (C) 55440 (D) 161051
if (i == 0||a[i]<0||a[i]<a[i - 1])
{
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\n", maxsum);
}
3
17. Consider the following C function. int f1(void);
int f2(void);
int fun (int n){ int f3(void);
int x = 1, k; int x=30;
if (n == 1) int main()
return x; {
for (k = 1; k < n; ++k) int x=1;
x = x + fun(k) * fun(n-k); x += f1() + f2 () + f3() + f2();
return x; printf("%d", x);
} return 0;
}
The return value of fun(5) is . int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;}
(A) 0 (B) 21 (C) 51 (D) 71
(A) 50 (B) 85 (C) 230 (D) 430
18. Consider the following program
21. Predict Output
int main()
{ int main()
int i, j, k = 0; {
j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; int a = 10, b = 10;
k-=--j; printf(a > b ? "A" : a > b ? "B" : "C");
for (i=0; i<5; i++) return 0;
{ }
switch(i+k)
{ (A) A (B) B (C) C (D) A B C
case 1:
case 2: printf("\n%d", i+k);
22. Predict Output
case 3: printf("\n%d", i+k);
default: printf("\n%d", i+k);
int main()
}
{
}
int i = 0;
return 0;
for (i; i < 4; i++)
}
switch (i)
{
The number of times printf statement is executed case 0:
is . printf("A ");
break;
(A) 8 (B) 9 (C) 10 (D) 11 case 1:
printf("B ");
19. Predict Output default:
printf("C ");
}
int main()
{
return 0;
for (int i = 1; i < 21; i++)
}
{
if (i % 3 != 0)
(A) A B C C (C) A B C C C
continue;
printf("%d ", i); (B) A B C B C C (D) A B C
}
return 0;
}
(A) 3 6 9 12 15 18 21 (C) 6 9 12 15 18 21
(B) 3 6 9 12 15 18 (D) 2 4 6 8 10
4
23. Predict Output (A) 5 (C) 8
(B) 11 (D) Compilation error
int main()
{
int i = 0; 27. Every C Variable must have.?
A: i++;
B: printf("%d ", i++);
(A) Type
if (i < 10) goto A;
return 0; (B) Storage Class
} (C) Both Type and Storage Class
(A) 0 2 4 6 8 (C) 0 2 4 6 8 10 (D) Either Type or Storage Class
(B) 1 3 5 7 9 (D) 0 1 3 5 7 9
28. Which of the following is the fastest memory?
24. Predict Output
(A) Random Access Mem- (C) Register Memory
int main() ory
{ (B) Read Only Memory (D) Cache Memory
float x=0.9;
if(x==0.9)
printf("if"); 29. Predict Output
else if (x==0.9f)
printf("else if"); int main()
else {
printf("else"); char ch[10];
} int inum;
float fnum;
(A) if (C) else
(B) else if (D) Compilation error char buffer[50] = "IITP 1510.2";
sscanf (buffer, "%s%d%f", ch, &inum, &fnum);
25. Which of the following is true about the name of 1D-Array?
printf("%s ", ch);
(A) It holds the address of the first element of array. printf("%d ", inum);
(B) It can be incremented or decremented using pointer printf("%f", fnum);
arithmetic. }
(C) Both (A) and (B)
(D) None of these (A) IITP 1510 0.200000 (C) IITP 151 0.200000
(B) IITP 15 10.200000 (D) Syntax Error
26. Predict Output
int main() 30. Which of the storage classes limits the re-initialization of a
{ variable?
int a=5;
a=(a+1, a+2, a+3);
printf("%d",a); (A) auto (C) extern
} (B) static (D) register