This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What would be output of the following program?
void main()
{
int num=45;
printf("%d %d ",num,num);
{
int num=55;
printf("%d",num);
}
}
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: 2What is the output of the below code
void main()
{
extern int num=10;
printf("%d",num);
}
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: 3When is a static variable initialized?
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: 4What is the output of the below code
int num;
void main()
{
extern int num;
printf("%d",num);
}
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: 5What would be output of the following program
void main()
{
extern int num;
num=13;
printf("%d",num);
}
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: 6What is the output of the below code
void main()
{
extern int num;
printf("%d",num);
}
int num=25;
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: 7What is the following?
extern int num;
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.
Q: 8Which variable has the longest scope in the following C code?
#include <stdio.h>
int b;
int main()
{
int c;
return 0;
}
int a;
Option D
Variable a:
Variable a is a global variable because it is declared outside all functions. It has file scope, meaning it is accessible throughout the entire source file.
Variable b:
Variable b is also declared outside all functions, so it is a global variable. Like a, it has file scope.
Variable c:
Variable c is declared inside the main() function, so it is a local variable. It has block scope and is accessible only within the main() function.
You have reached the end of this topic. Continue learning with the next topic below.
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.