Object Oriented Programming using C++ Questions and Answers – Downcasting


This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Downcasting”.

1. 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
View Answer

Answer: b
Explanation: 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.

2. 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
View Answer

Answer: c
Explanation: 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.

3. Downcasting is _______________________
a) Always safe
b) Never safe
c) Safe sometimes
d) Safe, depending on code
View Answer

Answer: b
Explanation: 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.

4. 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
View Answer

Answer: a
Explanation: 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.

5. What should be used for safe downcast?
a) Static cast
b) Dynamic cast
c) Manual cast
d) Implicit cast
View Answer

Answer: b
Explanation: The dynamic cast can be done using the operator dynamic_cast. This converts one type to another type in a safe way.

6. 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
View Answer

Answer: a
Explanation: 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.

7. If dynamic_cast fails, which value is returned?
a) void
b) null
c) void pointer
d) null pointer
View Answer

Answer: d
Explanation: 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.

8. 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)
View Answer

Answer: c
Explanation: 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.

9. Which is the exception handler for the exceptions of downcasting?
a) CastException
b) ClassCastingExeption
c) ClassCasting
d) ClassCastException
View Answer

Answer: d
Explanation: The exception handler for the exceptions produced during the downcasting exception. This handler can be called during runtime to handle any exception thrown.

10. 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
View Answer

Answer: a
Explanation: 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.

11. Java supports direct downcasting.
a) True
b) False
View Answer

Answer: b
Explanation: The downcasting is not possible in java directly. This has to be done explicitly. The downcasting is not safe but can be checked for safe casting using instanceof function.

12. Which way the downcasting is possible with respect to inheritance?
a) Upward the inheritance order
b) Downward the inheritance order
c) Either upward or downward the inheritance order
d) Order of inheritance doesn’t matter
View Answer

Answer: b
Explanation: The downcasting is always downward the inheritance order. Since the base class object have to be casted into derived class type. This is a basic definition of downcasting.

13. What happens when downcasting is done but not explicitly defined in syntax?
a) Compile time error
b) Runtime error
c) Code write time error
d) Conversion error
View Answer

Answer: a
Explanation: The implicit downcasting is not possible. If tried, the compiler produces an error. Since the compiler doesn’t allow coasting to a type that is not compatible.

14. When is the downcasting used?
a) To separate inherited class from base class
b) To write a more complex code
c) To compare two objects
d) To disable one class in inheritance
View Answer

Answer: c
Explanation: The downcasting can be used whenever there is a need to compare one object to another. Equals() function can be used to compare whether the objects were of same age. We can use getClass() function too.

15. Why is downcasting possible in any language?
a) Because inheritance follows has-a relationship
b) Because inheritance follows is-a relationship
c) Because inheritance doesn’t follow any relationship
d) Because inheritance is not involved in casting
View Answer

Answer: b
Explanation: The downcasting is possible because the classes in inheritance follow is-a relationship. Hence the derived class is a base class. Which in turn make the downcasting possible.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter