0
0
C Sharp (C#)programming~10 mins

String interpolation and formatting in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the variable name using string interpolation.

C Sharp (C#)
string name = "Alice";
Console.WriteLine($"Hello, [1]!");
Drag options to blanks, or click blank then click option'
Auser
B"name"
CName
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the variable name inside the braces, which prints the word instead of the value.
Using a different variable name that does not exist.
2fill in blank
medium

Complete the code to format the number score with two decimal places using string interpolation.

C Sharp (C#)
double score = 95.6789;
Console.WriteLine($"Score: [1]{score}");
Drag options to blanks, or click blank then click option'
A:F2
B:F1
C:N2
D:P2
Attempts:
3 left
💡 Hint
Common Mistakes
Using :F1 which shows only one decimal place.
Using :P2 which formats as percentage.
Using :N2 which adds commas but also shows decimals.
3fill in blank
hard

Fix the error in the code to correctly align the text to the right with width 10 using string interpolation.

C Sharp (C#)
string word = "Hi";
Console.WriteLine($"[1],10}");
Drag options to blanks, or click blank then click option'
A"word"
Bword
CWord
Dword.ToString()
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the variable name, which prints the word 'word' instead of the variable value.
Using a capitalized variable name that does not exist.
4fill in blank
hard

Fill both blanks to create a formatted string that shows the price with 2 decimals and left-aligns the item in 15 spaces.

C Sharp (C#)
string item = "Apple";
double price = 1.2345;
string output = $"[1],-15} costs [2]:F2}";
Drag options to blanks, or click blank then click option'
Aitem
Bprice
Citem.ToUpper()
Dprice.ToString()
Attempts:
3 left
💡 Hint
Common Mistakes
Using price in the first blank which is a number, not a string.
Using item.ToUpper() which changes the text but is not required.
Using price.ToString() which is unnecessary inside interpolation.
5fill in blank
hard

Fill all three blanks to create a string that shows the uppercase city, the temperature with 1 decimal, and the unit symbol.

C Sharp (C#)
string city = "Paris";
double temperature = 23.456;
string unit = "°C";
string report = $"[1]: [2]:F1}[3]";
Drag options to blanks, or click blank then click option'
Acity.ToUpper()
Btemperature
Cunit
Dcity
Attempts:
3 left
💡 Hint
Common Mistakes
Using city instead of city.ToUpper() for uppercase.
Putting quotes around variables inside braces.
Not formatting the temperature to one decimal.