C#.NET — Trace static vs instance calls and method overloading to predict console output. Program: namespace CuriousTabConsoleApplication { class Sample { public static void fun1() { Console.WriteLine("CuriousTab1 method"); } public void fun2() { fun1(); Console.WriteLine("CuriousTab2 method"); } public void fun2(int i) { Console.WriteLine(i); fun2(); } } class MyProgram { static void Main(string[] args) { Sample s = new Sample(); Sample.fun1(); s.fun2(123); } } } What will be printed in order?

Difficulty: Easy

Correct Answer: Tab1 method 123 Tabl method Tab2 method

Explanation:


Introduction / Context:
This item checks understanding of static vs instance method calls and overload resolution, along with the exact order of outputs as control flows across fun1, fun2(), and fun2(int). The answer options use abbreviated labels ("Tab1"/"Tab2").


Given Data / Assumptions:

  • fun1() is static and writes a “Tab1 method” line.
  • fun2() calls fun1() then writes “Tab2 method”.
  • fun2(int) writes the integer, then calls fun2().
  • Main calls Sample.fun1() then s.fun2(123).


Concept / Approach:
Follow the sequence: a static call, then an overload that prints and invokes the parameterless overload, which in turn invokes the static method and prints again. Keep line order exact.


Step-by-Step Solution:

Main → Sample.fun1() prints “Tab1 method”. Main → s.fun2(123) prints “123”. fun2(int) → fun2() → calls fun1() again, printing “Tab1 method”. fun2() then prints “Tab2 method”.


Verification / Alternative check:
Replace WriteLine calls with numbered markers to confirm the order: 1 (Tab1), 2 (123), 3 (Tab1), 4 (Tab2).


Why Other Options Are Wrong:

  • B omits the second “Tab1 method”.
  • C/D/E rearrange or omit required lines.


Common Pitfalls:
Forgetting that fun2(int) delegates to fun2(), which again calls fun1() before writing “Tab2 method”.


Final Answer:
Tab1 method → 123 → Tab1 method → Tab2 method

More Questions from Constructors

Discussion & Comments

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