What is the output of the following C code?
int x = 5;
printf("%d", x++);
Answer: B
The post-increment operator (x++) returns the current value of x before incrementing. So printf prints 5, and then x becomes 6.
Q.2Easy
Which of the following is the correct syntax to declare a pointer in C?
Answer: A
In C, pointers are declared using the asterisk (*) symbol before the variable name. The syntax is 'datatype *pointer_name;'
Q.3Easy
What will be the size of the following array in bytes?
char arr[10];
Answer: A
Each char occupies 1 byte in memory. An array of 10 chars will occupy 10 × 1 = 10 bytes.
Q.4Easy
What is the purpose of the 'void' keyword in C?
Answer: C
void is used in two contexts: (1) as a function return type when a function doesn't return a value, and (2) to declare a generic pointer (void *) that can point to any data type.
Q.5Easy
What is the output of the following C code?
int a = 10, b = 20;
int c = (a > b) ? a : b;
printf("%d", c);
Answer: B
The ternary operator (condition ? true_value : false_value) evaluates the condition (a > b). Since 10 is not greater than 20, it returns b which is 20.
Advertisement
Q.6Easy
What does the strlen() function return?
Answer: B
strlen() returns the length of a string without counting the null terminator ('\0'). For example, strlen("hello") returns 5, not 6.
Q.7Medium
What is the output of the following C code?
int x = 5;
int y = ++x + x++;
printf("%d", y);
Answer: D
This code exhibits undefined behavior because x is modified twice between sequence points without an intervening sequence point. The result depends on the compiler's implementation.
Q.8Medium
What is the difference between scanf() and gets() functions?
Answer: A
scanf() reads formatted input based on format specifiers and stops at whitespace. gets() reads a string until a newline is encountered. Note: gets() is deprecated due to buffer overflow vulnerabilities.
Q.9Medium
What is the correct way to allocate memory for a single integer using malloc()?
Answer: D
Both A and B are correct. Option A uses explicit type casting which is optional in C (not in C++). Option B avoids casting. Using sizeof(int) is preferred over hardcoding 4, as int size may vary across systems.
Q.10Medium
What is the output of the following C code?
int x = 5;
int *ptr = &x;
printf("%d %d", *ptr, x);
Answer: A
*ptr dereferences the pointer to access the value at the address it points to, which is x = 5. So both *ptr and x print 5.
Q.11Medium
What happens when you use the strcpy() function without bounds checking?
Answer: B
strcpy() does not perform bounds checking. If the source string is longer than the destination buffer, it will write beyond the buffer boundary, causing a buffer overflow. This is a security vulnerability. Using strncpy() is safer.
Q.12Medium
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.13Medium
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.14Medium
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.15Hard
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.16Hard
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.
Q.17Hard
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.18Hard
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.19Hard
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.20Easy
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().