1.
Option (A):
"At most one activation record exists between the current activation record and the activation
record for the main."
Incorrect.
• This implies a linear function call: main() → A() → B() with no nested or deeper calls.
• But C allows deep nesting of function calls, and there can be multiple activation
records on the call stack at any time.
• Example:
CopyEdit
void A() { B(); }
void B() { C(); }
void C() { /* current function */ }
int main() { A(); }
At the point when C() is running, the stack has 4 activation records: for main(), A(), B(), and C().
Option (B):
"The number of activation records between the current activation record and the activation
record from the main depends on the actual function calling sequence."
Correct.
• This is exactly how a function call stack works.
• Activation records (stack frames) are created when functions are called and destroyed
when they return.
• So the depth of the stack depends on how functions are called.
• It's not fixed — it varies during program execution.
Option (C):
"The visibility of global variables depends on the actual function calling sequence."
Incorrect.
• In C, variable scoping is static (lexical).
• This means the visibility of a variable is determined at compile time, not based on who
called what at runtime.
• A global variable is accessible from any function unless hidden by a local variable of the
same name — the function call chain does not affect visibility.
Option (D):
"Recursion requires the activation record for the recursive function to be saved in a different
stack before the recursive function can be called."
Incorrect.
• In C, all activation records (recursive or not) are stored on the same call stack.
• Recursion reuses the same function code, but each recursive call gets its own
activation record (stack frame) on the same stack.
2. The Function:
CopyEdit
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0)
return ((n % r) + foo(n / r, r));
else
return 0;
What does it do?
This function:
• Takes two unsigned integers n and r
• Recursively breaks down n into smaller parts using division by r
• At each step, it adds n % r (i.e., the remainder when n is divided by r)
• The recursion ends when n == 0
This means:
The function returns the sum of digits of n in base r
In our case: foo(513, 2)
→ This gives us the sum of bits (i.e., count of 1s) in the binary representation of 513.
Binary Representation:
Let's convert 513 to binary:
go
CopyEdit
513 ÷ 2 = 256 → remainder 1
256 ÷ 2 = 128 → remainder 0
128 ÷ 2 = 64 → remainder 0
64 ÷ 2 = 32 → remainder 0
32 ÷ 2 = 16 → remainder 0
16 ÷ 2 = 8 → remainder 0
8 ÷ 2 = 4 → remainder 0
4 ÷ 2 = 2 → remainder 0
2 ÷ 2 = 1 → remainder 0
1 ÷ 2 = 0 → remainder 1
So, 513 in binary = `1000000001`
Now count the number of 1s → There are 2 ones.
Recursive Calls Breakdown:
CopyEdit
foo(513, 2)
→ 513 % 2 = 1 + foo(256, 2)
→ 256 % 2 = 0 + foo(128, 2)
→ 128 % 2 = 0 + foo(64, 2)
→ 64 % 2 = 0 + foo(32, 2)
→ 32 % 2 = 0 + foo(16, 2)
→ 16 % 2 = 0 + foo(8, 2)
→ 8 % 2 = 0 + foo(4, 2)
→ 4 % 2 = 0 + foo(2, 2)
→ 2 % 2 = 0 + foo(1, 2)
→ 1 % 2 = 1 + foo(0, 2)
→ foo(0, 2) = 0
Now adding up the remainders:
CopyEdit
1+0+0+0+0+0+0+0+0+1+0=2
Final Answer: (D) 2
3. What is the question asking?
You are given a recursive function to reverse an input string, character by character. The input
ends with a newline character ('\n').
You are to fill in two blanks ?1 and ?2 in this recursive function:
CopyEdit
void reverse (void) {
int c;
if (?1) reverse() ;
?2
Main function:
CopyEdit
main() {
printf("Enter Text\n");
reverse();
printf("\n");
}
Goal:
To read characters one by one, store them via recursion (i.e., in call stack), and print them in
reverse order using recursion's unwinding phase.
Step-by-Step Reasoning:
Let’s examine each part logically:
Part ?1 — Condition for Recursion:
We want to read a character and check if it is not newline.
To do this, we must:
• Read a character from input using getchar()
• Assign it to c
• Check if it’s not newline: c != '\n'
• Important: = (assignment) has lower precedence than !=, so we must use parentheses
So the correct expression for ?1 is:
CopyEdit
((c = getchar()) != '\n')
Part ?2 — Print the Character After Recursive Call
Once we reach the end (newline), we want to start printing the characters in reverse order (i.e.,
after recursion returns).
We already stored the character in c, so now we just need to print it.
So ?2 is:
c
CopyEdit
putchar(c);
So final function is:
CopyEdit
void reverse (void) {
int c;
if ((c = getchar()) != '\n')
reverse();
putchar(c);
4. Given Recursive Function:
unsigned int foo(unsigned int n, unsigned int r)
if (n > 0)
return ((n % r) + foo(n / r, r));
else
return 0;
What does this function do?
• It repeatedly divides n by r
• At each step, it adds n % r, which gives the last digit in base r
• It stops when n == 0
• So the function returns the sum of digits of n in base r
In Our Case:
We are given:
CopyEdit
foo(345, 10)
• n = 345
• r = 10
So the function returns the sum of digits of 345 in base 10
Sum of Digits of 345:
makefile
CopyEdit
345 → digits = 3, 4, 5
Sum = 3 + 4 + 5 = 12
Recursive Call Breakdown:
CopyEdit
foo(345, 10)
→ 345 % 10 = 5 + foo(34, 10)
→ 34 % 10 = 4 + foo(3, 10)
→ 3 % 10 = 3 + foo(0, 10)
→ foo(0, 10) = 0
So, total = 5 + 4 + 3 = 12
Final Answer: (B) 12
6. Given Function:
CopyEdit
void foo(char *a){
if (*a && *a != ' '){
foo(a + 1);
putchar(*a);
And the input string: "ABCD EFGH"
Key Concepts:
• The function foo uses recursion and prints characters after the recursive call → this
means the characters are printed in reverse order.
• The condition in if: if (*a && *a != ' '):
o First checks: *a != '\0' (non-null character)
o Then: *a != ' ' (not a space)
• So it stops recursion when it sees a space or end of string
Step-by-step Execution:
Let’s walk through foo("ABCD EFGH")
String:
mathematica
CopyEdit
Index: 0 1 2 3 4 5 6 7 8
Char : A B C D E F G H
The recursion only proceeds while characters are not null and not space ' '
First Call: foo("ABCD EFGH")
• *a = 'A' → passes condition
• Recurse with a + 1
Second Call: foo("BCD EFGH")
• *a = 'B' → recurse with a + 1
Third Call: foo("CD EFGH")
• *a = 'C' → recurse with a + 1
Fourth Call: foo("D EFGH")
• *a = 'D' → recurse with a + 1
Fifth Call: foo(" EFGH")
• *a = ' ' → fails condition → returns
Now recursion unwinds:
• From 4th call: prints 'D'
• From 3rd call: prints 'C'
• From 2nd call: prints 'B'
• From 1st call: prints 'A'
🖨 Output:
nginx
CopyEdit
DCBA
Final Answer: (D) DCBA
10. Given C function:
CopyEdit
float f(float x, int y) {
float p, s;
int i;
for (s = 1, p = 1, i = 1; i < y; i++) {
p *= x / i;
s += p;
return s;
What this function is doing:
We initialize:
• s = 1 → this accumulates the sum (result)
• p = 1 → this holds each term in the series
Then, for i = 1 to y - 1, it does:
• p = p * (x / i) → each term is calculated using the previous one
• s = s + p → sum updated with each term
Series Being Computed:
Let’s look at the terms being added:
ini
CopyEdit
s = 1 + (x/1) + (x^2 / 2!) + (x^3 / 3!) + ... + (x^(y-1) / (y-1)!)
This is the Taylor series expansion of exe^xex:
ex=1+x1!+x22!+x33!+⋯e^x = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdotsex=1+1!x
+2!x2+3!x3+⋯
• The more terms (i.e., larger y), the better the approximation
• The loop goes up to i < y, so total terms = y
Final Answer: (B) exe^xex