Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Console.ReadLine for input in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Console Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this code reading input?

Consider the following C# program that reads a line from the console and prints it back with a greeting.

using System;
class Program {
  static void Main() {
    string name = Console.ReadLine();
    Console.WriteLine($"Hello, {name}!");
  }
}

If the user types Anna and presses Enter, what will be printed?

C Sharp (C#)
using System;
class Program {
  static void Main() {
    string name = Console.ReadLine();
    Console.WriteLine($"Hello, {name}!");
  }
}
AHello, !
BHello, Anna!
CHello, name!
DError: name is not defined
Attempts:
2 left
💡 Hint

Remember that Console.ReadLine() reads the exact text the user types.

❓ Predict Output
intermediate
1:30remaining
What happens if no input is given?

Look at this C# code:

using System;
class Program {
  static void Main() {
    string input = Console.ReadLine();
    if (input == "") {
      Console.WriteLine("Empty input");
    } else {
      Console.WriteLine("Input received");
    }
  }
}

If the user just presses Enter without typing anything, what will be printed?

C Sharp (C#)
using System;
class Program {
  static void Main() {
    string input = Console.ReadLine();
    if (input == "") {
      Console.WriteLine("Empty input");
    } else {
      Console.WriteLine("Input received");
    }
  }
}
ANo output
BInput received
CError: input is null
DEmpty input
Attempts:
2 left
💡 Hint

Pressing Enter without typing anything returns an empty string, not null.

🔧 Debug
advanced
2:00remaining
Why does this code throw an exception?

Examine this C# code snippet:

using System;
class Program {
  static void Main() {
    int number = int.Parse(Console.ReadLine());
    Console.WriteLine(number * 2);
  }
}

If the user types abc and presses Enter, what error will occur?

C Sharp (C#)
using System;
class Program {
  static void Main() {
    int number = int.Parse(Console.ReadLine());
    Console.WriteLine(number * 2);
  }
}
AFormatException
BDivideByZeroException
CNullReferenceException
DNo error, output is 0
Attempts:
2 left
💡 Hint

Think about what happens when you try to convert a non-number string to an integer.

📝 Syntax
advanced
1:30remaining
Which option correctly reads and converts input to double?

Which of the following C# code snippets correctly reads a line from the console and converts it to a double variable named value?

Adouble value = double.Parse(Console.ReadLine());
Bdouble value = Convert.ToDouble(Console.ReadLine());
Cdouble value = double.Parse(Console.ReadLine);
Ddouble value = Convert.ToDouble(Console.ReadLine);
Attempts:
2 left
💡 Hint

Remember to call methods with parentheses ().

🚀 Application
expert
2:30remaining
How many times will the loop run given this input?

Consider this C# program:

using System;
class Program {
  static void Main() {
    int count = int.Parse(Console.ReadLine());
    int sum = 0;
    for (int i = 0; i < count; i++) {
      int num = int.Parse(Console.ReadLine());
      sum += num;
    }
    Console.WriteLine(sum);
  }
}

If the user inputs the following lines:

3
10
20
30

How many times will the for loop run, and what will be the output?

ALoop runs 3 times; output is 60
BLoop runs 4 times; output is 60
CLoop runs 3 times; output is 100
DLoop runs 0 times; output is 0
Attempts:
2 left
💡 Hint

The first input line tells how many numbers to read next.

Practice

(1/5)
1. What does Console.ReadLine() do in a C# program?
easy
A. Reads a line of text input from the user as a string
B. Prints a line of text to the console
C. Terminates the program immediately
D. Converts a string to an integer

Solution

  1. Step 1: Understand the purpose of Console.ReadLine()

    Console.ReadLine() waits for the user to type something and press Enter, then returns that input as a string.
  2. Step 2: Differentiate from other console methods

    Console.WriteLine prints text, and conversion methods change data types, but Console.ReadLine specifically reads input as string.
  3. Final Answer:

    Reads a line of text input from the user as a string -> Option A
  4. Quick Check:

    Console.ReadLine() = reads string input [OK]
Hint: Remember: ReadLine always returns a string from user input [OK]
Common Mistakes:
  • Thinking Console.ReadLine prints output
  • Assuming it converts input automatically
  • Confusing it with Console.ReadKey
2. Which of the following is the correct way to read a user's input into a string variable named name?
easy
A. string name = Console.Read();
B. string name = Console.ReadLine();
C. string name = Console.WriteLine();
D. string name = Console.ReadKey();

Solution

  1. Step 1: Identify the correct method to read a full line

    Console.ReadLine() reads the entire line of input as a string, suitable for storing in a string variable.
  2. Step 2: Check other options for correctness

    Console.Read() reads a single character as int, Console.WriteLine() outputs text, Console.ReadKey() reads a single key press.
  3. Final Answer:

    string name = Console.ReadLine(); -> Option B
  4. Quick Check:

    Use Console.ReadLine() to read full string input [OK]
Hint: Use Console.ReadLine() to get full user input line [OK]
Common Mistakes:
  • Using Console.Read() which reads only one character
  • Confusing ReadLine with WriteLine
  • Using ReadKey which reads a single key press
3. What will be the output of this code if the user types 25 and presses Enter?
Console.WriteLine("Enter your age:");
string input = Console.ReadLine();
int age = int.Parse(input);
Console.WriteLine($"You are {age} years old.");
medium
A. You are age years old.
B. You are input years old.
C. You are 25 years old.
D. Runtime error

Solution

  1. Step 1: Read input as string and convert to int

    The user types "25", Console.ReadLine() returns "25" as string, then int.Parse converts it to integer 25.
  2. Step 2: Output the formatted string with age value

    The Console.WriteLine prints "You are 25 years old." using string interpolation with the integer age.
  3. Final Answer:

    You are 25 years old. -> Option C
  4. Quick Check:

    Input "25" parsed to int 25 outputs correct message [OK]
Hint: Parse input string to int before using as number [OK]
Common Mistakes:
  • Not converting string to int before use
  • Using variable names as literal text
  • Expecting Console.ReadLine to return int directly
4. Identify the error in this code snippet:
Console.WriteLine("Enter a number:");
int number = Console.ReadLine();
Console.WriteLine(number);
medium
A. Console.ReadLine() cannot be assigned directly to an int variable
B. Console.WriteLine should be Console.ReadLine
C. Missing semicolon after Console.WriteLine
D. int cannot be used for numbers

Solution

  1. Step 1: Check the assignment of Console.ReadLine()

    Console.ReadLine() returns a string, but the code tries to assign it directly to an int variable, causing a type error.
  2. Step 2: Correct way to convert input string to int

    The input string should be converted using int.Parse() or int.TryParse() before assigning to an int variable.
  3. Final Answer:

    Console.ReadLine() cannot be assigned directly to an int variable -> Option A
  4. Quick Check:

    Convert string input to int before assignment [OK]
Hint: Always convert Console.ReadLine() string to int before assignment [OK]
Common Mistakes:
  • Assigning string directly to int without conversion
  • Confusing Console.WriteLine and Console.ReadLine
  • Ignoring type mismatch errors
5. You want to read two numbers from the user, add them, and print the result. Which code correctly does this using Console.ReadLine()?
hard
A. Console.WriteLine("Enter two numbers:"); int sum = Console.ReadLine() + Console.ReadLine(); Console.WriteLine(sum);
B. string a = Console.ReadLine(); string b = Console.ReadLine(); int sum = a + b; Console.WriteLine(sum);
C. int a = Console.ReadLine(); int b = Console.ReadLine(); int sum = a + b; Console.WriteLine(sum);
D. int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int sum = a + b; Console.WriteLine(sum);

Solution

  1. Step 1: Read input lines and convert to integers

    Use int.Parse(Console.ReadLine()) to convert each input string to an integer before adding.
  2. Step 2: Add the integers and print the sum

    Sum the two integers and print the result using Console.WriteLine.
  3. Final Answer:

    int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int sum = a + b; Console.WriteLine(sum); -> Option D
  4. Quick Check:

    Convert inputs to int before adding to get correct sum [OK]
Hint: Parse inputs to int before adding numbers [OK]
Common Mistakes:
  • Adding strings instead of integers
  • Assigning Console.ReadLine() directly to int
  • Not converting input before arithmetic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard