Showing 1–10 of 19 questions
in Basics & Syntax
Consider the following declaration:
int (*func_ptr)(int, int, int);
Which of the following function signatures can be correctly assigned to func_ptr?
A
int add(int x, int y, int z) { return x+y+z; }
B
int add(int x, int y) { return x+y; }
C
void add(int x, int y, int z) { }
D
float add(int x, int y, int z) { return x+y+z; }
Correct Answer:
A. int add(int x, int y, int z) { return x+y+z; }
EXPLANATION
func_ptr must point to a function that takes exactly 3 int parameters and returns int. Only option A matches this signature exactly.
Which of the following correctly uses memcpy()?
A
memcpy(dest, src, strlen(src));
B
memcpy(dest, src, sizeof(src));
C
memcpy(&dest, &src, count);
D
memcpy(dest, src, count);
Correct Answer:
D. memcpy(dest, src, count);
EXPLANATION
memcpy(dest, src, count) copies 'count' bytes from src to dest. strlen() and sizeof() may not give correct results for all data types.
What is the output of: float x = 5/2; printf("%f", x);
A
2.5
B
2.0
C
2.500000
D
Error
EXPLANATION
5/2 is integer division (not float division), so result is 2 (integer), then converted to 2.0 (float).
Which of the following about function pointers in C is correct?
A
int *func();
B
int (*func)();
C
int func*();
D
int *func*();
Correct Answer:
B. int (*func)();
EXPLANATION
int (*func)(); declares a pointer to a function that returns int. Option A declares a function returning pointer to int.
What will be the output of: int a = 1; int b = 2; a = a + b; b = a - b; a = a - b; printf("%d %d", a, b);
EXPLANATION
Initial: a=1, b=2. After step 1: a=3, b=2. After step 2: a=3, b=1. After step 3: a=2, b=1. Output is '2 1'.
What is the scope of a variable declared inside a block with 'static'?
A
Global scope
B
Function scope
C
Block scope with static storage duration
D
File scope
Correct Answer:
C. Block scope with static storage duration
EXPLANATION
A static variable declared in a block has block scope but static storage duration, persisting between function calls.
What is the output of: int x = 5; int y = ++x + x++;
A
y = 12
B
y = 13
C
Undefined behavior
D
Compilation error
Correct Answer:
C. Undefined behavior
EXPLANATION
This involves undefined behavior due to modification of x multiple times without intervening sequence points.
What is the result of: int x = 10; int y = x++ + x++;
A
y = 20
B
y = 21
C
y = 22
D
Undefined behavior
Correct Answer:
D. Undefined behavior
EXPLANATION
Multiple post-increments without sequence points lead to undefined behavior.
What will be the output of: int x = 5; printf("%d", x++ + ++x);
A
11
B
12
C
10
D
Undefined behavior
Correct Answer:
D. Undefined behavior
EXPLANATION
This exhibits undefined behavior due to multiple modifications of the same variable 'x' without intervening sequence points.
Which of the following best describes the 'register' storage class in modern C compilers?
A
Guarantees storage in CPU register
B
A hint to the compiler to optimize variable storage; compiler may ignore it
C
Forces the variable to be global
D
Automatically allocates memory on the stack
Correct Answer:
B. A hint to the compiler to optimize variable storage; compiler may ignore it
EXPLANATION
The 'register' keyword is a hint for compiler optimization. Modern compilers often ignore it as they have sophisticated optimization strategies.