Tricky Questions about C#

Some questions may seem too basic, but they still contain tiny tricks. Sometimes even a simple question may nail to the wall. These questions will be useful to all who study the language.

So, let’s start!

1. What will be the result of execution of the following code?

Answer

str == null

1/1/0001 12:00:00 AM

Both variables are not initialized, but a string is a reference type (to be more specific, it is an immutable type, which means reference type with the semantics of the value type). DateTime is a value type. The default value of the initialized DateTime type is 1/1/0001 12:00:00 AM.

2. Let’s play with inheritance. What will be outputted?

Answer

abc from B.

3. A similar question. What will be the result?

Answer

abc from B.

Here everything is more obvious than in the previous question.

4. A typical polymorphism understanding swindle. The main thing is not to forget and overlook anything. What will be the result of execution of the following code?

Answer

constructor A
constructor B
A

During initialization of the B class, the constructor of the A class will be executed by default, then constructor of the B class. After assignment of the b value to a type variable of the A class, we will get an instance of the B class in it. One would think that abc() from the B class should be called, but since there is no specification of any predicate of the abc method in the B class, it hides abc from the A class. The example is not quite correct and abc() in the B class will be underlined, since the new predicate is required.

5. I have the following class:

Also, I have 3 instances of the class. Will the following initialization of the third instance work? If it won’t, what should be done?
Answer

Of course, the example will not work. To make this code work, we need to add the addition operator overloading to the Point class. For example, the following one:

6. What will be the execution result of the following query?

Answer

An empty string will be outputted instead of ‘Hello world!’. The SaySomething() task was called without await, and that is why, SaySomethinf is executed synchronously till the first await, i.e. till the first string:
After this, execution is returned to btnStartClick. If we use await during the call of SaySomething(), the result will be expected, – ‘Hello world’ will be outputted.

7. A must-know question. What will be the execution result of the following query?

Answer

The program will output the number 10 times.

The delegate was added 10 times. The reference to the i variable was taken. The reference, not the value. That is why, when calling a delegate, the last value of the i variable is taken. It is a typical example of closure.

8. What will be outputted with the following code?

Answer

Method 1

Method 2

The if block won’t be executed since SomeMethod1 returns false. But, since the & logical operator is used, the second condition, SomeMethod2, will be also checked. If a more conventional && operator was used, only the value of the first method would be checked.

9. One more simple question. What will be the output of the following code?

Answer

This code won’t work and will throw an exception in the last string. However, the next casting (in other words, explicit conversion) of the error should not call the error, but just looses fractional part of a number.But during unboxing, the object is checked for the value of the requested type. And the value is copied into the variable only after the first check. And the next one will make unboxing without the error:

But during unboxing, the object is checked for the value of the requested type. And the value is copied into the variable only after the first check.

And the next one will make unboxing without the error:

The following code will firtsly execute upcast of the o object to the dynamic tye, and then will make casting, not unboxing:

However, it is similar to the following code:

As a result, it will be identical to the first example:

Although, it seemed to be a new trick.

10. What will happen after the execution of the following code?

Answer

Infinity will be outputted.

Float and double are not integral types. That is why, if we use checked, overflow does not take place. Though, if we used int, byte, short or long, an expected error would occur. Unchecked will not work with not built-in types as well. For example, the following code:

will trhow System.OverflowException.

11. Let’s play with the decimal type. Will something be outputted? If it will, what will it be?

Answer

This example will output 0 because x has not been reduced to the decimal type. Since x is of the integer type, when 5 is divided by 12, the result is a number less than 1, that is, an integral zero. The correct result can be obtained with the following line:

12. What is the output of the following code?

Answer

Division of the double and float types by 0 will return infinity, while decimal will throw System.DivideByZeroException.

Just in case, a few words about about the diffs between decimal and float:

  • decimal (128-bit data type with precision of 28-29 digits) is used for finacial calculations which require high precision and no errors in rounding;
  • double ( a 54-bit type with precision accuracy to 15-16 digits) is a basic type for storing floating-point values. Used in most cases (except financial);
  • float (32-bit data type with precision of 7 digits) – a type with the lowest precision and range, but with the highest performance. Rounding errors are possible. Used for heavy-loaded calculations.

13. Consider, we have the following method:

Can we call it in the following way?
Answer

Yes, we can. We can also call it in a different way:

But we can’t call it in the following way:

UPDATE: As from C# 7.2, making such call might become impossible.

14. What is the output of the following code?

Answer

Nothing bad will happen. The following will be outputted:

2
1
1

Since we call SomeMethod2 with the out keyword first, someInt may be passed without initialization. If we used SomeMethod or SomeMethod1, the compilation error would occur.

Since SomeMethod does not contain the ref or out keyword in the parameter, the value is passed by value, not by reference, which means that someInt will not be changed.

The ref and out keywords mean that the values are passed by reference. But in the second case, a value must be assigned to the parameter in the method. In our example, in the SomeMethod2 method, a value must be assigned to the value parameter.

15. Will the following code work?

Answer

Yes, but it looks strange. Only Bye, cruel world! will be outputted. Inside the method, we can declare inclusive local region between curly braces. Variables from this region will be unavailable outside of it. That is, the following code will not Fbe compiled:

Strange enough, goto is still not supported in C#. Though, it is not required much.

16. What will be outputted?

Answer

True, True, False will be outputted.

It is a typical example of string interning. Situations when strings storing the same value represent one object in memory. This mechanizm allows using memory more sparingly.

17. Can pointers be used in C# as in C++?

Answer

Yes, they can be used inside the method declared with the unsafe modifier or within the unsafe block.

Do not forget to specify ‘Allow unsafe code’ in the project properties.

18. What are verbatim strings?

Answer

Everybody knows what these strings are and constantly uses them, but not everyone knows their name. Verbatim strings are strings that start with the @ symbol in which the escape sequences are not processed.

See Also:

Alexey Sommer

Alexey Sommer

Alexey Sommer is mostly working on UWP development. He has the MVP award in the Windows Development category, as well as MCP and MCSD certificates.
Alexey Sommer

Latest posts by Alexey Sommer (see all)

Alexey Sommer

Alexey Sommer is mostly working on UWP development. He has the MVP award in the Windows Development category, as well as MCP and MCSD certificates.

  • jethro daniel

    This is definatley a good read

  • I believe you have a stray “not” in the last line of the answer to #15.