Here is a listing of basic C questions on “Scope of a Variable” along with answers, explanations and/or solutions:
1. What will be the sequence of allocation and deletion of variables in the following C code?
-
#include
-
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
Clarification: None.
2. Array sizes are optional during array declaration by using ______ keyword.
a) auto
b) static
c) extern
d) register
Answer: c
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int x = 3;
-
{
-
x = 4;
-
printf("%d", x);
-
}
-
}
a) 4
b) 3
c) 0
d) Undefined
Answer: a
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
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
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int x;
-
void main()
-
{
-
m();
-
printf("%d", x);
-
}
-
void m()
-
{
-
x = 4;
-
}
a) 0
b) 4
c) Compile time error
d) Undefined
Answer: b
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
static int x = 5;
-
void main()
-
{
-
int x = 9;
-
{
-
x = 4;
-
}
-
printf("%d", x);
-
}
a) 9
b) 5
c) 4
d) 0
Answer: c
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
{
-
int x = 8;
-
}
-
printf("%d", x);
-
}
a) 8
b) 0
c) Undefined
d) Compile time error
Answer: d
Clarification: None.