To read an entire string from a file, instead of a single character, use the _______ function.
- getline
- read
- fetch
- getString
The getline function is used in C++ to read an entire line from an input stream, such as a file. This is particularly useful when dealing with strings that have spaces or when reading full sentences.
A friend function is defined outside the class but has the ability to access the _______ members of the class.
- static
- private
- public
- mutable
A friend function, although not a member of a class, can access its private and protected members. This provides an external function the capability to interact closely with the internals of the class.
Consider a scenario where you have a large dataset and you need to frequently erase and insert elements in the middle of the data. Which STL container should be avoided in order to prevent frequent reallocations and data movements?
- vector
- list
- deque
- set
The STL vector maintains its elements in a contiguous block of memory. Inserting or erasing elements in the middle requires shifting elements, which can be expensive, especially for large datasets. This might cause frequent reallocations and data movements, impacting performance.
Which type of loop is best suited when the number of iterations is known ahead of time?
- do-while loop
- while loop
- for loop
- recursive function
A for loop is best suited when the number of iterations is known ahead of time, as it allows for an initial statement (typically for initialization), a condition check, and an increment/decrement operation in a compact syntax.
How does C++ enforce encapsulation and abstraction in multi-level inheritance scenarios?
- By using abstract classes only.
- By private and protected access specifiers.
- Through use of virtual functions alone.
- By using friend functions.
In C++, encapsulation and abstraction in multi-level inheritance are primarily enforced using private and protected access specifiers. While private members are inaccessible outside the class, protected members can be accessed by derived classes, ensuring a controlled inheritance mechanism.
What is the main purpose of using protected access specifier in base class?
- To restrict all external access
- To allow all classes to access
- To enable inheritance only
- To share methods with friends
The protected access specifier in C++ is mainly used to ensure that members are inaccessible from outside the class (just like private members). However, the difference is that protected members can be accessed by derived classes. This allows for more secure inheritance, giving derived classes access without exposing to the external world.
Which of the following data types can store a non-integer number?
- char
- float
- int
- short
In C++, the float data type is used to store single-precision floating-point numbers, which can represent non-integer values (like 3.14). It provides a way to store decimal numbers with a floating decimal point.
Which of the following data types is not a primitive data type in C++?
- int
- float
- string
- double
While "int", "float", and "double" are primitive data types in C++, the "string" data type is a part of the C++ Standard Library and is, in fact, a class, not a primitive data type.
How does encapsulation aid in reducing software development complexity?
- By allowing multiple inheritance.
- By segregating the program into separate modules.
- By promoting reusability of code.
- By bundling data and methods that operate on that data.
Encapsulation in C++ is the bundling together of data and the methods that operate on that data, restricting direct access to some of the object's components. This is a fundamental concept in object-oriented programming. By encapsulating data and functions into a single unit, we can hide the internal details and reduce complexity.
To achieve runtime polymorphism in C++, _______ are used.
- classes
- macros
- virtual functions
- inline functions
Runtime polymorphism in C++ is achieved through the use of virtual functions. These are functions in the base class that you can override in derived classes. The decision about which method to call (the one in the base class or the one in the derived class) is made at runtime, based on the actual type of the object being pointed to, rather than the type of the pointer. This allows for more dynamic behavior and is a core feature of object-oriented programming in C++.