An enumeration is a user-defined data type that consists of integral ________.
- Characters
- Functions
- Pointers
- Values
An enumeration is a user-defined data type that consists of integral values, typically used for defining a set of named integer constants.
When opening a file with fopen, what happens if the file does not exist and the mode is set to 'r'?
- It appends data to the existing file.
- It creates a new file.
- It overwrites the file's content.
- It returns a NULL pointer.
When opening a file with fopen in read mode ('r') and the file doesn't exist, it returns a NULL pointer, indicating that the file was not successfully opened.
When passing an array to a function in C, what is actually being passed?
- A pointer to the first element
- The entire array
- The size of the array
- The value of the first element in the array
In C, when passing an array to a function, only a pointer to the first element is passed, allowing the function to access the entire array.
When declaring a string literal in C, which character is automatically appended at the end?
- Exclamation mark '!'
- Null terminator ' '
- Question mark '?'
- Semicolon ';'
In C, a null terminator ' ' is automatically appended at the end of a string literal. This null character indicates the end of the string, allowing C functions to determine the string's length.
What would be the result of trying to access an array with a pointer that has not been initialized?
- It will access a random memory location, leading to undefined behavior.
- The pointer will point to the beginning of the array.
- The program will compile but throw a runtime error.
- The program will not compile.
Accessing an array with an uninitialized pointer in C leads to undefined behavior because the pointer doesn't point to a valid memory location. The result is unpredictable and can lead to crashes or incorrect data manipulation.
The ________ directive is used to define symbolic names or constants in a C program.
- #constant
- #define
- #enum
- #import
The correct answer is (b) #define. In C, the #define preprocessor directive is used to define symbolic names or constants. These names can simplify your code and make it more readable.
How does the execution stack change when a recursive function is called in C?
- A new thread is created
- Each function call adds a new stack frame
- The stack remains unchanged
- The stack shrinks
In C, when a recursive function is called, each function call adds a new stack frame to the execution stack. These stack frames store the local variables and return addresses for each function call. This process continues until the base case is reached.
In a graphics program, you need to define colors where each color can be represented as either a name or an RGB triplet. How would you efficiently represent this in C?
- Using a function
- Using a structure
- Using a union
- Using an enum
In C, a union would efficiently represent colors that can be either a name or an RGB triplet because it allows sharing the same memory location for different data types. Enums, structures, and functions are not suitable for this purpose.
In C++, what happens when two overloaded functions have the same number and types of parameters but differ in return type?
- It's a compilation error
- It's undefined behavior
- The function with the least specific return type is called
- The function with the most specific return type is called
When two overloaded functions in C++ have the same number and types of parameters but differ in return type, it results in a compilation error. C++ uses the function's signature, including the return type, to differentiate between overloaded functions. If two functions have the same parameters and differ only in return type, it becomes ambiguous, and the compiler cannot determine which function to call.
In what scenario would using an array of structures be more beneficial than using multiple arrays?
- When data access patterns are unpredictable
- When dealing with unrelated data
- When each element has multiple attributes
- When memory efficiency is not a concern
Using an array of structures is advantageous when dealing with related data that share the same attributes or fields, making it more organized and efficient compared to multiple arrays.