Tag: TechQuiz

TechQuiz Question 8: On precedence and operators

What is the output of the following program? class Program {     static void Main()     {         int result = x() + y() *         Console.WriteLine(result     }     static int x()     {         Console.WriteLine(“x”);         return 3;     }     static int y()     {         Console.WriteLine(“y”);         return 2;     }     static int z()    ...

Tech Quiz: Some micro optimization

Optimize the following code: doubletotalCount = 0; for (int index = 0; index < numberOfRuns; index++) {     string[] values = GetData(index);     totalCount += values.Count(); } Answer A simple optimization is to replace Count() with Length. Count() is an extension method that looks something like this: public static int Count<TSource>(this IEnumerable<TSource> source){    if (source == null)    { ...

TechQuiz Question 7: Calling derived methods..

What is the result of the following code? using System; class Base {     public virtual void Foo(int x) { Console.WriteLine(“Base.Foo(int)”); } } class Derived : Base {     public override void Foo(int x)  { Console.WriteLine(“Derived.Foo(int)”); }     public void Foo(object o)  { Console.WriteLine(“Derived.Foo(object)”); } } class Test {     static void Main()     {         Derived d = new...

TechQuiz Question 6: Getting some closure…

What will be the output of the following code? using System; using System.Collections.Generic; class Test {     delegate void Printer();         static void Main()     {         List<Printer> printers = new List<Printer>();         for (int i = 0; i < 10; i++)         {             printers.Add(delegate { Console.WriteLine(i); });         }                 foreach (Printer printer in printers)        ...

TechQuiz Question 1 Why doesn’t my code throw an exception?

Inside VincisAqtion we’ve started with a little TechQuiz. We’re planning on  creating weekly questions to publish them on our intranet and next to the coffee machine. The first question is: Why doesn’t the following code throw an exception? static IEnumerable<char> CapitalLetters(string input){    if (input == null)    {        throw new ArgumentNullException(input);    }    foreach (char c in input)    {        yield return char.ToUpper(c);    }}          static void Main()         {             CapitalLetters(null);         } The code...