scanf() reads formatted input from the standard input device (usually keyboard). printf() displays output. Both require #include <stdio.h>.
Q.42Medium
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.43Medium
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.44Medium
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'.
Q.45Medium
What is the return type of the malloc() function?
Answer: B
malloc() returns a void pointer (void*) which can be cast to any pointer type. It returns NULL if memory allocation fails.
Advertisement
Q.46Medium
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.47Medium
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.48Medium
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.49Medium
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.
Q.50Medium
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.51Medium
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.52Medium
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.53Medium
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.54Medium
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.55Medium
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.56Medium
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.57Medium
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.58Medium
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.59Medium
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.60Medium
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).