Which loop construct in C guarantees execution at least once?
Awhile loop
Bfor loop
Cdo-while loop
Dnested loop
Correct Answer:
C. do-while loop
Explanation:
The do-while loop executes the body at least once before checking the condition. The syntax is: do { statements; } while(condition);. In contrast, while and for loops check the condition first.
What is the scope of a variable declared inside a block?
AGlobal scope
BLocal scope (block scope)
CStatic scope
DExternal scope
Correct Answer:
B. Local scope (block scope)
Explanation:
Variables declared inside a block (enclosed in curly braces) have local or block scope. They are accessible only within that block and cease to exist once the block execution is complete.
How many times will the following loop execute: for(int i=0; i
A2 times
B3 times
C4 times
D5 times
Correct Answer:
B. 3 times
Explanation:
The loop executes for i=0, i=1, and i=2. When i=3, the break statement is encountered, which immediately terminates the loop. Therefore, the loop executes exactly 3 times.
Which of the following is a correct way to initialize a pointer to NULL?
Aint *ptr = 0;
Bint *ptr = NULL;
Cint *ptr = (void*)0;
DAll of the above
Correct Answer:
D. All of the above
Explanation:
All three methods are correct ways to initialize a pointer to NULL. 0, NULL, and (void*)0 all represent a null pointer. NULL is typically a macro defined as 0 or ((void*)0) in the standard library.
Which of the following correctly describes the relationship between arrays and pointers in C?
AArrays and pointers are identical
BAn array name decays to a pointer to its first element
CPointers can only be used with dynamically allocated arrays
DArrays are pointers stored on the stack
Correct Answer:
B. An array name decays to a pointer to its first element
Explanation:
In C, when an array name is used in most contexts, it decays (converts) to a pointer to its first element. For example, in int arr[5];, arr behaves like &arr[0]. However, arrays and pointers are not identical; arrays have fixed size while pointers are variables.
What is the output of the expression: 10 / 3 * 3 in C?
A10
B9
C3.33
DCannot be determined
Correct Answer:
B. 9
Explanation:
Division and multiplication have the same precedence and are evaluated left-to-right. Step 1: 10 / 3 = 3 (integer division). Step 2: 3 * 3 = 9. Therefore, the result is 9.
Which preprocessor directive is used to include a custom header file?
A#include
B#include "filename"
C#include [filename]
D#include {filename}
Correct Answer:
B. #include "filename"
Explanation:
Custom/local header files are included using double quotes: #include "filename". Standard library headers use angle brackets: #include <filename>. The compiler searches for quoted files in the current directory first.
What is the difference between a function declaration and a function definition in C?
ADeclaration provides the function signature; definition provides the implementation
BThey are the same thing
CDeclaration is optional; definition is mandatory
DDefinition must appear before declaration
Correct Answer:
A. Declaration provides the function signature; definition provides the implementation
Explanation:
A function declaration (or prototype) tells the compiler about the function's name, return type, and parameters. A function definition includes the declaration along with the function body containing the actual implementation. Declaration is optional if the function is defined before use.
Which of the following is the correct way to declare a pointer to a function that returns an int and takes two int parameters?
Aint *ptr(int, int);
Bint (*ptr)(int, int);
Cint *ptr[int, int];
Dint ptr*(int, int);
Correct Answer:
B. int (*ptr)(int, int);
Explanation:
The correct syntax for a function pointer is: int (*ptr)(int, int);. The parentheses around (*ptr) are necessary to indicate that ptr is a pointer to a function, not a function returning a pointer. Option A declares a function returning an int pointer.