What is the output of the following C code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
Answer: B
ptr points to arr[0]. ptr + 1 points to arr[1]. *(ptr + 1) dereferences to get the value at arr[1] which is 20. Pointer arithmetic adds sizeof(int) to the address for each increment.
Q.22Medium
Which of the following correctly describes the scope of a static variable declared inside a function?
Answer: A
A static variable declared inside a function has local scope (visible only within that function) but persists for the entire program lifetime. Its value is retained between function calls and is initialized only once.
Q.23Medium
What will be the output of the following C code?
int x = 10;
int y = 20;
int z = x < y ? x++ : y++;
printf("%d %d %d", x, y, z);
Answer: B
Step 1: Evaluate condition x < y → 10 < 20 → true. Step 2: Execute true branch: x++ returns 10, then x becomes 11. Step 3: z = 10. Step 4: printf prints x=11, y=20, z=10.
Q.24Hard
What is the output of the following C code?
#include <stdio.h>
int main() {
int a = 5;
printf("%d %d %d", a++, ++a, a);
return 0;
}
Answer: D
This code contains undefined behavior because variable 'a' is modified multiple times (a++, ++a) without intervening sequence points in the same expression. The order of evaluation is unspecified, making the result compiler-dependent.
Q.25Hard
What is the correct way to declare a constant pointer to a constant integer?
Answer: D
Both declarations are equivalent. 'const int * const ptr' and 'int const * const ptr' declare a constant pointer to a constant integer. The first const makes the integer constant, the second const makes the pointer constant.
Advertisement
Q.26Hard
Consider the following C code. What will be printed?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
Answer: B
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.
Q.27Hard
What is the output of the following C code?
#define MAX 5
int main() {
int arr[MAX];
printf("%d", sizeof(arr)/sizeof(arr[0]));
return 0;
}
Answer: B
Step 1: sizeof(arr) = 5 × sizeof(int) = 20 bytes (assuming 4-byte int). Step 2: sizeof(arr[0]) = sizeof(int) = 4 bytes. Step 3: 20 ÷ 4 = 5. This calculates the number of elements in the array.
Q.28Hard
What is the difference between struct and union in C?
Answer: B
In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.
Q.29Easy
Which header file is required to use the printf() function in C?
Answer: B
The stdio.h header file contains declarations for standard input/output functions like printf() and scanf().
Q.30Easy
Which of the following is a valid variable name in C?
Answer: B
Variable names in C must start with a letter or underscore, not a digit. _variable is valid. '2var' starts with digit, 'var-name' contains hyphen (invalid), 'var name' contains space (invalid).
Q.31Easy
What is the return type of the strlen() function?
Answer: C
The strlen() function returns the length of a string as an int value, representing the number of characters.
Q.32Easy
Which operator has the highest precedence in C?
Answer: C
Parentheses () have the highest precedence among all operators in C. They are always evaluated first in any expression.
Q.33Medium
In C, a pointer variable stores which of the following?
Answer: B
A pointer is a variable that stores the memory address of another variable. It is declared using the * symbol.
Q.34Medium
What is the purpose of the malloc() function in C?
Answer: B
malloc() (memory allocation) allocates a block of memory dynamically during program execution and returns a pointer to it.
Q.35Medium
Which of the following correctly initializes an array of 5 integers?
Answer: A
Arrays in C are declared with square brackets containing the size, followed by initialization in curly braces. Option A is the correct syntax.
Q.36Medium
What is the default return type of a function in C if not explicitly specified?
Answer: B
In C, if a function's return type is not explicitly specified, it defaults to int. However, modern C standards require explicit return type declaration.
Q.37Medium
What does the break statement do in a loop?
Answer: B
The break statement immediately terminates the loop and transfers control to the statement following the loop.
Q.38Medium
Which of the following is used to access members of a structure using a pointer?
Answer: B
The arrow operator (->) is used to access structure members through a pointer. The dot operator (.) is used for direct access.
Q.39Medium
In C, what is the purpose of the #define directive?
Answer: B
#define is a preprocessor directive used to define symbolic constants (macros) and performs text substitution before compilation.
Q.40Medium
What is the correct way to declare a function that takes no parameters and returns an integer?
Answer: B
To explicitly declare a function with no parameters in C, use 'void' as the parameter. int func(); is ambiguous in older C standards.