C Questions and Answers Part-23

1. What is the scope of a function?
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled

Answer: d

2. Comment on the output of the following C code.
#include < stdio.h >
int main()
{
int i;
for (i = 0;i < 5; i++)
int a = i;
printf("%d", a);
}
a) a is out of scope when printf is called
b) Redeclaration of a in same scope throws error
c) Syntax error in declaration of a
d) No errors, program will show the output 5

Answer: c

3. Which variable has the longest scope in the following C code?
#include < stdio.h >
int b;
int main()
{
int c;
return 0;
}
int a;
a) a
b) b
c) c
d) Both a and b

Answer: b

4. Comment on the following 2 C programs.
#include < stdio.h > //Program 1
int main()
{
int a;
int b;
int c;
}
#include < stdio.h > //Program 2
int main()
{
int a;
{
int b;
}
{
int c;
}
}
a) Both are same
b) Scope of c is till the end of the main function in Program 2
c) In Program 1, variables a, b and c can be used anywhere in the main function whereas in Program 2, variables b and c can be used only inside their respective blocks
d) None of the mentioned

Answer: c

5. What will be the sequence of allocation and deletion of variables in the following C code?
#include < stdio.h >
int main()
{
int a;
{
int b;
}
}
a) a->b, a->b
b) a->b, b->a
c) b->a, a->b
d) b->a, b->a

Answer: b

6. Array sizes are optional during array declaration by using ______ keyword.
a) auto
b) static
c) extern
d) register

Answer: c

7. What will be the output of the following C code?
#include < stdio.h >
void main()
{
int x = 3;
{
x = 4;
printf("%d", x);
}
}
a) 4
b) 3
c) 0
d) Undefined

Answer: a

8. What will be the output of the following C code?
#include < stdio.h >
int x = 5;
void main()
{
int x = 3;
m();
printf("%d", x);
}
void m()
{
x = 8;
n();
}
void n()
{
printf("%d", x);
}
a) 8 3
b) 3 8
c) 8 5
d) 5 3

Answer: a

9. What will be the output of the following C code?
#include < stdio.h >
int x;
void main()
{
m();
printf("%d", x);
}
void m()
{
x = 4;
}
a) 0
b) 4
c) Compile time error
d) Undefined

Answer: b

10. What will be the output of the following C code?
#include < stdio.h >
static int x = 5;
void main()
{
int x = 9;
{
x = 4;
}
printf("%d", x);
}
a) 9
b) 5
c) 4
d) 0

Answer: c