Which keyword is often advised against using due to its potential to make code less readable and maintainable?
- class
- public
- goto
- private
The goto keyword allows for arbitrary jumps in code, which can make the code's flow hard to follow, leading to decreased readability and maintainability. Although it's available in C++, its use is generally discouraged in modern programming practices.
The _______ keyword is used to include standard libraries in a C++ program.
- import
- include
- namespace
- return
In C++, the "include" keyword is used to include header files, which contain the declarations for the standard libraries. It's vital for utilizing predefined functions and classes, thus facilitating easier and more efficient coding.
To insert an element into a C++ list at a specified position, you should use the _______ function.
- push
- emplace
- put
- insert
The insert function in C++ STL is used to insert elements at a specific position in a list. emplace is also used for insertion, but it constructs the object in-place, while insert just places a copy or a move of the object.
Imagine you are developing a system where you need to represent a fixed set of constant integral values that represent different states, and you want to restrict the variables to only allow these states. Which C++ feature would be most appropriate to use?
- Class
- Enum
- Union
- Template
Enums, short for enumerations, allow the definition of a type that can hold a set of integral constants. They provide a way to assign names to the integral constants which makes the code more readable and maintainable.
Can a friend function be a member function of another class?
- Always
- Never
- Only if it's a static member function.
- Yes, if it's declared as a friend in the original class.
A friend function can indeed be a member function of another class. In this context, one class allows a member function of another class to access its private and protected members by declaring that member function as a friend.
What is the primary difference between abstraction and encapsulation in C++?
- Abstraction hides details; encapsulation bundles them.
- Abstraction uses inheritance; encapsulation doesn't.
- Abstraction is a data type; encapsulation is a method.
- Encapsulation is a principle; abstraction is an attribute.
The primary difference between abstraction and encapsulation in C++ lies in their purposes and implementations. Abstraction focuses on hiding the complex reality of an object and showing only the necessary features, essentially giving a high-level view. On the other hand, encapsulation is about bundling data and the methods, shielding them from outside interference.
Which function is used to open a file in C++?
- openFile()
- create()
- open()
- initiateFile()
The open() function is used in C++ to open a file. It is a member function of the file stream classes (like fstream, ifstream, ofstream). By providing the filename and mode, a user can access or modify the contents of a file.
You are developing a game where numerous conditions dictate the character's state (e.g., running, jumping, idle, etc.). To manage these states efficiently, which approach might be preferable?
- Use multiple if-else statements
- Use a state pattern
- Use global variables
- Implement recursion
The State Pattern allows an object to change its behavior when its internal state changes. This would be ideal for game character states as it encapsulates state-specific behaviors and transitions, making the code more organized, scalable, and easy to extend with new states.
What happens to the control flow of a program when the break statement is encountered inside a while loop?
- The program exits
- The while loop terminates and control moves to the next statement after the loop
- The loop skips to the next iteration
- The loop starts from the beginning
When a break statement is encountered inside a while loop, the loop terminates immediately and the control moves to the next statement following the loop, regardless of the loop's condition.
The syntax to declare a pure virtual function is to follow the function declaration with _______.
- '= 0'
- 'virtual'
- 'override'
- '= 1'
In C++, a pure virtual function is declared by assigning 0 in its declaration. The syntax is virtual function_name() = 0;. It indicates that the function doesn't have a body in this class and must be implemented in any non-abstract derived class.
Which year was the C++ programming language introduced?
- 1970
- 1980
- 1983
- 1995
C++ was introduced in 1983 by Bjarne Stroustrup as an enhancement to the C language.
Which loop is NOT compatible with the break statement in C++?
- for loop
- do-while loop
- while loop
- None, all loops support break
All loops in C++ (for, while, and do-while) support the break statement. The break statement is a control flow statement that can be used to exit a loop prematurely when a specific condition is met, irrespective of the loop's primary condition.