How does C++ handle char literals that are assigned to unsigned char variables?
- Converts to signed char
- Preserves exact value
- Converts to int
- Triggers an error
When a char literal is assigned to an unsigned char variable, C++ preserves its exact value. If the char literal's value is negative, when assigned to an unsigned char, it wraps around using standard two's complement arithmetic.
What is the default access specifier for a base class in C++?
- private
- public
- protected
- internal
In C++, if you don't specify an access specifier for the members of a class, they are implicitly set to private. This means that they can't be accessed or viewed from objects of the class or any derived class unless friends.
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++.
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.
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.
Your team is developing a C++ application involving several classes with complex interrelationships and data handling. How can abstraction be effectively implemented to simplify the interactions between different class objects and the user?
- Use multiple inheritance for all classes.
- Avoid using classes and focus on procedural programming.
- Use a single class for all functions and data.
- Define clear interfaces for classes and hide complex implementations behind those interfaces.
Abstraction involves isolating the complex reality while exposing only the necessary parts. In the context of object-oriented programming, this means defining clear interfaces for classes, which allows interactions based on these interfaces, hiding the internal complex workings. This makes the software design more understandable and manageable.
When passing by reference, changes made to the parameter within the function _______ the original argument.
- alter
- bypass
- skip
- hide
When passing arguments by reference in C++, any modifications made to the parameter within the function directly alter the original argument. This is different from passing by value, where the function works on a copy and the original remains unchanged.
The _______ operator is used to compare whether two C++ values are not equal.
- !=
- ==
- >=
- <=
The != operator in C++ is the "not equal to" operator. It returns true if the operands on either side are not equal. For example, 5 != 3 evaluates to true, while 5 != 5 evaluates to false. The other options represent other comparison operations.
In situations where the argument should not be modified, passing by _______ is preferable to ensure the original data remains unaltered.
- value
- reference
- name
- pointer
Passing by value means the function receives a copy of the argument. As a result, any changes made to the parameter within the function don't affect the original data, ensuring it remains unaltered outside the function's scope.
Which keyword is used to inherit a class in C++?
- import
- extend
- inherit
- public
In C++, inheritance is specified by placing the colon (:) followed by an access specifier (public, protected, or private) and then the name of the base class. The public keyword itself is not used for inheritance, but rather indicates the type of inheritance.
You're developing an embedded system with constrained memory. What should be the main consideration when choosing between using float and double data types?
- Precision needs
- Popularity of type
- Storage size requirement
- Arithmetic operations
In embedded systems with memory constraints, storage size becomes a primary concern. A float generally occupies 4 bytes, while a double occupies 8 bytes. Hence, if precision offered by float is acceptable for the application, it would be preferable to use float to save memory space.
When a continue statement is encountered in a loop, the program control resumes from _______.
- next statement
- loop condition
- beginning of loop
- end of loop
The continue statement, when encountered inside a loop, causes the program to skip the rest of the current iteration and move directly to the loop condition to check if the next iteration should commence. In essence, it "continues" to the next iteration of the loop without finishing the current one.