Question 1 · Programming and Data Structures · 2026_Set2
MCQ
Consider the following three ANSI-C programs, P1, P2, and P3.
| P1 | P2 | P3 |
|---|
#include <stdio.h>
int a=5;
int main(){
int a=7;
return(0);
} | #include <stdio.h>
int main(){
int a=5;
int a=7;
return(0);
} | #include <stdio.h>
int main(){
int a=5;
float a=7;
return(0);
} |
Which one of the following statements is true?
- A.
Only P1 will compile without any error
- B.
Only P2 will compile without any error
- C.
Only P3 will compile without any error
- D.
All three programs P1, P2, and P3 will compile without any error
Step-by-Step Solution
Insight: C allows variable shadowing across different scopes but forbids redeclaration within the same scope, regardless of type.
Exam route: Check the scope of each variable declaration. Global vs local is shadowing. Two locals in the same block is redeclaration.
Learning route:
- P1:
int a=5; is at file scope. int a=7; is inside main (block scope). The local a shadows the global a. This is perfectly valid C and compiles without error.
- P2:
int a=5; and int a=7; are both declared inside the exact same block scope (main). This is a redeclaration error. The compiler will reject it.
- P3:
int a=5; and float a=7; are both in the same block scope. Even though the types differ, C does not allow overloading or redeclaration with different types in the same scope. This is a compilation error.
- Therefore, only P1 compiles successfully.
Trap: Believing that shadowing causes a compilation error or that different types in the same scope are allowed. Shadowing is strictly cross-scope; redeclaration is strictly intra-scope.
Verification: Compile P1 mentally: global a exists, local a hides it. No conflict. P2: compiler sees two as in main's symbol table -> error. P3: same symbol table, conflicting types -> error.
Question 2 · Programming and Data Structures · 2026_Set1
NAT
Consider the following program in C:
#include <stdio.h>
void func(int i, int j) {
if(i < j) {
int i = 0;
while (i < 10) {
j += 2;
i++;
}
}
printf("%d", i);
}
int main() {
int i = 9, j = 10;
func(i, j);
return 0;
}
The output of the program is _________. (answer in integer)
Note: Assume that the program compiles and runs successfully.
Step-by-Step Solution
Insight: Block scope shadowing hides the outer variable, but the outer variable is untouched and reappears when the block ends.
Exam route: Parameter i is 9. Inner i is 0, loops to 10. Block ends. printf uses parameter i (9).
Learning route:
main calls func(9, 10). Inside func, parameter i = 9 and j = 10.
- The condition
i < j (9<10) is true, so the if block is entered.
- Inside the
if block, int i = 0; is declared. This creates a new local variable i that <b>shadows</b> the parameter i. The parameter i is hidden but still exists with the value 9.
- The
while loop runs 10 times. The inner i increments from 0 to 10. j increases by 20 (becoming 30).
- The
if block ends. The inner i goes out of scope and is destroyed.
- The
printf("%d", i); statement executes. Since the inner i is gone, this refers to the parameter i, which was never modified and remains 9.
Trap: Assuming the while loop modifies the parameter i (which would output 10), or assuming that redeclaring i inside the block causes a compilation error.
Verification: Shadowing is strictly scoped. Once the closing brace } of the if block is passed, the shadow is lifted, revealing the original untouched parameter.
Question 3 · Programming and Data Structures · 2025_Set2
NAT
Consider the following C program:
#include <stdio.h>
int g(int n) {
return (n+10);
}
int f(int n) {
return g(n*2);
}
int main() {
int sum, n;
sum=0;
for (n=1; n<3; n++)
sum += g(f(n));
printf ("%d", sum);
return 0;
}The output of the given C program is ________. (Answer in integer)
Step-by-Step Solution
Insight: Nested function calls require resolving the innermost call first and passing its return value outward.
Exam route: Trace the loop for n=1 and n=2. For each, compute f(n), then pass the result to g(), and accumulate in sum.
Learning route:
- The loop runs for
n=1 and n=2 (since n<3).
- Iteration 1 (
n=1):
- Evaluate inner call
f(1): calls g(1*2) → g(2) → returns 2+10=12.
- Evaluate outer call
g(f(1)) → g(12) → returns 12+10=22.
sum becomes 0+22=22.
- Iteration 2 (
n=2):
- Evaluate inner call
f(2): calls g(2*2) → g(4) → returns 4+10=14.
- Evaluate outer call
g(f(2)) → g(14) → returns 14+10=24.
sum becomes 22+24=46.
- Loop terminates.
printf outputs 46.
Trap: Students sometimes evaluate the outer function first or confuse the argument passed to the outer function. Always resolve from the inside out.
Verification: g(f(1))=g(12)=22. g(f(2))=g(14)=24. Total sum = 46.
Question 4 · Programming and Data Structures · 2025_Set2
NAT
int x=126,y=105;
do {
if(x>y) x=x-y;
else y=y-x;
} while(x!=y);
printf("%d",x);The output of the given C code segment is ________. (Answer in integer)
Step-by-Step Solution
Insight: This is the subtraction-based Euclidean algorithm for computing GCD. The loop repeatedly subtracts the smaller value from the larger until both are equal, at which point that common value is the GCD.
Exam route: Recognize the algorithm immediately, then compute GCD(126, 105) using prime factorization or the modulo-based Euclidean algorithm as a shortcut.
Learning route:
- Initial:
x = 126, y = 105.
- The
do-while loop guarantees at least one iteration.
- Iteration 1:
x > y (126 > 105), so x = 126 - 105 = 21. State: x=21, y=105.
- Iteration 2:
x > y is false (21 < 105), so y = 105 - 21 = 84. State: x=21, y=84.
- Iteration 3:
y = 84 - 21 = 63. State: x=21, y=63.
- Iteration 4:
y = 63 - 21 = 42. State: x=21, y=42.
- Iteration 5:
y = 42 - 21 = 21. State: x=21, y=21.
- Condition
x != y is now false (21 == 21). Loop terminates.
printf("%d", x) prints 21.
Shortcut verification using prime factorization:
- 126=2×63=2×9×7=2×32×7
- 105=5×21=5×3×7
- Common factors: 3×7=21
- GCD(126, 105) = 21. Confirmed.
Alternative verification using modulo-based Euclidean algorithm:
- 126mod105=21
- 105mod21=0
- GCD = 21. Confirmed.
Wrong path: A student who makes an arithmetic error in the subtraction chain (e.g., computing 105−21=83 instead of 84) would get a wrong final answer. Another error is assuming the loop terminates after the first subtraction and prints 21 immediately without checking the while condition properly, though in this case the answer happens to be correct regardless.
Generalization: The subtraction-based GCD loop if(x>y) x-=y; else y-=x; while(x!=y) always terminates with both variables equal to GCD(x_initial, y_initial). For large numbers, use the modulo shortcut to verify quickly.
Question 5 · Programming and Data Structures · 2025_Set1
NAT
Consider the following C program:
#include <stdio.h>
int gate (int n) {
int d, t, newnum, turn;
newnum = turn = 0; t=1;
while (n>=t) t *= 10;
t /=10;
while (t>0) {
d = n/t;
n = n%t;
t /= 10;
if (turn) newnum = 10*newnum + d;
turn = (turn + 1) % 2;
}
return newnum;
}
int main () {
printf ("%d", gate(14362));
return 0;
}
The value printed by the given C program is ______ . (Answer in integer)
Step-by-Step Solution
Insight: The function extracts digits of n from left to right (most significant to least significant) and selectively builds a new number using alternating digits.
Exam route: Trace the digit extraction loop. The first while finds the highest power of 10. The second while extracts digits one by one. turn toggles between 0 and 1, acting as a filter.
Learning route:
n = 14362. First loop sets t = 10000.
- Second loop begins.
turn = 0, newnum = 0.
- Iteration 1:
d = 14362 / 10000 = 1. n becomes 4362. t becomes 1000. turn is 0, so if(turn) is false. turn becomes 1.
- Iteration 2:
d = 4362 / 1000 = 4. n becomes 362. t becomes 100. turn is 1, so newnum = 10*0 + 4 = 4. turn becomes 0.
- Iteration 3:
d = 362 / 100 = 3. n becomes 62. t becomes 10. turn is 0, skip. turn becomes 1.
- Iteration 4:
d = 62 / 10 = 6. n becomes 2. t becomes 1. turn is 1, newnum = 10*4 + 6 = 46. turn becomes 0.
- Iteration 5:
d = 2 / 1 = 2. n becomes 0. t becomes 0. turn is 0, skip. Loop ends.
- Returns
46.
Trap: Misunderstanding the order of extraction. The code extracts from most significant to least significant (left to right). If it extracted right to left, the alternating digits would be different. Also, forgetting that turn starts at 0, meaning the 1st, 3rd, and 5th digits are skipped.
Verification: The digits of 14362 are 1, 4, 3, 6, 2. The kept digits are at indices 1 and 3 (0-indexed), which are 4 and 6. Concatenating them gives 46.
Question 6 · Programming and Data Structures · 2024_Set2
MCQ
Consider the following C program. Assume parameters to a function are evaluated from right to left.
#include <stdio.h>
int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }
void f(int x, int y) {
g(x);
h(y);
}
int main() {
f(g(10),h(20));
}
Which one of the following options is the CORRECT output of the above C program?
- A.
20101020
- B.
10202010
- C.
20102010
- D.
10201020
Step-by-Step Solution
Insight: Right-to-left evaluation means the rightmost argument is fully evaluated (including side effects like prints) before the left argument.
Exam route: Evaluate h(20) first (prints 20), then g(10) (prints 10). Then execute f(10, 20), which prints 10 then 20. Total output: 20101020.
Learning route:
- The call is
f(g(10), h(20)). The problem explicitly states parameters are evaluated right-to-left.
- Rightmost argument:
h(20) is evaluated first. It prints 20 and returns 20.
- Leftmost argument:
g(10) is evaluated next. It prints 10 and returns 10.
- Now the function
f is called with the evaluated arguments: f(10, 20).
- Inside
f(int x, int y):
g(x) is called → g(10). It prints 10.
h(y) is called → h(20). It prints 20.
- Concatenating the prints in order:
20 (from arg eval), 10 (from arg eval), 10 (from f body), 20 (from f body).
- Final output string:
20101020.
Trap: Assuming left-to-right evaluation (which yields 10201020) or assuming the function body executes before arguments are evaluated.
Verification: Right-to-left argument evaluation is a standard GCC behavior and explicitly mandated by the problem statement.
Question 7 · Programming and Data Structures · 2024_Set2
MCQ
Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices.
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
int first=0, second=0, len1=0, len2=0, maxlen=0;
for (int i=0; i < n; i++) {
if (X[i] == first) {
len2++; len1++;
} else if (X[i] == second) {
len2++;
len1 = (P) ;
second = first;
} else {
len2 = (Q) ;
len1 = 1; second = first;
}
if (len2 > maxlen) {
maxlen = len2;
}
first = X[i];
}
Which one of the following options gives the CORRECT missing expressions?
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
- A.
(P) len1+1 (Q) len2+1
- B.
(P) 1 (Q) len1+1
- C.
(P) 1 (Q) len2+1
- D.
(P) len2+1 (Q) len1+1
Step-by-Step Solution
Insight: This is a sliding window algorithm tracking the longest subarray with at most two distinct elements using O(1) space. len1 tracks the suffix of identical elements, and len2 tracks the valid two-element window.
Exam route: Analyze the state transitions when a new element matches the older distinct value (second) versus when it is a completely new third value.
Learning route:
len1 maintains the length of the contiguous trailing sequence of identical elements ending at X[i-1].
len2 maintains the longest valid subarray ending at X[i-1] with at most two distinct values.
- When
X[i] == first: The element extends the current identical suffix. Both len1 and len2 increment.
- When
X[i] == second: The element matches the older distinct value in our window. The window extends (len2++), but the trailing identical suffix is broken and restarts at length 1. Thus, (P) must be 1. The roles of first and second swap.
- When
X[i] is a third distinct value: The window must shrink to keep only the previous identical suffix plus this new element. The new window length is exactly the old len1 plus 1. Thus, (Q) must be len1 + 1. len1 resets to 1.
Trap: Confusing len1 and len2 updates. If you set (Q) to len2 + 1, you incorrectly include elements from before the trailing identical suffix, violating the "at most two distinct" rule when a third value appears.
Verification: Trace array [2, 2, 3, 2].
- i=0 (2): len1=1, len2=1.
- i=1 (2): len1=2, len2=2.
- i=2 (3): 3rd value? No, first=2, second=0. It goes to
else, len2 = len1+1 = 3. len1=1. second=2, first=3.
- i=3 (2): Matches
second (2). len2++ (4). len1 = 1. first=2, second=3. Window [2,2,3,2] is valid, length 4. Correct.
Question 8 · Programming and Data Structures · 2024_Set1
MCQ
Consider the following C program:
#include <stdio.h>
int main(){
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;}
printf("%d", a);
return 0;}
Which one of the following statements is CORRECT?
- A.
The program prints 9 as output
- B.
The program prints 10 as output
- C.
The program gets stuck in an infinite loop
- D.
The program prints 6 as output
Step-by-Step Solution
Insight: Integer division truncates toward zero, creating a fixed point that prevents the loop variable from ever reaching the termination condition.
Exam route: Evaluate just two iterations. Observe that a maps to 1 and then stays at 1 forever. The loop never terminates.
Learning route:
- Initial state:
a = 6, b = 0. Check condition: 6 < 10 is true, enter loop.
- Iteration 1:
a = a / 12 + 1. In C integer arithmetic, 6 / 12 = 0 (truncation toward zero).
- So
a = 0 + 1 = 1.
a += b gives a = 1 + 0 = 1.
- Check condition:
1 < 10 is true, continue.
- Iteration 2:
a = 1 / 12 + 1. Integer division: 1 / 12 = 0.
- So
a = 0 + 1 = 1.
a += 0 gives a = 1.
- The value of
a is now pinned at 1. Every subsequent iteration produces the same result. The condition a < 10 remains true forever.
- The program enters an infinite loop and never reaches
printf.
Wrong path producing "prints 10": A student who mentally evaluates 6/12 as 0.5 and rounds up would get a = 0.5 + 1 = 1.5, then perhaps a = 2 on the next step, and eventually reach 10. But C integer division strictly truncates, never rounds.
Wrong path producing "prints 6": A student who assumes the loop body never executes (perhaps misreading the condition as a > 10) would select this. But 6 < 10 is clearly true.
Verification: The function f(a)=⌊a/12⌋+1 has a fixed point at a=1 since ⌊1/12⌋+1=0+1=1. Since the loop condition a<10 is satisfied at this fixed point, the loop cannot terminate.
Generalization: Whenever a while-loop updates its control variable using integer division by a larger number, check whether the variable reaches a fixed point below the termination threshold. If so, the loop is infinite.
Question 9 · Programming and Data Structures · 2024_Set1
MSQ
Consider the following C function definition.
int f(int x, int y) {
for (int i=0; i<y; i++) {
x=x+x+y;
}
return x;
}
Which of the following statements is/are TRUE about the above function?
- A.
If the inputs are x=20, y=10, then the return value is greater than 220
- B.
If the inputs are x=20, y=20, then the return value is greater than 220
- C.
If the inputs are x=20, y=10, then the return value is less than 210
- D.
If the inputs are x=10, y=20, then the return value is greater than 220
Correct Answer: ["B","D"]
Step-by-Step Solution
Insight: The loop computes the linear recurrence xk+1=2xk+y exactly y times, yielding a closed-form involving powers of 2.
Exam route: Derive the closed form xy=2y⋅x+y(2y−1), then compare each option's result against the stated power-of-2 bound.
Learning route:
- The loop runs from
i=0 to i=y-1, so exactly y iterations.
- Each iteration applies x←2x+y.
- Unrolling:
- After 1 step: x1=2x+y
- After 2 steps: x2=2(2x+y)+y=4x+3y=22x+(22−1)y
- After 3 steps: x3=2(4x+3y)+y=8x+7y=23x+(23−1)y
- General form after k steps: xk=2kx+(2k−1)y.
- After y steps (the loop bound): result=2y⋅x+(2y−1)⋅y.
Now test each option:
Option A (x=20,y=10): 210⋅20+(210−1)⋅10=20480+10230=30710. Compare with 220=1048576. Since 30710<1048576, the statement "greater than 220" is FALSE.
Option B (x=20,y=20): 220⋅20+(220−1)⋅20=20⋅220+20⋅220−20=40⋅220−20. This is clearly >220. TRUE.
Option C (x=20,y=10): Result is 30710. Compare with 210=1024. Since 30710>1024, the statement "less than 210" is FALSE.
Option D (x=10,y=20): 220⋅10+(220−1)⋅20=10⋅220+20⋅220−20=30⋅220−20. This is >220. TRUE.
Verification by back-substitution: For Option B, 40⋅220−20≈41943020, which is indeed >1048576. Confirmed.
Generalization: Any loop of the form x = a*x + b repeated n times yields anx0+b⋅a−1an−1. Recognize this pattern to avoid tedious manual tracing.
Question 10 · Programming and Data Structures · 2023
NAT
The integer value printed by the ANSI-C program given below is __________.
#include<stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d\n", (x+y));
return 0;
}
Step-by-Step Solution
Insight: A static local variable retains its value across function calls, while local variables in main are independent. The question tests whether you can track two distinct variables named x in different scopes.
Exam route: Trace the static x inside funcp across two calls, then combine with the local x and y in main.
Learning route:
- First call
x = funcp();:
- Inside
funcp: static x is initialized to 1 (this happens only once, ever).
x++ increments static x to 2.
- Returns 2.
- In
main: local variable x is assigned 2.
- Second call
y = funcp() + x;:
- Inside
funcp: static x retains its value of 2 from the previous call.
x++ increments static x to 3.
- Returns 3.
- In
main: the expression evaluates to 3 + 2 (the local x in main is still 2).
- Local
y is assigned 5.
- Print:
printf("%d\n", (x+y)) prints 2 + 5 = 7.
Wrong path producing 5: A student who assumes static x resets to 1 on every call would get: first call returns 2, second call also returns 2, so y = 2 + 2 = 4, and x + y = 2 + 4 = 6. Or they might confuse the two x variables entirely.
Wrong path producing 9: A student who thinks the local x in main is the same variable as the static x in funcp might set x = 3 after the second call, then compute y = 3 + 3 = 6 and print 3 + 6 = 9.
Verification: Static x inside funcp: 1 → 2 → 3 (across two calls). Local x in main: 2 (set once, never modified again). Local y: 5. Sum: 7. Confirmed.
Generalization: Variables with the same name in different scopes are completely independent. Static variables persist across calls; automatic variables do not. Always maintain separate columns for each scope in your trace.