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

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

Choose your learning style9 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.