This test is yours. You answer the questions, then open the solution and count your points yourself. We don’t see your answers, there’s nothing to submit, and you get no grade from us.

How to get the most out of it:

  • Answer each question in writing first, on paper or in a text file, and only then open the solution.
  • Do not run the programs in the questions before you’ve answered. Afterward you may; it’s even good practice.
  • Take about 30 minutes. There’s no time limit.

Questions 1 to 16 are worth one point each, programming task 17 up to four. That makes 20 points in all.

Question 1

What does the compiler do?

  • a) It runs the program line by line and shows the output.
  • b) It checks the source code and translates it into a form the computer can run.
  • c) It saves the program’s files.
  • d) It downloads missing programs from the internet.

Solution 1

b) The compiler checks and translates. If it finds an error, the program doesn’t even start. Only the translated program is run. (lesson 1)

Question 2

You’ve saved calculation.cs, and your terminal is in the same folder. Which command starts the program?

  • a) run calculation.cs
  • b) dotnet run calculation.cs
  • c) csharp calculation
  • d) calculation.cs --start

Solution 2

b) dotnet run calculation.cs. The short form dotnet calculation.cs counts as correct too. (lesson 3)

Question 3

What does this program print?

C#
// question-3.cs
Console.Write("A");
Console.WriteLine("B");
Console.Write("C");
Console.Write("D");
Console.WriteLine();
Console.WriteLine("E");

Solution 3

AB
CD
E

Write writes without ending the line; WriteLine ends it afterward. Console.WriteLine() without any text just ends the line. (lesson 3)

Question 4

When you start a program, the terminal reports:

notes.cs(12,40): error CS1002: ; expected

What does this message tell you, and what do you do?

Solution 4

In the file notes.cs, on line 12 at character 40, the compiler noticed that a semicolon is missing. It’s an error (error), so the program doesn’t start. You add the semicolon, usually at the end of the statement on line 12, save and start again. If there are several messages, fix the top one first. You get a point if you named the line, the missing semicolon and “doesn’t start.” (lesson 3)

Question 5

Which type fits each one best: string, int, double or bool?

  • a) the number of notes
  • b) the title of a book
  • c) whether a task is done
  • d) a body temperature in degrees Fahrenheit, such as 98.6

Solution 5

a) int, b) string, c) bool, d) double. You get the point only if all four are right. (lesson 4)

Question 6

What does this program print?

C#
// question-6.cs
int a = 23;
int b = 4;
Console.WriteLine(a / b);
Console.WriteLine(a % b);

Solution 6

5
3

Both numbers are int, so a / b is an integer division: 4 goes into 23 five times, and the remainder is dropped. a % b is exactly that remainder: 23 minus 20 is 3. (lesson 4)

Question 7

What does this program print?

C#
// question-7.cs
string input = "5";
Console.WriteLine(input + 3);
Console.WriteLine(int.Parse(input) + 3);

Solution 7

53
8

input is a text. Text plus a number tacks the number onto the text. Only int.Parse turns the text “5” into the number 5, and then the program does the math. (lesson 4)

Question 8

A program reads input with int.Parse, and someone types “twelve.” What happens, and how do you prevent it?

Solution 8

int.Parse can’t make a number out of “twelve” and throws an exception (FormatException). The program stops, and the exit code isn’t 0. You can prevent this with int.TryParse in an if: it reports with true or false whether the conversion worked, and the program can ask again politely. (lessons 4 and 5)

Question 9

What does this program print?

C#
// question-9.cs
int points = 70;
if (points >= 90)
{
    Console.WriteLine("excellent");
}
else if (points >= 60)
{
    Console.WriteLine("passed");
}
else if (points >= 50)
{
    Console.WriteLine("almost");
}
else
{
    Console.WriteLine("not yet");
}

Solution 9

passed

Just one line. 70 isn’t at least 90, but it is at least 60. The first condition that holds wins; the others aren’t even checked. That 70 is also at least 50 doesn’t matter. (lesson 5)

Question 10

Assume int age = 20; and bool member = false;. Which of these conditions are true? There may be more than one.

  • a) age >= 18 && member
  • b) age >= 18 || member
  • c) !member
  • d) age > 20

Solution 10

b) and c). a) is false, because with “and” both sides must be true, and member is false. b) is true, because with “or” one true side is enough. c) is true, because “not false” is true. d) is false, because 20 isn’t greater than 20; for that you’d need >=. You get the point only for exactly b) and c). To check:

C#
// question-10.cs
int age = 20;
bool member = false;
Console.WriteLine(age >= 18 && member);
Console.WriteLine(age >= 18 || member);
Console.WriteLine(!member);
Console.WriteLine(age > 20);
False
True
True
False

(lesson 5)

Question 11

How many numbers does this loop print, and which are the first and the last?

C#
// question-11.cs
for (int i = 0; i < 5; i++)
{
    Console.Write($"{i} ");
}
Console.WriteLine();

Solution 11

Five numbers, from 0 to 4:

0 1 2 3 4

The counter starts at 0. The condition i < 5 holds for 0, 1, 2, 3 and 4. At 5 it’s false, and the loop ends. (lesson 6)

Question 12

This program prints nothing and never ends. Why? How do you stop it, and how do you fix it?

C#
// question-12.cs
int number = 10;
int sum = 0;
while (number > 0)
{
    sum += number;
}
Console.WriteLine(sum);

Solution 12

Inside the block, number never changes. The condition number > 0 stays true forever, and the loop runs without end. You stop the program with Ctrl+C in the terminal. You fix it by making number smaller in every round:

C#
// question-12.cs
int number = 10;
int sum = 0;
while (number > 0)
{
    sum += number;
    number--;
}
Console.WriteLine(sum);
55

You get a point if you answered all three parts: why, Ctrl+C and the missing line. (lesson 6)

Question 13

What does this program print? And what would happen with animals[3] in the last line?

C#
// question-13.cs
List<string> animals = ["Dog", "Cat", "Mouse"];
animals.Add("Hedgehog");
animals.RemoveAt(0);
Console.WriteLine(animals.Count);
Console.WriteLine(animals[0]);
Console.WriteLine(animals[animals.Count - 1]);

Solution 13

3
Cat
Hedgehog

After Add there are four animals; RemoveAt(0) removes the first one, the dog. What’s left is cat, mouse and hedgehog, counted from 0. The last item has the number Count - 1, so 2. animals[3] doesn’t exist: the program would stop with an ArgumentOutOfRangeException. You get the point for the output and the stop. (lesson 7)

Question 14

What does this program print?

C#
// question-14.cs
Console.WriteLine(Double(Double(3)));
Console.WriteLine(Greet("Ada"));

static int Double(int number)
{
    return number * 2;
}

static string Greet(string name)
{
    return $"Hello, {name}";
}

Solution 14

12
Hello, Ada

The inner call is worked out first: Double(3) is 6, and Double(6) is 12. Greet doesn’t print the text; it gives it back with return, and only Console.WriteLine writes it. So a method can give back a text too, not just a number. (lesson 8)

Question 15

A program calls File.WriteAllLines("list.txt", lines);. Where is the file list.txt afterward?

  • a) always in the folder where the .cs file is
  • b) in the current folder of the terminal the program was started in
  • c) on the desktop
  • d) nowhere, as long as the program didn’t end with 0

Solution 15

b) A plain file name refers to the current folder. If you start the program in the folder that also holds the .cs file, that’s the same folder. That’s why a) often looks right, but it isn’t always. (lesson 10)

Question 16

A program writes “Done!” at the end, but the exit code is 1. What’s true?

  • a) Everything worked, because the program wrote “Done!”
  • b) Something went wrong, because only exit code 0 means success.
  • c) The exit code only counts on Windows.

Solution 16

b) Whether a program succeeded is decided by the exit code, not by a message that looks friendly. That holds on every system. (lesson 3)

Question 17

The programming task, worth up to four points. Write threes.cs. On one line, it prints every number from 1 to 30 that can be divided by 3, separated by spaces. The line below it shows their sum:

3 6 9 12 15 18 21 24 27 30
Sum: 165

This time you may, and should, run your program.

Solution 17

One possible solution:

C#
// threes.cs
int sum = 0;
for (int number = 1; number <= 30; number++)
{
    if (number % 3 == 0)
    {
        Console.Write($"{number} ");
        sum += number;
    }
}
Console.WriteLine();
Console.WriteLine($"Sum: {sum}");

A loop that counts in steps of three right away is just as correct: for (int number = 3; number <= 30; number += 3), then without the if. Give yourself points like this:

  • 1 point: The program starts without an error and without a warning.
  • 1 point: The numbers come from a loop, not written out by hand.
  • 1 point: Exactly the ten numbers from 3 to 30 appear.
  • 1 point: The sum is 165, and the program calculates it.

A space at the end of the first line isn’t a mistake.

Your score

Add up your points.

  • 17 to 20 points: You’ve understood the essentials and can apply them. You’re well prepared for the next step.
  • 11 to 16 points: The basics are in place. Read the lessons again for the questions where you didn’t get a point, and do the exercises there once more without looking at the solution.
  • 0 to 10 points: That’s perfectly normal at the start, and it says little about your talent. Go through lessons 4 to 8 again at your own pace and type in every example. Take the test again in a few days.

Which question belongs to which lesson:

Questions Lesson
1 1 What a program is
2, 3, 4, 16 3 Your first program
5, 6, 7, 8 4 Variables, math and input
8, 9, 10 5 Decisions
11, 12, 17 6 Loops
13 7 Lists
14 8 Methods of your own
15 10 Saving notes

Does programming suit you?

Points show what you know. Whether programming suits you, they show only in part. Answer these questions for yourself, with no points at all:

  • Did you feel the urge to keep hunting for an error until the program ran?
  • Did you have ideas of your own for what else the notebook should be able to do?
  • While practicing, did the time sometimes go by faster than you expected?
  • Do you find it more exciting than annoying that one forgotten semicolon holds everything up?
  • With a solution, did you want to know why it works, and not just that it works?

If you say yes several times, a lot suggests that programming suits you, even if not every question above went smoothly yet. Knowledge comes with practice; the curiosity is something you bring yourself. And if you notice that it isn’t for you, you’ve learned something important about yourself in a few hours. That’s a good result too.