OOPS of C#.Net Interview Questions(50)
1.What is OOP?
A)Acronym for Oop is
Object oriented Programming.All managed languages in the .Net Framework such as
Visual Basic and C# provides full
support for object oriented programming principles.
2.What are the features
of OOP?
A)Any programming
language which supports OOPs principles should follow the following
features.They are
Ø Abstraction
Ø Encapsulation
Ø Polymorphism
Ø Inheritance
3.What is Class?
A)Class is a collection
of things which posses a common similarities.Class is a user defined data type
is known as reference type.
(OR)
Class is a collection of
entities
Declaration: Acessspecifier Class ClassName
E.G:Public Class A (Here
‘A’ is a name of a class)
4.What are the members
of class?
A)Class contains the
following members.They are:
Ø Data Fields
Ø Functions
Ø Constructors
Ø Destructors
Ø Properties
Ø Indexers
Ø Events
5.What is Object?
A)Instance of class is
called object.It is used to represent a class.
6.Define Data Field?
A)DataField is used to
store the data related to the class.Except data fields no other member of a
class can store the data.
Syntax:Access modifier datatype
datafieldname;
E.G:public int empid;
7.Define method?
A)A method is a
re-usable piece of code which can be called again and again and used to perform
required tasks.Any programming language will have methods.
8.Define Constructor?
A) A constructor is a special kind of method that
creates new instances of a class or structure. Like any other method, a
constructor can include parameters; however, constructors have no return value
(that is, they return void).Constructor is a member method of a class which is
invoked automatically when an object to the class is created.
9.What are the
prerequisites of constructor?
A)It can be listed as
follows:
Ø A Constructor name should be same name as class
name.
Ø A Constructor does not have any return type even
void also.
Ø A Constructor is used to initialize require
values in to the data fields of the class or to pass the required value in to
the data fields.
10.How many types of
constructors?
A)Constructors can be
classified into different types they are:
I.
Instance Constructors
Ø Default Constructor
v User Defined Constructor
v System Defined Default Constructor
Ø Parameterized Constructor
Ø Copy Constructor
Ø Private Constructor
II.
Non-Instance
Constructors
Ø Static Constructor
11.Define User Defined
Default Constructor?
A)This Constructor is
created by the programmer.This constructor doesn’t accept any arguments or
parameters.This constructor is used to initialize required values in to the
data fields.But there is no restriction to write particular code in to the
constructor.
12.Define System Defined
Default Constructor?
A)If there is no
constructor defined within a class then system will create its own constructor
and will assign default values into the data fields.
13.Define Parameterized
Constructor?
A) A constructor with at least one parameter is
called a parametrized constructor. The advantage of a parametrized constructor
is that you can initialize each instance of the class to different values.
14.Define Copy
Constructor?
A) The constructor which creates an object by
copying variables from another object is called a copy constructor. The purpose
of a copy constructor is to initialize a new instance to the values of an
existing instance.
15.What is static
Constructor?
A) When a constructor is created as static, it will
be invoked only once for all of instances of the class and it is invoked during
the creation of the first instance of the class or the first reference to a
static member in the class. A static constructor is used to initialize static
fields of the class and to write the code that needs to be executed only once.
16.what is Private
Constructor?
A) When a constructor is created with a private
specifier, it is not possible for other classes to derive from this
class,neither is it possible to create an instance of this class. They are
usually used in classes that contain static members
only. Some key points of
a private constructor are:
Ø One use of a private constructor is when we have
only static members.
Ø It provides an implementation of a singleton
class pattern
Ø Once we provide a constructor that is either
private or public or any, the compiler will not add the parameter-less public
constructor to the class.
17. In C#, What will happen if you do not explicitly
provide a constructor for a class?
A)If you do not provide
a constructor explicitly for your class, C# will create one by default that
instantiates the object and sets all the member variables to their default
values.
18.Structs are not
reference types. Can structs have constructors?
A)Yes, even though
Structs are not reference types, structs can have constructors.
19.We cannot create
instances of static classes. Can we have constructors for static classes?
A)Yes, static classes
can also have constructors.
20.Can you prevent a
class from being instantiated?
A)Yes, a class can be
prevented from being instantiated by using a private constructor as shown in
the example below.
using System;
namespace TestConsole
{
class Program
{
public static void
Main()
{
//Error cannot create
instance of a class with private constructor
SampleClass SC = new
SampleClass();
}
}
class SampleClass
{
double PI = 3.141;
private SampleClass()
{
}
}
}
21.Can a class or a
struct have multiple constructors?
A)Yes, a class or a
struct can have multiple constructors. Constructors in csharp can be
overloaded.
22.Can a child class
call the constructor of a base class?
A)Yes, a child class can
call the constructor of a base class by using the base keyword as shown in the
example below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string
str)
{
Console.WriteLine(str);
}
}
class ChildClass :
BaseClass
{
public ChildClass(string
str): base(str)
{
}
public static void
Main()
{
ChildClass CC = new
ChildClass("Calling base class constructor from child class");
}
}
}
23.If a child class
instance is created, which class constructor is called first - base class or
child class?
A)When an instance of a
child class is created, the base class constructor is called before the child
class constructor. An example is shown below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass()
{
Console.WriteLine("I
am a base class constructor");
}
}
class ChildClass :
BaseClass
{
public ChildClass()
{
Console.WriteLine("I
am a child class constructor");
}
public static void
Main()
{
ChildClass CC = new
ChildClass();
}
}
}
24.Will the following
code compile?
A)using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string
str)
{
Console.WriteLine(str);
}
}
class ChildClass :
BaseClass
{
public ChildClass()
{
Console.WriteLine("I
am a child class constructor");
}
public static void
Main()
{
ChildClass CC = new ChildClass();
}
}
}
No, the above code will
not compile. This is because, if a base class does not offer a default
constructor, the derived class must make an explicit call to a base class
constructor by using the base keyword as shown in the example below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string
str)
{
Console.WriteLine(str);
}
}
class ChildClass :
BaseClass
{
//Call the base class
contructor from child class
public ChildClass() :
base("A call to base class constructor")
{
Console.WriteLine("I
am a child class constructor");
}
public static void
Main()
{
ChildClass CC = new
ChildClass();
}
}
}
25.Can a class have
static constructor?
A)Yes, a class can have
static constructor. Static constructors are called automatically, immediately
before any static fields are accessed, and are generally used to initialize
static class members. It is called automatically before the first instance is
created or any static members are referenced. Static constructors are called
before instance constructors. An example is shown below.
using System;
namespace TestConsole
{
class Program
{
static int I;
static Program()
{
I = 100;
Console.WriteLine("Static
Constructor called");
}
public Program()
{
Console.WriteLine("Instance
Constructor called");
}
public static void
Main()
{
Program P = new
Program();
}
}
}
26.Can you mark static
constructor with access modifiers?
A)No, we cannot use
access modifiers on static constructor.
27.Can you have
parameters for static constructors?
A)No, static
constructors cannot have parameters.
28.What happens if a
static constructor throws an exception?
A)If a static
constructor throws an exception, the runtime will not invoke it a second time,
and the type will remain uninitialized for the lifetime of the application
domain in which your program is running.
29.Give 2 scenarios
where static constructors can be used?
A)1. A typical use of
static constructors is when the class is using a log file and the constructor
is used to write entries to this file.
2. Static constructors
are also useful when creating wrapper classes for unmanaged code, when the
constructor can call the LoadLibrary method.
30.What is the use of
New keyword?
A) New keyword may be used as a modifier and as an
operator. When used as an operator, it creates an object on a heap to invoke
constructors. When used an a modifier, it hides an inherited member from the
base class member.
As an operator, it can be used to create
an object and then to invoke the constructor of the class. See example below.
Example
SomeClass objSomeClass
= new SomeClass(); //Creating a class
object and invoking its constructor
float amount = new
float(); //Creating an object of the type, and invoking its constructor
31.What is a Destructor?
A)A Destructor has the
same name as the class with a tilde character and is used to destroy an
instance of a class.
32.Can a class have more
than 1 destructor?
A)No, a class can have
only 1 destructor.
33.Can structs in C#
have destructors?
A)No, structs can have
constructors but not destructors, only classes can have destructors.
34.Can you pass
parameters to destructors?
A)No, you cannot pass
parameters to destructors. Hence, you cannot overload destructors.
35.Can you explicitly
call a destructor?
A)No, you cannot
explicitly call a destructor. Destructors are invoked automatically by the
garbage collector.
36.Why is it not a good
idea to use Empty destructors?
A)When a class contains
a destructor, an entry is created in the Finalize queue. When the destructor is
called, the garbage collector is invoked to process the queue. If the
destructor is empty, this just causes a needless loss of performance.
37.Is it possible to
force garbage collector to run?
A)Yes, it possible to
force garbage collector to run by calling the Collect() method, but this is not
considered a good practice because this might create a performance over head.
Usually the programmer has no control over when the garbage collector runs. The
garbage collector checks for objects that are no longer being used by the
application. If it considers an object eligible for destruction, it calls the
destructor(if there is one) and reclaims the memory used to store the object.
38.Usually in .NET, the
CLR takes care of memory management. Is there any need for a programmer to
explicitly release memory and resources? If yes, why and how?
A)If the application is
using expensive external resource, it is recommend to explicitly release the
resource before the garbage collector runs and frees the object. We can do this
by implementing the Dispose method from the IDisposable interface that performs
the necessary cleanup for the object. This can considerably improve the
performance of the application.
39.When do we generally
use destructors to release resources?
A)If the application
uses unmanaged resources such as windows, files, and network connections, we
use destructors to release resources.
40.What is struct?
A) A struct type is a value type that is typically
used to encapsulate small groups of related variables, such as the coordinates
of a rectangle or the characteristics of an item in an inventory.
41.How can we declare
struct?
A) The following example shows a simple struct
declaration:
public struct Book
{
public decimal price;
public string title;
public string author;
}
42.What is Enum?
A) The enum keyword is used to declare an
enumeration, a distinct type that consists of a set of named constants called
the enumerator list.
Usually it is best to define
an enum directly within a namespace so that all classes in the namespace can
access it with equal convenience. However, an enum can also be nested within a
class or struct.
43.How can we declare
enum?
A) enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
44.Can we override enum?
A)Yes. Enumerators can use initializers to override the
default values, as shown in the following example.
enum Days {Sat=1, Sun,
Mon, Tue, Wed, Thu, Fri};
45.Can a struct have a
default constructor (a constructor without parameters) or a destructor in C#?
A)No
46.Can you instantiate a
struct without using a new operator in C#?
A)Yes, you can
instantiate a struct without using a new operator
47.Can a struct inherit
from another struct or class in C#?
A)No, a struct cannot
inherit from another struct or class, and it cannot be the base of a class.
48.Can a struct inherit
from an interface in C#?
A)Yes
49.Are structs value
types or reference types?
A)Structs are value
types.
50.What is the base type
from which all structs inherit directly?
A)All structs inherit
directly from System.ValueType, which inherits from System.Object.
0 comments:
Post a Comment