Quiz Yourself: Classes and OOP II

Assess your understanding of inheritance, polymorphism, casting, and static members in C#.

We'll cover the following...
Technical Quiz
1.

You have defined an explicit cast from Fahrenheit to Celsius.

public static explicit operator Celsius(Fahrenheit f) { ... }

What happens if you try to assign a Fahrenheit object to a Celsius variable without a cast?

Fahrenheit f = new Fahrenheit(100);
Celsius c = f; // Missing cast
A.

A compiler error occurs because the explicit cast syntax is missing.

B.

The code compiles, but the value of c is set to null.

C.

The compiler automatically treats it as an implicit cast to prevent data loss.

D.

A runtime InvalidCastException is thrown immediately.


1 / 12