Test Series - oops

Test Number 71/78

Q: Which among the following is a mandatory condition for downcasting?
A. It must not be done explicitly
B. It must be done implicitly
C. It must be done explicitly
D. It can’t be done explicitly
Solution: The downcasting of any object must be done explicitly. This is because the compilers don’t support the implicit conversion of a supertype to subtype.
Q: What is downcasting?
A. Casting subtype to supertype
B. Casting supertype to subtype
C. Casting subtype to supertype and vice versa
D. Casting anytype to any other type
Solution: The downcasting concept includes only the casting of supertypes to the sub types. This casting is generally done explicitly. Larger size types are made to fit into small size types explicitly.
Q: Downcasting is _______________________
A. Always safe
B. Never safe
C. Safe sometimes
D. Safe, depending on code
Solution: The downcasting concept is made for exception cases. When there is a need to represent an entity in the form which is not suitable for it. Representing a base type in derived type is not right but can be done for special cases.
Q: Downcasting ____________________
A. Can result in unexpected results
B. Can’t result in unexpected result
C. Can result only in out of memory error
D. Can’t result in any error
Solution: The result of downcasting can be unexpected. This is because downcasting is done on the objects into the objects which doesn’t contain any information of data in lateral object.
Q: What should be used for safe downcast?
A. Static cast
B. Dynamic cast
C. Manual cast
D. Implicit cast
Solution: The dynamic cast can be done using the operator dynamic_cast. This converts one type to another type in a safe way.
Q: What does dynamic_cast return after successful type casting?
A. Address of object which is converted
B. Address of object that is used for conversion
C. Address of object that is mentioned in the syntax
D. Doesn’t return any address
Solution: The address of the object which is converted is returned by the dynamic_cast operator. This is done to safely convert the subtype to supertype. This ensures the proper assignment and conversion from one type to another.
Q: If dynamic_cast fails, which value is returned?
A. void
B. null
C. void pointer
D. null pointer
Solution: The null pointer is returned by the dynamic_cast, if it fails. The conversion sometimes fails because of too complex type conversion. The conversion may also fail due to memory or some related issues.
Q: Which is the proper syntax of dynamic_cast?
A. dynamic_cast(object)
B. dynamic_cast new (object)
C. dynamic_cast(object)
D. dynamic_cast(object)
Solution: The dynamic_cast is the name of the operator, which is followed by the new type in which the object have to be converted. Then the object name is given. This object name is then used after the type conversion.
Q: Which is the exception handler for the exceptions of downcasting?
A. CastException
B. ClassCastingExeption
C. ClassCasting
D. ClassCastException
Solution: The exception handler for the exceptions produced during the downcasting exception. This handler can be called during runtime to handle any exception thrown.
Q: How to prevent the ClassCastExceptions?
A. By using instanceof
B. By using is-a check
C. By using arrow operator with check function
D. By checking type of conversion
Solution: The instanceof operator can be used to check the compatibility of the conversion. This has to be done to check whether the casting would be safe or not.

You Have Score    /10