Interview Questions (Click on Question to view the answer)

Data Types

Q 1 : How many Bytes 'Long' Data type takes in C#?
A. 8
B. 4
C. 2
D. 1

Ans : A (8)

Q 2 : Choose .Net class Name from which data type UInt is derivevd?
A. System.Int16
B. System.UInt32
C. System.UInt64
D. System.UInt16

Ans : B ( System.UInt32 )

Q 3 : What is the Size of 'Char' data type?
A. 8 bit
B. 12 bit
C. 16 bit
D. 20 bit

Ans : C ( 16 bit )

Q 4 : Which of the following is not a valid csharp data type ?
A. int
B. var
C. struct
D. None of the above

Ans : D (All the types are valid types in c#)

Q 5 : Which of the following is not a reference type ?
A. string
B. enum
C. Object
D. None of the above

Ans : B (enum)

Q 6 : Difference between int.Parse,Convert.ToInt32 and int.TryParse ?

 int.Parse :-  Int32.Parse (string s) method converts the string representation of a number to its 32-bit signed integer equivalent. When s is a null reference, it will throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException. For example:
                                    
 string s1 = "4444"; 
string s2 = "4444.23"; 
string s3 = null; 
string s4 = "9999999990000000001111111111234523232323232"; 
int r; 
r = Int32.Parse(s1); //-- 4444 
r = Int32.Parse(s2); //-- FormatException 
r = Int32.Parse(s3); //-- ArgumentNullException 
r = Int32.Parse(s4); //-- OverflowException 
                               
Convert.ToInt32(string ) :- Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException
result = Convert.ToInt32(s1); //-- 4444 
result = Convert.ToInt32(s2); //-- FormatException 
result = Convert.ToInt32(s3); //-- 0 
result = Convert.ToInt32(s4); //-- OverflowException 
                               
Int32.TryParse(string, out int) :- Int32.Parse(string s, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. In case of null reference, it will return 0 rather than throw ArgumentNullException. In case of other than an integer value, the out variable will have 0 rather than FormatException. When string represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException
success = Int32.TryParse(s1, out result); //-- success => true; result => 4444
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0

Q 7 : What will be the output of below program ?
 class TechiesEra
    {
        static void Main(string[] args)
        {
            int a = 260;
            byte b = (byte)a;
            Console.Write(b);
            Console.Read();
        }
    }

A. Compile time error
B. 260
C. 4
D. None of the above

Ans : C (4) byte range is from 0 to 255. when it will jump to 256 then counter will again start from 0 so out put will be 4.

Q 8 : which of the following is not correct initilization in c# ?
A. int i = -32768
B. byte = -2
C. byte = 268
D. int i = 32768

Ans : B (byte = -2) byte range is from 0 to 255. we can not store any negative value in byte.

Q 9 : What will be the output of below program ?
class TechiesEra
    {
        static void Main(string[] args)
        {
            byte someVar=10;
            someVar = someVar - 3;
            Console.Read();
        }
    }

A. 7
B. 10
C. -3
D. compile time error

D (Compile time error)

Explanation : Surprisingly, when you perform operations on bytes the computations will be done using int values, with the bytes implicitly cast to (int) first. This is true for shorts as well, and similarly floats are up-converted to double when doing floating-point arithmetic.

byte someVar;
someVar = (int) someVar - 3;

Because of this you must cast the result back to (byte) to get the compiler to accept the assignment.

someVar = (byte) (someVar - 3);

Q 10 : Which of the following method never gives exception while converting string to integer ?
A. Convert.ToInt32(string a);
B. Int32.Parse(string a);
C. Int32.TryParse(string a,out b);
D. None of the above

Ans :- C :- Int32.TryParse(string a,out b);

Q 11 : difference between value type and refrence type ?

Value Type Refrence Type
Stored on stack. Stored on heap.
Contains actual value. Contains reference to a value.
Cannot contain null values. However this can be achieved by nullable types. Can contain null values.
Value type is popped on its own from stack when they go out of scope. Required garbage collector to free memory.
Memory is allocated at compile time. Memory is allocated at run time.

Q 12 : What is the top.Net class that everything is derived from?
A. Object
B. System.Net
C. System.Object
D. System

Ans : C (System.Object)

Static

Q 1 : Explain Static?

Static is not a part of any object.Static is part of the class. We can access Static Members throug class Name. Static is common to all the objects.Suppose I have a class as below
 
In the above example we can see that a is not part of the object (obj) but we can access a through class Name
 
So we can see we can accesss a through class name.

Q 2 : Where we can use the Static KeyWord

1.Fields 
2.Functions
3.Constructor
4.Class

Q 3 : Can “this” be used within a static method?

No, We cannot use this in static method. this refer to the current object and static methods loads when class loads and at that time no object will be accessible in static method.

Q 4 : What will be the output of below program
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra.Print();
            Console.Read();
        }
    }
    class TechiesEra
    {
        static TechiesEra()
        {
            Console.Write("Vijay ");
        }
        public static void Print()
        {
            Console.Write("Print method");
        }
    }
A. Vijay Print method
B. Print method
C. Vijay
D. Print method Vijay

Ans : A (Vijay Print method)

Q 5: What is the output of the program below?
class Program {
  static String location;
  static DateTime time;
 
  static void Main() {
    Console.WriteLine(location == null ? "location is null" : location);
    Console.WriteLine(time == null ? "time is null" : time.ToString());
  }
}

The output will be:
location is null
1/1/0001 12:00:00 AM
Although both variables are uninitialized, String is a reference type and DateTime is a value type. As a value type, an unitialized DateTime variable is set to a default value of midnight of 1/1/1 , not null.

Q 6 : Can we create a object of Static Class?

No, We can not create a object of static class.Because Static class contains only static methods and only Static Constructor. Static Class contains only static constructor not the Instance Constructor Thats why we can not create a object of a Static Class.

Q 7 : Can we inherit a Static Class?

No, We can not inherit the static class.Static class is internally Sealed class thats why we can not inherit the Static class.

Partial

Q 1: Define Partial ?

Partial class, structure and interface introduce in c# 2.0. We can split the defination of a class,struct and interface in multiple source files but they should be in same namespace. All the source files will be compiled in Single Source file.

Advantages of a Partial Class, Struct or Interface

1.We can separate UI design code and business logic code so that it is easy to read and understand. For example, you are developing a web application using Visual Studio and add a new web form then there are two source files, "aspx.cs" and "aspx.designer.cs". These two files have the same class with the partial keyword. The ".aspx.cs" class has the business logic code while "aspx.designer.cs" has user interface control definition.
2. Partial Class Allows more than one developers to work simultaneously on the same class, struct or interface.
3. Partial classes are mainly used by code generator . When code generator tool generate the code then tool may define some partial class, interface, methods and further customization of partial class, interface is done by the developers without messing with the system generated code.

Too much talked and its time of some examples

Partial Class

   partial  class ABC
    {
       public void Print();
    }
    partial class ABC
    {
        public void Display();
    }
At the time of compile both class will be merge in one class. So if we will create a method of ABC class we will get two methods named Print() and Display().

Partial Interface

We can split the defination of an interface as well as below
  public partial interface IFirst
    {
        void First();
    }
    public partial interface IFirst
    {
        void Second();
    }
    public class ABC : IFirst
    {
        public void First()
        {
           //First Method
        }
        public void Second()
        {
           // Second Method
        }
    }

Partial Struct

We can split the defination of strut as well as below
   public partial struct First
    {
       public void A();
    }
    public partial struct First
    {
       public void B();
    } 
     

Rules of Partial Classes

1. All the parts must use the partial keyword.
2. All the parts must be available at compile time to form the final class.
3. All the parts must have the same access modifiers.
4. Any class members declared in a partial definition are available to all the other parts.
5. The final class is the combination of all the parts at compile time.

Q 2 : Explain Partial Methods?

A partial class or struct can contain a partial method. One part of the partial class contains partial method signature and another part of the partial class may define the partial methods. Suppose if we don’t provide the definition of Partial method than all the partial method and calls to partial method will remove at compile time.

Why Partial Methods

Partial Methods are use full for customizing the auto generated code by some tool. Such type of tools generates some Partial methods and it depends on developers that whether they want to define that method or not. If Developer defines than these methods includes in class otherwise these methods ignored by compiler at compile time.

Rules of Partial Methods

1.Partial methods can be defined only in partial class or struct.
2.Partial methods must be private.
3.Partial methods must return void.
4.Partial methods implementation is optional.
5.Partial methods can be static and generic.
6.Partial methods can have ref parameters but not out parameters since these can't return value.

We declare Partial Method as below

   public partial class First
    {
        partial void A();
    }
    public partial class First
    {
        partial void A()
        {
            Console.WriteLine("TechiesEra");
        }
    }

Q 3 : What will be the output of below program?
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra(10, 50);
            obj.Print();
            Console.ReadKey();
        }
    }
    public partial class TechiesEra
    {
        private int x;
        private int y;
        public TechiesEra(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    public partial class TechiesEra
    {
        public void Print()
        {
            Console.WriteLine("{0},{1}", x, y);
        }
    }

Ans : 10,50

Q 4 : Can we use ref in partial methods?

Ans : Yes

Q 5 : Can we use out in partial methods?

Ans : No, because partial methods can't return value.

Q 6 : Will the below code compile?
class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            Console.Read();
        }
    }
    public partial class TechiesEra
    {
        public int a;
    }
   internal  partial class TechiesEra
    {
        protected int b;
    } 

Ans : No, All the partial class should have same access modifiers.

Q 7 : Will the below code compile?
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            Console.Read();
        }
    }
    public partial class TechiesEra
    {
        public void First()
        {
            Console.WriteLine("First");
        }
    }
    public abstract partial class TechiesEra
    {
        public void Second()
        {
            Console.WriteLine("Second");
       } 

Ans : No, a compile time error will be generated stating "Cannot create an instance of the abstract class.

Q 8 : Can you create partial delegates and enumerations?

Ans : No, you cannot create partial delegates and enumerations.

Q 9 : Can different parts of a partial class inherit from different interfaces?

Ans : Yes, different parts of a partial class can inherit from different interfaces.

Q 10 : Can you specify nested classes as partial classes?

Ans : Yes, nested classes can be specified as partial classes even if the containing class is not partial.
  class TechiesEra
    {
        public partial class First
        {
            void Test1() { }
        }
        public partial class First
        {
            void Test2() { }
        }
    }

Q 11 : Which of the following statements is incorrect about delegate?
A. Delegates are reference types.
B. Delegates are object oriented.
C. Delegates are type-safe.
D. Delegates serve the same purpose as function pointers in C and pointers to member function operators in C++.
E. Only one method can be called using a delegate.

Ans : E

Q 12 : In which of the following areas are delegates commonly used?
A. Remoting
B. Serialization
C. File Input/Output
D. Multithreading
E. Event handling

Ans : D and E

Q 13 : Which of the following is the necessary condition for implementing delegates?
A. Class declaration
B. Inheritance
C. Run-time Polymorphism
D. Exceptions
E. Compile-time Polymorphism

Ans : A

Constructor

Q 1 : Define Constructor?

Constructor is a special type of member function which has the same name as class name. Constructor does not return any value even void.Constructor initialize the object with some initial value.

Constructor calls automatically when we create an object. We don’t need to call explicitly. If we don’t write any constructor in the class then c# compiler automatically add a constructor in the class which is called default constructor

C# Supports 4 types of Constructor

1. Default or Parameterless Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor

Q 2 : Explain different Types of Constructor?

Default or Parameterless Constructor

When we do not declare any constructor, the class will call its default constructor which has a default public access modifier. The default constructor is a parameter less constructor which will be called by a class object.

  class TechiesEra
    {
        int x;
        int y;
        public TechiesEra() //Default or Parameterless Constructor
        {
            x = 10;
            y = 30;
        }
    }

Parameterized Constructor

Default constructors always initialize all the objects with the same values. In case you want to initialize the objects with different values, we can use Parameterized constructor.

 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();//This will call Parameterless constructor
            TechiesEra obj2 = new TechiesEra(10,20);//This will call Parameterized constructor
        }
    }
    class TechiesEra
    {
        int x;
        int y;
        public TechiesEra() // Parameterless
        {
            x = 10;
            y = 50;
        }
        public TechiesEra(int a, int b) //Parameterized
        {
            x = a;
            y = b;
        }
    }
Note : If we have more than one constructor then this is called Constructor Overloading

Copy Constructor

its also a Parameterized constructor.If we want to copy a object values into Other objects then we can use Copy Constructor ie: We can initialize a object with the same values as Other object

 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();//This will call Parameterless Constructor
            TechiesEra obj2 = new TechiesEra(10, 20);//This will call Parameterized Constructor
            TechiesEra obj3 = new TechiesEra(obj2);//Copy Constructor. Obj3 will also initialize with 10 and 20 . Copy obj2 to obj3
        }
    }
    class TechiesEra
    {
        int x;
        int y;
        public TechiesEra() // Parameterless
        {
            x = 10;
            y = 50;
        }
        public TechiesEra(int a, int b) //Parameterized
        {
            x = a;
            y = b;
        }
        public TechiesEra(TechiesEra obj) //Copy Constructor
        {
            x = obj.x;
            y = obj.y;
        }
    }

Static Constructor

Static constructor is a special constructor that gets called before the first object of the class is created. Static constructor is used to initialize the static data members of the class.Static constructor is only called once while creation of the first instance of the class. After that, no instance of a class will call the static constructor. You can also use static constructor to execute some code of the class which must be executed only once.

 class TechiesEra
    {
        int x;
        int y;
        static int z;
        public TechiesEra() // Parameterless
        {
            x = 10;
            y = 50;
        }
        public TechiesEra(int a, int b) //Parameterized
        {
            x = a;
            y = b;
        }
        public TechiesEra(TechiesEra obj) //Copy Constructor
        {
            x = obj.x;
            y = obj.y;
        }
        static TechiesEra() //Static Constructor //This will Initialize Static Members only
        {
            z = 500;
        }
    }

Points to remember about Static Constructor

1. We can not use any access modifier with Static Constructor
2. It will always be parameterless. we can not create parameterized static constructor.
3. We can create only one static constructor in a class.
4. Static Constructor can access only static members.

Q 3 : What will be the output of below program?
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();
            obj1.Print();
            Console.Read();
         
        }
    }
    class TechiesEra
    {
     
       static int z;
       public static TechiesEra() 
        {
            z = 500;
        }
       public void Print() 
       {
           Console.WriteLine(z);
       }
    }

Ans : Error, Because access modifiers are not allowed in static constructor.

Q 4 : What will be the output of below program?
    class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();
            obj1.Print();
            Console.Read();
         
        }
    }
    class TechiesEra
    {
     
       static int z;
       static TechiesEra()
        {
            z = 500;
        }
       static TechiesEra(static int x)
       {
           z = x;
       }
       public void Print() 
       {
           Console.WriteLine(z);
       }
    }

Ans : Error, Because We can not pass parameters in static constructor.

Q 5 : What will be the output of below program?
   class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();
            obj1.Print();
            Console.Read();
         
        }
    }
    class TechiesEra
    {
     
       static int z;
       int x;
       static TechiesEra()
        {
            z = 500;
        }
       public TechiesEra()
       {
           x = 500;
           z = 900;
       }
       public void Print()
       {
           Console.WriteLine(x + z);
       }
    }
                                

Ans :1400. It seems that it will be an error because we have two constructor with no parameters. It breaks the method overloading rule. But we will not get any error because execution time of both the constructor is different.

Q 6 : Can constructors be inherited?

Ans : No

Q 7 : How can you call a base class constructor from a child class?

Ans : we use base keyword.

Q 8 : In which order constructor is called in inheritance?

Ans : In inheriteance , constructor calls from parent to child. For ex.

 class Program
    {
        static void Main(string[] args)
        {
            CSharp obj = new CSharp();
            Console.Read();
        }
    }
    class TechiesEra
    {
        public TechiesEra()
        {
            Console.WriteLine("Vijay");
        }
    }
    class DotNet : TechiesEra 
    {
        public DotNet()
        {
            Console.WriteLine("Rana");
        }
    }
    class CSharp : DotNet
    {
        public CSharp()
        {
            Console.WriteLine("CSharp");
        }
    }
This program will print First Vijay than Rana and than CSharp. because when we will create object of CSharp class then compiler will call TechiesEra constructor first after that compiler will call DotNet class constructor and that Csharp class constructor.

Q 9 : Can constructors be overloaded?

Ans : Yes

Q 10 : How do you call one constructor from another in the same class ?

Ans : We use this keyword to call one constructor from another in same class.
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            Console.Read();
        }
    }
    class TechiesEra
    {
        public TechiesEra():this(10)
        {
            Console.WriteLine("Vijay");
        }
        public TechiesEra(int x)
        {
            Console.WriteLine(x);
        }
    }
Output of this will be 10 and Vijay. When we will create object then Parameterless constructor gets called but this constructor first call to the parameterized constructor.

Q 11 : Can a constructor call itself by using this keyword ?

Ans : No

Q 12 : What is default access modifier of constructor?

Ans : Private

Q 13 : What will be the output of below program?
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            Console.Read();
        }
    }
    class TechiesEra
    {
        public TechiesEra():this(10)
        {
            Console.WriteLine("Vijay");
        }
        public TechiesEra(int x):this(50,60)
        {
            Console.WriteLine(x);
        }
        public TechiesEra(int x,int y)
        {
            Console.WriteLine(x+y);
        }
    }

Ans : 110,10,Vijay

Q 14 : What will be the output of below program?
 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            Console.Read();
        }
    }
    class TechiesEra
    {
        public TechiesEra():this()
        {
            Console.WriteLine("Vijay");
        }
    }

Ans : Error, Constructor can not call itself.

Q 15 : What will be the output of below program?
 class Program
    {
        static void Main(string[] args)
        {
            DotNet obj = new DotNet();
            obj.Display();
            Console.Read();
        }
    }
    class TechiesEra
    {
        int x;
        public TechiesEra(int a)
        {
            x = a;
            Console.WriteLine(x);
        }
    }
    class DotNet : TechiesEra
    {
        int x;
        public DotNet(): base(100)
        {
            x = 500;
        }
        public void Display()
        {
            Console.WriteLine(x);
        }
    }

Ans : 100,500

Q 16 : Which of the following statements are correct about constructors in C#.NET??
1. Constructors cannot be overloaded.
2. Constructors always have the name same as the name of the class.
3. Constructors are never called explicitly.
4. Constructors never return any value.
5. Constructors allocate space for the object in memory.

A. 1, 3, 5
B. 2, 3, 4
C. 3, 5
D. 4, 5
E. None of these

Ans : B (2,3,4)

Q 17 : How many times can a constructor be called during lifetime of the object?
A. As many times as we call it.
B. Only once.
C. Depends upon a Project Setting made in Visual Studio.NET.
D. Any number of times before the object gets garbage collected.
E. Any number of times before the object is deleted.

Ans : B (Only Once)

Polymorphism

Q 1 : What is Polymorphism?

Ans : Polymorphism is one of the main concepts of OOP.polymorphism means more than one forms.

Example 1 : A dog behaves differently when you give him a bread and behaves differently when you beat him. So same thing, different situation different behaviour is called Polymorphism.

Example 2 : Person behaves differently with father and same person behaves differently with his girlfriend or wife.

There are two ways to implement polymorphism in c#

1. Method Overloading or Compile time polymorphism

2. Method Overriding or Run time polymorphism

Q 2 : What is Method Overloading?

Ans :Method Overloading means Same function, different signature, different behavior is called method overloading.All the methods should be in same scope. We can give multiple definitions for the same function name in the same scope. The definition of the function must differ from each other either in types or the number of arguments in the argument list. Method Overloading only depends on method signature not on return type.

 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            obj.Area(10); // This will call the first function because we are passing single parameter.
            obj.Area(10, 20);// This will call the second function because we are passing two parameters
            Console.Read();
        }
    }
    class TechiesEra
    {
        public void Area(int l)
        {
            int area = l * l;
            Console.WriteLine("Area of Square=" + area);
        }
        public void Area(int l,int b)
        {
            int area = l * b;
            Console.WriteLine("Area of Rectangle=" + area);
        }
    }
In the above example we have two methods named Area but parameters is different(In First Method we have only one parameter but in Second Method we have two Parameters) and behavior is also different (First method will calculate the area of a square and other method will calculate the area of a Rectangle).

output will be : Area of Square=100 and Area of Rectangle=200

Note : C# recognizes the method by its parameters and not only by its name.

Suppose i have class as below
    class TechiesEra
    {
       public void Print()
        {
        }
       public int Print()
       {
       }
    }
If we will compile this then it will give us below error

Error 1 Type 'ConsoleApplication1.TechiesEra' already defines a member called 'Print' with the same parameter types
We will get this error because parameter should differ either in number or in type. But here only return type is different and compiler does not look to the return type.

Note : The return value/parameter type of a method is never the part of method signature if the names of the methods are same. So this is not polymorphism.

Q 3 : Will this compile?
static void Print(int a)  {   }
public void Print(int a) {   }

No, Modifiers such as static are not considered as part of method signature.

Q 4 : What will be the output?
   class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            int x = 100;
            obj.Print(x);
            obj.Print(ref x);
            Console.WriteLine(x);
            Console.Read();
        }
    }
    class TechiesEra
    {
        public void Print(int a) 
        {
            Console.WriteLine(a);
        }
        public void Print(ref int a) 
        {
            a = a + 30;
        }
    }
Options :

A. 100,130
B. Compile Time Error
C. Run Time Error
D. 100,30

Ans : A(100,130)

Q 5 : What will be the output?
    class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            int x = 100;
            obj.Print(x);
            obj.Print(ref x);
            Console.WriteLine(x);
            Console.Read();
        }
    }
    class TechiesEra
    {
        public void Print(ref int a) 
        {
            Console.WriteLine(a);
        }
        public void Print(out int a) 
        {
            a = a + 30;
        }
    }
Options :

A. 100,130
B. Compile Time Error
C. Run Time Error
D. 100,30

Ans : B (Compile Type Error : Cannot define overloaded method 'Print' because it differs from another method only on ref and out) )

Q 6 : What will be the output?
    class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            int x = 100;
            obj.Print(x);
            int y;
            obj.Print(out y);
            Console.WriteLine(y);
            Console.Read();
        }
    }
    class TechiesEra
    {
        public void Print(int a) 
        {
            Console.WriteLine(a);
        }
        public void Print(out int a) 
        {
            a = 100;
            a = a + 30;
        }
    }
Options :

A. 100,130
B. Compile Time Error
C. 100,100
D. 100,30

Ans : A ( 100,130) )

Q 7 : What will be the output?
     class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            obj.Display();
            Console.Read();
        }
    }
    class TechiesEra
    {
        private int a = 10;
        public void Display()
        {
            Display2(ref a, ref a);
            System.Console.WriteLine(a);
        }
        private void Display2(ref int x, ref int y)
        {
            System.Console.WriteLine(a);
            x = 10 ;
            System.Console.WriteLine(a);
            y = 100;
            System.Console.WriteLine(a);
            x = 1000;
        }
    }
Options :

A. 10,10,100,1000
B. 10,10,10,10
C. 100,10,1000,10
D. Compile Time Error

Ans : A ( 10,10,100,1000 ) )

Q 8 : What will be the output?
      class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            obj.Print(10, 20);
            obj.Print(10, 20, 30);
            Console.Read();
        }
    }
    class TechiesEra
    {
        
        public void Print(int x, int y)
        {
            Console.WriteLine(x+y);
        }
        public void Print(params int[] pArray)
        {
            int sum = 0;
            foreach (int x in pArray)
            {
                sum = sum + x;
            }
            Console.WriteLine(sum);
        }
    }
Options :

A. 30,30
B. 30,60
C. 60,30
D. 60,60

Ans : B ( 30,60 ) )

Q 9 : can we Overload main() method in C#?

Ans : Yes, Main() method can be overloaded. We can have any number of main method but only one main method will be public static void main().

Q 10 : Explain Operator Overloading?

Operator overloading provides the additional capabilities to C# operators when they are applied to user defined data types. All the built in types have operators associated with them for mathematical operations but for User defined data types we dont have any operator associated. To use operators with user defined data types, they need to be overloaded depending on the requirements.

For operator Overloading we use Operator keyword.

Suppose we want to add tow objects of my class named TechiesEra then we will use Operator keyword with + as below

class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj1 = new TechiesEra();
            obj1.x = 100;
            TechiesEra obj2 = new TechiesEra();
            obj2.x = 500;
            TechiesEra obj3 = obj1 + obj2;
            Console.WriteLine(obj3.x);
            Console.Read();
        }
    }
    class TechiesEra
    {
        public int x;
      
        public static TechiesEra operator +(TechiesEra t1, TechiesEra t2)
        {
            TechiesEra t3 = new TechiesEra();
            t3.x = t1.x + t2.x;
            return t3;
        }
    }
In the above example we are adding two objects named obj1 and obj2. So if we will print obj3.x then it will print 600.

Q 11 : what will be the output of below program?

 class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            int j = 6;
            TechiesEra.add(ref i);
            TechiesEra.add(6);
            Console.WriteLine(i);
            Console.Read();
        }
    }
    class TechiesEra
    {
       public static void add(ref int x)
        {
            x = x * x;
        }
       public static void add(int x)
        {
            Console.WriteLine(x * x * x);
        }
    }

Options :

A) 216,0
B) 216,25
C)25,0
D) 25,216

Ans : B ( 216,25 )

Q 12 : Which of the following unary operators can be overloaded?

1. true
2. false
3. +
4. new
5. is

Options :
A) 1, 2, 3
B) 3, 4, 5
C) 3 only
D) 4 only

Ans : A ( 1,2,3 )

Q 13 : Which of the following keyword is used to overload user-defined types by defining static member functions?

Options :
A. op
B. opoverload
C. operator
D. operatoroverload
E. udoperator

Ans : C ( operator )

Q 14 : Which of the followings is the correct way to overload + operator?

Options :
A.
public TechiesEra operator + ( TechiesEra a, TechiesEra b )

B.
public abstract operator + ( TechiesEra a, TechiesEra b)

C.
public abstract TechiesEra operator + (TechiesEra a, TechiesEra b )

D.
public static TechiesEra operator + ( TechiesEra a, TechiesEra b )

Ans : D ( public static TechiesEra operator + ( TechiesEra a, TechiesEra b ) )

Q 15 : Which of the following operators cannot be overloaded?
1. true
2. false
3. ~
4. new
5. sizeof

Options :
A. 1, 3
B. 2, 4
C. 4, 5
D. All of the above

Ans : C ( 4, 5 )

Q 16: What are the operators which can not be overloaded in c#?

Ans : &&, ||,[] (Array index operator),() (Conversion operator),=, . , ?:, ->, new, is, as, sizeof

Q 17: What will be the out put of below program?
                                
    class A
    {
        public void show()
        {
            Console.WriteLine("Base Class");
            Console.ReadLine();
        }
    }
    class B : A
    {
        public void show()
        {
            Console.WriteLine("Derived Class");
            Console.ReadLine();
        }
    }
    class TechiesEra
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
        }
    }
                                
                            

Ans :-
Base Class
Derived Class
Base Class

Q 18: What will be the out put of below program?
                                
    class A
    {
        public void show()
        {
            Console.WriteLine("Base Class");
        }
    }
    class B : A
    {
        public new void show()
        {
            Console.WriteLine("Derived Class");
          
        }
    }
    class TechiesEra
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
        }
    }
                                
                            

Ans :-
Base Class
Derived Class
Base Class

Q 19: What will be the out put of below program?
                                
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Base Class");
          
        }
    }
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Derived Class");
           
        }
    }
    class TechiesEra
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            A a2 = new B();
            a2.show();
        }
    }
                                
                            

Ans :-
Base Class
Derived Class
Derived Class

Q 20: What will be the out put of below program?
                                
   class A
    {
        public virtual void show()
        {
            Console.WriteLine("Base Class");
        }
    }
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Derived Class");
        }
    }
    class C : B
    {
        public new void show()
        {
            Console.WriteLine("Vijay");
        }
    }
    class TechiesEra
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            C c1 = new C();
            c1.show();
            A a2 = new B();
            a2.show();
            A a3 = new C();
            a3.show();
            B b3 = new C();
            b3.show();
        }
    }
                                
                            

Ans :-
Base Class
Derived Class
Vijay
Derived Class
Derived Class
Derived Class

Q 21: Select the sequence of execution of function f1(),f2() and f3()?
                                
   class TechiesEra
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.f1();
            obj.f2();
            obj.f3();
            Console.Read();
        }
    }
    public class A
    {
        public void f1()
        {
        }
        public virtual void f2() 
        { 
        }
        public virtual void f3()
        {
        }
    }
    public class B : A
    {
        public new void f1()
        {
        }
        public override void f2()
        {
        }
        public new void f3()
        {
        }
    }
                                
                            
A. f1() of B class gets exected;f2() of B class get executed;f3() of A class get executed
B. f1() of A class gets exected;f2() of B class get executed;f3() of A class get executed
C. f1() of A class gets exected;f2() of B class get executed;f3() of B class get executed
D. f1() of B class gets exected;f2() of A class get executed;f3() of A class get executed

Ans :-
B :f1() of A class gets exected;f2() of B class get executed;f3() of A class get executed

Q 22: What we need to change in order to compile the below Program (Method Name, data type of Parameters and number of parameters should not be changed.)?
                                
   class TechiesEra
    {
        public void TechiesEra(int x) 
        { 
        }
        public void TechiesEra(int x)
        {
        }
    }

Ans :-
We can use ref or int? in the signature.
                                
   class TechiesEra
    {
        public void TechiesEra(int? x) 
        { 
        }
        public void TechiesEra(int x)
        {
        }
    }       
Or

                                
   class TechiesEra
    {
        public void TechiesEra(ref int x) 
        { 
        }
        public void TechiesEra(int x)
        {
        }
    } 

Exception Handling

Q 1: What is the base class from which all the exceptions are derived ?

Ans :- System.Exception

Q 2: Explain the difference between error and exception in C# ?

Ans :- Exceptions are the unusual situation occured at run time where as errors occured at compile time.

Q 3: What are the two main kind of exceptions class?

Ans :-
System Exception :- For inbuilt exceptions for example : DivideByZeroException,Argument Exception ....etc
Application Exception :- This class is used for user defined exceptions.

Q 4:Can finally bock have return statement ?

Ans :-
No, we can not. There will be a compile time error "Control cannot leave the body of a finally clause".

Q 5: Will the below code compile ?
           try
            {
                int a = 10;
                int b = 0;
                int c = a / b;
            }
            catch (Exception ex)
            {
            }
            catch (DivideByZeroException ex)
            {
            }
                            

Ans :-
No, This will not compile. we will get below error
"A previous catch clause already catches all exceptions of this or of a super type."

Q 6: Whats the syntax for catching all kinds of exception. ?

Ans :-
             
          try
          {
          
          }
          catch    
           {
           
           }  

This will catch exceptions thrown by managed as well as unmanaged code.

Q 7: Whats the diff between throw and throw ex ?

Ans :-
throw ex :- resets the stack trace
throw :- doesn't - the original offender would be preserved.

Q 8: what will be the output of below program ?
 public static void main(String args[]) 
    {
 
     try 
       {
             int a = 9;
             int b = 5;
             int c = a / (b - 5);
             Console.WriteLine("TechiesEra");
       }
     catch(Exception e) 
       {
             Console.Write("Vijay");
       } 
     finally 
       {
             Console.Write("Rana");
       }

Ans :-
VijayRana

Q 9: what will be the output of below program ?
  class Program
  {
      static void Main(string[] args)
      {            
          try 
          {
              Console.WriteLine("TechiesEra" + " " + 1/0);
          }
          finally
          {
              Console.WriteLine("CSharp");        	
          }
          Console.ReadLine();
      }
  }
A. CSharp
B. Run time Exception generation
C. Compile time error
D. TechiesEra CSharp

Ans :-
B. Run time Exception generation

Q 10: what will be the output of below program ?
  static void main(string[] args)
     {
         int a = 5;
         int b = 40;
         int[] c = new int[4];
         try
         {
             c[a] = b;
         }
         catch(IndexOutOfRangeException e)
         {
             Console.WriteLine("TechiesEra");
         }
         Console.WriteLine("CSharp");
     }

Ans :-
TechiesEra
CSharp

Q 11: The System.Exception is the base class for all predefined system exception in c# ?
a. true
b. false

Ans :- true

Generics

Q 1: Define Generics?

Ans : Generics is one of the Stongest feature of c#. In c# , If we want to store some data than we need to declare a variable prior to store the data in variable.Generics enables us to write a function which can be operated on multiple data types.When we use generics, the compiler makes compile time checks on your code for conformance to type safety.Generics promotes type safety, code maintenance, code efficiency, and improved performance.Lets Understand Generics with some examples

Suppose we have a class Named TechiesEra where we have a method named test as below

 class TechiesEra
    {
        public void Test(int a)
        {
            Console.WriteLine(a);
        }  
    }
To call this function we will create a object of this class and will call this. Suppose after some we need to create one more method where Test Should accept a string type of variable then we will have to write a another overload of the Test Method as below

 class TechiesEra
    {
        public void Test(int a)
        {
            Console.WriteLine(a);
        }
        public void Test(string a)
        {
            Console.WriteLine(a);
        }  
    }
We are good so far but what if the have a new requirment where we need to write one more overload of Test method which will accept a char value. So now the question is how many overload we will write. we have Generics to rescue us. We can write a Generic function which will work for any data type and we dont need to write so much overloads.

Inheritance

Q 1 : Will the following code compile. Explain Your answer?
 internal class TechiesEra
    {
        void A();
    }
    public class DotNet : TechiesEra
    {
        public void A()
        {
        }
    } 

This code will not compile dut to Inconsistent accessibility.

Error will be : Inconsistent accessibility: base class 'ConsoleApplication1.TechiesEra' is less accessible than class 'ConsoleApplication1.DotNet' C:\Vijay\ConsoleApplication1\ConsoleApplication1\Program.cs 24 18 ConsoleApplication1

Q 2 :What will be the output?(CTS,HCL)

 class Program
    {
        static void Main(string[] args)
        {
            C2 obj = new C2();
            Console.Read();
        }
    }
    class C1
    {
        static C1()
        {
            Console.WriteLine("Static c1");
        }
        public C1()
        {
            Console.WriteLine("public c1");
        }
    }
     class C2 : C1
    {
        static C2()
        {
            Console.WriteLine("Static c2");
        }
         public C2()
        {
            Console.WriteLine("public c2");
        }
       
    }
Output

Static c2
Static c1
public c1
public c2

Explanation

Static Constructor gets called in the below senerio
1. An instance of the class is created.
2. Any of the static members of the class are referenced.
So when we create object of C2 then first class C2 static constructor get called and after that Class C1 Static Constructor. In case of normal constructors first base class Constructor will get called and then drive class so out put will be

Q 3 :does c# support multiple inheritance?

Output

No, C# does not support multiple inheritance

7. Ref and Out

Q 1 :What will be the output?(cognizant)

 class Program
    {
        static void Main(string[] args)
        {
            TechiesEra t = new TechiesEra();
            t.Test();
            Console.ReadLine();
        }
    }
    class TechiesEra
    {
        public void Test()
        {
            List list = new List();
            List list2 = list;
            list.Add(3);
            MethodByVal(list);
            Console.WriteLine(list.Count);
            Console.WriteLine(list[0]);
            Console.WriteLine(list == list2);
            MethodByRef(ref list);
            Console.WriteLine(list.Count);
            Console.WriteLine(list == list2);
        }
        private void MethodByVal(List list)
        {
            list[0] = 5;
            list = new List();
            list.Add(1);
            list.Add(2);
        }
        private void MethodByRef(ref List list)
        {
            list[0] = 5;
            list = new List();
            list.Add(1);
            list.Add(2);
        }
      
    }
Output

1
5
true
2
false

Explanation

The only difference is that in the second case MethodByRef(ref List list) will be able to modify list variables in the caller, for example, by setting a different list object into it
void MethodByRef(ref List list) {
    list = new [] {1, 2, 3}.ToList(); // Modifies list  in the caller
}
while MethodByVal would not be able to modify the variable (but it would be able to modify the list).
void SomeMethod2(List list) {
    list.Add(123); //We can modify the list
    list = new [] {1, 2, 3}.ToList(); // We can not
}

Q 2 :What will be the output?(cognizant)

   class Program
    {
        static void Main(string[] args)
        {
            int i = 3;
            int j;
            Method1(ref i);
            Method2(out j);
            Console.WriteLine(i + " " + j);
            Console.ReadLine();
        }
        static void Method1(ref int x)
        {
            x = x + x;
        }
        static void Method2(out int x)
        {
            x = 4;
            x = x * x;
        }
    }
Output :-
6 16

Q 3 :What will be the output?

    class Program
    {
        static void Main(string[] args)
        {
            int n = 2;
            method(n);
            Console.WriteLine(n);
            method1(ref n);
            Console.WriteLine(n);
            Console.ReadLine();
        }
        static void method(int num)
        {
            num += 40;
            Console.WriteLine(num);
        }
        static void method1(ref int num)
        {
            num += 40;
            Console.WriteLine(num);
        }
    }
Output :-
42
2
42
42

Q 4 :What will be the output?

     class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 2, 3, 4, 5 };
            fun(ref arr);
            for (int i = 0; i < arr.Length; i++)
                Console.WriteLine(arr[i] + "  ");
            Console.ReadLine();
        }
        static void fun(ref int[] a)
        {
            a = new int[6];
            a[2] = 50;
            a[4] = 100;
        }
    }
Output :-
0
0
50
0
100
0

Q 5 :What will be the output?

      static void Main(string[] args)
        {
            int a = 10;
            int b = 0, c = 0;
            method(a, ref b, ref c);
            Console.WriteLine(b + "  " + c);
            Console.ReadLine();
        }
        static int method(int x, int p, ref int k)
        {
            p = x + x * x;
            k = x * x + p;
            return 0;
        }
Output :-
Compile time error because p should be declared with ref as below
  static int method(int x, ref int p, ref int k)
        {
            p = x + x * x;
            k = x * x + p;
            return 0;
        }

Q 6 :What will be the output?

       static void Main(string[] args)
        { 
            int[]arr = new int[]{ 10, 20, 30, 40, 50}; 
            fun(ref arr);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " "); 
            }
            Console.Read();
        }
        static void fun(ref int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = a[i] * 5;
               
            }
        }
Output :-
50 100 150 200 250

Q 7 :What will be the output?

       static void Main(string[] args)
        {
            int a = 10;
            int s = 0, c = 0;
            Proc(a, ref s, ref c);
            Console.WriteLine(s + " " + c);
            Console.Read();
        }
        static void Proc(int x, ref int ss, ref int cc)
        {
            ss = x * x;
            cc = x * x * x;
        } 
Output :-
100 1000

Diffrences

Q 1 : What is the difference between == and .Equals method in c#?

“==” compares if the object references are same while “.Equals()” compares if the contents are same.

In the below example we have two stringbuilder with same content as below

 StringBuilder s1 = new StringBuilder("TechiesEra");
            StringBuilder s2 = new StringBuilder("TechiesEra");
            Console.WriteLine(s1 == s2);
            Console.WriteLine(s1.Equals(s2));
s1 == s2 will check the refrence and thats why this will print false. s1.Equals(s2) will check the content so it will print true. For value type both are same.

Q 2 :What will be the output?

            StringBuilder s1 = new StringBuilder("TechiesEra");
            StringBuilder s2 = new StringBuilder("TechiesEra");
            Console.WriteLine(s1 == s2);
            Console.WriteLine(s1.Equals(s2));
Output :-
false true

Q 3 :What will be the output?

            int x = 20;
            int y = 20;
            Console.WriteLine(x == y);
            Console.WriteLine(x.Equals(y));
Output :-
true true

Explanation :-
For value type == and Equals method is same.

Q 4 :What will be the output?

            StringBuilder s1 = new StringBuilder("TechiesEra");
            StringBuilder s2 = s1;
            Console.WriteLine(s1 == s2);
            Console.WriteLine(s1.Equals(s2));
Output :-
true true

Explanation :-
Now Refrence and Content both are same.

Q 5 : What will be the output of below program and why?
class Program
{
    static void Main(String[] args)
    {
        Employee e1 = new Employee();
        e1.Name = "Vijay Rana";
        Employee e2 = new Employee();
        e2.Name = "Vijay Rana";
        Console.WriteLine(e1 == e2);
        Console.ReadKey();
    } 
}
                            

False
== operator always checks that whether both the variables refer to the same memory location. In this example both are different instance and they dont share same memory location that's why it will return False.

Q 6 : What will be the output of below program and why?
class Program
    {
        static void Main(string[] args)
        {
            string a = "Vijay Rana";
            string b = "Vijay Rana";
 
            Console.WriteLine(a == b);
            Console.Read();
        }
    }
                            

True
If we compare refrence type through == it always compare that whether both the operends refer to same memory location or not and accordingly returns true or false. but in case of string it compare the contents rather then memory location.

Q 7 :Difference between ref and out ?

Ref Out
The ref keyword is used to pass an argument as a reference. The out keyword is used to pass an argument as a reference
An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method. An argument that is passed using a out keyword can be passed without assigning any value to it
An argument that is passed using an ref keyword can be returns back without reinitializing in called method. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.


Example :
class Program
    {
        static void Main(string[] args)
        {
            TechiesEra obj = new TechiesEra();
            int i = 10;//Need to be initialized for ref in calling method. 
            int k = 20;//Need to be initialized for ref in calling method. 
            Console.WriteLine(i);//i=10
            Console.WriteLine(k);//20
            obj.Test(ref i,ref k);//I am passing i =10 so this can be used for input and output both.
            Console.WriteLine(i);//i=100; (It has been changed in called method)
            Console.WriteLine(k);//20
            int j;//Can be passed without assigning any value to it.
            int a = 100;// can be initialized also but its not mandatory.
            obj.Test1(out j,out a);//J can be used only output.
            Console.WriteLine(j);//500;
            Console.WriteLine(a);//1000
            Console.Read();
        }
    }
    class TechiesEra
    {
        public void Test(ref int i, ref int k)
        {
            i = 100;//We dont need to reinitialize k here.
        }
        public void Test1(out int j,out int a)
        {
            j = 500; //must be initialized here
            a = 1000;//must be initialized in the 
        }
    }

Q 8 :Difference between struct and class ?

Struct Class
The struct is value type in C# and it inherits from System.ValueType. The class is reference type in C# and it inherits from the System.Object Type.
Structs are value types. Classes are reference (pointer) types.
Structs Cannot have a null reference (unless Nullable is used). Classes reference can be null
The struct value stores on the stack memory. The class object is stores on the heap memory.
Cannot support inheritance. Can support inheritance.
The struct can only inherit the interfaces. The class can inherit the interfaces, abstract classes.
The struct can't have the default constructor (From c# 6.0 Struct supports default Constructor also). The class will have the default constructor.

Q 9 :Difference between var and dynamic ?

var dynamic
var was introduced in C# 3.0. dynamic was introduced in C# 4.0.
var is a statically typed variable.It means the type of variable declared is decided by the compiler at compile time. dynamic are dynamically typed variables.It means the type of variable declared is decided by the compiler at runtime time.
var type of variables are required to be initialized at the time of declaration.
var a=10;
the compiler will treat the variable a as int.
dynamic type variables need not be initialized when declared.
dynamic a;
a=10; 
a="Vijay Rana";
                                     
Errors are caught at compile time.Since the compiler knows about the type and the methods and properties of the type at the compile time itself Errors are caught at runtime:-Since the compiler comes to about the type and the methods and properties of the type at the run time.
Intellisense help is available for the var type of variables. Intellisense help is not available for the dynamic type of variables.
var does not allow the type of value assigned to be changed after it is assigned to. This means that if we assign an integer value to a var then we cannot assign a string value to it.
var a=10;
a= "Vijay Rana";//Will be a compile time error.
                                     
dynamic allows the type of value to change after it is assigned to initially. In the code above, if we use dynamic instead of var, it will not only compile, but will also work at run-time.
dynamic a;
a=10; 
a="Vijay Rana";//Will Compile
                                     
We can not pass var as parameter to function. We can pass dynamic as parameter to function.

Q 10 :Difference between yield and return ?

var dynamic
var was introduced in C# 3.0. dynamic was introduced in C# 4.0.
var is a statically typed variable.It means the type of variable declared is decided by the compiler at compile time. dynamic are dynamically typed variables.It means the type of variable declared is decided by the compiler at runtime time.
var type of variables are required to be initialized at the time of declaration.
var a=10;
the compiler will treat the variable a as int.
dynamic type variables need not be initialized when declared.
dynamic a;
a=10; 
a="Vijay Rana";
                                     
Errors are caught at compile time.Since the compiler knows about the type and the methods and properties of the type at the compile time itself Errors are caught at runtime:-Since the compiler comes to about the type and the methods and properties of the type at the run time.
Intellisense help is available for the var type of variables. Intellisense help is not available for the dynamic type of variables.
var does not allow the type of value assigned to be changed after it is assigned to. This means that if we assign an integer value to a var then we cannot assign a string value to it.
var a=10;
a= "Vijay Rana";//Will be a compile time error.
                                     
dynamic allows the type of value to change after it is assigned to initially. In the code above, if we use dynamic instead of var, it will not only compile, but will also work at run-time.
dynamic a;
a=10; 
a="Vijay Rana";//Will Compile
                                     
We can not pass var as parameter to function. We can pass dynamic as parameter to function.

Q 11 :Which of the following statements are true about the C#.NET code snippet given below?
String s1, s2;
s1 = "TechiesEra";
s2 = "TechiesEra";

A. String objects cannot be created without using new.
B. Only one object will get created.
C. s1 and s2 both will refer to the same object.
D. Two objects will get created, one pointed to by s1 and another pointed to by s2.
E. s1 and s2 are references to the same object.

Ans : B,C,E

LINQ

Q 1 : What will be the output of below program?
static void Main(string[] args)
        {
            string[] numbers = { "zero", "one", "two", "three", "four", "five" };
            var sortedDigits =
                from n in numbers
                orderby n.Length, n
                select n;
          
            foreach (var n in sortedDigits)
            {
                Console.WriteLine(n);
            } 
            Console.Read();
        }
                            

One
two
five
four
zero
three
This will first sort by the length and then will sort alphabetically.

Q 2 : What will be the output of below program?
static void Main(string[] args)
        {
            string[] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
            var di = (
                from n in numbers
                where n[1] == 'i'
                select n)
                .Reverse();
           
            foreach (var n in di)
            {
                Console.WriteLine(n);
            } 
            Console.Read();
        }
                            

nine
eight
six
five

Q 3 : What will be the output of below program?
static void Main(string[] args)
        {
            int[] numbers = {10, 14, 22, 93, 98, 85, 63, 71, 22, 20 };
            var result = numbers.Take(4);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
            Console.Read();
        }
                            

10
14
22
93

Q 4 : What will be the output of below program?
 static void Main(string[] args)
        {
            int[] numbers = {10, 14, 22, 93, 98, 85, 63, 71, 22, 20 };
            var result = numbers.Skip(7);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
            Console.Read();
        }
                            

71
22
20

Q 5 : What will be the output of below program?
 static void Main(string[] args)
        {
            int[] numbers = {95, 98, 89, 93, 98, 85, 63, 71, 22, 20 };
            var result = numbers.TakeWhile(a => a > 90);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
            Console.Read();
        }
                            

Ans
95
98
This will traverse the array untill condition is true.

Q 6 : What will be the output of below program?
 int[] numbers = {9, 9, 8, 9, 9, 5, 3, 1, 2, 2 };
            var result = numbers.TakeWhile((a, b) => a > b);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
                            

Ans
9
9
8
9
9
In (a,b) a is the source element and b is the index of the element. so from starting it will find all the elements untill element is greater then index.

Q 7 : What will be the output of below program?
 int[] numbers = {9, 9, 8, 9, 9, 5, 3, 1, 2, 2 };
            var result = numbers.SkipWhile(n=>n%5!=0);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
            Console.Read();
                            

Ans
5
3
1
2
2
Wil skip untill condition is true.

Q 8 : What will be the output of below program?
  static void Main(string[] args)
        {
            int[] numbers = {9, 9, 8, 9, 9, 5, 3, 1, 2, 2 };
            var result = numbers.SkipWhile((a,n)=>n<7); foreach (var n in result)
                                                        {
                                                        Console.WriteLine(n);
                                                        }
                                                        Console.Read();
                                                        }
                            

Ans
1
2
2

Q 9 : What will be the output of below program?
  static void Main(string[] args)
        {
            int[] numbers = {9, 9, 8, 9, 9, 5, 3, 1, 2, 2 };
            var result = numbers.SkipWhile((a,n)=>a>n);
            foreach (var n in result)
            {
                Console.WriteLine(n);
            }
            Console.Read();
        }
                            

Ans
5
3
1
2
2

Q 10 : How you will find the Sum of total number of characters of all words in the array?
 string[] words = { "Vijay", "Rana", "TechiesEra" };

  int t = words.Sum(w => w.Length);

Q 11 : How you will find the length of shortest word in the array?
 string[] words = { "Vijay", "Rana", "TechiesEra" };

  int t = words.Min(w => w.Length);

Q 12 : How you will find the shortest word in the array?
 string[] words = { "Vijay", "Rana", "TechiesEra" };

  string t = words.Min();

Q 13 :what will be the output?
  static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 4, 5 };
            int result = array.Aggregate((x, y) =>x + y);
            Console.WriteLine(result);
            result = array.Aggregate((a, b) => a * b);
            Console.WriteLine(result);
            Console.Read();
        }

15
120
First One
// 1 + 2 = 3 // 3 + 3 = 6 // 6 + 4 = 10 // 10 + 5 = 15
Second One
// 1 * 2 = 2 // 2 * 3 = 6 // 6 * 4 = 24 // 24 * 5 = 120

Operators

Q 1 :Explain ?? Operator?

?? operator returns a value (right hand operand) if left operand has a nullible type whose value is null.
This is known as null-coalescing operator and it acts as following, assume x is a nullable int and y is a normal int
y = x ?? 1;
is equal to
y = (x != null ? (int)x : 1);
which is equal to
if(x != null)
    y = (int)x;
else
    y = 1;

Collections

Q 1 :What will be the output of below program?
class TechiesEra
    {
        static void Main(string[] args)
        {
            int[] arr = new int[0];
            Console.WriteLine(arr[0]);
            Console.Read();
        }
    }

System.IndexOutOfRangeException

Q 2 :What will be the output of below program?
 class TechiesEra
    {
        static void Main(string[] args)
        {
            int[] arr = { 5, 10, 15 };
            Console.WriteLine(arr[-1]);
            Console.Read();
        }
    }

System.IndexOutOfRangeException

Q 3 :What will be the output of below program?
  class TechiesEra
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[10];
            Print(arr1);
            Console.WriteLine(arr1.Length);
            Console.Read();
        }
        static void Print(int[] arr1)
        {
            int[] arr2 = new int[5];
            arr1 = arr2;
            Console.WriteLine(arr1.Length);
            Console.WriteLine(arr2.Length);
        }
    }

5
5
10

Q 4 :If you want to add an array in ArrayList then which method of ArrayList will be used?
A. AddRange
B. Add
C. AddArray
D. None of the Above

Ans : A (AddRange)

Pre Processor Directives

Q 2 :What will be the output of below program?
 class TechiesEra
    {
        static void Main(string[] args)
        {
          
            #if TEST
                Console.WriteLine("Vijay Rana");
            #else
            Console.WriteLine("Techies Era");
            #endif
            Console.Read();
        }
    }

Techies Era

Q 2 :What will be the output of below program?
 class TechiesEra
    {
        static void Main(string[] args)
        {
        #if (!pi)
            Console.WriteLine("Vijay");
        #else
            Console.WriteLine("pi not define");
        #endif
            Console.WriteLine("Techies Era");
            Console.Read();
        }
    }

Vijay
Techies Era

Access Modifiers

Q 1 :What is accessibility modifier "protected internal"?
A. It is aaviliable to classess that are within the same assembly and derived from the specified base class.
B. It is aviliable within the class defination
C. It is most permissive access level.
D. It is the lease permissive access level.

Ans : A

Garbage Collection

Q 1 :What will be the output of below Program?
class Program
	{
		static void Main(string[] args)
		{
			using (TechiesEra obj = new TechiesEra())
			{
				obj.Print();
			}
			Console.Read();
		}
	}
class TechiesEra
	{
		public void Print()
		{
			Console.WriteLine("1");
		}
	}
A. 1.
B. Compile Time Error.
C. Run Time Error.
D. None of the Above.

Ans : B ('TechiesEra': type used in a using statement must be implicitly convertible to 'System.IDisposable')
We need to change this class as below in order to run this
class TechiesEra : IDisposable
	{
		public void Dispose()
		{
			
		}
		public void Print()
		{
			Console.WriteLine("1");
		}
	}

We need to Implement the IDisposable Interface and needs to implement the Dispose() Method.

C# 6.0

Q 1 :Explain using Static?

This is new feature in C# 6.0.using Static renders static methods available in global scope, without a type prefix of any kind
Before this feature we were using the static methods(WriteLine in the below method) as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpNewFeature
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
        }
    }
}
After using static feature we can use this as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace CSharpNewFeature
{
    class Program
    {
        static void Main(string[] args)
        {
                                 WriteLine("Hello");
        }
    }
}
This works with the classes created by us as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static CSharpNewFeature.TechiesEra;
namespace CSharpNewFeature
{
    class Program
    {
        static void Main(string[] args)
        {
                                 Test();
            Console.Read();
        }
    }
    class TechiesEra
    {
        public static void Test()
        {
                                WriteLine("Vijay Rana");
        }
    }
}

In the above example i am not calling Static Test method through class Name. i am using "using static CSharpNewFeature.TechiesEra" and calling Test method directly.

About Company

TechiesEra is a quality Training Center of Noida.TechiesEra provides summer training in Noida on Hadoop, MongoDB,CouchDB,NoSql,Dot Net, Android, Java,PHP etc with live projects. TechiesEra is group of highly qualified professionals in the field of IT education & recruitments.

Get in Touch

  • Phone:
    +91-9711965544 / 7503772600
  • Email:
    info@TechiesEra.com
  • Address:
    C-33,Sector - 2
    Near By : Nirula Hotel
    Noida -201301