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.62Medium
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.63Medium
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.64Medium
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.65Hard
Which of the following is an invalid identifier in C?
Answer: C
In C, identifiers can only contain alphanumeric characters (a-z, A-Z, 0-9) and underscores (_), and must start with a letter or underscore. The dollar sign ()isnotallowed,making′myvar' invalid.
Advertisement
Q.66Hard
What will be the output of: int x = 5; printf("%d %d", x++, ++x);?
Answer: A
x++ is post-increment (returns 5, then x becomes 6), ++x is pre-increment (x becomes 7, then returns 7). The order of evaluation in printf is implementation-dependent, but typically right to left, giving 5 7.
Q.67Hard
What is the difference between structure and union in C?
Answer: B
Structure allocates separate memory for each member (total size = sum of all members). Union shares a single memory location among all members (total size = size of largest member). This is a fundamental difference in memory allocation.
Q.68Hard
Which of the following function declarations is correct for a function that takes no parameters and returns no value?
Answer: A
The correct syntax is 'void function(void);' where 'void' in parentheses explicitly states no parameters. Option B is also valid in C (defaults to no parameters), but option A is more explicit and portable across C standards.
Q.69Hard
In C, what is the purpose of the typedef keyword?
Answer: B
typedef creates an alias (synonym) for existing data types. For example, 'typedef int Integer;' creates 'Integer' as an alias for 'int'. This improves code readability and portability. It doesn't create entirely new types, but provides alternative names.
Q.70Easy
In C programming, which of the following is NOT a valid data type?
Answer: C
In C, 'string' is not a primitive data type. C uses 'char' arrays to represent strings. The valid primitive data types are int, float, double, char, void, and their variants.
Q.71Easy
What will be the output of: int a = 10; int b = 20; int c = a + b; printf("%d", c);?
Answer: B
Variable 'a' is initialized to 10, 'b' is initialized to 20. When c = a + b, c becomes 10 + 20 = 30. Therefore, printf outputs 30.
Q.72Easy
Which of the following is used to declare a constant in C?
Answer: B
In C, the 'const' keyword is used to declare constants. Once a const variable is initialized, its value cannot be changed. Options A, C, and D are not valid C syntax.
Q.73Easy
What is the purpose of the return statement in a C function?
Answer: B
The return statement is used to exit a function and return a value (if any) to the calling function. If the function has return type void, no value is returned.
Q.74Medium
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.75Medium
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.76Medium
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.77Medium
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.78Medium
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.79Medium
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.80Medium
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).