C# control flow quiz

  • Admin
  • 1 min read

Nine questions on conditions, switch expressions, loops, arrays and methods.

C#

C# control flow quiz

9 questions, instant feedback

Question 1 of 9
Question 1 of 9 Which condition type does an if statement require?
Why this is the answer

C# requires a bool. Unlike C, an int does not convert to bool.

Question 2 of 9
Question 2 of 9 What does the null-coalescing operator ?? return?
Why this is the answer

a ?? b evaluates to a when a is not null, otherwise b.

Question 3 of 9
Question 3 of 9 What does a switch expression need to be exhaustive?
Why this is the answer

The compiler requires every path to produce a value, so an arm for the remaining cases or an arm that throws is needed.

Question 4 of 9
Question 4 of 9 Which loop checks its condition after the body?
Why this is the answer

do-while runs the body once before testing, which suits prompts that must appear at least once.

Question 5 of 9
Question 5 of 9 What is the index range of new int[4]?
Why this is the answer

Arrays are zero-indexed, so four elements occupy 0 through 3. Accessing 4 throws IndexOutOfRangeException.

Question 6 of 9
Question 6 of 9 Which property gives an array's length?
Why this is the answer

Arrays expose Length. Lists expose Count.

Question 7 of 9
Question 7 of 9 What is an expression-bodied method?
Why this is the answer

int Square(int x) => x * x; is shorthand for a body with a single return statement.

Question 8 of 9
Question 8 of 9 What does a method returning void produce?
Why this is the answer

void methods perform an action and return no value, so they cannot be used in an expression.

Question 9 of 9
Question 9 of 9 What does the params keyword allow?
Why this is the answer

params int[] values lets a caller pass values individually, which the compiler collects into an array.

Your answers stay in this browser. Nothing is sent to a server.