Interview Questions and Answers on Dot Net Framework CSharp Dot Net OOPS
- Public – Access to same
assembly or another assembly that references it.
- Private – Access to the same class
or struct.
- Protected – Access to same
class or struct, or in a class that is derived.
- Internal – Access to any code
in the same assembly, but not from another assembly.
- Protected Internal – Access to
any code in the assembly in which it is declared, or from within a derived
class in another assembly.
Question 72 – What are the differences between Class and Struts?
|
Question 73 – What are the Similarities
between Class and Struts?
Both
are user defined types.
Both
of them can have constructor without parameter and with parameter. Both can
have delegates and events.Both can have methods, properties , fields, constants
, enumerations, events.
Question
74 – What is the term Virtual means?
When
we need to override a method of the base class in the sub class, then we give
the virtual keyword in the base class method. This makes the method in the
base class to be overridable. Methods, properties, and indexers can be
virtual, which means that their implementation can be overridden in
derived classes. You cannot use the virtual modifier with the following
modifiers:
- Static,
- Abstract
- Override
Question 75 – What is a Sealed Class?
- Sealed classes are used to
restrict the inheritance feature of object oriented programming.
- Once class is defined as sealed
class, this class cannot be inherited so we can’t derive class.
- Keywords: C# – Sealed , VB.NET
– NotInheritable
- If you have ever noticed,
struct are sealed. You cannot derive a class from a struct.
- A sealed class cannot be used
as a base class. For this reason, it cannot also be an abstract class.
- The best usage of sealed
classes is when you have a class with static members.
- Example – The Pens and Brushes
classes of the System.Drawing namespace. The Pens class represent the pens
for standard colors.
- This class has only static
members. Pens.Blue represents a pen with blue color. Similarly, the
Brushes class represents standard brushes.
- The Brushes.Blue represents a
brush with blue color.
Question 76 – What is Polymorphism?
- Polymorphism is one of the
primary characteristics (concept) of object-oriented programming.
- Poly means many and morph means
form. Thus, polymorphism refers to being able to use many forms of a type
without regard to the details.
- Polymorphism is the
characteristic of being able to assign a different meaning specifically,
to allow an entity such as a variable, a function, or an object to have
more than one form.
- Polymorphism is the ability to
process objects differently depending on their data types.
- Polymorphism is the ability to
redefine methods for derived classes.
Question 77 – What are the Types of Polymorphism?
- Compile time Polymorphism
(method overloading)
- Run time Polymorphism (method
overriding)
Question 78 – What is Method Overloading? Or What is Early
Binding?
Method
overloading means having two or more methods with the same name but with
different signatures
Question 79 – What is Method Overriding or What is Late Binding?
Method
overriding means having two or more methods with the same name , same signature
but with different implementation. (Base class and Child class implementation
of a method with same name and signature)
Question 80 – What is an Inheritance?
It
is the ability to use all of the functionality of an existing class, and extend
those capabilities without re-writing the original class. It is the process by
which one object acquires the properties of another object. A new class that is
created by inheritance is sometimes called a child class or a subclass. The
class you originally inherited from is called the base class, parent class, or
the superclass.
Question 81 – What are the Types of Inheritance?
- Implementation inheritance refers to the ability to use a base class’s
properties and methods with no additional coding.
- Interface inheritance refers to the ability to use just the names of
the properties and methods, but the child class must provide the
implementation.
- Visual inheritance refers to the ability for a child form (class) to
use the base forms (class) visual representation as well as the
implemented code.
Question 82 – What is Multiple Inheritance?
C#
does not support multiple implementation inheritance. A class cannot be derived
from more than one class, However, a class can be derived from multiple
interfaces.
Question 83 – What are the examples of Multiple Inheritance?
Imagine
a class named TransmitData, whose function is to transmit data, and another
class named ReceiveData, whose function is to receive data. Now imagine that
you want to create a class named SocketPort, whose function is to transmit and
receive data. In order to accomplish this, you would want to derive SocketPort
from both TransmitData and ReceiveData.
Question 84 – What are the Advantages of Inheritance?
- Once a behavior (method) or
property is defined in a super class(base class),that behavior or property
is automatically inherited by all subclasses (derived class).
- Code reusability increased
through inheritance.
- Inheritance provide a clear
model structure which is easy to understand without much complexity Using
inheritance, classes become grouped together in a hierarchical tree
structure Code are easy to manage and divided into parent and child
classes.
Question 85 – What is an Encapsulation?
- Encapsulation is a process of
hiding all the internal details of an object from the outside world.
- Encapsulation is the ability to
hide its data and methods from outside the world and only expose data and
methods that are required
- Encapsulation gives us
maintainability, flexibility and extensibility to our code.
- Encapsulation makes
implementation inaccessible to other parts of the program and protect from
whatever actions might be taken outside the function or class.
- Encapsulation provides a way to
protect data from accidental corruption
- Encapsulation hides information
within an object
- Encapsulation is technique or
process of making fields in a class private and providing access to the
fields using public methods
- Encapsulation allows us to
create a “black box” and protects an objects internal state from
corruption by its clients.
- The idea of encapsulation comes from the need to cleanly distinguish between the specification and the implementation of an operation and the need for modularity.
Question 86 – What are the examples of Encapsulation?
- Let’s say you have an object
named Bike and this object has a method named start(). When you create an
instance of a Bike object and call its start() method you are not worried
about what happens to accomplish this, you just want to make sure the
state of the bike is changed to ‘running’ afterwards. This kind of
behavior hiding is encapsulation and it makes programming much easier.
- Video Recorder, which has a
record, play, pause buttons is another example of encapsulation, so VCR is
encapsulated into a single object where the internals can change but stays
the same for users interface point of view.
- Medical Capsules i.e. one drug
is stored in bottom layer and another drug is stored in Upper layer these
two layers are combined in single capsule.
Question 87 – What is an Abstraction?
- Abstraction means to show only
the necessary details to the client of the object.
- Abstraction is about paying
attention to the details that are relevant and ignoring the rest.
- It refers to act of
representing essential features without including background details /
explanations.
Question 88 – What are the examples of Abstraction?
- Do you know the inner details
of the Monitor of your PC? What happen when you switch ON Monitor? No
Right, Important thing for you is weather Monitor is ON or NOT.
- When you change the gear of
your vehicle are you really concern about the inner details of your
vehicle engine? No but what matter to you is that Gear must get changed
that’s it!!
- Let’s say you have a method
“CalculateSalary” in your Employee class, which takes EmployeeId as
parameter and returns the salary of the employee for the current month as
an integer value. Now if someone wants to use that method. He does not
need to care about how Employee object calculates the salary? An only
thing he needs to be concern is name of the method, its input parameters
and format of resulting member. This is abstraction; show only the details
which matter to the user.
- TV Remote Button in that number
format and power buttons and other buttons there just we are seeing the
buttons, we don’t see the button circuits .i.e buttons circuits and
wirings all are hidden.
Question 89 – Difference between Encapsulation and Abstraction ?
|
Question 90 – What is an Abstract Class?
- It is a class that cannot be
instantiated, it exists extensively for inheritance and it must be
inherited.
- Abstract classes cannot be used
to instantiate objects; because abstract classes are incomplete
- Abstract classes may contain
only definition of the properties or methods.
- Derived classes that inherit
the abstract class needs to implements it’s properties or methods.
- An abstract class is
essentially a blueprint for a class without any implementation.
- An abstract class is a class that must be inherited and have the methods overridden.
- An abstract class cannot be a sealed class.
- An abstract method cannot be private.
- An abstract member cannot be static.
- An abstract method cannot have
the modifier virtual. Because an abstract method is implicitly virtual.
- The access modifier of the
abstract method should be same in both the abstract class and its derived
class. If you declare an abstract method as protected, it should be
protected in its derived class. Otherwise, the compiler will raise an
error.
Question 91 – What is an Interface?
- An interface looks like a
class, but has no implementation.
- An interface is a named set of
method signatures.
- An Interface is a reference
type and it contains only abstract members.
- An interface is an array of
related function that must be implemented in derived type.
- Members of an interface are implicitly
public & abstract.
- It can contain definitions of
events, indexers, methods parameter less and parameterful properties.
- The interface can’t contain
constants, data fields, constructors, destructors and static members.
- All the member declarations inside
interface are implicitly public.
- Interfaces are great for
putting together plug-n-play like architectures where components can be
interchanged at will. Since all interchangeable components implement the
same interface, they can be used without any extra programming.
Question 92 – What is a difference between Abstract Class and
Interface?
|
Question 93 – What is a Constructor?
- Constructor is used to
initialize an object (instance) of a class.
- Constructor is a like a method
without any return type.
- Constructor has same name as
class name.
- Constructor follows the access
scope (Can be private, protected, public, Internal and external).
- Constructor can be overloaded,
means we can have constructors with different set of parameters.
- We can always make the call to
one constructor from within the other constructor.
- Only this and base keywords
allowed in initializing constructors, other method calls will raise error.
Question 94 – What is a Constructor chaining?
Overloading
the constructor using the this and base keywords so that it overload is called
constructor chaining
Question 95 – What are the Types of constructors?
- Static Constructor
- Default Constructor
- Private Constructor
- Copy Constructor
- Parameterized Constructor
Question 96 – What is a Private Constructor?
- Used to prevent the user to
instantiate the class directly.
- Used to prevent the creation of
instances of a class when there are no instance fields or methods
- A private constructor is a
special instance constructor.
- It is commonly used in classes
that contain static members only.
- If a class has one or more
private constructors and no public constructors, then other classes
(except nested classes) are not allowed to create instances of this class.
- Note that if you don’t use an
access modifier with the constructor it will still be private by default.
- Private constructors are used
to restrict the instantiation of object using ‘new’ operator.
- This type of constructors is
mainly used for creating singleton object.
- Can use nested class (Inner
Class) or static method to initialize a class having private
constructor.
- Example of Private Constructor
– Math class
Question 97 – What is a Static Constructors?
- Special constructor and gets
called before the first object is created of the class.
- The time of execution cannot be
determined, but it is definitely before the first object creation – could
be at the time of loading the assembly.
- Static constructors might be
convenient, but they are slow. The runtime is not smart enough to optimize
them in the same way it can optimize inline assignments.
- The static constructor for a
class executes before any of the static members for the class are
referenced.
- The static constructor for a
class executes after the static field initializers (if any) for the class.
- A static constructor cannot be
called directly.
- The user has no control on when
the static constructor is executed in the program.
- Example – When the class is
using a log file and the constructor is used to write entries to this
file.
Question 98 – What are the features of Static Constructor?
- Only one Static constructor – Overloading needs the two methods to be
different in terms to methods definition, so you can have at the most one
static constructor
- Without parameters – It is going to be called by CLR, who can pass the
parameters to it, if required, No one, so we cannot have parameterized
static constructor.
- Access only static members – If allowed to work on non-static members, will
reflect the changes in all the object instances, which is impractical.
Non-static members in the class are specific to the object instance
- No access modifier – The call to the static method is made by the
CLR and not by the object, so we do not need to have the access modifier
to it.
Question 99 – What is a Default Constructor?
- A default constructor is a
constructor in both that has no parameters or where it has parameters they
are all defaulted.
- If no constructor is supplied
then the compiler will supply a default constructor.
- This default constructor is a
parameter less constructor with no body, which calls the parameter less
constructor of the base class.
Question 100 – What is a COPY Constructor?
- C# does not provide a copy
constructor.
- A copy constructor is a special
constructor used to create a new object as a copy of an existing object.
- This constructor takes a single
argument: a reference to the object to be copied.
- It is a great convenience to
create copy constructor for C# classes using Reflection.
- If you create a new object and
want to copy the values from an existing object, you have to write the
appropriate method yourself.
Question 101 – What is a Parameterized constructor?
Constructor
that accepts arguments is known as parameterized constructor. There may be
situations, where it is necessary to initialize various data members of
different objects with different values when they are created. Parameterized
constructors help in doing that task.
Question 102 – What is a Singleton Class?
- A singleton class is such kind
of class in which only one object is created throughout the life time of
the class.
- A Singleton class is used when
you wish to restrict instantiation of a class to only one object.
Question 103 – What is a Partial Class?
- It is possible to split the
definition of a class or a struct, or an interface over two or more source
files
- Each source file contains a
section of class definition, and all parts are combined at compile time.
- All the partial definitions must proceed with the key word “Partial”.
- All the partial types must be
defined within a same assembly and module.
- Method signatures (return type,
name of the method, and parameters) must be unique
- The partial types must have the
same accessibility.
- If any part is sealed, then the
entire class is sealed.
- If any part is abstract, the
entire class is abstract.
- Inheritance at any partial type
applies to the entire class.
Question 104 – What is a Partial Method?
- A partial method is like a
usual method in a class except that the user may or may not implement it.
- A partial method gets executed only when it has an implementation.
- Definition of a partial method is in one part of the partial class and implementation in another, but it
is legal to have both in the same part of the partial class. Also, you can
use a partial method in a partial structure but not in partial interface.
- Partial methods are indicated
by the partial modifier.
- Partial methods must be
private.
- Partial methods must return
void.
- Partial methods must only be
declared within partial classes.
- Partial methods do not always
have an implementation.
- Partial methods can be static
and generic.
- Partial methods can have
arguments including ref but not out.
- You cannot make a delegate to a
partial method.
No comments:
Post a Comment