In C programming, when you declare a variable as 'const int x = 5;', what happens if you try to modify it within the program?
AThe compiler will generate a warning but allow modification at runtime
BThe compiler will generate a compilation error and prevent modification
CThe variable will be modified but will revert to original value after each operation
DThe modification will be allowed only in the main() function
Correct Answer:
B. The compiler will generate a compilation error and prevent modification
EXPLANATION
The 'const' keyword creates a read-only variable. Once initialized, attempting to modify a const variable results in a compilation error (error: assignment of read-only variable). This is enforced at compile-time, not runtime.
What happens when a volatile variable is declared in a multi-threaded C program?
AIt becomes thread-safe automatically
BCompiler won't optimize it away and will fetch value from memory each time
CIt can only be accessed by one thread
DIt doubles in size for redundancy
Correct Answer:
B. Compiler won't optimize it away and will fetch value from memory each time
EXPLANATION
volatile tells compiler not to optimize variable accesses and to fetch fresh value from memory, useful for hardware registers and shared memory, but doesn't guarantee thread-safety without synchronization.
Consider a scenario where you need to store a variable that can hold values up to 10 billion. Which data type is most suitable?
Aint
Blong int
Clong long int
Dfloat
Correct Answer:
C. long long int
EXPLANATION
int max is ~2.1 billion, long int varies (may be 32 or 64 bits). long long int guarantees 64 bits with max ~9.2 quintillion, sufficient for 10 billion.
Which storage class has both 'static' and 'auto' properties in certain contexts?
Aregister
Bextern
Cstatic
Dvolatile
Correct Answer:
A. register
EXPLANATION
register requests compiler to store variable in CPU register for faster access but falls back to memory if unavailable, combining automatic allocation with optimization hints.
Which keyword is used to declare a variable that cannot be modified after initialization?
Astatic
Bvolatile
Cconst
Dextern
Correct Answer:
C. const
EXPLANATION
The 'const' keyword declares a constant variable whose value cannot be modified after initialization. Attempting to modify it results in a compile-time error.