Showing 461–470 of 499 questions
What will be the output of: int a = 5; int b = a < 10 ? 20 : 30; printf("%d", b);
EXPLANATION
This is the ternary operator. Since 5 < 10 is true, b = 20. If false, b would be 30.
What is the purpose of the getchar() function in C?
A
Get a string from input
B
Get a single character from input
C
Get a number from input
D
Get a pointer from input
Correct Answer:
B. Get a single character from input
EXPLANATION
getchar() reads a single character from standard input and returns it. For strings, fgets() or scanf() is used.
In C99 and later standards, what is the purpose of 'inline' keyword?
A
To declare variables inline
B
A suggestion to the compiler to inline function calls
C
To restrict variable scope
D
To allocate memory inline
Correct Answer:
B. A suggestion to the compiler to inline function calls
EXPLANATION
'inline' is a hint to the compiler suggesting it should inline the function, though the compiler may ignore it.
What will be printed by: int arr[3] = {1, 2, 3}; printf("%d", *(arr + 1));
EXPLANATION
arr + 1 points to the second element, so *(arr + 1) dereferences it to give 2.
Which of the following statements about NULL is correct?
A
NULL is a keyword in C
B
NULL is typically defined as (void *)0
C
NULL and 0 are interchangeable for pointers
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
NULL is a macro (not a keyword) typically defined as (void *)0, and can be used interchangeably with 0 for pointer assignments.
In C, what is the difference between #include and #include "stdio.h"?
A
They are identical
B
searches standard directories; "" searches current directory first
C
"" is only for user-defined headers
D
is only for system headers
Correct Answer:
B. searches standard directories; "" searches current directory first
EXPLANATION
Angle brackets search standard include directories, while quotes search the current directory first, then standard directories.
Which of the following is a correct way to initialize a character array with a string?
A
char str[] = "Hello";
B
char str[5] = "Hello";
C
char *str = "Hello";
D
All of the above
Correct Answer:
D. All of the above
EXPLANATION
All three are valid ways to work with strings in C, though they differ in memory allocation and mutability.
What will be printed by: printf("%d", sizeof(float));
A
2
B
4
C
8
D
Compiler dependent
EXPLANATION
On most systems, sizeof(float) is 4 bytes. Though technically implementation-defined, 4 bytes is standard.
What is the correct syntax to define a macro with arguments in C?
A
#define MAX(a, b) ((a) > (b) ? (a) : (b))
B
#macro MAX(a, b) ((a) > (b) ? (a) : (b))
C
#define MAX a, b ((a) > (b) ? (a) : (b))
D
define MAX(a, b) ((a) > (b) ? (a) : (b))
Correct Answer:
A. #define MAX(a, b) ((a) > (b) ? (a) : (b))
EXPLANATION
The correct syntax uses #define followed by macro name and parameters, with the replacement text in parentheses.
What does the 'static' keyword do when used with a global variable?
A
Makes it constant
B
Limits its scope to the current file
C
Allocates it on the stack
D
Requires initialization
Correct Answer:
B. Limits its scope to the current file
EXPLANATION
Static global variables have internal linkage, restricting visibility to the translation unit where they are defined.