PrepAway - Latest Free Exam Questions & Answers

Which code segment should you insert at line 03?

You are developing a game that allows players to collect from 0 through 1000 coins. You are creating
a method that will be used in the game. The method includes the following code. (Line numbers are
included for reference only.)

01 public string FormatCoins(string name, int coins)
02 {
03
04 }
The method must meet the following requirements:
Return a string that includes the player name and the number of coins.
Display the number of coins without leading zeros if the number is 1 or greater.
Display the number of coins as a single 0 if the number is 0.
You need to ensure that the method meets the requirements.
Which code segment should you insert at line 03?

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

2 Comments on “Which code segment should you insert at line 03?

  1. Hans Werner says:

    It’s not answer “D”, since this one throws an exception!

    Answer “A” meets the requirements, but is unnecessary overcomplicated with the additional ToString.

    For everyone to verify:

    string name = “Hans Werner”;
    int coins = 123;

    // Output: “Player Hans Werner collected 123 coins.”
    Console.WriteLine(String.Format(“Player {0} collected {1} coins.”, name, coins.ToString(“###0”)));

    // Output: “Player Hans Werner collected 0123 coins.”
    Console.WriteLine(String.Format(“Player {0} collected {1:000#} coins.”, name, coins));

    // Throws: “System.FormatException: Invalid format.”
    Console.WriteLine(String.Format(“Player {name} collected {coins.ToString(‘000’)} coins.”));

    // Throws “System.FormatException: The index of a format item is less than zero, or greater than or equal to the number of arguments in the arguments list.”
    Console.WriteLine(String.Format(“Player {1} collected {2:D3} coins.”, name, coins));




    4



    0

Leave a Reply