Vous êtes sur la page 1sur 10

C# Questions

C# Interview Questions

1.) What are Classes and Objects?


1. Class is a template of which objects are instances. The
characteristics of the object are called properties (data
members). The actions of the object are called methods (function
members).
2. Classes provide a convenient method for packing together a group
of logically related data items and functions that work on them.
3. Class contain data members (constants, fields and events) and
Function members (methods, properties, indexers, operators,
constructors and destructors)
4. Class has five forms of accessibility:
a. public b. protected c. internal d. protected internal
d. private internal.
5. Class modifiers are a. new b. public c. protected d.
internal e. private f. abstract g. sealed.

2.) Define Encapsulation?


1. It is Known as Information Hiding
2. It is used to describe the process of defining the code and data
that form an Object.

3.) Define Abstraction?


Abstraction is the process of design that allows non-essential
details to be ignored.

4.) Define Inheritance?


1. Inheritance is the process by which objects of a particular class
get access to the data and can exhibit the behavior of objects of
another class.

2. The concept of Inheritance is Reusability. This means that


additional features can be added to an existing class without
modifying it by deriving a new class from existing one. The new
class will have combined features of both the classes.

5.) Define Polymorphism?


It is the ability to behave in more than one form.
For example, Addition operator, in this the operation of addition
will generate a sum. If the operands were strings, then the
operation would produce a third string by concatenation.

1 of 10
C# Questions

6.) Important Features of C# ?


1. Automatic Garbage Collection.
2. Support for Interfaces.
3. Type safe and automatically initialized variables.
4. COM and Windows API support.
5. Versioning
6. Delegates.

7.) What is automatic garbage collection?


Once the reference to the created object is lost, the memory can
be safely cleared and reclaimed by the GC. This helps save work for
programmers and prevents memory leaks.

8.) What is Versioning?


The new version is said to be compatible with the older
version if the code written in the old version works with the new version
when recompiled. When projects become more complex and team size is
big, the need of versioning becomes significant. In C# it is possible to
version each component, thus allowing each component to evolve
independently and reduce version conflict.

9.) What is a delegate?


Delegate allows programmer to encapsulate a reference to a
method inside a delegate object. The delegate object can then be
passed to code, which can call the reference method without
having to know which method is invoked. They are object
oriented, type safe, & secure. (Or)
Delegates serve the purpose of containing reference to a function.
At runtime, the actual class of the object need not be known, so
objects of different classes can be substituted provided they
adhere to the same method signature.

10.)What is implicit and explicit conversion?


Implicit conversion is assigning a lower size value to the higher
size value; the compiler will automatically convert LT to HT.
Explicit Conversion is assigning a higher size value to the lower
size value; this is done by conversion of a data type to another.
Byte Y, Int X, Y= (byte) X

11.)What is checked and unchecked operator?


Used to control the overflow-checking context for integral-type
arithmetic operation conversions.

2 of 10
C# Questions

12.) What is Lock statement?


Lock statement is used to obtain a mutually exclusive
lock for a given object .It is released after executing the statement. It
must denote a value of the reference type. An implicit boxing
conversion is not performed of a lock statement and hence it is invalid
for expression to denote a value of a value-type.

12.)What is the using statement?


It is used to obtain one or more resources, executes a statement
and then disposes the resource.

13.)What are arrays?


1.) Arrays contain a number of variables accessed through
computed indices.
2.) All elements inside the array must be of the same type.
3.) Arrays are Zero indexed.
4.) They can be single or multi dimensional.
5.) Initializing Array:
Single Dimensional:
Int [] numbers = new int [3] {1,2,3};
Multi Dimensional:
Int [,] numbers=new int [2,2]{{1,2},{2,3}};
Jagged Array
Int [][] numbers = new int [2][] {new int [] {3,2,4},
new int []{5,6,7,8,9}};
6.) Accessing Array:
They are accessed by assigning the number to the
member located at indices.
Int [,] numbers={{1,2}, {3,4}, {5,6}, {7,8}, {9,10}}
Numbers [1,1]=5;

14.)What is enum or enumerations?


1.) Enum is used to declare a distinct type consisting of named
constants.
2.) It can be declared in the place of Class Declaration.
3.) Enum example:
Enum Days {Sat=1,Sun.Mon, Tue, Wed, Thur, Fri}
4.) Default Type of enum is Int. and the value of the First Element
is Zero. Each successive element is increased by one. Explicit
Value can also be specified.

3 of 10
C# Questions

15.)What is a structure and why do we need it?


1.) Can contain a number of data types grouped together in a single
variable.
2.) Can contain constants, fields, methods, properties, indexers,
events, operators, constructors, and nested type declarations.
3.) Can also implement interfaces.
4.) Does not support inheritances.
5.) Syntax:
<Struct-modifier>struct <struct-name>[:parent
interfaces>]
{
<Access specifier> <member type> <member
name>
}[;]

16.)What are function members?


Function members are used to implement computations and
actions that can be performed by a class.

17.)What are method parameters?


Parameters are of two
Formal parameters: Parameters in a function declaration and they
are separated by commas.
Actual Parameters: Parameters in a function call.

18.) What are four Formal parameters?


Value parameters, Reference parameter, Output parameter,
Params Parameter

19.) What is a Value Parameter?


Parameters that are declared with no modifier.
Ex: Class test {
Static void Swap (int x, int y)
{ Int temp=x; x=y; y=temp; }
static void Main (){int I=1,j=2;Swap (I, j); };
}
20.) What is a reference parameter?
1.) Reference parameter is declared with a ref modifier.
2.) Unlike Value parameter, they represent the same storage
location as the variable given in the argument in the method invocation.
Ex: Class test {static void Swap (ref int x, ref int y) {int temp=x;
x=y; y=temp;} static void Main (){Swap (ref I, ref j)}}

4 of 10
C# Questions

21.) What are output parameters?


1.) A parameter declared with out modifier.
2.) Similar to Ref parameter, they represent the same storage
location as the variable given in the argument in the method
invocation.
Ex: Class test {static void SplitPath (string path, out string dir, out
string name) {int temp=x; x=y; y=temp; dir=path.substring (0,i); name=
path. Substring (i);} static void Main (){SplitPath (“C:\\Hello.txt”, out dir,
out name)}}
22.) What are Params parameter?
The params keyword lets you specify a method parameter that takes an
argument where the number of arguments is variable.

No additional parameters are permitted after the params keyword in a method


declaration, and only one params keyword is permitted in a method
declaration.

23.) What are Static and Instance Method?

Static Methods:

1. Static Methods Includes a Static modifier.


2. Does not operate on Specific Instances.
3. An error if virtual, abstract, or override modifier included

Instance Methods:

1.No static modifier is used.

2.Operates on a given instance of a class.

3.The Instance can be accessed with this keyword.

24.) What is Fields?

Fields are members that represent variables.

24.) What is readonly modifier?

When a field declaration includes a readonly modifier, assignments to


the fields introduced by the declaration can only occur a part of the
declaration or in a constructor in the same class.

5 of 10
C# Questions

26.) Difference between Structures and Classes?

Structs Classes
They are Value Types They are Reference Types
Structs can be used to create objects
that behave like built-in types & use
them wherever required
Occupies Less Memory Space since Occupies More Memory Space since
new instance for struct are not new instances are created, data will
created, so the data occupies less occupy more space.
memory area

27.) What are Value Types

1.) Variables of a certain value type always contains value of that type

2.) Can never be null.

3.) Implicitly Sealed and a variable of certain value type cannot


reference an object of a more derived type.

4.) Value Types are Simple Types(Integral Type, Bool Type, Floating Point,
Decimal Type), Struct Types, and Enumeration Type

28.) What are Reference Type?

1.) A reference type stores the reference of an object instead of storing


its value.
2.) It includes Object Type, Class Type, Interfaces, Delegates, String
Type, and Arrays.

29.) What is the difference between value And Reference Types?


Value Type Reference Type
Variables of Value Types directly Variables of the Reference type stores
contain their data references to Objects.

30.) Define Boxing and Unboxing?


Boxing is an implicit conversion of a value type to the type object or to
any interface type implemented by this value type.
Unboxing is an explicit conversion from the type object to the value type
or from an interface type to a value type that implements the interface.

6 of 10
C# Questions

31.) Define Constructor?


1.) Special Member method whose main operation is to allocate memory
and initialize the objects of its class.
2.) Distinct from other member functions of the class and has the same
name as its class.
3.) Executed automatically when a class is instantiated.
4.) Constructors are not inherited. Thus a class has no other constructor
than those that are actually declared in the class. If a class contains no
constructor declaration, a default constructor is automatically provided.

32.) What are the two types of Constructor?


1.) Static and 2.) Instance Constructor

33.) What is static constructor?


1.) Implements actions required to initialize a class.
2.) It is executed before any instances of the class get executed.
3.) It is executed before any static constructor of its derived classes is
executed.
4.) Static constructor for a class never executed more than once.

34.) What is Instance Constructor?


1.) Implements Actions required to initialize instances of a class.

35.) What are Default Constructors?


1.)If a class contains no constructor declarations, a default constructor is
automatically provided.
2.) It is of the Form:
public C(): base(){}

36.) What are Private Constructor?


1.) When a class declares only private constructors it is not possible for
other classes to derive from the class or create instances of the class.
2.) It is used only in classes that contain static members.
3.) Atleast one private constructor must be declared to suppress the
automatic generation of a default constructor.

37.) What are Destructors?


1.) A special member function invoked when an object is destroyed.
2.) Called for Local, non-static variables when the function in which they
are defined is about to terminate.
3.) Called for static or global variables before the program terminates.

7 of 10
C# Questions

38.) What are virtual methods?


1.) When an instance method declaration includes a virtual modifier, the
method is said to be virtual.
2.) Includes Virtual Keyword/ modifier.
3.) They are overridden by derived classes using override keyword.
4.) Used to implement late binding. (Dynamic binding)
5.) It is abstract when the method is without a body.

39.) Define Binding?


It is the process of associating a function name with an object.

40.) Define Early Binding or Static Polymorphism?


If the binding is made at runtime, then it is EB or SP.

41.) Define Late Binding or Dynamic Polymorphism?


If binding is done at runtime, then it is LB or DP. And it is invoked
through Reference, virtual, and override.

42.) What are abstract methods?


1.) Instance method which includes an abstract modifier.
2.) Also a virtual method implicitly
3.) It is always blank, since no body is allowed
4.) Declaration introduces a new virtual method but does not provide
Implementation of the method.
4.) It must be overridden by derived classes else derived class will be
considered abstract and they cannot be instantiated.
5.) They are intended to be a base class of other classes.

43.) What are Sealed Classes?


1.) It is used to prevent derivation from a class.
2.) Cannot also be an abstract class.
3.) Sealed modifier is primarily used to prevent unintended
derivation.

44.) What are Interfaces?


1.) An interface defines a contract. A class or structure that implements
an interface must adhere to its contract.
2.) An interface may inherit from multiple base interfaces, and a
class or struct may implement multiple interfaces.
3.) Interfaces can contain methods, properties, events, and
indexers. 4.) The interface itself does not provide implementations
for the members that it defines.
5.) The interface merely specifies the members that must
be supplied by classes or interfaces that implement the interface.
6.) Interfaces are abstract classes that are left unimplemented.
7.) Member data is limited to static final variables.

8 of 10
C# Questions

8.) Declaring Interfaces: <access modifier> interface


<identifier>{<Interface body>}
9.) Accessing Interfaces: <access modifier> class <identifier>:
<Interface> {<class body>}

45.) Define Pointers?


1.) Variable that contains an address.
2.) Used when passing data to a function.
3.) Used for passing arrays, structs, classes to function.
4.) Unsafe specifies unsafe context that is it shows that pointers are
used to access the address, which is unsafe.
5.) unsafe keyword is used in Main() statement while working with
pointers.

46.) What is method overloading?


1.) The method inside a class has same method name but different
signature (arguments). This process is known as Method Overloading.
2.) Example for Polymorphism Is operator overloading

47.) What is method overriding?


1.) In a derived class, the method has the same name and same
signature but the implementation in the derived class differs from the
base/deriving class.

48.) What are properties?


1.) Properties provide a way to read and write the field in a class.
2.) It is the natural Extension of Fields.
3.) Do not denote storage locations.
4.) Have Accessors that specify the statements to execute.
5.) Property having only get accessor is said to be Read-only
6.) Property having only set accessor is said to be Write-only

49.) What are Indexers?


1.) Indexers allow instances of a class to be indexed which is similar to
array functionalities.
2.) Indexers are not classified as variables
3.) Indexers do not have user-defined names.
4.) this denotes the indexer on the default interface.(For an explicit
interface member implementation)

9 of 10
C# Questions

50.) Difference between Property and Indexer?


Property Indexer
1.) Identified by its name Identified by its signature
2.)Accessed through a simple name or Access through an element access.
a member access.
3.) Can be a static or an instance Must be an instance member
member
4.) A get accessor of a property has no A get accessor of an indexer has the
parameters. same formal parameter list as the
indexer.
5.) A set accessor of a property A set accessor of an indexer has the
contains the implicit value parameter. same formal parameter list as the
indexer, in addition to the value
parameter.

51.) What is exception?


1.) They provide a structured, uniform and type safe way of handling
error conditions at System level and Application Level.
2.) It can be thrown in two cases. A.) when exceptional conditions occur
during processing of certain C# statements and Expressions. Or B.) as a result
of executing throw statement.

52.) How to handle exception?


Using Try Catch Finally statement

53.) What is Try, Catch statement?


It provides mechanism for catching exceptions that occur during
execution of a block.

54.) What is finally Block?


1.) Must not contain a return statement.
2.) Always executed when control leaves a try statement.

55.) What is Throw Statement?


1.) It is used to explicitly throw an exception
2.) It is generated by evaluating expressions and also must denote
a value of class type System.Exception or its Sub class.

56.) Define Accessors?


1.) The accessor of a property contains the executable statements
associated with getting (reading or computing) or setting (writing) the
property.
2.) Its declarations contain a get accessor, a set accessor or both.

10 of 10

Vous aimerez peut-être aussi