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.2Medium
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.3Medium
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.4Medium
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.5Medium
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.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.9Medium
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.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.
Q.17Medium
What will be the memory size of the following struct?
struct Point { int x; char c; int y; }
Answer: C
Due to memory alignment/padding: int x (4 bytes), char c (1 byte) + 3 bytes padding, int y (4 bytes) = 12 bytes total. The compiler adds padding to align data members to their natural boundaries for efficient memory access.
Q.18Medium
What is the correct way to initialize an array of 5 integers with all elements as 0?
Answer: C
Both {} and {0} will initialize all array elements to 0. When you provide fewer initializers than array size, remaining elements are automatically set to 0.
Q.19Medium
Which of the following correctly declares a pointer to a pointer?
Answer: B
A pointer to a pointer is declared using two asterisks (). int ptr declares a pointer that points to another pointer that points to an integer.
Q.20Medium
What is the scope of a variable declared inside a function in C?
Answer: B
Variables declared inside a function have local scope (also called function scope). They are only accessible within that function and are destroyed when the function returns.