#include <stdio.h>
int main() {
int a = 5, b = 7;
int sum;
sum = a + b;
printf("Sum of %d and %d is: %d\n", a, b, sum);
return 0;
}
Correct Answer: 12
Explanation: The sum of ‘a’ and ‘b’ is assigned to ‘sum’, resulting in 12.
#include <stdio.h>
int main() {
const int AGE = 30;
printf("Age: %d\n", AGE);
return 0;
}
Correct Answer: 30
Explanation: The constant variable `AGE` is initialized to 30 and cannot be modified.
#include <stdio.h>
#define PI 3.14159
int main() {
const float RADIUS = 5.0;
float area = PI * RADIUS * RADIUS;
printf("Area of Circle: %.2f\n", area);
return 0;
}
Correct Answer: Depends on the value of ‘PI’
Explanation: The variable ‘area’ is calculated using the constant ‘PI’ and ‘RADIUS’.
#include <stdio.h>
int globalVariable = 10;
void func() {
printf("Global Variable: %d\n", globalVariable);
}
int main() {
func();
printf("Global Variable from main: %d\n", globalVariable);
return 0;
}
Correct Answer: Outside of any function
Explanation: The global variable `globalVariable` is declared outside of any function, making it accessible globally.
#include <stdio.h>
void func() {
int localVariable = 20;
printf("Local Variable: %d\n", localVariable);
}
int main() {
func();
return 0;
}
Correct Answer: 20
Explanation: The variable ‘localVariable’ is initialized to 20 inside the function ‘func()’, making its value 20 within the scope of the function.
#include <stdio.h>
int main() {
printf("Global Variable from main: %d\n", globalVariable);
return 0;
}
Correct Answer: It will result in a compilation error.
Explanation: ‘localVariable’ is not accessible outside the ‘func()’ function, so attempting to access it in ‘main()’ will result in a compilation error.
#include <stdio.h>
int main() {
int globalVariable = 10;
printf("Global Variable from main: %d\n", globalVariable);
return 0;
}
Correct Answer: It will print the value of the local variable.
Explanation: When a local variable shares the same name as a global variable, the local variable takes precedence inside its scope.
#include <stdio.h>
int main() {
int a = 5;
{
int a = 10;
printf("Inner 'a': %d\n", a);
}
printf("Outer 'a': %d\n", a);
return 0;
}
Correct Answer: 10
Explanation: The inner block has its own scope, so the inner ‘a’ variable with the value 10 exists only within that block.
#include <stdio.h>
int main() {
int x = 5;
if (x == 5) {
int y = 10;
printf("'y' inside if block: %d\n", y);
}
// printf("'y' outside if block: %d\n", y); // Uncommenting this line would result in a compilation error
return 0;
}
Correct Answer: It will result in a compilation error.
Explanation: Variables declared within an if block are not accessible outside of it.
#include <stdio.h>
int main() {
const int a = 5;
a = 10;
printf("Value of 'a': %d\n", a);
return 0;
}
Correct Answer: It will result in a compilation error.
Explanation: Constants cannot be modified once they are assigned a value. Any attempt to modify them will result in a compilation error.