C# Coding Interview Questions (Part – 5)

Datetime:2016-08-22 22:12:09          Topic: C#           Share

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

namespace InterviewNamespace
{
    delegatevoid A(refstring str);
    public class sample
    {
        public static void DelegatePuzzle(refstring a)
        {
            a = a.Substring(5, a.Length - 5);
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            A str1;
            string str = "Welcome to CsharpStar !!";
            str1 = sample.DelegatePuzzle;
            str1(refstr);
            Console.WriteLine(str);
        }
    }
}

Output:

Welcome to CsharpStar !!

2. Predict the output for below program.

 public class Program
    {
        public static void Main(string[] args)
        {
        #if (DEBUG && !MYTEST)
                    Console.WriteLine("DEBUG is defined");
        #elif (!DEBUG && MYTEST)
                Console.WriteLine("MYTEST is defined");
        #elif (DEBUG && MYTEST)
                Console.WriteLine("DEBUG and MYTEST are defined");
        #else
                Console.WriteLine("DEBUG and MYTEST are not defined");
        #endif
            Console.ReadLine();
        }
    }

Output:

DEBUG is defined

3. What will be the output for below program?

public class Program
    {
        public static void Main(string[] args)
        {
            int a = 4;
            int c = 2;
            bool b = (a % c == 0 ? true : false);
            Console.WriteLine(b.ToString());
            if (a / c == 2)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
            Console.ReadLine();
        }
    }

Output:

True

true

4. Predict the output for below Program

 public class Program
    {
        public static void Main(string[] args)
        {
            bool a = true;
            bool b = false;
            a |= b;
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }

Output:

True

5. What will be the output for below program?

 public class Program
    {
        public static void Main(string[] args)
        {
            {
                try
                {
                    throw new NullReferenceException("C");
                    Console.WriteLine("A");
                }
                catch (ArithmeticException e)
                {
                    Console.WriteLine("B");
                }
                Console.ReadLine();
            }
        }
    }

Output:

System.NullReferenceException:C

6. Predict the output for below Program.

namespace InterviewNamespace
{
    class list
    {
        ArrayListarray = new ArrayList();
        public object this[int index]
        {
            get
            {
                if (index < 0 || index >= array.Count)
                {
                    return null;
                }
                else
                {
                    return (array[index]);
                }
            }
            set
            {
                array[index] = value;
            }
        }
        public int Count
        {
            get;
            set;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            listlist1 = new list();
            list1[0] = "123";
            list1[1] = " abc ";
            list1[2] = "xyz";
            for (int i = 0; i <= list1.Count; i++)
                Console.WriteLine(list1[i]);
            Console.ReadLine();
        }
    }
}

Output:

Compiletime Error.Index out of range which arises only when index is non negative or less than the collection of size

7. What will be the output for below program?

namespace InterviewNamespace
{
    class access
    {
        public int x;
        private int y;
        public void cal(int a, int b)
        {
            x = a + 1;
            y = b;
        }
    }
    public class Program
    {
 
        public static void Main(string[] args)
        {
 
            accessobj = new access();
            obj.cal(2, 3);
            Console.WriteLine(obj.x + " " + obj.y);
        }
    }
}

Output:

Compile time error.‘y’ is defined privately which cannot be accessed outside its scope.

8. Predict the Output for below program

public class Program
    {
      
      public static void Main(string[] args)
        {
          int x = 100;
          int i = 100;
          x += i;    
          x = x + i;
        }
    }

Output:

Compiled successfully

9. What will be the output for below program?

 class Program
    {
        static void Main(string[] args)
        {
            char A = 'G';
            char B = Convert.ToChar(76);
            A++;
            B++;
            Console.WriteLine(A + "  " + B);
            Console.ReadLine();
        }
    }

Output:

H M

10. Predict the output for below program?

  class Program
    {
        static void Main(string[] args)
        {
            String s1 = "Welcome to Csharpstar";
            String s2 = "welcome to topjavatutorial";
            String s3 = s2;
            Console.WriteLine((s1 == s2) + " " + s2.Equals(s3));
            Console.ReadLine();
        }
 
    }
}

Output:

false True

Thanks for Visiting !!

© 2016,admin. All rights reserved.





About List