What term is used to describe the process of calling a function within itself?
- Casting
- Looping
- Recursion
- Structuring
The process of calling a function within itself is known as "recursion." It allows for solving problems that can be broken down into smaller, similar subproblems.
Function pointers in C can be passed as arguments to functions, thereby providing a degree of ________.
- Abstraction
- Encapsulation
- Flexibility
- Polymorphism
In C, function pointers enable polymorphism by allowing you to call different functions dynamically. This provides flexibility in your code.
Pointers can lead to ________ if not managed correctly in a C program.
- Compilation errors
- Memory leaks
- Segmentation faults
- Stack overflows
Pointers in C can cause segmentation faults when not handled properly. A segmentation fault occurs when a program tries to access memory that it's not allowed to access.
What function is used to open a binary file for both reading and writing in C?
- fopen()
- open()
- openfile()
- readfile()
In C, the fopen() function is used to open a binary file for both reading and writing. It returns a file pointer, which is essential for performing file operations.
In C, what happens if you attempt to open a file for reading using fopen() but the file does not exist?
- fopen() returns NULL, indicating that the file does not exist.
- fopen() raises an exception.
- fopen() creates a new file with the specified name.
- fopen() prompts the user to enter the correct file name.
The correct option is "fopen() returns NULL, indicating that the file does not exist." If the file does not exist or cannot be opened for some reason, fopen() returns a NULL pointer, indicating an error in opening the file.
What potential problem might occur when using fseek and ftell with large binary files?
- Data corruption
- Inaccurate file positioning
- Memory overflow
- Performance degradation
When working with large binary files, using fseek and ftell may result in inaccurate file positioning. This is because these functions use a single long integer to represent the file position, which may not be sufficient to handle the large offsets required for large files. This can lead to errors in file positioning and unintended behavior in your code.
What might be a reason to use bit fields when designing a system with strict memory constraints?
- They allow more precise control over memory usage
- They improve code performance
- They offer greater data integrity
- They simplify code logic
Bit fields can be useful in systems with strict memory constraints because they allow more precise control over memory usage by packing data in smaller units.
In a 'switch' statement, the ________ keyword is used to specify the code to execute if none of the cases match.
- Break
- Continue
- Default
- Exit
The correct option is (c) Default. In a 'switch' statement in C, the 'default' keyword is used to specify the code to execute if none of the cases match the provided value. It acts as a fallback option.
What is the significance of using pointers in function arguments?
- It allows passing arguments by reference
- It enforces strong typing
- It reduces code readability
- It simplifies function declarations
Using pointers in function arguments allows passing arguments by reference, meaning the function can modify the original data, not just a copy. This is valuable when you want a function to modify variables from the caller's scope.
You are debugging a C program and notice that the program crashes after running for a few minutes. Upon investigation, you find that a recursive function is being called multiple times. What could be the most likely cause of the crash?
- Hardware Failure
- Infinite Loop
- Memory Leak
- Stack Overflow
The most likely cause of the crash in this scenario is a stack overflow. When a recursive function is called multiple times without a proper base case or termination condition, it can fill up the call stack, causing a stack overflow and program crash.
The C function ________ is used to compare two strings lexicographically.
- strcmp
- strncmp
- strcompare
- strcasecmp
The correct answer is strcmp. This function compares two strings character by character and returns an integer value representing their lexicographical order. strncmp is used for comparing a specific number of characters, and the other options are not valid C functions.
The strlen function returns the length of a string, excluding the ________.
- null terminator
- first character
- whitespace characters
- special characters
The correct answer is the null terminator. The strlen function in C/C++ returns the length of a string by counting characters until it encounters the null terminator (' '). It excludes the null terminator from the count. The other options are not what strlen excludes.