Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Upcasting”.
1. What is upcasting?
a) Casting subtype to supertype
b) Casting super type to subtype
c) Casting subtype to super type and vice versa
d) Casting anytype to any other type
Answer: a
Clarification: The upcasting concept includes only the casting of subtypes to the super types. This casting is generally done implicitly. Smaller size types can fit into larger size types implicitly.
2. Which among the following is true for upcasting in inheritance?
a) Downward to the inheritance tree
b) Upward to the inheritance tree
c) Either upward or downward
d) Doesn’t apply on inheritance
Answer: b
Clarification: The upcasting concept in inheritance is always applied upward the inheritance tree. The derived class objects can be type casted to any of its parent class type. Since is a relationship applies in general inheritance.
3. Which among the following is safe?
a) Upcasting
b) Downcasting
c) Both upcasting and downcasting
d) If upcasting is safe then downcasting is not, and vice versa
Answer: a
Clarification: The upcasting is always safe since the derived type or the smaller type is converted into the base type or the larger size. This results in allocating a smaller size data into bigger type data. No data is lost in casting, hence safe.
4. Which among the following is the best situation to use upcasting?
a) For general code dealing with only subtype
b) For general code dealing with only supertype
c) For general code dealing with both the supertype and subtype
d) For writing a rigid code with respect to subtype
Answer: b
Clarification: When a general code has to be written where we use only the supertype object or the data of bigger size, then upcasting would be the best option. Since the whole code will require only the supertype name references.
5. Which property is shown most when upcasting is used?
a) Code reusability
b) Code efficiency
c) Complex code simple syntax
d) Encapsulation
Answer: c
Clarification: The code written using upcasting mostly shows complex code in simpler syntax features. This is because the upcasting concept can be applied as polymorphism and to group the similar type of objects.
6. Upcasting and downcasting objects are the same as casting primitive types.
a) True
b) False
Answer: b
Clarification: It is a bit confusing concept since both casting concepts are different. Primitive casting depends on the type and size of data being typecast. Whereas in objects casting, the classes and inheritance order plays a big role.
7. Which casting among the following is allowed for the code given below?
class A { public :int a; } class B:public A { int b; } main() { B b=new A(); //casting 1 A a=new B(); //casting 2 }
a) Casting 1
b) Casting 2
c) casting 1 and casting 2
d) casting 1 nor casting 2
Answer: b
Clarification: The casting 2 is correct. The objects casting must be done from derived class object to a parent class object. That is, the object of the superclass can be made an object of subclass only. Vice versa is not possible.
8. If multiple inheritance is implemented, which upcasting will be correct?
a) Upcast to first base class listed in inheritance
b) Upcast to send base class listed in inheritance
c) Upcast to any base class
d) Upcast is not possible
