Section A — 1 Mark Questions
1. What is the primary function of the scope resolution operator (::)? [1 Mark]
Answer: It is used to access global variables or identify the class to which a member function belongs.
2. Give an example of a member dereferencing operator. [1 Mark]
Answer: The arrow operator (->) is a member dereferencing operator.
3. What is implicit type casting? [1 Mark]
Answer: It is an automatic conversion of data types performed by the compiler without programmer intervention.
4. What symbol is used for the scope resolution operator? [1 Mark]
Answer: The double colon symbol (::).
5. What happens during explicit type casting? [1 Mark]
Answer: The programmer manually forces a conversion from one data type to another.
6. Name the operator used to access members of a class through a pointer. [1 Mark]
Answer: The arrow operator (->).
7. Is the assignment operator (=) unary or binary? [1 Mark]
Answer: It is a binary operator.
8. What is the result of casting a double 5.9 to an integer? [1 Mark]
Answer: The result is 5 (truncation).
9. What is another name for explicit type casting? [1 Mark]
Answer: Type casting or type conversion.
10. Which operator is used to access a static member of a class? [1 Mark]
Answer: The scope resolution operator (::).
11. Can a smaller data type be implicitly promoted to a larger data type? [1 Mark]
Answer: Yes, this is known as type promotion.
12. What does the dot (.) operator do in a class context? [1 Mark]
Answer: It accesses members of a class object directly.
13. Is C++ type casting operator (type)variable considered C-style casting? [1 Mark]
Answer: Yes.
14. What is the purpose of the member dereferencing operator .*? [1 Mark]
Answer: It is used to access a class member given a pointer to that member and an object of the class.
15. What is a binary operator? [1 Mark]
Answer: An operator that requires two operands to function.
16. What is the scope of a variable? [1 Mark]
Answer: It refers to the portion of the program where the variable is accessible.
17. Define the function of the ->* operator. [1 Mark]
Answer: It accesses a class member given a pointer to the member and a pointer to the object.
18. What is the danger of explicit casting? [1 Mark]
Answer: It can lead to data loss or undefined behavior if performed incorrectly.
19. Does implicit casting occur between incompatible types? [1 Mark]
Answer: No, it typically occurs between compatible arithmetic types.
20. What is an unary operator? [1 Mark]
Answer: An operator that acts on only one operand.
Section B — 2/3 Mark Questions
1. Explain the difference between implicit and explicit type casting with an example. [3 Marks]
Answer: Implicit casting is done automatically by the compiler to prevent data loss (e.g., int to float). Explicit casting is forced by the user using syntax like (type)value to override compiler behavior, often resulting in potential data loss or truncation (e.g., casting float 3.9 to int 3).
2. Why is the scope resolution operator essential in C++? [2 Marks]
Answer: It is essential for accessing global variables that are hidden by local variables with the same name and for defining class member functions outside of the class declaration.
3. What is the difference between the dot (.) and arrow (->) operators? [3 Marks]
Answer: The dot operator is used to access members of a class via an object name. The arrow operator is used to access members of a class when you have a pointer to the object. They both serve the purpose of member access but differ based on the reference type.
4. Describe the role of type promotion in implicit casting. [2 Marks]
Answer: Type promotion ensures that during arithmetic operations, smaller types (like char or short) are converted to larger types (like int) to maintain precision and avoid overflow.
5. What are member dereferencing operators? List two. [2 Marks]
Answer: These operators allow accessing class members through pointers. Two examples are the arrow operator (->) and the pointer-to-member operator (.*).
6. How can you access a global variable if a local variable has the same name? [2 Marks]
Answer: You use the scope resolution operator (::) prefixing the variable name, such as ::variableName, to instruct the compiler to look in the global namespace.
7. Explain the concept of type casting truncation. [2 Marks]
Answer: Truncation occurs during explicit casting when a floating-point number is converted to an integer, causing the fractional part to be discarded.
8. What is the function of the (.*) operator? [3 Marks]
Answer: The (.*) operator is a pointer-to-member operator. It is used to access a non-static class member when you have an object and a pointer to that specific member. It essentially binds the member pointer to the object instance.
9. Why is explicit casting sometimes necessary? [3 Marks]
Answer: It is necessary when the compiler cannot safely perform a conversion, when the programmer needs to override type safety for specific hardware-level tasks, or when specific arithmetic results require a different type than what the operands provide.
10. State two rules for implicit type conversion. [2 Marks]
Answer: 1. Smaller types are always converted to larger types to prevent data loss. 2. Operands of different types are promoted to the type of the most complex operand involved in the expression.
Section C — 5 Mark Questions
1. Explain the concept of the Scope Resolution Operator (::) with examples and applications. [5 Marks]
Answer:
Introduction
The scope resolution operator (::) is a fundamental C++ operator used to identify and specify the context to which an identifier belongs.
Main Uses
- Accessing Global Variables: When a local variable and a global variable share the same name, the global one is masked. The :: operator allows access to the global one.
- Defining Methods Outside Classes: In C++, member functions are often declared inside the class and defined outside. The operator links the function to its class.
- Accessing Static Members: It is used to access static variables or functions of a class without creating an instance.
Example
If a class 'Calculator' has a static function 'add', you access it via 'Calculator::add()'. For global variable 'x', '::x' accesses the global scope.
2. Compare implicit and explicit type casting in detail. [5 Marks]
Answer:
Comparison Table
| Feature | Implicit Casting | Explicit Casting |
|---|---|---|
| Initiator | Compiler | Programmer |
| Safety | Safer, no data loss | Potentially risky, data loss possible |
| Syntax | Automatic | Manual (e.g., (int)x) |
| Usage | Routine conversions | Specific logic requirements |
Detailed Explanation
Implicit casting, or coercion, happens automatically when types are compatible, like assigning an int to a float. The compiler handles this to ensure program stability. Explicit casting, or typecasting, forces the compiler to treat a value as a different type. While powerful, it requires programmer responsibility to ensure that the memory interpretation is correct, as incorrect casting can lead to logical errors or segmentation faults.
3. Describe the role and syntax of member dereferencing operators in C++. [5 Marks]
Answer:
Definition
Member dereferencing operators are specialized operators that facilitate access to class members through pointers and references.
Key Operators
- Arrow Operator (->): Used to access members of an object when we have a pointer to the object. Syntax: pointer->member.
- Pointer-to-Member (.*): Used to access a member through a member pointer and an object.
- Pointer-to-Member (->*): Used to access a member through a member pointer and a pointer to an object.
Importance
These operators are vital when working with dynamic memory allocation, class hierarchies, and callback functions where we operate on addresses rather than static object instances. They bridge the gap between pointer arithmetic and object-oriented structure.
4. Discuss the rules and behavior of arithmetic type conversion in C++. [5 Marks]
Answer:
Hierarchy of Types
C++ follows a specific hierarchy for implicit conversions to ensure expressions remain accurate. The rank usually proceeds from: bool -> char -> short -> int -> long -> float -> double -> long double.
Rules of Conversion
- If an operation involves two types of different rank, the lower rank operand is promoted to the higher rank type.
- For signed and unsigned types, the type is usually promoted to the unsigned version if the values fit.
- The compiler performs these conversions automatically so that calculations are performed in a uniform data format.
Conclusion
Understanding these rules is crucial to prevent precision errors. If a calculation involving a float and an int occurs, the int is automatically treated as a float to maintain the fractional result of the calculation.
5. Explain how C++ handles member functions defined outside a class using the scope resolution operator. [5 Marks]
Answer:
Introduction
In C++, defining member functions outside the class body is a common practice to maintain cleaner code and separation of concerns.
Process
When a function is declared within a class, the compiler needs to know that the definition provided later belongs to that specific class. This is achieved using the syntax: ReturnType ClassName::FunctionName() { // body }.
Mechanism
- Namespace Association: The scope resolution operator tells the compiler that the function scope is within the 'ClassName' namespace.
- Visibility: Despite being defined outside, the function retains full access to all private and protected members of the class.
- Organization: This allows developers to keep header files lightweight by only including function prototypes, while implementation details remain in separate source files.