Monday 14 January 2019

C# Interview Questions

It is C# basic interview question for beginners level.


1.       What is Class?
Ans: Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.

2.       Difference between Class and Objects?
Ans: Any entity that has state and behavior is known as an object. An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory.

3.       What is Oops concept?
a.       What is inheritance?
Ans: When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

b.       What is Encapsulation?
Ans: Binding (or wrapping) code and data together into a single unit are known as encapsulation.

c.       What is Polymorphism?
Ans: If one task is performed by different ways, it is known as polymorphism.

d.       What is Abstract?
Ans: Hiding internal details and showing functionality is known as abstraction


4.       What is Interface? Example.
Ans: Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated.
It is used to achieve multiple inheritance which can't be achieved by class. It is used to achieve fully abstraction because it cannot have method body.
Its implementation must be provided by class or struct. The class or struct which implements the interface, must provide the implementation of all the methods declared inside the interface. Example: IWebDriver, IWebElement




5.       Difference between Abstract and Virtual?
Ans: Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method. So, abstract methods have no actual code in them, and subclasses HAVE TO override the method.

6.       What is access modifiers PrivatePublicProtectedInternalProtected Internal.
Ans:
Private: Scope of the variable/method will be limited at class level. It will not be used/access outside of class.

Public: Variable/method will be accessed outside of the class.

Protected: Variable/method will be accessed only by derived/child classes.

InternalThis specifier is accessible only within files in the same assembly

Protected Internal: Variable or function declared protected internal can be accessed in the assembly in which it is declared. It can also be accessed within a derived class in another assembly

7.       If you are not override virtual Method in derived class then what error, it will give.
Ans: No error will be thrown; however, compiler will give warning message.

8.       Suppose you have properties which is required in different classes then what will be your approach. For example: if properties like Created User, Date Modified, Date Created is required in different classed then what will be your approach.
Ans: Create a base class for the common properties and extend that to the child/derived class.

9.       Difference between vardynamic, and object?
Ans:
Variables declared with var are implicitly but statically typed. When using the 'var' keyword, the type is decided by the compiler at compile time
e.g.       var myName =”TestMember”;
myName = 1;

Variables declared with dynamic are dynamically typed. whereas when using the 'dynamic' keyword, the type is decided by the runtime
e.g.         dynamic myName =”TestMember”;
myName = 1;

Each object in C# is derived from object type.
object myName =”TestMember”;
myName = (int)1;


10.   What is difference between string and stringBuilder?
Ans: string instance is immutable. Immutable means once we create a string object we cannot modify the value of the string Object in the memory

StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap

11.   How to format string, suppose if I want to print “Hello ‘World’, I am here!
Result:
string value = “World”;
string.format(“Hello {0}I am here!”, value);

12.   What is Attribute? How can we create Attributes?
Ans: An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute

13.   What is exception how to define it?
Ans:
try {

} catch (Exception e) {

} finally {

}

14.   What is Anonymous function. When we can use this function.
Ans: As the name suggests, an anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.

15.   What are type of collection?
C# includes specialized classes that hold many values or objects in a specific series, that are called 'collection'.

There are two types of collections available in C#: non-generic collections and generic collections.
Non-Generic collections
Generic collections
ArrayList
List<T>
SortedList
Dictionary<TKey,TValue>
Stack
SortedList<TKey,Tvalue>
Queue
Hashset<T>
Hashtable
Queue<T>
BitArray
Stack<T>


16.   Difference between array and ArrayList.
Ans: An array is a special type of data type which can store fixed number of values sequentially using special syntax. The length of array cannot change during run time.
However, ArrayList stores objects of any type like an array. However, there is no need to specify the size of the ArrayList like with an array as it grows automatically.

17.   What is List and it belongs to which namespace?
Ans: C# List<T> class is used to store and fetch elements. It can have duplicate elements. It is found in System.Collections.Generic namespace.

18.   What is difference between List and IList?
Ans:  The difference is that IList is an interface and List is a class. List implements IList, but IList can't be instantiated.
List<int> intList = new List<int>();

19.   What is difference from List and Tuple?
List is a mutable type meaning that lists can be modified after they have been created. A tuple is similar to a list except it is immutable. There is also asemantic difference between a list and a tuple.

List<int> intList = new List<int>();

Tuple<intstringstring> person = new Tuple <intstringstring>(1, "Steve""Jobs");

20.   What is difference in dictionary and HashTable?
21.   What is use of “Using” Keyword in C#?
using statement, in C#, is a statement that contains the "using" keyword and provides the syntax to specify the scope of the use of a resource object. The using statement is useful for objects whose lifetimes are within the method or block in which they are created.

22.   What is basethis keyword?
base keyword is used to refer Parent Class members or methods
this is used to reference member and methods of instance of the same object.


23.   How you will call base method in derived class.?
using System; 

using System; 
public class Animal
    public string color = "white"; 

public class Dog: Animal 
    string color = "black"; 
    public void showColor() 
    { 
        Console.WriteLine(base.color); 
        Console.WriteLine(color); 
   }     
}

White
black

24.   What is static keyword and what is its scope?
Ans: In C#, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C#, static can be field, method, constructor, class, properties, operator and event.

25.   Difference between staticreadonly and constant.
Ans:
Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program

Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method. Let's see

Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime. Let's understand it practically.

26.   Difference between finalize and Dispose.
Ans:  IDisposable interface expose Dispose method where code to release unmanaged resource will be written. Finalize method also called destructor to the class. Finalize method cannot be called explicitly in the code. Finalize method cannot be implemented directly it can only be implement via declaring destructor.


27.   Write a C# code which can read text file and find the duplicate words in it?

No comments:

Post a Comment