250+ TOP MCQs on Runtime Type and Answers

C# MCQs 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

Answer: b
Clarification: 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

Answer: d
Clarification: 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

Answer: c
Clarification: None.

4. What does the following C# code signify?

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

  250+ TOP MCQs on Structures and Answers

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

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

  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

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
Clarification: 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

  250+ TOP MCQs on Character Stream and Answers

Answer: b
Clarification: 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

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

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

Answer: b
Clarification: 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

Answer: c
Clarification: 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

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top