www.w3asp.net
TOP 20 C# INTERVIEW
QUESTION AND ANSWERS
1.What is concept of Boxing and Unboxing ?
Ans. Boxing is used to convert value types to object.
E.g. int x = 1;
object obj = x ;
Unboxing is used to convert the object back to the value type.
E.g. int y = (int)obj;
Boxing/unboxing is quiet an expensive operation.
2. Define Constructors in C#?
Ans. 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.
www.w3asp.net
www.w3asp.net
3.Explain namespaces in C#?
Ans. The built-in libraries are organized within namespaces.
Namespaces organize objects in an assembly. An assembly is a
reusable, version able and self-describing building block of a CLR
application. Assemblies can contain multiple namespaces.
Namespaces can contain other namespaces. We can add namespaces
in our application used the “using” keyword. Example-
using System;
using System.Collections.Generic;
4. Is C# fully object oriented language?
Ans. Yes, you can say C# is a fully object oriented language. It not
only supports the four criteria of OOPs, it also requires that
essentially all of your constructs be encapsulated in objects. That is,
C# does not let you develop outside of the OOPs methodology in any
meaningful way.
www.w3asp.net
5.What is a delegates in c#?
Ans. A delegate is a type safe function pointer. Using delegates you
can pass methods as parameters. To pass a method as a parameter,
to a delegate, the signature of the method must match the signature
of the delegate. This is why, delegates are called type safe function
pointers.
Delegate declaration determines the methods that can be referenced
by the delegate. A delegate can refer to a method, which has the
same signature as that of the delegate.For example, consider a
delegate:
public delegate int MyDelegate (string s);
The preceding delegate can be used to reference any method that
has a single string parameter and returns an int type variable.
Syntax for delegate declaration is:
delegate <return type> <delegate-name> <parameter list>
www.w3asp.net
6. What type of class cannot be inherited?
Ans. A sealed class cannot be inherited. A sealed class is used
primarily when the class contains static members. Note that a struct
is implicitly sealed; so they cannot be inherited.
7.Is it possible to use multiple inheritance in .NET?
Ans. Multiple Inheritance is an ability to inherit from more than one
base class i.e. ability of a class to have more than one super class, by
inheriting from different sources and thus combine separately-defined
behaviors in a single class. There are two types of multiple
inheritance:
* multiple type/interface inheritance
* multiple implementation inheritance
C# supports only multiple type/interface inheritance. There is no
support for multiple implementation inheritance in .NET. That means
a class can only derived from one class.
www.w3asp.net
8.What is an object in c-sharp?
Ans. An object is an instance of a class through which we access the
methods of that class. “New” keyword is used to create an object. A
class that creates an object in memory will
contain the information about the methods, variables and behavior of
that class.
9.What is Event?
Ans. Notification by an application or operating system that some
event has occurred. An event is fired—raised—when a predefined
condition occurs, e.g., focus change in a GUI, interval timer, mouse
click. An event handler is called in response to an event. The C#
event model is publish and subscribe: Publishers create and publish
events: Subscribers register to receive specific events: Then,
publishers send out their events only to subscribers.
www.w3asp.net
10.What is the Interface (C#)?
Ans. The interface contains only member declarations. A class
implementing an interface must provide the implementation of the
interface members. An interface cannot contain constants,
constructors, data fields, destructors, or static members. Interface
member declarations are implicitly public. Classes or structs may
inherit any number of interfaces. A C# interface is declared with the
interface keyword.
11. Are C# constructors inherited?
Ans. C# constructors cannot be inherited. If constructor inheritance
were allowed, then necessary initialization in a base class constructor
might easily be omitted. This could cause serious problems which
would be difficult to track down. For example, if a new version of a
base class appears with a new constructor, your class would get a
new constructor automatically.
www.w3asp.net
12. What is the Abstract class (C#)?
Ans. A class representing a base class which other types can
subclass. A normal, non-abstract class is called a concrete class. An
abstract class may contain either or both abstract and concrete
methods. (Properties cannot be marked abstract.) As in ordinary
inheritance, a type derived from an abstract class inherits all the base
type members including any method implementations. But, if any
abstract methods are inherited, the class must either declare itself
abstract or provide an implementation. A C# abstract class is
declared with the abstract keyword.
13. How many times can a constructor be called during
lifetime of the object?
Ans. Only once.
www.w3asp.net
14. Advantages of Generics in c#?
Ans. Advantages of generics are:
1-Reusability
2-Improves Performance
15. Explain Destructor in c#?
Ans. Destructors’ are used to destruct instances of classes. When we
are using destructors in C#, we have to keep in mind the following
things:
* A class can only have one destructor.
* Destructors cannot be inherited or overloaded.
* Destructors cannot be called. They are invoked automatically.
* A destructor does not take modifiers or have parameters.
www.w3asp.net
16. What is JIT in asp.net?
Ans. JIT is a compiler that converts MSIL to native code. The native
code consists of hardware specific instructions that can be executed
by the CPU. Rather than converting the entire MSIL file to native
code, the JIT converts the MSIL as it is needed during execution. This
converted native code is stored so that it is accessible for subsequent
calls.
17. What is the use and purpose of using keyword?
Ans. The using keyword is used to define block of code with the
guarantee that anything present within the body of using will be
dispose off (even if the exception has been thrown).The use of this
keyword is excellent as it cleans the garbage in the memory
automatically by disposing which implements IDisposable interface.
www.w3asp.net
18. What is difference between a file and stream.
19. What is the execution entry point for a C# console
application?
Ans. A file is an ordered collection of the bytes of data with proper
name and physical existence. A file is stored on the disk in a directory
having specific path. The file can also be stored in other storage
media such as tapes, CD, DVD, USB drive etc.
Stream is the way to read and write bytes of the data from different
storage mediums.
Ans. The Main method.
www.w3asp.net
20. Class Vs Structure in c#?
Ans. Following are the key differences between them: -
1. Structures are value types and classes are reference types. So
structures use stack and classes use heap.
2. Structures members can not be declared as protected, but class
members can be. You can not do inheritance in structures.
3. Structures do not require constructors while classes require.
4. Objects created from classes are terminated using Garbage
collector. Structures are not destroyed using GC.
www.w3asp.net
For more please visit-
www.w3asp.net

Top 20 c# interview Question and answers

  • 1.
    www.w3asp.net TOP 20 C#INTERVIEW QUESTION AND ANSWERS
  • 2.
    1.What is conceptof Boxing and Unboxing ? Ans. Boxing is used to convert value types to object. E.g. int x = 1; object obj = x ; Unboxing is used to convert the object back to the value type. E.g. int y = (int)obj; Boxing/unboxing is quiet an expensive operation. 2. Define Constructors in C#? Ans. 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. www.w3asp.net
  • 3.
    www.w3asp.net 3.Explain namespaces inC#? Ans. The built-in libraries are organized within namespaces. Namespaces organize objects in an assembly. An assembly is a reusable, version able and self-describing building block of a CLR application. Assemblies can contain multiple namespaces. Namespaces can contain other namespaces. We can add namespaces in our application used the “using” keyword. Example- using System; using System.Collections.Generic; 4. Is C# fully object oriented language? Ans. Yes, you can say C# is a fully object oriented language. It not only supports the four criteria of OOPs, it also requires that essentially all of your constructs be encapsulated in objects. That is, C# does not let you develop outside of the OOPs methodology in any meaningful way.
  • 4.
    www.w3asp.net 5.What is adelegates in c#? Ans. A delegate is a type safe function pointer. Using delegates you can pass methods as parameters. To pass a method as a parameter, to a delegate, the signature of the method must match the signature of the delegate. This is why, delegates are called type safe function pointers. Delegate declaration determines the methods that can be referenced by the delegate. A delegate can refer to a method, which has the same signature as that of the delegate.For example, consider a delegate: public delegate int MyDelegate (string s); The preceding delegate can be used to reference any method that has a single string parameter and returns an int type variable. Syntax for delegate declaration is: delegate <return type> <delegate-name> <parameter list>
  • 5.
    www.w3asp.net 6. What typeof class cannot be inherited? Ans. A sealed class cannot be inherited. A sealed class is used primarily when the class contains static members. Note that a struct is implicitly sealed; so they cannot be inherited. 7.Is it possible to use multiple inheritance in .NET? Ans. Multiple Inheritance is an ability to inherit from more than one base class i.e. ability of a class to have more than one super class, by inheriting from different sources and thus combine separately-defined behaviors in a single class. There are two types of multiple inheritance: * multiple type/interface inheritance * multiple implementation inheritance C# supports only multiple type/interface inheritance. There is no support for multiple implementation inheritance in .NET. That means a class can only derived from one class.
  • 6.
    www.w3asp.net 8.What is anobject in c-sharp? Ans. An object is an instance of a class through which we access the methods of that class. “New” keyword is used to create an object. A class that creates an object in memory will contain the information about the methods, variables and behavior of that class. 9.What is Event? Ans. Notification by an application or operating system that some event has occurred. An event is fired—raised—when a predefined condition occurs, e.g., focus change in a GUI, interval timer, mouse click. An event handler is called in response to an event. The C# event model is publish and subscribe: Publishers create and publish events: Subscribers register to receive specific events: Then, publishers send out their events only to subscribers.
  • 7.
    www.w3asp.net 10.What is theInterface (C#)? Ans. The interface contains only member declarations. A class implementing an interface must provide the implementation of the interface members. An interface cannot contain constants, constructors, data fields, destructors, or static members. Interface member declarations are implicitly public. Classes or structs may inherit any number of interfaces. A C# interface is declared with the interface keyword. 11. Are C# constructors inherited? Ans. C# constructors cannot be inherited. If constructor inheritance were allowed, then necessary initialization in a base class constructor might easily be omitted. This could cause serious problems which would be difficult to track down. For example, if a new version of a base class appears with a new constructor, your class would get a new constructor automatically.
  • 8.
    www.w3asp.net 12. What isthe Abstract class (C#)? Ans. A class representing a base class which other types can subclass. A normal, non-abstract class is called a concrete class. An abstract class may contain either or both abstract and concrete methods. (Properties cannot be marked abstract.) As in ordinary inheritance, a type derived from an abstract class inherits all the base type members including any method implementations. But, if any abstract methods are inherited, the class must either declare itself abstract or provide an implementation. A C# abstract class is declared with the abstract keyword. 13. How many times can a constructor be called during lifetime of the object? Ans. Only once.
  • 9.
    www.w3asp.net 14. Advantages ofGenerics in c#? Ans. Advantages of generics are: 1-Reusability 2-Improves Performance 15. Explain Destructor in c#? Ans. Destructors’ are used to destruct instances of classes. When we are using destructors in C#, we have to keep in mind the following things: * A class can only have one destructor. * Destructors cannot be inherited or overloaded. * Destructors cannot be called. They are invoked automatically. * A destructor does not take modifiers or have parameters.
  • 10.
    www.w3asp.net 16. What isJIT in asp.net? Ans. JIT is a compiler that converts MSIL to native code. The native code consists of hardware specific instructions that can be executed by the CPU. Rather than converting the entire MSIL file to native code, the JIT converts the MSIL as it is needed during execution. This converted native code is stored so that it is accessible for subsequent calls. 17. What is the use and purpose of using keyword? Ans. The using keyword is used to define block of code with the guarantee that anything present within the body of using will be dispose off (even if the exception has been thrown).The use of this keyword is excellent as it cleans the garbage in the memory automatically by disposing which implements IDisposable interface.
  • 11.
    www.w3asp.net 18. What isdifference between a file and stream. 19. What is the execution entry point for a C# console application? Ans. A file is an ordered collection of the bytes of data with proper name and physical existence. A file is stored on the disk in a directory having specific path. The file can also be stored in other storage media such as tapes, CD, DVD, USB drive etc. Stream is the way to read and write bytes of the data from different storage mediums. Ans. The Main method.
  • 12.
    www.w3asp.net 20. Class VsStructure in c#? Ans. Following are the key differences between them: - 1. Structures are value types and classes are reference types. So structures use stack and classes use heap. 2. Structures members can not be declared as protected, but class members can be. You can not do inheritance in structures. 3. Structures do not require constructors while classes require. 4. Objects created from classes are terminated using Garbage collector. Structures are not destroyed using GC.
  • 13.
    www.w3asp.net For more pleasevisit- www.w3asp.net