In a C++ application, you notice that a function is being called with different types of arguments, but there is only one function definition. What feature of C++ could be allowing this behavior?
- Dynamic casting
- Function overloading
- Inheritance and polymorphism
- Operator overloading
The behavior of calling a function with different argument types using a single function definition is achieved through function overloading in C++. This feature allows multiple function definitions with the same name but different parameter types.
You are working on a program that processes user input, and you want to ensure that the input string does not exceed a certain length to prevent buffer overflow. Which string handling function would be appropriate to use?
- strcpy
- strncat
- strncpy
- strstr
To limit the length of user input and prevent buffer overflow, the strncpy function is appropriate. It allows you to copy a specified number of characters from the input to the destination, ensuring the destination buffer does not overflow. strstr searches for a substring, strncat concatenates strings with a specified limit, and strcpy copies a string without length control.
In a program that manipulates text, you need to store multiple strings. What is a potential issue with using character arrays instead of string literals?
- Character arrays are less efficient in terms of memory usage compared to string literals.
- Character arrays can't accommodate variable-length strings, making it challenging to store strings of different sizes.
- Character arrays may lead to null-termination errors if not handled carefully.
- Memory management becomes complex with character arrays, as you need to manually handle memory allocation and deallocation.
Using character arrays for storing multiple strings in a text-manipulation program can be problematic because character arrays have a fixed size and don't easily adapt to variable-length strings. This can lead to memory wastage or buffer overflows. String literals, on the other hand, are more flexible.
To declare a pointer to an integer in C, you would write ________.
- float* y;
- int x;
- int* x;
- x = 5;
To declare a pointer to an integer in C, you would write int x;* The int* indicates that x is a pointer to an integer.
When a function receives a pointer as an argument, it can modify the ________ that the pointer points to.
- memory
- pointer
- value
- variable
When a function receives a pointer as an argument in C, it can modify the variable that the pointer points to because it operates directly on the memory location pointed to by the pointer.
What is the result of adding an integer to a pointer?
- It causes a compilation error.
- It increments the integer.
- It increments the pointer.
- It multiplies the integer.
Adding an integer to a pointer in C increments the pointer by the product of the integer and the size of the data type pointed to. It does not increment the integer itself. If the result is outside the valid memory range, it can lead to undefined behavior.
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.