Pull to refresh

C# and .NET tricky questions

Reading time4 min
Views15K


I have collected some most interesting and not well-known questions from the web. And also based on interesting books, articles and videos I have seen, I have wrote some of questions myself.
I want propose you to guess right answer.


1. What would be displayed? (if something would be displayed at all)

string nullString = (string)null;
Console.WriteLine (nullString is string);

Answer
False, because Null doesn’t have type in runtime
Credits: https://www.dotnetcurry.com/csharp/1417/csharp-common-mistakes


2. What would be displayed on the screen in .NET console application?

Console.WriteLine (Math.Round(1.5));
Console.WriteLine (Math.Round(2.5));

Answer
2 and 2. Because by default rounding to the nearest even number is known as Bankers' Rounding. Surprisingly in Mono result would be 2 and 3.


3. What would be the result of

static void Main(string[] args)
{
    float f = Sum(0.1f, 0.2f);
    float g = Sum(0.1f, 0.2f);
    Console.WriteLine(f == g);
}

static float Sum(float f1, float f2)
{
    return f1 + f2;
}

a) True
b) False
c) Exception would be thrown
d) That depend (could be either true or false)

Answer
d
When running normally, the JIT can store the result of the sum more accurately than a float can really represent — it can use the default x86 80-bit representation, for instance, for the sum itself, the return value, and the local variable.
Credits:
Binary floating point and .NET


4. What would be the result of

float price = 4.99F;
int quantity = 17;
float total = price * quantity;
Console.WriteLine("The total price is ${0}.", total);

a) The total price is $85
b) The total price is $84.83
c) The total price is $84
d) The total price is $84.82999



5. What is the right way to store passwords in database? Choose one or multiple correct answers

a) In plain text
b) Encrypted with DES
c) Encrypted with AES
d) Hashed with MD5
e) Hashed with SHA512

Answer
e
Don’t use MD5 anymore
By the way it is also possible to use keyed hashing algorithms: HMACSHA1 or MACTripleDES


6. In between different .NET technologies there is a lot of timers. Which one timer does not exist?

a) Timer from System.Windows.Forms
b) DispatchTimer from System.Windows.Threading
c) DispatchTimer from Windows.UI.XAML
d) Timer from System.Timers
e) Timer from System.Windows.Threading.Timers
f) Timer from System.Threading

Answer
e
Credits: CLR via C# Jeffrey Richter


7. Which one .NET REPL does not exists?

a) dotnetfiddle.net
b) repl.it/languages/csharp
c) csharpcompiler.net
d) dotnet.microsoft.com/platform/try-dotnet
e) csharppad.com

Answer
c


8. If you want to set a type long for a number in C# you can add at the end letter L or l. For example:

var l = 138l;

With which letter it is possible to mark a decimal type?

a) C or c
b) D or d
c) M or m
d) E or e

Answer
c


9. What will display on the screen console application that will contain next code:

     class Program
    {
         static Program()
        {
            Console.WriteLine("Static constructor");
        }

         public Program()
        {
            Console.WriteLine("Constructor");
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Main");
            Console.ReadLine();
        }
    }


Answer
«Static constructor» and «Main»
As you might know, static constructor is called automatically before the first instance is created or any static members are referenced. And only then public constructor is called. But in this case, we are in console application and public constructor doesn’t called.


10. What would be displayed as result?

      [Flags]
      public enum Status 
      {
         Funny = 0x01,
         Hilarious = 0x02,
         Boring = 0x04,
         Cool = 0x08,
         Interesting = 0x10,
         Informative = 0x20,
         Error = 0x40
      }

  public static void Main (string[] args) {
         var code = 24;
         Console.WriteLine (String.Format("This Quiz is: {0}",  (Status)code));
      }

Answer
This Quiz is: Cool Interesting

24 that would be 0011000 in binary system
Writing numbers in column
Funny = 0
Hilarious = 0
Boring = 0
Cool = 1
Interesting = 1
Informative = 0
Error = 0
Credits:
C# Often Surprises Me, part: 000001
Enumeration types (C# reference)


11. Where is a difference between String and string?

Answer
String is .NET alias for c# string. Similar like short and int in C# are mapped to Int16 and Int32 in .NET
Use next link to get more details Difference between string and String in C#


12. What does mean (or at least has meant initially) Visual Studio sign?

Answer
Intersection of Intranet and Internet worlds.
Credits: That Visual Studio logo--it's not what you think it is


13. Please match:
Async/await
Try/catch
ValidateAntiForgeryToken
with abbreviations:
TAP, SEH, STP

Answer
Async/await -> TAP (Task asynchronous programming model)
Try/catch -> SEH (Structured Exception Handling)
ValidateAntiForgeryToken -> STP (Synchronizer Token Pattern)


14. Which one is not .NET CMS?

a) mojoPortal
b) N2 CMS
c) Atomic CMS
d) Composite C1
e) Concrete5
f) Piranha CMS

Answer
e


15. What would be displayed on the screen?

  
     class Program
    {
        private static int y = x;
        private static int x = 5;

        static void Main(string[] args)
        {
            Console.WriteLine(y);
            Console.ReadLine();
        }
    }

Answer
0
Sometimes declaring variables order plays a role.


16. Which Concurrent Collection class is missing in .Net

a) ConcurrentQueue
b) ConcurrentStack
c) ConcurrentList
d) ConcurrentDictionary
e) ConcurrentBag

Answer
c


17. What is the preferable way to run async code synchronously (if it’s not possible to use await):

a) Wait()
b) Result()
c) GetAwaiter().GetResult()

Answer
c
Wait and Result are throwing aggregate exception instead of normal
Credits: Correcting Common Async/Await Mistakes in .NET — Brandon Minnick
Tags:
Hubs:
Total votes 4: ↑1 and ↓3-2
Comments3

Articles