C# objects quiz

  • Admin
  • 1 min read

Nine questions on classes, constructors, properties, inheritance and interfaces.

C#

C# objects quiz

9 questions, instant feedback

Question 1 of 9
Question 1 of 9 What does a property add over a public field?
Why this is the answer

A property can validate, compute or restrict access while keeping the calling syntax simple.

Question 2 of 9
Question 2 of 9 What does an auto-property { get; set; } generate?
Why this is the answer

The compiler creates a private field and the get and set bodies for you.

Question 3 of 9
Question 3 of 9 What does init allow on a property?
Why this is the answer

init accessors make a property settable in an object initialiser or constructor, then immutable.

Question 4 of 9
Question 4 of 9 What does a constructor do?
Why this is the answer

The constructor runs on creation and is the place to validate and assign required state.

Question 5 of 9
Question 5 of 9 Which keyword prevents a class from being inherited?
Why this is the answer

sealed stops inheritance. Sealing a class is a commitment that its behaviour will not be extended.

Question 6 of 9
Question 6 of 9 What is required to override a method?
Why this is the answer

C# requires the base method to be virtual, abstract or override, and the derived method to use override.

Question 7 of 9
Question 7 of 9 What does an interface member without a body require?
Why this is the answer

An interface declaration is a contract; implementing classes must supply the member unless a default implementation exists.

Question 8 of 9
Question 8 of 9 What does a record give you?
Why this is the answer

Records generate value-based equality, a readable ToString and non-destructive mutation through with.

Question 9 of 9
Question 9 of 9 What does the override of ToString affect?
Why this is the answer

ToString is used by string interpolation, Console.WriteLine and debuggers, which makes it worth writing.

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