Module 1 · First programs: see what each line does · Lesson 2 of 20
Values and variables: give your program something to remember
Asha changes her plan from 30 to 45 minutes. Will a reminder copied earlier change too? Predict both numbers before running the program.
Predict a tiny plan
In the previous lesson, the messages were fixed text. A variable gives a value a name that later instructions can use. Read the program below and predict the three output lines. In the middle line, the slash is just printed text separating two numbers.
using System;
string learner = "Asha";
int plannedMinutes = 30;
int reminderMinutes = plannedMinutes;
bool finished = false;
plannedMinutes = 45;
Console.WriteLine(learner);
Console.WriteLine($"{plannedMinutes}/{reminderMinutes}");
Console.WriteLine(finished);What does the middle line print?
Show answer
Correct answer: C
reminderMinutes received the int value 30 when it was assigned. A later assignment changes plannedMinutes to 45.
Option A: plannedMinutes is assigned 45 before printing.
Option B: Copying the int value did not create a continuing link between the variables.
Option D: The interpolation prints plannedMinutes before reminderMinutes.
Hint: Write the value of each variable after each assignment.
Three types you can use today
The type describes what kind of value a variable can hold. In this program, string holds text, int holds a whole number within its supported range, and bool holds true or false. The words true and false are C# values, so they have no quotes. Writing "false" would create text instead. When WriteLine displays a bool, its normal text is True or False with a capital first letter.
| Variable | Declared type | Initial value | Value when printed |
|---|---|---|---|
| learner | string | Asha | Asha |
| plannedMinutes | int | 30 | 45 |
| reminderMinutes | int | 30 copied from plannedMinutes | 30 |
| finished | bool | false | False |
The equals sign means assign the value on the right to the variable on the left. First calculate or read the right-hand side, then store the result. This is why assigning plannedMinutes = plannedMinutes + 10 is useful: it reads the old value, adds 10, then replaces that variable’s value.
Trace the two duration variables
- plannedMinutes starts with 30.
- reminderMinutes receives its own int value 30.
- plannedMinutes is assigned 45.
- Printing reads the current values: 45 and 30.
Which declared type best fits a whole-number count of planned study minutes?
Show answer
Correct answer: B
int represents whole-number values and supports arithmetic on them.
Option A: A string can contain digits as text, but that text is not an int value.
Option C: bool has only the two logical values true and false.
Option D: Console is the API used for console input and output here, not a duration type.
Put values inside a message
The dollar sign immediately before the opening quote makes an interpolated string. A name inside braces is evaluated and its value is inserted into the message. The surrounding words and punctuation remain text. Without the dollar sign, a normal string prints the braces and variable name literally.
Choose names that explain the value and its unit: plannedMinutes is clearer than x. Keep the declared type in mind when editing. Assigning a text name to an int variable is a type error; changing a variable’s value does not change its declared type. You can update a variable many times, but each update happens only when that instruction executes.
Build a different plan
Start in a blank Program.cs. Create a learner named Ravi, start with 20 planned minutes, add 10 minutes, and mark finished as true. Print the learner, duration and finished state. Predict before running; your result should match this complete solution:
using System;
string learner = "Ravi";
int plannedMinutes = 20;
plannedMinutes = plannedMinutes + 10;
bool finished = true;
Console.WriteLine($"{learner}: {plannedMinutes} minutes");
Console.WriteLine($"Finished: {finished}");Why does the solution print Ravi: 30 minutes?
Show answer
Correct answer: D
The arithmetic assignment produces 30. The interpolated string then reads that value.
Option A: The expression in braces is evaluated; its variable name is not printed as text.
Option B: Interpolation formats a message; it does not repeat instructions.
Option C: finished is separate from the duration calculation.
Try changing only the initial duration to 35. The duration line should now contain 45. If your result differs, trace the assignments rather than changing several lines at once.
Explain back: Why does reminderMinutes stay at 30 in the first program? What does the dollar sign change about a string?
References: Microsoft — built-in type keywords, string interpolation.
Quick reference
- string stores text, int stores whole numbers, and bool stores true or false.
- A variable has a declared type and a current value.
- Assignment evaluates the right-hand side, then stores its value on the left.
- Copying an int value does not keep two variables linked.
- An interpolated string starts with $ and uses braces around expressions.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·