PLAY READ BLOG

Assess your experience with C#

I estimate it would take you 39 steps in 15 minutes to Assess your experience with C#.
Consider this code:

string[] allNames = await GetCustomerNames();

What type must GetCustomerNames() return for this to compile?
  • void
  • Task
  • string[]
  • Task<string[]>
  • Task<IEnumerable<string>>
Look at this event declaration:

public event EventHandler<OrderAddedEventArgs> AddOrder
{
add { /* What goes here? */ }
remove { /* ... */ }
}

What should the code that replaces /* what goes here? */ do?
  • Attach a handler to the event.
  • Construct and initialize the event.
  • Determine if each handler should be invoked each time the event is raised
  • Act as a default handler for the event.
Look at the following code, which shows a base and derived class, each containing a constructor:

public class Base
{
public Base()
{
Console.WriteLine("In base");
}
}
public class Derived : Base
{
public Derived()
{
Console.WriteLine("In derived");
}
}

What will be written to the console when an instance of Derived is instantiated?
  • In derived
  • In derived In base
  • In base In derived
  • The strings, "In base" & "in derived" will both be written to the console
On the whole, which types can you inherit from?
  • class
  • struct or class that is not marked as sealed
  • class that is not marked as sealed
  • type except for the C# built-in types (int, bool, string, etc.)
Consider this implementation of an enumerator:

public IEnumerable<string> GetEnumerator()
{
try
{
yield return "Hello";
yield return "World";
yield return "!";
}
finally
{
Console.WriteLine("In finally block!");
}
}

If this is enumerated, at what point will the message "In finally block!" be written to the console?
  • Once, after the first item in the enumeration has been returned.
  • It won't, the yield return bypasses the finally block.
  • Three times, once after returning each item in the enumeration.
  • Once, after the enumeration is complete.
Given the following code:

string[] names = { "C#", "VB", "F#" };
var lengths = from name in names select name.Length;

Given that the string.Length property returns an int, what type will the compiler deduce lengths to be?
  • IEnumerable<int>
  • IEnumerable<object>
  • ICollection<int>
  • List<int>
  • int[ ]
Which operator would you use to attach a handler to an event?
  • ->
  • =
  • ++
  • =>
  • +=
Which C# statement can be used to prevent multiple threads from simultaneously attempting to modify a value?
  • synchronize
  • await
  • block
  • lock
Which data type would normally be recommended to store monetary values intended to be used in currency calculations?
  • float
  • decimal
  • double
  • ulong
Where would you normally use the C# await operator?
  • Inside the body of a lock statement
  • In a class or struct that is marked async
  • In a method that is marked async
  • In a method whose immediate caller is marked async
💡FUN FACT 💡The name "C sharp" was inspired by the musical notation where a sharp indicates that the written note should be made a semitone higher in pitch.
What is the main purpose of an extension method?
1. To add additional behaviour to an existing method

2. To add functionality to an existing type without directly modifying that type

3. To add functionality to a derived class not available in its base class
  • 1.
  • 2.
  • 3.
Will the following code compile? If not, why not?

IEnumerable<int> values = new List<int>();
1. Yes.

2. No, because List<T> does not implement IEnumerable<T>

3. No, because interfaces cannot be instantiated

4. No, because the List<T> constructor requires specifying the initial list size
  • 1.
  • 2.
  • 3.
  • 4.
What is the significance of the keyword this in the following code?

if (this._myVar == null) { //
1. It tells the compiler that the expression refers to either a static or instance member field, even if a local variable of the same name exists.

2. It tells the compiler that the expression _myVar refers to an instance field, even if a local variable of the same name exists.

3. It tells the compiler that the expression refers to an instance field, even if a static field of the same name exists.
  • 1.
  • 2.
  • 3.
Given the following:

string greeting = "Hello, World!";

Strings in C# are immutable. What does that fact mean for the above variable declaration?
1. The string that greeting points to can never be modified, but greeting can be modified to point to a different string.

2. greeting cannot be modified unless it is additionally declared as volatile.

3. greeting will always refer to the same memory location as long as it is in scope (but the contents of that memory may change).

4. greeting cannot be modified; it always contains "Hello, World!" as long as it is in scope.
  • 1.
  • 2.
  • 3.
  • 4.
What does the following statement do?

using System;
1. Allows types in the System namespace to be referenced without specifying the namespace explicitly.

2. Imports the System.dll assembly AND allows types in that assembly to be referenced without specifying the namespace.

3. Imports the System.dll assembly, so that types in that assembly can be used.
  • 1.
  • 2.
  • 3.
Consider the following code:

public class OperatingSystem
{
protected int _version;
/* other members */
}

The _version field is visible to which types?
1. Any types declared within the same assembly as OperatingSystem

2. OperatingSystem and any classes declared as friends of OperatingSystem

3. OperatingSystem and any classes that inherit from OperatingSystem

4. Only OperatingSystem
  • 1.
  • 2.
  • 3.
  • 4.
What is the problem with the following code that causes it not to compile?

try
{
LoadTheData();
}
catch (Exception ex)
{
LogTheError(ex);
}
catch (OperationCanceledException ex)
{
NotifyCancel();
}
1. Only one catch block can be associated with each try block.

2. The variable ex is not used in the last catch block.

3. The first catch block will catch every exception making the second catch block unreachable.

4. The variable ex has been declared twice in the same context.
  • 1.
  • 2.
  • 3.
  • 4.
What does this expression evaluate to?

3 - 5 + 2 * 4
  • 0
  • -10
  • 6
  • -9
  • -25
Get Started   

Or consider 74 other intentions.