C# Coding Interview Questions (Part – 6)

Datetime:2016-08-22 22:11:20          Topic: C#           Share

1. What happens when you compile and run the below program?

public class Program
    {
        public static void Main()
        {
            int[] data = { 1, 2, 3 };
            int i = 1;
            data[i++] = data[i] + 10;
            Console.WriteLine(String.Join(",", data));
        }
    }

Output:

1,13,3

2. Predict the output for below program.

class Program
    {
        public static int i = 0;
        public static void Main()
        {
            (i++).Print();
        }
    }
 
    static class Extensions
    {
        public static void Print(this int i)
        {
            Console.WriteLine(Program.i);
            Console.WriteLine(i);
        }
    }

Output:

1

0

3. What will be the output for below program?

    class Program
    {
        public static void Main()
        {
            var test = Singleton.Instance;
        }
    }
 
    class Singleton
    {
        static readonlySingleton_instance = new Singleton();
 
        public static Singleton Instance { get { return _instance; } }
 
        private Singleton()
        {
            Console.WriteLine("Instance Constructor");
        }
 
        static Singleton()
        {
            Console.WriteLine("Static Constructor");
        }
    }

Output:

Instance Constructor

Static Constructor

4. Predict the output for below Program

    class Program
    {
        public static void Main()
        {
            Testtest = new Test();
            test.Sample();
 
            ITest i = test;
            i.Sample();
        }
 
        public interface ITest
        {
            void Sample(string val = "Interface Default");
        }
 
        public class Test : ITest
        {
            public void Sample(string val = "Class Default")
            {
                Console.WriteLine(val);
            }
        }
    }

Output:

Class Default

Interface Default

5. What will be the output for below program?

 class Program
    {
        public static void Main()
        {
            int i = 0;
            (i++);
            Console.WriteLine(i);
        }
    }

Output:

Compilation error. you need to remove the bracket around i++ to fix the error.

6. What is wrong in below code?

 class Outer
    {
        static int sField = 0;
 
        public class Nested
        {
            public virtualint GetFieldValue()
            {
                return sField;
            }
        }
    }
 
    class SubClass : Outer.Nested
    {
        public overrideint GetFieldValue()
        {
            return sField + 5;
        }
    }

Output:

sField is accessible within nested class and not in scope for subclass.

7. What is the output for below program?

 class Program
    {
        public static void Main()
        {
            string s1 = "\U0010FADE";
            string s2 = "\U0000FADE";
            Console.WriteLine(s1.Length);
            Console.WriteLine(s2.Length);
        }
      
    }

Output:

2

1

8. Predict the output for below program

class Program
    {
        public static void Main()
        {
            int[] singleDimension = { 1, 2, 3, 4 };
            int[,] multiDimension = { { 1, 2 }, { 3, 4 } };
            int[][] jagged = { new int[] { 1, 2 }, new int[] { 3, 4 } };
 
            Console.WriteLine(singleDimension.Length);
            Console.WriteLine(multiDimension.Length);
            Console.WriteLine(jagged.Length);
        }
      
    }

Output:

4

4

2

9. What will be the Output for below program?

 class Program
    {
      public static void Main()
        {
            float number = 56, zero = 0;
 
            try
            {
                Console.WriteLine(number / zero);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Division By Zero");
            }
        }
      
    }

Output:

Infinity

Only integral (int, long, short, byte, uint, ulong, ushort, sbyte, char) and the decimal types throw a DivisionByZero exception when divided by zero.

Float and Double types will return values such as Infinity or NaN.

10. What will be the Output for below program?

class Program
    {
      public static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.Write(i + ' ');
            }
        }
      
    }

Output:

The output is 32333435363738394041.

A char is a numeric value that is expressed as a character so 1 + ‘ ‘ is 1 + 32.

You may also Like:

Thanks for visiting !!





About List