What is the purpose of the 'switch' statement in C?
- To perform iterative loops
- To handle exceptions
- To provide multiple choices
- To enhance code readability
The 'switch' statement in C is used to provide multiple choices based on the value of an expression. It allows for a concise way to write code when there are multiple possible execution paths. Each 'case' represents a different possible value of the expression, improving code readability and maintainability, especially when dealing with menu-driven programs or handling different options.
In C, subtracting two pointers that point to elements of the same array yields a result of type ________.
- double
- int
- ptrdiff_t
- size_t
In C, subtracting two pointers that point to elements of the same array yields a result of type ptrdiff_t. This type represents the signed integral difference between the two pointers. It's important to use ptrdiff_t when performing pointer subtraction to ensure portability and accurate results, especially in situations involving pointer arithmetic.
The memory consumed by an array declared as float arr[10][20]; is ________.
- 2000 bytes
- 400 bytes
- 80 bytes
- 800 bytes
The memory consumed by a two-dimensional array is calculated by multiplying the number of rows by the number of columns and then multiplying it by the size of the data type. In this case, it's 10 (rows) * 20 (columns) * 4 bytes (float) = 800 bytes.
What does the strlen function from the C standard library do?
- Allocates memory for a new string
- Calculates the length of a string
- Compares two strings
- Copies one string to another
The strlen function in C calculates the length of a string by counting the number of characters until it reaches a null-terminator. It does not allocate memory or perform string copying.
What is the purpose of the 'sizeof' operator in C?
- It returns the address of a variable.
- It returns the size of a variable in bytes.
- It returns the type of a variable.
- It returns the value of a variable.
The 'sizeof' operator in C is used to determine the size (in bytes) of a variable or a data type. It is often used to allocate memory or work with data structures.
When designing a data packet structure for network communication, using ________ can help to minimize the packet size.
- Compression
- Encapsulation
- Fragmentation
- Segmentation
Encapsulation is a technique used to bundle data and functions into a single unit. It can help minimize the packet size by encapsulating data in a structured format for transmission over the network.
When dealing with an array of structures in C, what does each element of the array represent?
- A character array
- A floating-point number
- A function pointer
- A single structure instance
Each element of the array represents a single instance of the structure.
Which preprocessor directive would be used to conditionally compile certain blocks of code?
- #define directive
- #ifdef directive
- #include directive
- #pragma directive
The #ifdef directive is used to conditionally compile code based on whether a certain macro has been defined. It allows you to include or exclude specific code blocks based on preprocessor macros.
What is the function of the #define preprocessor directive in a C program?
- Controls the execution flow of the program
- Declares a variable within the program
- Defines a constant value for use in the program
- Includes a header file in the program
The #define directive is used to define constants. It allows you to specify a constant value that is used throughout the program, making it easier to maintain and modify the code.
What happens when realloc is called with a size parameter of zero?
- It frees the previously allocated memory.
- It leads to a memory leak.
- It reallocates the memory with a size of zero.
- It returns a null pointer.
When realloc is called with a size parameter of zero, it frees the previously allocated memory and returns a pointer to the freed memory, effectively deallocating it.