Function · Programming in C
68 questions · asked in 26 of 27 years (2000–2026)
MCQ 47 · NAT 211-mark 35 · 2-mark 33
2000Questions per year2026
Questions 61–65 of 68
Consider the following C function
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
} In order to exchange the values of two variables x and y.Answer: (D)
The function swap gets only copies of x and y, because the call is by value. It swaps the copies, so x and y in the caller stay the same. Passing x and y would not match the int parameters either.
Answer: swap(x,y) cannot be used as the parameters are passed by value
Answer: swap(x,y) cannot be used as the parameters are passed by value
Consider the C program shown below.
#include <stdio.h>
#define print(x) printf("%d ", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void)
{
x = 5;
P(&x);
print(x);
} The output of this program isAnswer: (A)
Set global x in main:
main: x = 5; //Global x becomes 5
Local x in P:
P: int x = *y + 2; //local x in P becomes 5+2 = 7
Local z in Q:
Q: z+=x; //local z in Q becomes 7 + 5 = 12
Output from Q:
Q: print(z); //prints 12
Update through pointer in P:
P: *y = x - 1; //content of address of local variable y (same as global variable x) becomes 7 - 1 = 6
Output from P:
P: print(x); //prints local variable x in P = 7
Output from main:
main: print(x); //prints the global variable x = 6
The program prints 12 7 6.
Answer: (A)
The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
} If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program areAnswer: (B)
Call by name replaces x by the expression i + j in the body of P and evaluates it again at every use. With dynamic scoping, a name means the most recent active declaration at that moment. Inside P that is P's local i (= 10) and the global j (= 5), since main has no variables of its own.
First print: x + 10 = i + j + 10 = 10 + 5 + 10 = 25.
Then i = 200 changes P's local i, and j = 20 changes the global j.
Second print: x = i + j = 200 + 20 = 220.
Output: 25, 220. Option (A) 115, 220 needs the global i = 100 in the first print but the local i = 200 in the second, which no single scoping rule gives.
Answer: (B)
First print: x + 10 = i + j + 10 = 10 + 5 + 10 = 25.
Then i = 200 changes P's local i, and j = 20 changes the global j.
Second print: x = i + j = 200 + 20 = 220.
Output: 25, 220. Option (A) 115, 220 needs the global i = 100 in the first print but the local i = 200 in the second, which no single scoping rule gives.
Answer: (B)
The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
} If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program areAnswer: (D)
With static scoping, the names in the actual argument are looked up in the caller's environment, which here is the global i and j. P's local i does not hide them. In call by need, the argument is evaluated at the first use of x, and that value is stored and reused for later uses.
First use:
Then
Second use:
The output is 115, 105.
Answer: (D)
First use:
print(x + 10) evaluates , so it prints .Then
i = 200 changes only P's local i, and j = 20 changes the global j.Second use:
print(x) does not evaluate again. It uses the stored value, so it prints . (Call by name would evaluate again and give .)The output is 115, 105.
Answer: (D)
The results returned by function under value-result and reference parameter passing conventions
Answer: (D)
In call by reference, the called function works on the memory locations of the passed variables, so every change takes effect at once.
In call by value-result, the called function works on copies. The final values are copied back to the original variables only when the function returns normally.
If an exception ends the function before it returns, call by reference has already changed the original variables, while call by value-result has not copied anything back. So the results may differ when an exception occurs. In an ordinary function that returns normally, with no aliasing, both give the same result, so (C) is wrong. A loop by itself does not create a difference, so (B) is wrong.
Answer: (D)
In call by value-result, the called function works on copies. The final values are copied back to the original variables only when the function returns normally.
If an exception ends the function before it returns, call by reference has already changed the original variables, while call by value-result has not copied anything back. So the results may differ when an exception occurs. In an ordinary function that returns normally, with no aliasing, both give the same result, so (C) is wrong. A loop by itself does not create a difference, so (B) is wrong.
Answer: (D)
Keep practising