malloc() returns a void pointer (void*) which can be cast to any pointer type. It returns NULL if memory allocation fails.
Q.42Medium
Which function is used to release dynamically allocated memory in C?
Answer: B
The free() function deallocates memory that was previously allocated using malloc(), calloc(), or realloc(). delete() is used in C++.
Q.43Medium
What is the difference between calloc() and malloc()?
Answer: D
calloc(n, size) allocates n blocks of size bytes and initializes to 0. malloc(size) allocates size bytes without initialization. calloc() is typically slower due to initialization.
Q.44Medium
Which of the following is used to comment multiple lines in C?
Answer: B
/* */ is used for multi-line comments in C. // is used for single-line comments and was introduced in C99.
Q.45Medium
What will be the output of: float x = 25; printf("%f", x);
Answer: B
Since both 5 and 2 are integers, integer division occurs (25 = 2). The result 2 is then converted to float as 2.000000.
Advertisement
Q.46Medium
How are structures different from unions in C?
Answer: A
In structures, each member has its own memory location. In unions, all members share the same memory location, so only one can hold a value at a time.
Q.47Medium
What will be the output of the following code?
int a = 5;
int b = ++a + a++;
printf("%d", b);
Answer: B
++a increments a to 6 and returns 6. Then a++ returns 6 and increments a to 7. So b = 6 + 6 = 12.
Q.48Medium
What will be the output of: int x = 5; printf("%d", x << 1);
Answer: B
The left shift operator (<<) shifts bits to the left by 1 position, which is equivalent to multiplying by 2. So 5 << 1 = 10.
Q.49Medium
Consider the code: int arr[10]; int *p = arr; What is p[5]?
Answer: B
p points to arr, so p[5] is equivalent to *(p+5) which gives the value at arr[5].
Q.50Medium
What is the purpose of the void pointer in C?
Answer: D
The void pointer is a generic pointer that can hold addresses of any data type. void is also used for functions returning nothing and parameters.
Q.51Medium
Which function is used to read a string from standard input?
Answer: D
All three functions can read strings, though gets() is deprecated due to security issues. fgets() is safer as it limits input size.
Q.52Medium
What is the output of: int arr[] = {1,2,3,4,5}; printf("%d", sizeof(arr)/sizeof(arr[0]));
Answer: A
sizeof(arr) gives total size of array. sizeof(arr[0]) gives size of one element. Dividing gives the number of elements: 420 = 5.
Q.53Medium
What will be the output of: char c = 'A'; printf("%d", c);
Answer: A
The %d format specifier prints the ASCII value of the character. 'A' has ASCII value 65.
Q.54Medium
What is the output of: int arr[5]; printf("%d", sizeof(arr));
Answer: C
sizeof(arr) returns the total size of the array in bytes. An array of 5 integers, where each int is 4 bytes, equals 5 × 4 = 20 bytes.
Q.55Medium
Which function is used to allocate memory dynamically at runtime?
Answer: B
malloc() is the standard C library function for dynamic memory allocation. It returns a void pointer to the allocated memory block, which can be cast to the required data type.
Q.56Medium
What is the purpose of the break statement in a switch case?
Answer: B
The break statement terminates the current loop or switch block and transfers control to the statement following the loop or switch. Without it, execution continues to the next case (fall-through).
Q.57Medium
What will be the output of: int x = 5, y = 10; if (x < y) printf("yes"); else printf("no");
Answer: A
The condition x < y evaluates to true (5 < 10), so the if block executes, printing 'yes'. The else block is skipped.
Q.58Medium
Which of the following is NOT a valid way to pass arguments to a function in C?
Answer: B
C supports pass by value and pass by pointer/address. 'Pass by reference' as a concept exists in C++, not in C. In C, to achieve reference-like behavior, we use pointers.
Q.59Medium
What will be the result of: int a = 10, b = 3; int c = a % b; printf("%d", c);
Answer: B
The modulus operator % returns the remainder of division. 10 % 3 = 1 (since 10 = 3 × 3 + 1). Therefore, c = 1 and the output is 1.
Q.60Medium
What will be the output of: char ch = 'A'; printf("%d", ch);
Answer: B
When a character is printed using %d format specifier, its ASCII value is printed. The ASCII value of 'A' is 65.