Challenge - 5 Problems
Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of implicit and explicit conversions
What is the output of the following C# code?
C Sharp (C#)
int a = 100; long b = a; int c = (int)b; Console.WriteLine(c);
Attempts:
2 left
💡 Hint
Implicit conversion from int to long is safe; explicit cast back to int is valid here.
✗ Incorrect
The int value 100 is implicitly converted to long without data loss. Casting back to int recovers the original value.
❓ Predict Output
intermediate2:00remaining
Result of casting double to int
What is the output of this C# code snippet?
C Sharp (C#)
double d = 9.99; int i = (int)d; Console.WriteLine(i);
Attempts:
2 left
💡 Hint
Casting from double to int truncates the decimal part.
✗ Incorrect
Casting double to int drops the fractional part, so 9.99 becomes 9.
❓ Predict Output
advanced2:00remaining
Behavior of unchecked overflow in casting
What will be printed by this C# code?
C Sharp (C#)
byte b = 255; int i = b + 1; byte c = (byte)i; Console.WriteLine(c);
Attempts:
2 left
💡 Hint
Casting int to byte truncates higher bits without exception in unchecked context.
✗ Incorrect
Adding 1 to 255 gives 256, which cast to byte wraps around to 0 due to overflow.
❓ Predict Output
advanced2:00remaining
Casting reference types and invalid cast exception
What happens when this C# code runs?
C Sharp (C#)
object obj = "hello"; int num = (int)obj; Console.WriteLine(num);
Attempts:
2 left
💡 Hint
Casting object holding a string to int is invalid.
✗ Incorrect
The cast tries to convert a string object to int, which is not allowed and throws an exception.
❓ Predict Output
expert3:00remaining
Output of custom conversion operator
Given the following code, what is the output?
C Sharp (C#)
class Temperature { public double Celsius { get; set; } public static explicit operator int(Temperature t) { return (int)(t.Celsius * 9 / 5 + 32); } } Temperature temp = new Temperature { Celsius = 25 }; int fahrenheit = (int)temp; Console.WriteLine(fahrenheit);
Attempts:
2 left
💡 Hint
The explicit operator converts Celsius to Fahrenheit as int.
✗ Incorrect
The explicit cast uses the operator to convert 25 Celsius to 77 Fahrenheit.