Tuesday 23 February 2021

Questions Mostly Asked To DotNet Developer

 1) OOPs Basic Concept? Ans: Oops have four basic concepts

*Abstraction: Hiding the background details and showing only relevant data . i.e putting common data in abstract class
* Encapsulation: Wrapping up the data in single container called class. for security purpose
* Inheritance: process by which one object acquires the properties of another object i.e creating the parent child relationship.
*Polymorphism: it means many forms. It is of two types. a)Compile Time b)Run Time polymorphism

2) Compile Time vs Run Time : Ans:-

Compile Time is also called as overloading whereas Run Time is also called as overriding
Compile Time is also called as static binding and early binding whereas Run Time is also called as dynamic binding and late binding.
Creating multiple methods in class with same name but different parameters is called ovrloading
Creating multiple methods in class with same name and same signature is called overriding
Compile Time is achieved by method overloading and operator overloading 
Run Time  is achieved by virtual  functions and pointers

3) What are the access Specifiers and Its Types?

Ans: There are five access specifiers. 
*Public: It has global access 
*Private: It can be accessed within the class
*Protected: Only child class can access the parent class
*Internal: It has access within the assembly
*Protected Internal: It has two behavior , within the assembly it has act has internal and outside assembly it act as protected means only child class can access.

4) Does private class members inherit to derived class?

Ans: Yes We can inherit the private class members to the derived class but we cant access them.

5) Constructors And its key points? Ans: It is special class which is invoked when the instance of class is created. It is used to initialized the private field. A class can have any number of constructors. Within the class we can create only one static constructors. It does not have return type or void

There are five types of Constructors. 
Default, Parameterized, Static , Private , Copy

6)Use of Private Constructor? Ans: To stop object creation of class, To create the singleton class, to stop class to be inherited

7) Destructors: It is automatically invoked when the object is destroyed , name of destructors is same as class with tilde (~) sign, it is used to free dynamic allocated memory

8) Delegates and Its Types: These are the function pointers and hold the method reference. There are 3 main types of delegates. Single delegates, Multicaste delegates, Generic delegates.

Generic delegates have 3 more types: Func:-It takes one or more input parameters but return one out parameters, Action: It takes one or more input parameters but returns nothing, predicate : It takes one input parameters and return Boolean true or false.

9) Use of Interface: Multiple inheritance, Extensibility, Loose Coupling

10) Abstract vs Interface

11)Explain sealed class in C#?  A:Sealed class is used to prevent the class from being inherited from other classes. So “sealed” modifier also can be used with methods to avoid the methods to override in the child classes.  

12)List out the differences between Array and ArrayList in C#? A:Array stores the values or elements of same data type but arraylist stores values of different datatypes.   Arrays will use the fixed length but arraylist does not uses fixed length like array.

13)Why to use “using” in C#? A: “Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.

14)What is the difference between “constant” and “readonly” variables in C#? A: “Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables.  “readonly” variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.

15) What is the difference between “dispose” and “finalize” variables in C#? A: Dispose - This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.  Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.

16)What is the difference between “finalize” and “finally” methods in C#? A:Finalize – This method is used for garbage collection. So before destroying an object this method is called as part of clean up activity.  Finally – This method is used for executing the code irrespective of exception occurred or not

17)What is the difference between “throw ex” and “throw” methods in C#? A:throw : If we use "throw" statement, it preserve original error stack information. In exception handling "throw" with empty parameter is also called re-throwing the last exception.  throw ex : If we use "throw ex" statement, stack trace of exception will be replaced with a stack trace starting at the re-throw point.

18)In try block if we add return statement whether finally block is executed in C#? A: Yes. Finally block will still be executed in presence of return statement in try block.

19)What you mean by inner exception in C#? A:Inner exception is a property of exception class which will give you a brief insight of the exception i.e, parent exception and child exception details.

20)Explain String Builder class in C#? A:This will represent the mutable string of characters and this class cannot be inherited. It allows us to Insert, Remove, Append and Replace the characters. “ToString()” method can be used for the final string obtained from StringBuilder. For example, StringBuilder TestBuilder = new StringBuilder("Hello"); TestBuilder.Remove(2, 3); // result - "He" TestBuilder.Insert(2, "lp"); // result - "Help" TestBuilder.Replace('l', 'a'); // result - "Heap"

21)What is the difference between “StringBuilder” and “String” in C#? A:StringBuilder is mutable, which means once object for stringbuilder is created, it later be modified either using Append, Remove or Replace. String is immutable and it means we cannot modify the string object and will always create new object in memory of string type.

22)What is the difference between methods – “System.Array.Clone()” and “System.Array.CopyTo()” in C#? A:“CopyTo()” method can be used to copy the elements of one array to other. “Clone()” method is used to create a new array to contain all the elements which are in the original array.

23)Explain Generics in C#? A:Generics in c# is used to make the code reusable and which intern decreases the code redundancy and increases the performance and type safety. Namespace – “System.Collections.Generic” is available in C# and this should be used over “System.Collections” types.

24)Explain object pool in C#? A:Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.

25)What you mean by delegate in C#? A:Delegates are type safe pointers unlike function pointers as in C++. Delegate is used to represent the reference of the methods of some return type and parameters.

26)What are the types of delegates in C#? A:Below are the uses of delegates in C# - a)Single Delegate b)Multicast Delegate c)Generic Delegate

27)What are the differences between events and delegates in C#? A:Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in subscriber/destination model.

28)What are the uses of delegates in C#? A:Below are the list of uses of delegates in C# - a)Callback Mechanism b)Asynchronous Processing c)Abstract and Encapsulate method d)Multicasting

29) Why to use “Nullable Coalescing Operator” (??) in C#? A: Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable.  For example, double? myFirstno = null; double mySecno; mySecno = myFirstno ?? 10.11;

30)What is the difference between “as” and “is” operators in C#? A:“as” operator is used for casting object to type or class. “is” operator is used for checking the object with type and this will return a Boolean value.

31)What is the difference between CType and Directcast in C#? A:CType is used for conversion between type and the expression. Directcast is used for converting the object type which requires run time type to be the same as specified type.

32)Why to use lock statement in C#? A:Lock will make sure one thread will not intercept the other thread which is running the part of code. So lock statement will make the thread wait, block till the object is being released.

33)Explain Hashtable in C#? A:It is used to store the key/value pairs based on hash code of the key. Key will be used to access the element in the collection. For example, Hashtable myHashtbl = new Hashtable(); myHashtbl.Add("1", "TestValue1"); myHashtbl.Add("2", "TestValue2");

34)How to check whether hash table contains specific key in C#? A:Method – “ContainsKey” can be used to check the key in hash table. Below is the sample code for the same – Eg: myHashtbl.ContainsKey("1");

35) What is enum in C#? A:enum keyword is used for declaring an enumeration, which consists of named constants and it is called as enumerator lists. Enums are value types in C# and these can’t be inherited. Below is the sample code of using Enums Eg: enum Fruits { Apple, Orange, Banana, WaterMelon};

36))What is the difference between “continue” and “break” statements in C#? A: “continue” statement is used to pass the control to next iteration. This statement can be used with – “while”, “for”, “foreach” loops.  “break” statement is used to exit the loop.

37)Write a sample code to write the contents to text file in C#? A:Below is the sample code to write the contents to text file – Using System.IO; File.WriteAllText(”mytextfilePath”, “MyTestContent”);

38)Explain Partial Class in C#? A:Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with “partial” keyword.

39) Explain Copy constructor in C#? A:If the constructor contains the same class in the constructor parameter then it is called as copy constructor. class MyClass {  public string prop1, prop2;  public MyClass(string a, string b)  {  prop1 = a;  prop2 = b;  }  public MyClass(MyClass myobj) // Copy Constructor  {  prop1 = myobj.prop1;  prop2 = myobj.prop2;  } }

40) Explain Copy constructor in C#? A:If the constructor contains the same class in the constructor parameter then it is called as copy constructor. class MyClass {  public string prop1, prop2;  public MyClass(string a, string b)  {  prop1 = a;  prop2 = b;  }  public MyClass(MyClass myobj) // Copy Constructor  {  prop1 = myobj.prop1;  prop2 = myobj.prop2;  } }

41)Explain Indexers in C#? A:Indexers are used for allowing the classes to be indexed like arrays. Indexers will resemble the property structure but only difference is indexer’s accessors will take parameters. For example, class MyCollection<T> {  private T[] myArr = new T[100];  public T this[int t]  {  get  {  return myArr[t];  }  set  {  myArr[t] = value;  }  } }

42)What are the collection types can be used in C#? A:Below are the collection types in C# - a)ArrayList b)Stack c)Queue d)SortedList e) HashTable f)Bit Array

43)Explain Attributes in C#? A:Attributes are used to convey the info for runtime about the behavior of elements like – “methods”, “classes”, “enums” etc.  Attributes can be used to add metadata like – comments, classes, compiler instruction etc.

44)List out the pre defined attributes in C#? A:Below are the predefined attributes in C# - a)Conditional b)Obsolete c)Attribute Usage

45)What is Thread in C#? A:Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time consuming processes then it’s better to use Multithreading., which involves multiple threads.

46)List out the states of a thread in C#? A:Below are the states of thread – a)Unstarted State b)Ready State c)Not Runnable State d)Dead State

47)Explain the methods and properties of Thread class in C#? A:Below are the methods and properties of thread class – a)CurrentCulture b)CurrentThread c)CurrentContext d)IsAlive e)IsThreadPoolThread f)IsBackground g)Priority

48)Explain Static Members in C# ? A:If an attribute's value had to be same across all the instances of the same class, the static keyword is used. For example, if the Minimum salary should be set for all employees in the employee class, use the following code. private static double MinSalary = 30000; To access a private or public attribute or method in a class, at first an object of the class should be created. Then by using the object instance of that class, attributes or methods can be accessed. To access a static variable, we don't want to create an instance of the class containing the static variable. We can directly refer that static variable as shown below. double var = Employee.MinSalary ;

49)Explain Overloading in C# ? A:When methods are created with the same name, but with different signature its called overloading. For example, WriteLine method in console class is an example of overloading. In the first instance, it takes one variable. In the second instance, “WriteLine” method takes two variable. Console.WriteLine(x); Console.WriteLine("The message is {0}", Message); Different types of overloading in C# are a)Constructor overloading b)Function overloading c)Operator overloading

50)Can Multiple Inheritance implemented in C# ? A:In C#, derived classes can inherit from one base class only. If you want to inherit from multiple base classes, use interface.

51)What is Polymorphism in C# ? A:The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism Compile time polymorphism. Best example is Overloading Runtime polymorphism. Best example is Overriding

52)Explain the use of Virtual Keyword in C# ? A:When we want to give permission to a derived class to override a method in base class, Virtual keyword is used.

53)What is an Interface in C# ? A:An interface is similar to a class with method signatures. There wont be any implementation of the methods in an Interface. Classes which implement interface should have an implementation of methods defined in the abstract class.

54)What is a Destructor in C# ? A:Destructor is a special method that get invoked/called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.

55)What's the difference between an interface and abstract class? A:Interfaces have all the methods having only declaration but no definition. In an abstract class, we can have some concrete methods. In an interface class, all the methods are public. An abstract class may have private methods.

56)Why can't you specify the accessibility modifier for methods inside the interface? A:In an interface, we have virtual methods that do not have method definition. All the methods are there to be overridden in the derived class. That's why they all are public.

57)How can we set the class to be inherited, but prevent the method from being over-ridden? A:Declare the class as public and make the method sealed to prevent it from being overridden.

58)What is the difference between a Struct and a Class? A:Structs are value-type variables, and classes are reference types. Structs stored on the Stack causes additional overhead but faster retrieval. Structs cannot be inherited.

59)How we can create an array with non-default values? A:We can create an array with non-default values using Enumerable.Repeat.

60)How to implement a singleton design pattern in C#? A:In a singleton pattern, a class can only have one instance and provides an access point to it globally. Eg: Public sealed class Singleton { Private static readonly Singleton _instance = new Singleton(); }

61) What is the difference between directcast and ctype? A:DirectCast is used to convert the type of object that requires the run-time type to be the same as the specified type in DirectCast.  Ctype is used for conversion where the conversion is defined between the expression and the type.


No comments:

Post a Comment