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.
Q.22Medium
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.23Medium
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.24Medium
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.25Medium
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
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.29Medium
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.30Medium
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.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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.36Medium
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.
Q.37Medium
In C, what does the scanf() function do?
Answer: B
scanf() reads formatted input from the standard input device (usually keyboard). printf() displays output. Both require #include <stdio.h>.
Q.38Medium
What will be the output of: for(int i = 0; i < 3; i++) { printf("%d ", i); }?
Answer: A
Loop runs with i=0,1,2. When i=3, condition i<3 becomes false and loop terminates. Output: 0 1 2 (with spaces).
Q.39Medium
Which of the following is the correct way to define a function that takes no parameters and returns no value?
Answer: A
In C, void function() { } is correct. In some contexts, void function(void) { } is more explicit. Options B and C use invalid keywords for this purpose.
Q.40Medium
What is the output of the following program: int x = 5; if(x > 3) printf("Greater"); else printf("Smaller");?
Answer: B
x=5 is greater than 3, so the condition (x > 3) evaluates to true. The if block executes, printing 'Greater'.