Which loop construct will execute at least once even if the condition is false?
Awhile loop
Bfor loop
Cdo-while loop
DAll of the above
Correct Answer:
C. do-while loop
Explanation:
The do-while loop executes the body first and then checks the condition, ensuring at least one execution. while and for loops check the condition first.
What is the difference between #include and #include "stdio.h"?
AThey are identical
B searches in system directories, "" searches in current directory first
C"" is for user-defined headers, is for system headers
DBoth B and C are correct
Correct Answer:
D. Both B and C are correct
Explanation:
Using <> tells the compiler to search in standard system directories. Using "" tells it to search in the current directory first, then system directories. Generally, "" is used for user-defined headers and <> for standard library headers.
Consider the code: int *p; int arr[5]; p = arr; What does p[2] represent?
AThe address of arr[2]
BThe value at arr[2]
CThe pointer itself
DA syntax error
Correct Answer:
B. The value at arr[2]
Explanation:
When p points to arr, p[2] is equivalent to *(p+2) and accesses the value at arr[2]. Pointers and array names decay to pointers, and pointer indexing retrieves the value.
Which of the following will correctly allocate memory for an array of 10 integers?
Aint *arr = malloc(10);
Bint *arr = malloc(10 * sizeof(int));
Cint arr[10] = malloc(10);
Dint *arr = calloc(10);
Correct Answer:
B. int *arr = malloc(10 * sizeof(int));
Explanation:
malloc() allocates memory in bytes. To allocate for 10 integers, we need 10 * sizeof(int) bytes. Option A allocates only 10 bytes, insufficient for 10 integers.
What will be the output of: int x = 5; int y = x++ + ++x; ?
Ay = 11
By = 12
Cy = 10
DUndefined behavior
Correct Answer:
D. Undefined behavior
Explanation:
This expression involves modifying the same variable (x) multiple times without an intervening sequence point. This is undefined behavior in C, and the result cannot be predicted reliably.
Which function is used to read a single character from standard input?
Ascanf()
Bgetchar()
Cgets()
Dfgetc()
Correct Answer:
B. getchar()
Explanation:
getchar() reads a single character from standard input (stdin). While fgetc() can also read a character, getchar() is the standard dedicated function for this purpose.