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 66–68 of 68
Consider the following program
Program P2
var n: int:
procedure W(var x: int)
begin
x=x+1;
print x;
end
procedure D
begin
var n: int;
n=3;
W(n);
end
begin //beginP2
n=10;
D;
end If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program ?Answer: (D)
D has its own local variable n, set to . The call W(n) inside D uses that n, because it is the n visible in D. The global n () is not used.
W gets the parameter by reference, so x is another name for D's n. Then makes D's n equal to , and
The program prints 4, which is not among (A), (B) and (C).
Answer: (D)
W gets the parameter by reference, so x is another name for D's n. Then makes D's n equal to , and
print x prints .The program prints 4, which is not among (A), (B) and (C).
Answer: (D)
What is printed by the print statements in the program P1 assuming call by reference parameter passing ?
Program P1()
{
x = 10;
y = 3;
func1(y,x,x);
print x;
print y;
}
func1(x,y,z)
{
y = y+4;
z = x+y+z;
}
Answer: (B)
With call by reference, the formal parameters are other names for the actual variables:
- formal x is the same location as P1's y (value )
- formal y and formal z are both the same location as P1's x (value )
: the location of P1's x becomes .
: formal x is , and formal y and z are both , so the result is . This is stored in the location of P1's x.
Nothing is copied back later, since y and z share P1's x directly. P1's y is never changed.
So P1 prints x = 31 and y = 3.
Answer: (B)
- formal x is the same location as P1's y (value )
- formal y and formal z are both the same location as P1's x (value )
: the location of P1's x becomes .
: formal x is , and formal y and z are both , so the result is . This is stored in the location of P1's x.
Nothing is copied back later, since y and z share P1's x directly. P1's y is never changed.
So P1 prints x = 31 and y = 3.
Answer: (B)
The value of j at the end of the execution of the following C program:
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main () {
int i, j;
for (i = 0; i <= 4; i++)
j = incr (i);
} is:Answer: (A)
count is a static variable, so it is created once and keeps its value between calls. Each call adds i to count and returns the new count, so j after the call is the running sum of i.
- : count = 0, so j = 0
- : count = 1, so j = 1
- : count = 3, so j = 3
- : count = 6, so j = 6
- : count = 10, so j = 10
So j ends as .
Answer: (A)
- : count = 0, so j = 0
- : count = 1, so j = 1
- : count = 3, so j = 3
- : count = 6, so j = 6
- : count = 10, so j = 10
So j ends as .
Answer: (A)
Keep practising