Q: 1 What would be output of the following program?
void main()
{
int num=45;
printf("%d %d ",num,num);
{
int num=55;
printf("%d",num);
}
}
45 45 55
45 45 45
55 55 55
Compile time error
[ Option A ]
In this program, the concept of variable scope is used. The variable num is first declared in the main() function with the value 45, so the first printf("%d %d ", num, num); prints 45 and 45.
After that, there is a new inner block with its own variable num declared as 55. This inner variable temporarily hides the outer one. Therefore, inside this block, the printf("%d", num); prints 55.
Q: 2 What is the output of the below code
void main()
{
extern int num=10;
printf("%d",num);
}
10
Garbage
Compile time error
0
[ Option C ]
In C, the keyword extern is used to declare a variable that is defined elsewhere. It tells the compiler that the variable exists, but does not allocate memory or initialize it.
We cannot combine extern with an initialization like extern int num=10; because that would be both a declaration and a definition at the same time, which is not allowed.
Q: 3 When is a static variable initialized?
First time when a loop is executed
All time when a loop is executed
It cannot be initialized
None of the above
[ Option A ]
A static variable is initialized only once, and that happens the first time the control reaches its declaration. After that, the variable retains its value between function calls or loop iterations.
#include <stdio.h>
int main()
{
for(int i = 1; i <= 3; i++)
{
static int p=5;
p++;
printf("Iteration %d : Value of P = %d
",i,p);
}
return 0;
}
OUTPUT
Iteration 1 : Value of P = 6
Iteration 2 : Value of P = 7
Iteration 3 : Value of P = 8
Q: 4 What is the output of the below code
int num;
void main()
{
extern int num;
printf("%d",num);
}
1
0
Compile time error
Either (a) or (b)
[ Option B ]
In this program, the variable num is defined globally as int num; but not explicitly initialized. In C, global variables are automatically initialized to 0 if no value is assigned.
Inside main(), the statement extern int num; tells the compiler that num is a variable defined elsewhere, which is correct here because the global num exists.
Q: 5 What would be output of the following program
void main()
{
extern int num;
num=13;
printf("%d",num);
}
13
Vary from compiler to compiler
Either (a) or (b)
Compile time error occurred
[ Option D ]
In this program, the variable num is declared using the keyword extern, which means it is only a declaration, not a definition. The extern keyword tells the compiler that the variable num is defined somewhere else in the program or in another file.
However, in this case, there is no actual definition of num anywhere in the program. Because of this, when the compiler tries to build the program, it cannot find memory allotted for the variable num, which leads to an error.
Q: 6 What is the output of the below code
void main()
{
extern int num;
printf("%d",num);
}
int num=25;
0
1
25
Compile time error
[ Option C ]
The variable num is first declared using extern int num; inside main(). The extern keyword tells the compiler that the variable is defined elsewhere. After main(), num is defined globally as int num=25;.
Since extern correctly references the global variable, the program prints the value of num, which is 25.
Q: 7 What is the following?
extern int num;
Declaration
Definition
Declaration & Definition
None of these
[ Option A ]
The statement extern int num; tells the compiler that a variable named num exists somewhere else in the program, but it does not allocate memory for it. This means it is only a declaration, not a definition. A definition would actually reserve memory, such as int num;. So extern int num; is simply a declaration.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.