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.22Medium
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.23Medium
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.24Medium
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.
Q.25Medium
What is the difference between calloc() and malloc() in C?
Answer: D
calloc() allocates memory and initializes all bytes to 0, takes (number of elements, size of each element). malloc() just allocates memory without initialization and takes total size. calloc() is slightly slower due to initialization.
Advertisement
Q.26Medium
What is the correct syntax to open a file in C?
Answer: A
The correct syntax is FILE *fp = fopen("filename", "mode");. fopen() returns a pointer to a FILE structure, and the mode string specifies how to open the file ("r" for read, "w" for write, etc.).
Q.27Medium
Consider the following code:
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
What is the value of *(ptr + 2)?
Answer: C
ptr points to arr[0]. ptr + 2 points to arr[2]. *(ptr + 2) dereferences to get the value at arr[2], which is 3. Pointer arithmetic moves by the size of the data type (integers).
Q.28Medium
What is the output of the following code?
for(int i = 1; i <= 3; i++) { if(i == 2) continue; printf("%d ", i); }
Answer: B
When i = 1, it prints 1. When i = 2, continue skips the printf() and moves to the next iteration. When i = 3, it prints 3. Output: 1 3
Q.29Medium
What is the output of: int x = 10; printf("%d", x++); printf("%d", x);
Answer: B
x++ is post-increment. First printf uses current value (10), then x is incremented. Second printf uses new value (11). Output: 1011
Q.30Medium
Which of the following correctly declares a pointer to an integer?
Answer: A
The correct syntax for declaring a pointer to an integer is 'int *ptr;' where the asterisk (*) indicates that ptr is a pointer. The placement of * before the variable name is crucial.
Q.31Medium
What will be printed if char ch = 'A'; printf("%d", ch); is executed?
Answer: B
When %d format specifier is used with a char variable, it prints the ASCII value. The ASCII value of 'A' is 65. If it were 'a', the value would be 97.
Q.32Medium
Which of the following is used to dynamically allocate memory in C?
Answer: C
malloc() (memory allocation) is the standard function in C for dynamic memory allocation. It returns a void pointer to the allocated memory. The syntax is: ptr = (type*)malloc(size);
Q.33Medium
What will be the output of: char str[] = "Hello"; printf("%c", str[1]);?
Answer: B
Array indexing in C starts from 0. str[0]='H', str[1]='e', str[2]='l', str[3]='l', str[4]='o', str[5]='\0'. Therefore, str[1] is 'e'.
Q.34Medium
Which function is used to compare two strings in C?
Answer: A
strcmp() is the standard library function used to compare two strings. It returns 0 if strings are equal, negative if first string is less, and positive if first string is greater. It's declared in string.h.
Q.35Medium
What is the output of the following: int x = 5; int *p = &x; printf("%d", *p);?
Answer: B
p is a pointer to x, so &x gives the address of x. *p (dereferencing) gives the value stored at that address, which is 5. The output will be 5.
Q.36Medium
What will be the output of: int x = 5; x += 3; printf("%d", x);?
Answer: B
The operator += means x = x + 3. So x = 5 + 3 = 8. The printf statement outputs 8.
Q.37Medium
Which loop in C does NOT check the condition before executing the loop body?
Answer: C
The do-while loop executes the loop body at least once before checking the condition. Other loops check the condition before execution. Syntax: do { } while(condition);
Q.38Medium
What will be the output of: int x = 5; int y = ++x; printf("%d %d", x, y);?
Answer: B
The pre-increment operator (++x) increments x first, then assigns it. So x becomes 6, and y is assigned 6. Output: 6 6.
Q.39Medium
Which function is used to allocate memory dynamically in C?
Answer: B
malloc() (memory allocation) is used to dynamically allocate memory on the heap in C. It requires #include <stdlib.h>. new() is used in C++, not C.
Q.40Medium
What is the output of: int a = 10, b = 20; int temp = a; a = b; b = temp; printf("%d %d", a, b);?
Answer: B
This is a swap operation. Initially a=10, b=20. After temp=a (temp=10), a=b (a=20), b=temp (b=10). Output: 20 10.