C Programming Questions and Answers – Scope of a Variable
C Programming Questions and Answers – Scope of a Variable
1.What is the output of this C code?
#include <stdio.h>
int i;
int main()
{
extern int i;
if (i == 0)
printf(“scope rules\n”);
}
a) scope rules
b) Compile time error due to multiple declaration
c) Compile time error due to not defining type in statement extern i
d) Nothing as i value is not zero being automatic variable
View Answer
2.What is the output of this C code (without linking the source file in which ary1 is defined)?
#include <stdio.h>
int main()
{
extern ary1[];
printf(“scope rules\n”);
}
a) scope rules
b) Linking error due to undefined reference
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
3.What is the output of this C code after linking with source file having definition of ary1?
#include <stdio.h>
int main()
{
extern ary1[];
printf(“%d\n”, ary1[0]);
}
a) Value of ary1[0] b) Compile time error due to multiple definition
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
4.What is the scope of an external variable?
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
View Answer
5.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
View Answer
6.Comment on the output of this 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
View Answer
7.Which variable has the longest scope?
#include <stdio.h>
int b;
int main()
{
int c;
return 0;
}
int a;
a) a
b) b
c) c
d) Both (a) and (b)
View Answer
8.Comment on the output of this 2 C code?
#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) They are both the same
b) Scope of C is till the end of program
c) All operation in Program 1 can also be performed in Program 2
d) Both (a) and (c)
View Answer
9.The sequence of allocation and deletion of variables for the following code is.
#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
View Answer
10.Array sizes are optional during array declaration by using ______ keyword.
a) auto
b) static
c) extern
d) register
View Answer
11.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 3;
{
x = 4;
printf(“%d”, x);
}
}
a) 4
b) 3
c) 0
d) Undefined
View Answer
12.What is the output of this 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
View Answer
13.What is the output of this 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
View Answer
14.What is the output of this 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
View Answer
15.What is the output of this C code?
#include <stdio.h>
void main()
{
{
int x = 8;
}
printf(“%d”, x);
}
a) 8
b) 0
c) Undefined
d) Compile time error