Correct Answer: auto
Explanation: The `auto` storage class is the default for local variables in C programming. Variables declared inside functions without any storage class specifier are automatically considered `auto`.
Correct Answer: Stack memory
Explanation: `auto` variables in C programming are typically stored in the stack memory of the function where they are declared. They are allocated dynamically during runtime and deallocated when the function exits.
Correct Answer: `auto` variables must be initialized at the point of declaration.
Explanation: `auto` variables in C programming must be initialized at the point of declaration. If not initialized explicitly, their initial value is undefined (garbage value).
Correct Answer: It is deallocated and its memory is freed.
Explanation: `auto` variables in C programming are local to the function where they are declared. They are automatically deallocated and their memory is freed when the function exits.
Correct Answer: Local to the function where they are declared.
Explanation: `auto` variables in C programming have scope limited to the function where they are declared. They are not visible or accessible outside of that function.
Correct Answer: register
Explanation: The `register` specifier is optional for declaring `auto` variables in C programming. It indicates that the variable should be stored in CPU registers for faster access, but it does not affect the storage class as `auto` is implicit.
Correct Answer: They allow efficient use of stack memory.
Explanation: `auto` variables in C programming allow efficient use of stack memory because they are allocated and deallocated dynamically during function execution.
Correct Answer: `auto` variables cannot have linkage.
Explanation: `auto` variables in C programming do not have linkage because they are local to the function where they are declared and are not accessible outside of that function.
Correct Answer: No, `static` cannot be used with `auto` variables.
Explanation: `static` and `auto` are mutually exclusive storage class specifiers in C programming. `static` cannot be used with `auto` variables because they have different meanings and storage durations.
Correct Answer: They can be declared without any storage class specifier.
Explanation: `auto` variables in C programming can be declared without explicitly using the `auto` specifier because it is the default storage class for local variables.
Correct Answer: To specify that a variable has external linkage.
Explanation: The `extern` storage class specifier in C programming is used to declare variables that have external linkage, meaning they can be accessed across different files when properly declared.
Correct Answer: Global to the file where they are declared.
Explanation: `extern` variables in C programming have scope limited to the file where they are declared. They can be accessed by other files when properly declared using `extern`.
Correct Answer: Garbage value
Explanation: If not explicitly initialized, `extern` variables in C programming are initialized to garbage values because they are not automatically initialized by the compiler.
Correct Answer: Data segment
Explanation: Memory for `extern` variables in C programming is allocated in the data segment of the program. This segment contains initialized and uninitialized global and static variables.
Correct Answer: `extern` variables have external linkage by default.
Explanation: `extern` variables in C programming have external linkage by default, allowing them to be accessed across different files when properly declared using `extern`.
Correct Answer: No, `extern` variables can only be initialized once.
Explanation: `extern` variables in C programming cannot be initialized multiple times across different files. They are typically declared in one file and initialized (if needed) in another file without re-declaring them.
Correct Answer: extern int x;
Explanation: In C programming, `extern int x;` is a valid declaration of an `extern` variable, indicating that `x` is defined elsewhere and its memory allocation is handled in another file.
Correct Answer: It will have a linker error.
Explanation: If an `extern` variable is declared without being defined (allocated memory) in any file, the linker will generate an error during the linking phase because it cannot find the variable definition.
Correct Answer: They can be declared multiple times but defined only once.
Explanation: `extern` variables in C programming can be declared multiple times in different files using `extern`, but they must be defined (allocated memory) only once in one of the files.
Correct Answer: static
Explanation: `extern` variables in C programming can be combined with the `static` storage class specifier to extend their scope across multiple files. This combination allows the variable to have internal linkage within the file where it is declared.
Correct Answer: To retain the value of a variable between function calls.
Explanation: The `static` storage class specifier in C programming is used to declare variables that retain their value between function calls, making them persistent throughout the program’s execution.
Correct Answer: Global to all functions within the file.
Explanation: `static` variables in C programming have file scope, meaning they are visible and accessible to all functions within the file where they are declared, but not outside that file.
Correct Answer: Zero
Explanation: If not explicitly initialized, `static` variables in C programming are automatically initialized to zero by the compiler. This ensures predictable behavior unless otherwise specified.
Correct Answer: Data segment
Explanation: Memory for `static` variables in C programming is allocated in the data segment of the program, alongside other global and static variables. This segment stores initialized and uninitialized static data.
Correct Answer: `static` variables have internal linkage by default.
Explanation: `static` variables in C programming have internal linkage by default, meaning they are limited to the file where they are declared and are not accessible from other files.
Correct Answer: Yes, by using the `extern` specifier.
Explanation: `static` variables in C programming can be accessed outside the function where they are declared by using the `extern` specifier along with their declaration in another file. This provides external linkage.
Correct Answer: static int x;
Explanation: In C programming, `static int x;` is a valid declaration of a `static` variable. It indicates that `x` has internal linkage and retains its value between function calls.
Correct Answer: It retains its value between function calls.
Explanation: `static` variables declared inside a function in C programming retain their value between function calls. They are initialized only once and persist throughout the program’s execution.
Correct Answer: They cannot be declared inside a loop.
Explanation: `static` variables in C programming cannot be declared inside a loop because their initialization and lifetime are managed differently from variables declared inside loops, which are created and destroyed with each iteration.
Correct Answer: auto
Explanation: `static` variables in C programming can be combined with the `auto` storage class specifier to limit their scope to a single function. This combination ensures that the variable is local to the function and retains its value between calls.
Correct Answer: To optimize the access speed of variables stored in CPU registers.
Explanation: The `register` storage class specifier in C programming is used to suggest that a variable should be stored in CPU registers for faster access, although modern compilers may ignore this suggestion.
Correct Answer: Local to the function where they are declared.
Explanation: `register` variables in C programming have local scope, similar to `auto` variables, and are limited to the function where they are declared. They are not accessible outside of that function.
Correct Answer: It retains its value between function calls.
Explanation: If a `register` variable is declared with the `static` specifier in C programming, it retains its value between function calls. This behavior combines the speed of register storage with the persistence of `static` variables.
Correct Answer: CPU registers
Explanation: `register` variables in C programming are stored in CPU registers, not in memory locations like stack, heap, or data segment. This storage class is intended for optimizing variable access speed.
Correct Answer: `register` variables have no linkage.
Explanation: `register` variables in C programming have no linkage because they are local to the function where they are declared and are stored in CPU registers for optimized access.
Correct Answer: Yes, using the `&` operator.
Explanation: Although `register` variables in C programming are stored in CPU registers and may not have a memory address in some cases, their address can still be accessed using the `&` operator to obtain the memory address.
Correct Answer: register int x;
Explanation: In C programming, `register int x;` is a valid declaration of a `register` variable. It suggests that `x` should be stored in a CPU register for faster access.
Correct Answer: It becomes accessible across multiple files.
Explanation: If a `register` variable is declared with the `extern` specifier in C programming, it indicates that the variable is defined in another file and can be accessed from multiple files, extending its scope.
Correct Answer: They must be declared at the beginning of a function.
Explanation: `register` variables in C programming must be declared at the beginning of a function. This requirement ensures that the compiler can allocate them to CPU registers for optimized access.
Correct Answer: extern
Explanation: `register` variables in C programming can be combined with the `extern` storage class specifier to specify external linkage, allowing them to be accessed across multiple files when properly declared.
Correct Answer:
Explanation: &The bitwise AND operator in C is represented by `&` and performs bitwise AND operation between corresponding bits of two operands.
Correct Answer: 1
Explanation: In bitwise AND operation, `5 & 3` results in `1` because in binary, `5` is `101` and `3` is `011`, their AND gives `001`.
Correct Answer: |
Explanation: The bitwise OR operator in C is represented by `|` and performs bitwise OR operation between corresponding bits of two operands.
Correct Answer: 7
Explanation: In bitwise OR operation, `5 | 3` results in `7` because in binary, `5` is `101` and `3` is `011`, their OR gives `111`.
Correct Answer: ^
Explanation: The bitwise XOR (exclusive OR) operator in C is represented by `^` and performs bitwise XOR operation between corresponding bits of two operands.
Correct Answer: 2
Explanation: In bitwise XOR operation, `5 ^ 3` results in `2` because in binary, `5` is `101` and `3` is `011`, their XOR gives `110`.
Correct Answer: ~
Explanation: The bitwise NOT (complement) operator in C is represented by `~` and performs bitwise negation (complement) of a single operand.
Correct Answer: -4
Explanation: In bitwise NOT operation, `~5` results in `-6` because it negates each bit of `5` (which in binary is `101`), resulting in `-110` in two’s complement representation, which is `-6` in decimal.
Correct Answer: <<
Explanation: The left shift operator in C is represented by `<<` and shifts the bits of the left operand to the left by a number of positions specified by the right operand.
Correct Answer: 20
Explanation: In left shift operation, `5 << 2` shifts the binary representation of `5` (`101`) two positions to the left, resulting in `10100` which is `20` in decimal.
Correct Answer: <<
Explanation: The left shift operator in C is represented by `<<` and shifts the bits of the left operand to the left by a number of positions specified by the right operand.
Correct Answer: 20
Explanation: In left shift operation, `5 << 2` shifts the binary representation of `5` (`101`) two positions to the left, resulting in `10100` which is `20` in decimal.
Correct Answer: 64
Explanation: In left shift operation, `8 << 3` shifts the binary representation of `8` (`1000`) three positions to the left, resulting in `1000000` which is `64` in decimal.
Correct Answer: >>
Explanation: The right shift operator in C is represented by `>>` and shifts the bits of the left operand to the right by a number of positions specified by the right operand.
Correct Answer: 4
Explanation: In right shift operation, `16 >> 2` shifts the binary representation of `16` (`10000`) two positions to the right, resulting in `100` which is `4` in decimal.
Correct Answer: -4
Explanation: In right shift operation, `-16 >> 2` shifts the binary representation of `-16` (using two’s complement) two positions to the right, resulting in `-100` which is `-4` in decimal.
Correct Answer: >>
Explanation: The `>>` operator in C performs arithmetic right shift when used with signed integers, preserving the sign bit and shifting zeros into the most significant bits.
Correct Answer: 7
Explanation: In arithmetic right shift operation, `15 >> 1` shifts the binary representation of `15` (`1111`) one position to the right, resulting in `111` which is `7` in decimal.
Correct Answer: It remains negative.
Explanation: In C, right shifting a negative number (using arithmetic right shift) preserves the sign bit, so the number remains negative after shifting.
Correct Answer:
Explanation: &The bitwise AND (`&`) operator can be used in combination with a mask to clear lower order bits in a number while preserving higher order bits.
Correct Answer: Clearing specific bits in a bit pattern
Explanation: Bit masking in C programming involves using bitwise operators to manipulate specific bits within a bit pattern, such as clearing bits, setting bits, or checking bit values.
Correct Answer:
Explanation: &The bitwise AND (`&`) operator is commonly used to create a mask in C programming by combining a bit pattern with another mask pattern to selectively manipulate bits.
Correct Answer: All bits set to 1 in the lower byte
Explanation: In hexadecimal notation, `0xFF` represents a byte with all bits set to 1 in the lower byte, which is commonly used as a mask to select or clear specific bits.
Correct Answer: `x &= 0x0F;`
Explanation: To clear the lower 4 bits of a variable `x`, you can use the bitwise AND operator with `0x0F` (which represents `00001111` in binary), effectively masking off the lower bits.
Correct Answer: Bitwise OR (|)
Explanation: The bitwise OR (`|`) operator can be used to set specific bits in a variable without affecting other bits, by combining the variable with a mask where the desired bits are set to 1.
Correct Answer: 0xBF
Explanation: If `x` is `0x3F` (binary `00111111`), then `x | 0x80` (binary `10000000`) results in `10111111` which is `0xBF` in hexadecimal.
Correct Answer: ~
Explanation: The bitwise NOT (`~`) operator inverts all bits of a variable in C programming, turning 0s into 1s and 1s into 0s.
Correct Answer: 0xA5
Explanation: If `0x5A` is `01011010` in binary, then `~0x5A` (inverting all bits) results in `10100101` which is `0xA5` in hexadecimal.
Correct Answer:
Explanation: &The bitwise AND (`&`) operator is used to check if a specific bit is set in a variable by combining the variable with a mask where only the desired bit is set to 1.
Correct Answer: 0x10
Explanation: If `x` is `0x1A` (binary `00011010`), then `x & 0x10` (binary `00010000`) results in `00010000` which is `0x10` in hexadecimal.
Correct Answer: Bitwise OR (|)
Explanation: The bitwise OR (`|`) operator can be used to set specific bits in a variable without affecting other bits, by combining the variable with a mask where the desired bits are set to 1.
Correct Answer: 0x17
Explanation: If `x` is `0x0F` (binary `00001111`), then `1 << 3` shifts `1` three positions to the left, becoming `00001000` in binary. Thus, `x | (1 << 3)` results in `00001111 | 00001000 = 00001111` which is `0x17` in hexadecimal.
Correct Answer: Bitwise AND (&)
Explanation: The bitwise AND (`&`) operator can be used to clear specific bits in a variable without affecting other bits, by combining the variable with a mask where the desired bits are set to 0.
Correct Answer: 0x1B
Explanation: If `x` is `0x1F` (binary `00011111`), then `1 << 2` shifts `1` two positions to the left, becoming `00000100` in binary. `~(1 << 2)` results in `11111011` in binary. Thus, `x & ~(1 << 2)` results in `00011111 & 11111011 = 00011011` which is `0x1B` in hexadecimal.
Correct Answer: Bitwise XOR (^)
Explanation: The bitwise XOR (`^`) operator can be used to toggle specific bits in a variable without affecting other bits, by combining the variable with a mask where the desired bits are set to 1.
Correct Answer: 0x0B
Explanation: If `x` is `0x0A` (binary `00001010`), then `1 << 1` shifts `1` one position to the left, becoming `00000010` in binary. Thus, `x ^ (1 << 1)` results in `00001010 ^ 00000010 = 00001000` which is `0x0B` in hexadecimal.
Correct Answer: Bitwise AND (&)
Explanation: The bitwise AND (`&`) operator can be used to check if a specific bit is set in a variable by combining the variable with a mask where only the desired bit is set to 1.
Correct Answer: 0x04
Explanation: If `x` is `0x14` (binary `00010100`), then `1 << 2` shifts `1` two positions to the left, becoming `00000100` in binary. Thus, `x & (1 << 2)` results in `00010100 & 00000100 = 00000100` which is `0x04` in hexadecimal.
Correct Answer: Bitwise NOT (~)
Explanation: The bitwise NOT (`~`) operator inverts all bits of a variable in C programming, turning 0s into 1s and 1s into 0s.
Correct Answer: 0xA5
Explanation: If `0x5A` is `01011010` in binary, then `~0x5A` (inverting all bits) results in `10100101` which is `0xA5` in hexadecimal.
#include <stdio.h>
int main() {
int x = 10;
if (x++ == 10) {
printf("Hello, ");
} else {
printf("Hi, ");
}
printf("world!\n");
return 0;
}
Correct Answer: Hello, world!
Explanation: The postfix increment operator (`x++`) evaluates to the current value of `x` and then increments `x`. Therefore, `x++ == 10` evaluates to true (`10 == 10`), so “Hello, ” is printed followed by “world!”.
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", x++);
printf("%d\n", ++x);
return 0;
}
Correct Answer: 5, 6
Explanation: In the first `printf`, `x++` post-increments `x` after printing its current value (`5`). In the second `printf`, `++x` pre-increments `x` before printing its incremented value (`6`).
#include <stdio.h>
int main() {
int x = 3;
x += x -= x * x;
printf("%d\n", x);
return 0;
}
Correct Answer: 3
Explanation: The expression `x += x -= x * x;` is evaluated as `x = x + (x – (x * x));`. Substituting `x = 3`, it becomes `x = 3 + (3 – (3 * 3));` which simplifies to `x = 3 + (3 – 9);` and finally `x = 3 + (-6);` resulting in `x = 3`.
#include <stdio.h>
void func(int x) {
if (x > 0) {
func(--x);
printf("%d ", x);
func(--x);
}
}
int main() {
func(3);
return 0;
}
Correct Answer: 0 1 2 0 1 0
Explanation: The function `func` is recursively called with decremented `x` values. It prints `x` when `x > 0` in the order of its calls, resulting in `0 1 2 0 1 0`.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d ", *(ptr++));
printf("%d ", *ptr);
return 0;
}
Correct Answer: 10 20
Explanation: Initially, `ptr` points to `arr[0]`. `*(ptr++)` dereferences `ptr` before incrementing, printing `10`. After `ptr++`, `ptr` points to `arr[1]`, so `*ptr` prints `20`.
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 15;
int *p[] = {&a, &b, &c};
printf("%d ", *p[0]);
printf("%d ", **(p + 1));
printf("%d ", **(p + 2));
return 0;
}
Correct Answer: 5 10 15
Explanation: `p` is an array of pointers to integers. `*p[0]` dereferences the first element of `p` (`&a`), printing `5`. `**(p + 1)` dereferences the second element (`&b`), printing `10`. `**(p + 2)` dereferences the third element (`&c`), printing `15`.
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
char *ptr = str;
printf("%c ", *ptr++);
printf("%c ", *(++ptr));
printf("%c ", *(ptr + 2));
return 0;
}
Correct Answer: H e o
Explanation: `*ptr++` prints the character pointed by `ptr` (`H`), then increments `ptr`. `*(++ptr)` increments `ptr` first, then prints the character (`e`). `*(ptr + 2)` prints the character two positions ahead (`o`).
#include <stdio.h>
int main() {
int i = 0;
for (i = 0; i < 5; i++) {
printf("%d ", i);
continue;
printf("Hello ");
}
return 0;
}
Correct Answer: 0 1 2 3 4
Explanation: The `printf("%d ", i);` statement is executed in the loop body. `continue;` skips the remaining code in the loop body (`printf("Hello ");`) and proceeds to the next iteration.
#include <stdio.h>
int main() {
int x = 10;
{
int x = 20;
printf("%d ", x);
}
printf("%d ", x);
return 0;
}
Correct Answer: 20 10
Explanation: Inside the inner block, `x` is `20` and is printed. Outside the block, `x` is `10` (the outer `x`), which is printed next.
#include <stdio.h>
int main() {
int i = 1;
printf("%d ", ++i + ++i);
return 0;
}
Correct Answer: 6
Explanation: The expression `++i + ++i` is evaluated as `(2 + 3)`, where `++i` increments `i` to `2` and `++i` increments `i` to `3`. Thus, `2 + 3 = 5`.
#include <stdio.h>
int main() {
int i = 0;
for (; i < 3; i++) {
switch (i) {
case 0: printf("0 ");
case 1: printf("1 ");
case 2: printf("2 ");
}
}
return 0;
}
Correct Answer: 0 1 2 1 2
Explanation: The `switch` statement falls through each case after matching `i`. After `i = 0`, "0 " is printed. After `i = 1`, "1 " and "2 " are printed (falling through). After `i = 2`, "2 " is printed.
#include <stdio.h>
int main() {
int x = 5;
printf("%d ", ++x++);
return 0;
}
Correct Answer: Compilation Error
Explanation: The expression `++x++` is invalid because it attempts to increment `x` twice in the same sequence point, which is not allowed in C.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d ", *ptr + 2);
printf("%d ", *(ptr + 1));
return 0;
}
Correct Answer: 12 20
Explanation: `*ptr + 2` adds `2` to the value pointed by `ptr` (`10`), resulting in `12`. `*(ptr + 1)` dereferences the second element of `arr`, printing `20`.
#include <stdio.h>
int main() {
int x = 2;
int y = x << 2 + 1;
printf("%d ", y);
return 0;
}
Correct Answer: 10
Explanation: Operator precedence makes `x << 2 + 1` equivalent to `x << (2 + 1)`, so `x << 3`. If `x` is `2`, then `2 << 3` is `16`.
#include <stdio.h>
#define SQUARE(x) x * x
int main() {
int y = 4 / SQUARE(2);
printf("%d ", y);
return 0;
}
Correct Answer: 4
Explanation: The macro `SQUARE(x)` expands to `x * x`. So, `4 / SQUARE(2)` expands to `4 / 2 * 2`, which evaluates as `(4 / 2) * 2 = 2 * 2 = 4`.
#include <stdio.h>
int main() {
int a[] = {1, 2, 3, 4, 5};
int *ptr = (int*)(&a + 1);
printf("%d ", *(ptr - 1));
return 0;
}
Correct Answer: 5
Explanation: `&a + 1` gives the address just after the end of array `a`. Casting it to `int*` makes `ptr` point to this address. `*(ptr - 1)` then accesses the last element of `a`, which is `5`.
#include <stdio.h>
int main() {
int x = 5;
x = x == 5 ? 10 : 15;
printf("%d ", x);
return 0;
}
Correct Answer: 10
Explanation: `x == 5 ? 10 : 15` evaluates to `10` because `x` is `5`. So, `x` is assigned `10` and printed.
#include <stdio.h>
int main() {
int i = 10;
int *ptr = &i;
printf("%d ", *ptr++);
printf("%d ", *ptr);
return 0;
}
Correct Answer: 10 10
Explanation: `*ptr++` dereferences `ptr` before incrementing it, printing `10`. After `ptr++`, `ptr` points to `i`, so `*ptr` also prints `10`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
*ptr++;
printf("%d ", x);
return 0;
}
Correct Answer: 10
Explanation: `*ptr++` post-increments `ptr`, but does not affect `x` because `*ptr` is not assigned any value. So, `printf("%d ", x);` prints `10`.
#include <stdio.h>
int main() {
char arr[] = "Hello";
printf("%c ", *arr++);
printf("%s ", arr);
return 0;
}
Correct Answer: e llo
Explanation: `*arr++` prints `H`. After `arr++`, `arr` now points to `ello`. So, `printf("%s ", arr);` prints `ello`.