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
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
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
Explanation: None.
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
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.
5. What will be the output of the following C# code snippet?
class B { }
class A : B { }
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
if (a is A)
Console.WriteLine("a is an A");
if (b is A)
Console.WriteLine("b is an A because it is derived from A");
if (a is B)
Console.WriteLine("This won’t display -- a not derived from B");
Console.ReadLine();
}
}
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
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
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
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?
class A {}
class B : A {}
class CheckCast
{
static void Main()
{
A a = new A();
B b = new B();
b = a as B;
b = null;
if(b==null)
Console.WriteLine("The cast in b = (B) a is NOT allowed.");
else
Console.WriteLine("The cast in b = (B) a is allowed");
}
}
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
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
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?
class UseTypeof
{
static void Main()
{
Type t = typeof(StreamReader);
Console.WriteLine(t.FullName);
if(t.IsClass) Console.WriteLine("Is a class.");
if(t.IsAbstract) Console.WriteLine("Is abstract.");
else Console.WriteLine("Is concrete.");
}
}
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 concreteView Answer
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.
- Check C# Books
- Practice MCA MCQs
- Apply for Computer Science Internship
- Check MCA Books
- Apply for C# Internship