C# Coding Interview Questions (Part – 7)

Datetime:2016-08-22 22:10:38          Topic: C#           Share

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

public class Concat
    {
        public static void Main()
        {
            Console.WriteLine(60 + 25 + "B");
            Console.WriteLine("B" + 60 + 25);
        }
    }

Output:

85B

B6025

2. What is wrong with below code?

 class Employee
    {
        public Employee(string Name)
        {
            this.Employee(Name, null);
        }
 
        public Employee(string Name, DateTime? DateEnrolled)
        {
            this.name = Name;
            this.dateEnrolled = DateEnrolled;
        }
 
        private string name;
        private DateTime? dateEnrolled;
 
    }

Output:

public Employee(string Name)
        {
            this.Employee(Name, null);//does not contain definition for Employee
        }

You cannot call constructor code regular method access syntax, even from another constructor.

To call another constructor, use this syntax:

public Employee(string Name) : this(Name, null)
{
}

3. Will this produce an output or compiletime error?

 enum Sample { Red, Blue, Black, }
    public class program
    {
        public static void Main()
        {
          
        }
    }

Output:

The program will be compiled successfully without any error.

4. Predict the output for below program

 public class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived();
            int i = 10;
            d.Foo(i);
            Console.ReadKey();
        }
    }
    public class Base
    {
        public virtualvoid Foo(int x)
        {
            Console.WriteLine("Base.Foo(int)");
        }
    }
    public class Derived : Base
    {
        public overridevoid Foo(int x)
        {
            Console.WriteLine("Derived.Foo(int)");
        }
        public void Foo(object o)
        {
            Console.WriteLine("Derived.Foo(object)");
        }
    }

Output:

Derived.Foo(object)

5. Predict the output for below program

 class Program
    {
        static void Main(string[] args)
        {
            double d1 = 1.000001;
            double d2 = 0.000001;
            Console.WriteLine((d1 - d2) == 1.0);
            Console.ReadKey();
        }
    }

Output:

false

6. What is wrong with below program

public class Program
    {
        public static void Main(string[] args)
        {
            int i = 10;
            int j = 11;
            while (i <= j && j <= i && i != j)
            {
                Console.WriteLine("Have a nice day !!");
            }
        }
    }    

Output:

The program will be compiled successfully.

7. Predict the Output for below program.

 public class Program
    {
        public static void Main(string[] args)
        {
            int i1 = -128;
            int i2 = -128;
 
            int i3 = 128;
            int i4 = 128;
            Console.WriteLine((i1 == i2) + "," + (i3 == i4));
            
        }
    }

Output:

True,True

8. Predict the Output for below program.

 public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("H" + 'I');
            Console.WriteLine('H' + 'I');
            
        }
    }

Output:

HI

145

9. What will be the output for below program?

 public class Program
    {
        public static void Main(string[] args)
        {
            String s = "";
 
            StringBuildersb1 = new StringBuilder("Hi");
            StringBuildersb2 = new StringBuilder("Hi");
            StringBuildersb3 = new StringBuilder("Hi");
            StringBuildersb4 = sb3;
 
            if (sb1.Equals(sb2)) s += "1";
            if (sb2.Equals(sb3)) s += "2";
            if (sb3.Equals(sb4)) s += "3";
 
 
            String s1 = "Hi";
            String s2 = "Hi";
            String s3 = s2;
 
            if (s1.Equals(s2)) s += "4";
            if (s2.Equals(s3)) s += "5";
    Console.WriteLine(s);
            
        }
    }

Output:

12345

10. Predict output for below program

class Program
    {
        static void Main(string[] args)
        {
            Double i = Double.NaN;
            while (i != i)
            {
                Console.WriteLine("Welcome");
            }
        }
    }

Output:

Infinite loop with Welcome

You may also Like:

Thanks for visiting !!





About List