C#.NET — Determine the printed output for the following program (note: the loop intentionally iterates to Length - 1, so the last element is skipped): namespace CuriousTabConsoleApplication { class SampleProgram { static void Main(string[] args) { object[] o = new object[] {"1", 4.0, "India", 'B'}; fun(o); } static void fun(params object[] obj) { for (int i = 0; i < obj.Length - 1; i++) Console.Write(obj[i] + " "); } } }

Difficulty: Medium

Correct Answer: 1 4 India

Explanation:


Introduction / Context:
This question tests understanding of params arrays, object boxing, ToString behavior, and loop bounds. The program prints selected elements of an object[] using a for loop that stops before the last element.



Given Data / Assumptions:

  • Array elements: "1" (string), 4.0 (double), "India" (string), 'B' (char).
  • The loop condition is i < obj.Length - 1, so the last element ('B') is never printed.
  • Console.Write(obj[i] + " ") concatenates using ToString.


Concept / Approach:
For a double like 4.0, default ToString with the general format produces "4" rather than "4.0" in most cultures. Strings are written as they are. Because the loop excludes the final index, only the first three elements are output.



Step-by-Step Solution:

Iteration 0 → prints "1".Iteration 1 → prints 4.0.ToString() → "4".Iteration 2 → prints "India".Iteration 3 would print 'B', but the loop ends at index 2.


Verification / Alternative check:
Manually evaluate ToString outputs and loop bounds or run a quick console test.



Why Other Options Are Wrong:

  • 1 4.0 India B: Includes 'B' and shows 4.0 with a trailing .0, which is not printed.
  • 1 4.0 India: Trailing .0 is incorrect.
  • 1 India B: Skips 4.
  • None of these: Incorrect because "1 4 India" matches.


Common Pitfalls:
Assuming the loop reaches the last element or that 4.0 prints as "4.0".



Final Answer:
1 4 India

More Questions from Functions and Subroutines

Discussion & Comments

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