C#
C#
com/interview-Questions/52-dot-net-interview-
Questions/417-c-oops-interview-Questions-and-answers?
showall=&start=8
http://career.guru99.com/top-50-c-sharp-interview-Questions-answers/
Question1.
What is C#?
Answer
Question2)
Answer
Question3
Can multiple catch blocks be executed?
Answer
No, Multiple catch blocks cant be executed. Once the proper catch code
executed, the control is transferred to the finally block and then the code
that follows the finally block gets executed.
Question4
Answer
Question5
. What is an object?
Answer
Question6
. Define Constructors?
Answer
A constructor is a member function in a class that has the same name as its
class. The constructor is automatically invoked whenever an object class is
created. It constructs the values of data members while initializing the
class.
Question7.
Answer
The array which has elements of type array is called jagged array. The
elements can be of different dimensions and sizes. We can also call jagged
array as Array of arrays.
Question8.
Answer
Question9.
Answer
The using block is used to obtain a resource and use it and then
automatically dispose of when the execution of block completed.
Question10.
What is serialization?
Answer
Question11.
Answer
We cant use This in a static method because we can only use static
variables/methods in a static method.
Question12
Answer
Constant variables are declared and initialized at compile time. The value
cant be changed afterwards. Read only is used only when we want to
assign the value at run time.
Question13.
Answer
Interface is an abstract class which has only public abstract methods and
the methods only have the declaration and not the definition. These abstract
methods must be implemented in the inherited classes.
Question14
Answer
Value types are stored in the Stack whereas reference types stored on heap.
Value types:
Reference Types:
Question15
Answer
Custom Controls are controls generated as compiled code (Dlls), those are
easier to use and can be added to toolbox. Developers can drag and drop
controls to their web forms. Attributes can be set at design time. We can
easily add custom controls to Multiple Applications (If Shared Dlls), If they
are private then we can copy to dll to bin directory of web application and
then add reference and can use them.
User Controls are very much similar to ASP include files, and are easy to
create. User controls cant be placed in the toolbox and dragged dropped
from it. They have their design and code behind. The file extension for user
controls is ascx.
Question16
Answer
We create sealed classes when we want to restrict the class to be inherited.
Sealed modifier used to prevent derivation from a class. If we forcefully
specify a sealed class as base class then a compile-time error occurs.
Question17.
Answer
Method overloading is creating multiple methods with the same name with
unique signatures in the same class. When we compile, the compiler uses
overload resolution to determine the specific method to be invoke.
Question18.
Answer
In an array, we can have items of the same type only. The size of the array is
fixed. An arraylist is similar to an array but it doesnt have a fixed size.
Question19
Answer
Answer
Answer
Question22.
Answer
Using Clone() method, we creates a new array object containing all the
elements in the original array and using CopyTo() method, all the elements
of existing array copies into another existing array. Both the methods
perform a shallow copy.
Question23.
Answer
Question24.
Answer
To catch an exception, we use try catch blocks. Catch block can have
parameter of system.Exception type.
Question25.
Answer
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.
Question26.
Answer
Question27.
Answer
Question28.
Answer
Generics are used to make reusable code classes to decrease the code
redundancy, increase type safety and performance. Using generics, we can
create collection classes. To create generic collection,
System.Collections.Generic namespace should be used instead of classes
such as ArrayList in the System.Collections namespace. Generics promotes
the usage of parameterized types.
Question29.
Answer
Question30.
Answer
ArgumentException, ArgumentNullException ,
ArgumentOutOfRangeException, ArithmeticException,
DivideByZeroException ,OverflowException , IndexOutOfRangeException
,InvalidCastException ,InvalidOperationException ,
IOEndOfStreamException , NullReferenceException ,
OutOfMemoryException , StackOverflowException etc.
Question31.
Answer
Sometimes there are some errors that need to be handeled as per user
requirements. Custom exceptions are used for them and are used defined
exceptions.
Question32.
Answer
Delegates are same are function pointers in C++ but the only difference is
that they are type safe unlike function pointers. Delegates are required
because they can be used to write much more generic type safe functions.
Question33
Answer
Colon is used as inheritance operator in C#. Just place a colon and then the
class name.
Question34.
What is the base class in .net from which all the classes are derived from?
Answer
System.Object
Question35.
Answer
Answer
Question37.
Why cant you specify the accessibility modifier for methods inside the
interface?
Answer
Question38.
How can we set class to be inherited, but prevent the method from being
over-ridden?
Answer
Declare the class as public and make the method sealed to prevent it from
being overridden.
Question39.
Answer
Implement is up to you as the method is inside your own class. There might
be problem when the methods from different interfaces expect different
data, but as far as compiler cares youre okay.
Question40.
Answer
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.
Question41.
Answer
Value types can take either their normal values or a null value. Such types
are called nullable types.
If(someID.HasVAlue)
If(someID.HasVAlue)
Question42.
Answer
Question43.
Question44
Answer
Question 45.
Answer
Indexers are known as smart arrays in C#. It allows the instances of a class
to be indexed in the same way as array.
Eg:
Question46.
What is difference between the throw and throw ex in .NET?
Answer
Question47.
Answer
Question48.
Answer
In singleton pattern, a class can only have one instance and provides access
point to it globally.
Eg:
{
Private static readonly Singleton _instance = new Singleton();
Question49.
Answer
DirectCast is used to convert the type of an 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.
Question50.
Answer
Question51
Answer
Easy to learn
Object oriented
Component oriented
Part of .NET framework
Question52
Answer53
Question54
Answer
Question55
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.
Question56
Answer
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.
Question57
Answer
Question58
Answer
Question59
Answer
Question60
Answer
protected internal can be accessed in the same assembly and the child
classes can also access these methods.
Question61
Answer
Question62
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.
Question63
Answer
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,
Question64
Answer
String is immutable and it means we cannot modify the string object and
will always create new object in memory of string type.
Question65
What is the difference between methods System.Array.Clone() and
System.Array.CopyTo() in C#?
Answer
Question66
Answer
Question67
Answer
This is a situation where in, multiple resources are dependent on each other
and this causes a lock condition and this makes the resource to be unused.
Question68
Answer
Generics in c# is used to make the code reusable and which intern
decreases the code redundancy and increases the performance and type
safety.
Question69
Answer
Object pool is used to track the objects which are being used in the code. So
object pool reduces the object creation overhead.
Question70
Answer
Question71
Answer
Single Delegate
Multicast Delegate
Generic Delegate
Question72
Answer
Func
Action
Predicate
Question73
Answer
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.
Question74
Answer
Question75
Callback Mechanism
Asynchronous Processing
Multicasting
Question76
Answer
Variable types does not hold null values so to hold the null values we have to
use nullable types. So nullable types can have values either null or other
values as well.
Question77
Answer
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;
Question78
Answer
is operator is used for checking the object with type and this will return
a Boolean value.
Question79
Answer
int x = 6;
int y = 7;
Question80
Answer
Directcast is used for converting the object type which requires run time
type to be the same as specified type.
Question81
Answer
C# code is managed code because the compiler CLR will compile the code
to Intermediate Language.
Question82
Answer
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.
Question83
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,
myHashtbl.Add("1", "TestValue1");
myHashtbl.Add("2", "TestValue2");
Question84
Answer
Method ContainsKey can be used to check the key in hash table. Below
is the sample code for the same
Eg: myHashtbl.ContainsKey("1");
Question85
Answer
Answer
For
While
Do.. While
Question87
Answer
Question88
Answer
Using System.IO;
File.WriteAllText(mytextfilePath, MyTestContent);
Question89
Answer
Boxing This is the process of converting from value type to reference type.
For example,
Question90
Answer
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.
Question91
Answer
Question)92
Answer
C# Compiler is CSC.
Question93
Answer
Question94
Answer
If the constructor contains the same class in the constructor parameter then
it is called as copy constructor.
class MyClass
prop1 = a;
prop2 = b;
prop1 = myobj.prop1;
prop2 = myobj.prop2;
Question95
Answer
If the constructor is declared as static then it will be invoked only once for
all number of instances of a class. Static constructor will initialize the static
fields of a class.
class MyClass
prop1 = a;
prop2 = b;
}
Static MyClass()
prop1 = myobj.prop1;
prop2 = myobj.prop2;
Question96
Answer
string.Concat(firstStr, secStr)
Question97
Answer
Indexers are used for allowing the classes to be indexed like arrays.
Indexers will resemble the property structure but only difference is
indexers accessors will take parameters. For example,
class MyCollection<T>
public T this[int t]
get
return myArr[t];
set
myArr[t] = value;
Question98
Answer
ArrayList
Stack
Queue
SortedList
HashTable
Bit Array
Question99
Answer
Attributes are used to convey the info for runtime about the behavior of
elements like methods, classes, enums etc.
Question100
Answer
Conditional
Obsolete
Attribute Usage
Question101
Answer
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 its better to use Multithreading., which involves
multiple threads.
Question102
Answer
Unstarted State
Ready State
Not Runnable State
Dead State
Question103
Answer
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.
Question104
Define Property in C# ?
Answer
Properties are a type of class member, that are exposed to the outside world
as a pair of Methods. For example, for the static field Minsalary, we will
Create a property as shown below.
get
return minimumSalary;
set
minimumSalary = value;
set Method will get triggered and value will be stored in minimumSalary
field.
Question105
Answer
set
{
if(value > 0)
minSalary = value;
Question106
Explain Inheritance in C# ?
Answer
class Car
public Car()
{
Console.WriteLine("Right Hand Drive");
public Ford()
CarFord.DriveType();
CarFord.Price();
What this means is that, all the methods and attributes of Base Class car are
available in Derived Class Ford. When an object of class Ford is created,
constructors of the Base and Derived class get invoked. Even though there
is no method called DriveType() in Class Ford, we are able to invoke the
method because of inheriting Base Class methods to derived class.
Question107
Answer
In C#, derived classes can inherit from one base class only. If you want to
inherit from multiple base classes, use interface.
Question108
What is Polymorphism in C# ?
Answer
Question109
Answer
class Car
public Car()
{
public Ford()
CarFord.DriveType();
CarFord.DriveType();
Right Hand
Question110
Answer
If the derived class doesn't want to use methods in the base class, derived
class can implement it's own version of the same method with same
signature. For example, in the classes given below, DriveType() is
implemented in the derived class with same signature. This is called Method
Hiding.