C#.NET — What does this program print? namespace CuriousTabConsoleApplication { class SampleProgram { static void Main(string[] args) { string str = "Hello World!"; Console.WriteLine(String.Compare(str, "Hello World?").GetType()); } } } Predict the exact console output when the program runs.

Difficulty: Easy

Correct Answer: System.Int32

Explanation:


Introduction / Context:
This C#.NET snippet tests understanding of return types, boxing of value types, and how Console.WriteLine formats objects. Specifically, it explores what happens when you call GetType() on the result of String.Compare.



Given Data / Assumptions:

  • String.Compare(string, string) returns an int (-1, 0, or 1 depending on ordering).
  • GetType() is available on all objects and value types (via boxing semantics and methods defined on System.ValueType).
  • Console.WriteLine calls ToString() on the object it receives.


Concept / Approach:
String.Compare(str, "Hello World?") produces an integer. Calling GetType() on an int yields a System.Type object that represents the type of the value, namely System.Int32. Printing a Type using WriteLine invokes its ToString(), which returns the fully qualified type name.



Step-by-Step Solution:

Compute comparison → some integer (the actual numeric value is irrelevant to the output).Call GetType() on that integer → returns a Type representing System.Int32.Print the Type → outputs the string System.Int32.


Verification / Alternative check:
Replace the call with Console.WriteLine(typeof(int)); the output is also System.Int32, confirming the behavior.



Why Other Options Are Wrong:
0 or 1 are possible compare results but not what gets printed. String and Hello World? are not the Type name. The exact printed value is the fully qualified type name.



Common Pitfalls:
Confusing the comparison result with the printed type, or assuming GetType() is unavailable on value types.



Final Answer:
System.Int32

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion