A member function that is used to initialize the class members is known as a _______.
- initializor
- starter
- setter
- constructor
In C++, a constructor is a special member function that is automatically called when an object is instantiated. Its primary purpose is to initialize the object's attributes.
In the context of encapsulation, how does C++ handle data hiding differently from other programming languages?
- By using namespaces exclusively.
- By disallowing external access to data completely.
- Through the use of private and protected access specifiers only.
- By mandatory encapsulation for all classes.
C++ supports data hiding primarily through the use of private and protected access specifiers. Private members are strictly inaccessible from outside the class, while protected members are accessible in derived classes. This mechanism is more flexible compared to some languages that might use other strategies.
Which class is primarily used for performing input operations on files in C++?
- ofstream
- ifstream
- fstream
- filestream
The ifstream class stands for input file stream and is primarily used for performing input operations on files. This means it is used for reading data from files, making it crucial for file handling in C++.
When a pointer is passed to a function, the function receives _______?
- A copy of the actual data
- The memory address
- The function stored in memory
- A blueprint of the data
When a pointer is passed to a function, the function gets a copy of the pointer, which contains the memory address of the data. Therefore, the function can access and modify the original data through this memory address. This is different from passing the actual data, where the function would receive a copy of that data, and any changes wouldn't affect the original data.
Which file opening mode in C++ will allow you to append data at the end of the file’s content?
- ios::binary
- ios::in
- ios::app
- ios::trunc
The "ios::app" mode in C++ stands for "append". When a file is opened in this mode, data will be appended to the end of the file's content if it already exists. If the file does not exist, a new one is created.
In C++, if a programmer forgets to deallocate memory using delete, it can result in a _______.
- segmentation fault
- memory leak
- compilation error
- type error
A memory leak in C++ occurs when programmers allocate memory (typically using the new operator) but forget to deallocate it (using the delete operator). This causes the program to consume memory resources continuously, which might lead to system slowdowns or crashes.
Which of the following is a correct file extension for a C++ source file?
- .cpp
- .html
- .java
- .py
C++ source files typically have the extension ".cpp", although other extensions like ".cc" or ".cxx" are also used sometimes.
In a function that needs to return multiple values, which method of parameter passing might be employed to facilitate this without using data structures like tuples or pairs?
- Using global variables
- Using static variables
- Passing by reference
- Using std::vector
When parameters are passed by reference to a function, the function can modify the original values of those parameters. By passing additional parameters by reference, a function can effectively "return" more than one value without relying on composite data structures like tuples or pairs. This method is often used for out parameters in functions.
What does the break statement do in a loop structure?
- Exits the entire program.
- Skips to the next iteration.
- Exits the loop immediately.
- Continues to execute the loop from the start.
The break statement in C++ is used to exit the loop immediately, bypassing the rest of the loop's code and any remaining iterations. This can be especially useful for exiting a loop when a specific condition is met.
Creating user-defined exceptions in C++ can be achieved by inheriting the _______ class.
- std::exception
- std::error
- std::runtime_error
- std::catchable
In C++, user-defined exceptions are often derived from the std::exception class or its sub-classes. This allows them to be caught using catch blocks designed for standard exceptions, while also adding custom behavior.