C# Question & Answers – Runtime Type

This section of our 1000+ C# MCQs focuses on runtime type in in C# Programming Languag

1. Which mechanism among the following helps in identifying a type during the execution of a program?
a) Reflection
b) Runtime type ID
c) Both Reflection & Runtime type ID
d) None of the mentioned
View Answer

Answer: b
Explanation: Runtime type ID is the mechanism that lets identify a type during the execution of a program. Using Runtime type ID we can construct and use objects at runtime.

2. Select the statement which are correct about RTTI(Runtime type identification)?
a) It allows the type of an object to be determined during program execution
b) It tells what type of object is being referred to by a base class reference determined by RTTI
c) Helps in prevention of an invalid cast exception in advance
d) All of the mentioned
View Answer

Answer: d
Explanation: Runtime type identification (RTTI) allows the type of an object to be determined during program execution. RTTI is useful for many reasons. For example, we can discover precisely what type of object is being referred to by a base-class reference. Another use of RTTI is to test in advance whether a cast will succeed, preventing an invalid cast exception.

3. Select the Keyword which supports the run time type identification?
a) is, as
b) as, typeof
c) Both is, as & as, typeof
d) Only is, as
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

4. What does the following C# code signify?

expr is type

a) Determines the type of an object
b) a simple deceleration
c) Both Determines the type of an object & a simple deceleration
d) None of the mentioned
View Answer

Answer: a
Explanation: The given expression determines the type of an object using the ‘is’ operator. Here, expr is an expression that describes an object whose type is being tested against type. If the type of expr is the same as, or compatible with, type, then the outcome of this operation is true. Otherwise, it is false.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. What will be the output of the following C# code snippet?

advertisement
  1. class B { }
  2. class A : B { }
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         A a = new A();
  8.         B b = new B();
  9.         if (a is A) 
  10.         Console.WriteLine("a is an A");
  11.         if (b is A)
  12.         Console.WriteLine("b is an A because it is derived from A");
  13.         if (a is B)
  14.         Console.WriteLine("This won’t display -- a not derived from B");
  15.         Console.ReadLine();
  16.     }
  17. }

a)

a is an A
This won’t display -- a not derived from B
advertisement

b)

a is an A
b is an A because it is derived from A

c)

b is an A because it is derived from A
This won’t display -- a not derived from B

d)

"Both ""a is an A
This won’t display — a not derived from B"" & ""a is an A
b is an A because it is derived from A"""
View Answer
Answer: a
Explanation: We have to include the line ‘This won’t display — a not derived from B’ this is because ‘a’ is object of class ‘A’ which itself is derived from class ‘B’. So, ‘a’ is a B
Output:

       a is an A
        This won’t display -- a not derived from B
 
 

6. Which operator among the following supports the operation of conversion at runtime without generating the exceptions?
a) is
b) as
c) typeof
d) all of the mentioned
View Answer

Answer: b
Explanation: By definition.

7. Which operator among the following is used to perform the operation of boxing, unboxing, reference and identity conversions?
a) is
b) as
c) typeof
d) all of the mentioned
View Answer

Answer: b
Explanation: use the as operator, which has this general form:

     expr as type

Here, expr is the expression being converted to type. If the conversion succeeds, then a reference to type is returned. Otherwise, a null reference is returned. The as operator can be used to perform only reference, boxing, unboxing, or identity conversions.

8. What will be the output of the following C# code snippet?

  1. class A {}
  2. class B : A {}
  3. class CheckCast 
  4. {
  5.     static void Main() 
  6.     {
  7.         A a = new A();
  8.         B b = new B();
  9.         b = a as B;
  10.         b = null;
  11.         if(b==null)
  12.         Console.WriteLine("The cast in b = (B) a is NOT allowed.");
  13.         else
  14.         Console.WriteLine("The cast in b = (B) a is allowed");
  15.     }
  16. }

a) Run time error
b) The cast in b = (B) a is NOT allowed
c) The cast in b = (B) a is allowed
d) Compile time error
View Answer

Answer: b
Explanation: since a is not a B, the cast of a to B is invalid and is prevented by the if statement.
Output:

The cast in b = (B) a is NOT allowed

9. Which operator among the following supplies the information about the characteristics of a typeof?
a) is
b) as
c) typeof
d) none of the mentioned
View Answer

Answer: c
Explanation: C# supplies the typeof operator. It retrieves a System.Type object for a given type. Using this object, we can determine the type’s characteristics.

10. What will be the output of the following C# code snippet?

  1. class UseTypeof 
  2. {
  3.     static void Main() 
  4.     {
  5.         Type t = typeof(StreamReader);
  6.         Console.WriteLine(t.FullName);
  7.         if(t.IsClass) Console.WriteLine("Is a class.");
  8.         if(t.IsAbstract) Console.WriteLine("Is abstract.");
  9.         else Console.WriteLine("Is concrete.");
  10.     }
  11. }

a)

Is a class
Is abstract

b) Is abstract
c)

System.IO.StreamReader
Is a class
Is concrete

d)

Both Is a class
Is abstract & System.IO.StreamReader
Is a class
Is concrete
View Answer
Answer: c
Explanation: This program obtains a Type object that describes StreamReader. It then displays the fullname, and determines if it is a class and whether it is abstract.
Output:

         System.IO.StreamReader
         Is a class
         Is concrete
 
 

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.