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.
You're writing a C program that needs to read from a file and display the content on the screen. How would you handle the situation if the file does not exist?
- Create an empty file
- Ignore and continue
- Print an error message and exit
- Prompt user for a new file name
Printing an error message and exiting the program is a common approach when a required file is not found. It informs the user about the issue and halts the program execution to prevent further errors.
When reading a text file in C, which function can be used to read data from the file?
- fgets
- fputs
- fread
- fwrite
When reading a text file in C, the fgets function is commonly used to read data from the file. It reads one line of text at a time, making it suitable for text file processing.
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.