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 36–40 of 68
Consider the following C code segment.
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
int a, b, c = 0;
void prtFun(void);
main( )
{ static int a = 1; /* Line 1 */
prtFun( );
a += 1;
prtFun( );
printf(" \n %d %d ", a, b);
}
void prtFun(void)
{ static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf(" \n %d %d ", a, b);
}What output will be generated by the given code segment if: Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
Answer: (D)
Now the variable a in prtFun is a register (automatic) variable. It is set to 2 again on every call.
- Every call: a = 2 + 2 = 4 and b = 2 so it prints 4 2 two times.
- In main: the local a = 1 + 1 = 2 and the global b = 0 so it prints 2 0.
Answer: 4 2, 4 2, 2 0
- Every call: a = 2 + 2 = 4 and b = 2 so it prints 4 2 two times.
- In main: the local a = 1 + 1 = 2 and the global b = 0 so it prints 2 0.
Answer: 4 2, 4 2, 2 0
Consider the following C code segment.
int a, b, c = 0;
void prtFun(void);
main( )
{ static int a = 1; /* Line 1 */
prtFun( );
a += 1;
prtFun( );
printf(" \n %d %d ", a, b);
}
void prtFun(void)
{ static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf(" \n %d %d ", a, b);
}What output will be generated by the given code segment?Answer: (C)
In prtFun, the static a starts at 2 and keeps its value between calls. The local b is set to 1 and becomes 2 on every call.
- First call: a = 2 + 2 = 4 the output is 4 2
- Second call: a = 4 + 2 = 6 the output is 6 2
- In main: its own static a = 1 + 1 = 2, and the global b = 0 the output is 2 0
Answer: 4 2, 6 2, 2 0
- First call: a = 2 + 2 = 4 the output is 4 2
- Second call: a = 4 + 2 = 6 the output is 6 2
- In main: its own static a = 1 + 1 = 2, and the global b = 0 the output is 2 0
Answer: 4 2, 6 2, 2 0
Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
}What is the return value of the function foo when it is called as foo (513, 2)?Answer: (D)
When called as foo(n, 2), the function adds up the binary digits of n. So it counts the 1 bits.
513 = 512 + 1 = 1000000001 in binary it has 2 ones.
Answer: 2
513 = 512 + 1 = 1000000001 in binary it has 2 ones.
Answer: 2
Consider the following recursive C function that takes two arguments
What is the return value of the function foo when it is called as foo (345, 10) ?
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
} What is the return value of the function foo when it is called as foo (345, 10) ?
Answer: (B)
The function foo(n, r) finds the sum of the digits of n written in base r.
So foo(345, 10) = 5 + 4 + 3 = 12
Answer: 12
So foo(345, 10) = 5 + 4 + 3 = 12
Answer: 12
What is the value printed by the following C program?
#include <stdio.h>
int f(int * a, int n)
{
if(n<=0)return 0;
else if(*a% 2 ==0) return *a+f(a+1,n-1);
else return *a-f(a+1,n-1);
}
int main ( )
{
int a[ ] = {12, 7, 13, 4, 11, 6};
printf ("%d", f(a,6));
return 0;
} Answer: (C)
The function f(a, n) returns 0 if . If the first element is even, it returns that element plus f of the rest. If it is odd, it returns that element minus f of the rest. The array is . Work from the last call back:
- : 6 is even, so
- : 11 is odd, so
- : 4 is even, so
- : 13 is odd, so
- : 7 is odd, so
- : 12 is even, so
Answer: (C)
Keep practising