Warm-up Exercises

  1. Assuming that myLastName and myFirstName are two string variables that have been initialized, write a statement that concatenates them, with a space and a comma in-between, and assign the resulting string to a variable named fullName. For instance, if the value of myLastName is “Holbertonand”, and the value of myFirstName is “Betty”, then the value of fullName after your operation should be “Holbertonand, Betty”.

Solution

Console.Write($"{myLastName}, {myFirstName}");

Questions

  1. What is string interpolation?

Solution

String interpolation is the use of variables in within a string.

  1. What is the difference, if any, between the WriteLine and Write methods?

Solution

The WriteLine method appends a newline character to the end of the argument passed into it while Write does not.

  1. Assume we have a variable whose name is myVariable, whose type is string, and whose value is “My message”. What would be displayed at the screen by the following statement?

Console.WriteLine($"Here is my variable: {myVariable}");

Solution

“My message”

  1. Is the name myVariable the same as myvariable? If not, why?

Solution

The variable names myVariable and myvariable are different because variable names are case sensitive.

Problems

  1. Write down, on a piece of paper, a program that:

  2. Declares a string variable named userName.

  3. Displays on the screen Please, enter your name, followed by enter:.

  4. Reads a string value from the keyboard and assigns the value to the userName variable.

  5. Declares an int variable named number.

  6. Displays on the screen Please, enter your area code, followed by enter: .

  7. Reads an int value from the keyboard and assigns the value to the number variable.

  8. Declares a string variable named id and initializes it with the string referenced by the userName variable, followed by the number entered by the user (you can concatenate a string and an int using the ` sign).

  9. Displays on the screen, Your id is and the content of the id variable.

Here is an example of execution, where the user input is underlined, and hitting “enter” is represented by ↵:

Please, enter your name, followed by enter:
Clément
Please, enter your area code, followed by enter:
828 ↵
Your ID is Clément828
Press any key to continue . . .

Solution

string userName;
 
Console.WriteLine("Please enter your name, followed by enter.");
userName = Console.ReadLine();
 
int number;
 
Console.WriteLine("Please enter your area code, followed by enter.");
number = int.Parse(Console.ReadLine());
 
string id = userName + number;
 
Console.WriteLine("Your ID is " + id);
  1. Write a series of statements that:
    1. Declare a string variable named favoriteColor
    2. Display on the screen a message asking the user his or her favorite color
    3. Read the value entered by the user and store it in the favoriteColor variable. You can combine some of the statements if you want, but do not display on the screen any information that was not explicitly asked.

Solution

string favoriteColor;
 
Console.Write("Please enter your favorite color: ");
favoriteColor = Console.ReadLine();