C# .NET formatting quiz

This quiz covers most common string formatting and datetime formatting methods

This quiz is in C# .NET quiz collection.

Start quiz
  • 1.

    What is the result?

    Decimal pricePerOunce = 17.36m;
    String s = String.Format("The current price is {0} per ounce.",
                             pricePerOunce);

     

  • 2.

    What is the result?

    Decimal pricePerOunce = 17.36m;
    String s = String.Format("The current price is {0:C2} per ounce.",
                             pricePerOunce);
  • 3.

    What is the valid result of s?

    string s = String.Format("It is now {0:d} at {0:t}", DateTime.Now);

     

  • 4.

    What is the output?

    int[] years = { 2013, 2014, 2015 };
    int[] population = { 1025632, 1105967, 1148203 };
    String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population");
    for(int index = 0; index < years.Length; index++)
       s += String.Format("{0,-10} {1,-10:N0}\n",
                          years[index], population[index]);

     

  • 5.

    What is the output?

    DateTime dat = new DateTime(2017, 1, 17, 9, 30, 0); 
    string city = "Chicago";
    int temp = -16;
    string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                                  dat, city, temp);
    Console.WriteLine(output);

     

  • 6.

    What is the output?

    int value; 
          
    value = 12345;
    Console.WriteLine(value.ToString("D"));

     

  • 7.

    What is the result?

    decimal value = 123.456m;
    Console.WriteLine("Your account balance is {0:C2}.", value);
  • 8.

    What is the output?

    double value = 12345.6789;
    Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));

     

  • 9.

    What is the output?

    double value = 12345.6789;
    Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));

     

  • 10.

    What is the ouput?

    int integerNumber;
    integerNumber = 17843;
    Console.WriteLine(integerNumber.ToString("F", 
                            CultureInfo.InvariantCulture));

     

© 2017 QuizBucket.org