The function ________ in C changes the size of the memory block pointed to by a given pointer.
- realloc()
- free()
- malloc()
- calloc()
The correct option is 'realloc()'. The realloc() function is used to change the size of a previously allocated memory block, making it suitable for resizing dynamic arrays.
You're writing a program that needs to efficiently calculate the power of 2 for a given exponent. Which operator would be most efficient to use?
- * (Multiplication)
- ** (Exponentiation)
- << (Left Shift)
- ^ (Bitwise XOR)
The correct answer is C) ** (Exponentiation). To efficiently calculate the power of 2 for a given exponent, you should use the exponentiation operator (**).
What will happen if the condition in a 'while' loop is initially false?
- It will enter the loop and execute it once.
- It will enter the loop and run indefinitely.
- It will result in a compile-time error.
- It won't enter the loop.
When the condition in a 'while' loop is initially false, the loop will not execute, and the program will proceed to the next statement.
In C, an array name acts as a ________ pointing to the first element of the array.
- label
- pointer
- reference
- variable
In C, an array name is a constant pointer that points to the address of the first element in the array.
The ________ loop checks the condition before executing the block of code.
- do-while
- for
- if
- while
The "do-while" loop is used in programming to check the condition after executing the block of code at least once. It guarantees that the loop body is executed at least once, as the condition is evaluated after the first iteration. On the other hand, "for" and "while" loops check the condition before entering the loop, and "if" is used for conditional statements, not loops.
What is the primary function of pointers in C?
- To manipulate strings
- To perform mathematical operations
- To provide dynamic memory allocation
- To store variables
Pointers in C are primarily used for dynamic memory allocation. They allow you to allocate and deallocate memory at runtime, which is essential for efficient memory usage and data structure implementation.
In C, when an array is passed to a function, is it passed by value or by reference?
- By reference
- By value
- It depends on the array size
- Not possible to pass arrays to functions
In C, arrays are passed to functions by reference, which means that changes made to the array inside the function affect the original array in the caller.
What is the primary cause of memory leaks in a C program?
- Excessive memory fragmentation.
- Failure to free dynamically allocated memory.
- Incorrect use of the malloc function.
- Insufficient memory allocation.
The primary cause of memory leaks in a C program is the failure to free dynamically allocated memory using functions like free(). When you allocate memory but don't deallocate it properly, it leads to memory leaks.
What is the scope of a global variable in a C program?
- Accessible throughout the entire program
- Limited to the file where it's declared
- Local to the function it's defined in
- Only within the main() function
In C, global variables have a scope that spans the entire program, meaning they can be accessed from any part of the code.
When a string is declared as a character array, the string must be terminated with a ________.
- comma
- null character
- semicolon
- space
In C, a string is a sequence of characters terminated by a null character (' '). This null character signifies the end of the string.