Which of the following correctly describes the difference between malloc() and calloc() in C?
Answer: B
The answer is correct because it accurately captures the fundamental distinction between these two memory allocation functions in C. malloc() allocates a block of uninitialized memory and requires only the total number of bytes as a single argument, while calloc() allocates memory for an array of elements, takes two arguments (number of elements and size per element), and crucially initializes all allocated memory to zero. This initialization feature of calloc() is a key advantage when you need guaranteed zero values, whereas malloc() leaves memory with whatever garbage values were previously there. Other options would be incorrect if they claimed malloc() initializes memory, if they stated calloc() takes only one argument, or if they reversed which function initializes to zero.
Q.2Easy
Which data structure uses the LIFO (Last In First Out) principle and is commonly used for function call management in programming languages?
Answer: B
A Stack follows LIFO principle where the last element inserted is the first one to be removed.
It is fundamental in managing function calls through the call stack, undo/redo operations, and expression evaluation.
Queues use FIFO, Heaps are used for priority ordering, and Linked Lists maintain sequential but flexible storage.
Q.3Easy
What is the output of the following C code?
int x = 5;
printf("%d", x++);
Answer: B
The post-increment operator (x++) returns the current value of x before incrementing. So printf prints 5, and then x becomes 6.
Q.4Easy
Which of the following is the correct syntax to declare a pointer in C?
Answer: A
In C, pointers are declared using the asterisk (*) symbol before the variable name. The syntax is 'datatype *pointer_name;'
Q.5Easy
What will be the size of the following array in bytes?
char arr[10];
Answer: A
Each char occupies 1 byte in memory. An array of 10 chars will occupy 10 × 1 = 10 bytes.
Advertisement
Q.6Easy
What is the purpose of the 'void' keyword in C?
Answer: C
void is used in two contexts: (1) as a function return type when a function doesn't return a value, and (2) to declare a generic pointer (void *) that can point to any data type.
Q.7Easy
What is the output of the following C code?
int a = 10, b = 20;
int c = (a > b) ? a : b;
printf("%d", c);
Answer: B
The ternary operator (condition ? true_value : false_value) evaluates the condition (a > b). Since 10 is not greater than 20, it returns b which is 20.
Q.8Easy
What does the strlen() function return?
Answer: B
strlen() returns the length of a string without counting the null terminator ('\0'). For example, strlen("hello") returns 5, not 6.
Q.9Easy
Which header file is required to use the printf() function in C?
Answer: B
The stdio.h header file contains declarations for standard input/output functions like printf() and scanf().
Q.10Easy
Which of the following is a valid variable name in C?
Answer: B
Variable names in C must start with a letter or underscore, not a digit. _variable is valid. '2var' starts with digit, 'var-name' contains hyphen (invalid), 'var name' contains space (invalid).
Q.11Easy
What is the return type of the strlen() function?
Answer: C
The strlen() function returns the length of a string as an int value, representing the number of characters.
Q.12Easy
Which operator has the highest precedence in C?
Answer: C
Parentheses () have the highest precedence among all operators in C. They are always evaluated first in any expression.
Q.13Easy
What is the size of an integer variable in most modern C compilers?
Answer: B
On most modern 32-bit and 64-bit systems, an int is typically 4 bytes (32 bits). However, the exact size can vary depending on the compiler and system architecture.
Q.14Easy
Which of the following is NOT a valid C data type?
Answer: B
C does not have a built-in boolean data type. The other options (float, double, char) are all valid primitive data types in C. Boolean functionality is typically implemented using int (0 for false, non-zero for true).
Q.15Easy
What is the purpose of the & operator in C?
Answer: C
The & operator has two uses in C: (1) When used before a variable, it returns the memory address of that variable (address-of operator), and (2) When used between two integers, it performs a bitwise AND operation.
Q.16Easy
Which keyword is used to create a constant variable in C?
Answer: C
The const keyword is used in C to declare a constant variable whose value cannot be modified after initialization. Variables declared as const are read-only.
Q.17Easy
What is the output of sizeof(char) in C?
Answer: B
By the C standard, sizeof(char) is always 1 byte. A char is the smallest addressable unit in C and is defined to be 1 byte.
Q.18Easy
How many times will the loop execute?
for(int i = 0; i < 5; i++)
Answer: B
The loop initializes i to 0 and continues while i < 5. Values of i: 0, 1, 2, 3, 4. After i becomes 5, the condition is false, so the loop executes 5 times.
Q.19Easy
What will be the value of x after: int x = 5; x += 3;
Answer: C
The += operator is a compound assignment operator. x += 3 is equivalent to x = x + 3. Therefore, 5 + 3 = 8.
Q.20Easy
What is the size of int data type in a 32-bit system?
Answer: B
In a 32-bit system, the int data type typically occupies 4 bytes (32 bits). This is the standard size for integer types on most 32-bit architectures.