Sunteți pe pagina 1din 48

Interview Questions and Answers

Table of Contents
OOPS Questions ............................................................................................................................................ 4
1. What is OOPS? ...................................................................................................................................... 4
2. Write basic concepts of OOPS? ............................................................................................................. 4
3. What is a class? ..................................................................................................................................... 4
4. What is an object? ................................................................................................................................ 4
5. What is Encapsulation? ..................................................................................................................... 4
Public Access Specifier .......................................................................................................................... 5
Private Access Specifier ........................................................................................................................ 6
Protected Access Specifier .................................................................................................................... 8
Internal Access Specifier ....................................................................................................................... 8
Protected Internal Access Specifier ...................................................................................................... 9
6. What is Polymorphism? ........................................................................................................................ 9
Real World Example of Polymorphism ............................................................................................... 10
 Static or Compile Time Polymorphism........................................................................................ 10
 Dynamic or Runtime Polymorphism ........................................................................................... 11
7. What is Inheritance? ........................................................................................................................... 18
8. What is Interface? ............................................................................................................................... 18
9. Define a constructor? ......................................................................................................................... 18
10. Define Destructor? ............................................................................................................................ 19
11. What is an Inline function? ............................................................................................................... 19
12. What is a virtual function? ................................................................................................................ 19
13. What is a friend function?................................................................................................................. 19
14. What is function overloading? .......................................................................................................... 19
15. What is operator overloading? ......................................................................................................... 19
16. What is an abstract class? ................................................................................................................. 20
17. What is a ternary operator?.............................................................................................................. 20
18. What is the use of finalize method? ................................................................................................. 20
19. What are different types of arguments? .......................................................................................... 20
20. What is the super keyword? ............................................................................................................. 20
21. What is method overriding? ............................................................................................................. 20
22. What is Private constructor? ............................................................................................................ 20
23. What is exception handling? ........................................................................................................... 22
24. What are tokens? .............................................................................................................................. 22
25. Difference between overloading and overriding? ............................................................................ 22
26. Difference between class and an object? ......................................................................................... 22
27. What is an abstraction? .................................................................................................................... 22
28. What are access modifiers? .............................................................................................................. 22
29. What are sealed modifiers? .............................................................................................................. 23
30. How can we call the base method without creating an instance? ................................................... 23
31. What is the difference between new and override? ........................................................................ 23
32. What are the various types of constructors? ................................................................................... 23
33. What is early and late binding? ........................................................................................................ 23
34. What is ‘this’ pointer? ....................................................................................................................... 23
35. What is the difference between structure and a class? ................................................................... 23
36. What is the default access modifier in a class? ................................................................................ 23
37. What is a pure virtual function? ....................................................................................................... 24
38. What are all the operators that cannot be overloaded? .................................................................. 24
39. What is dynamic or run time polymorphism? .................................................................................. 24
40. Do we require a parameter for constructors? .................................................................................. 24
41. What is a copy constructor? ............................................................................................................. 24
42. What does the keyword virtual represented in the method definition? ......................................... 24
43. Whether static method can use nonstatic members?...................................................................... 24
44. What is a base class, sub class, and super class? .............................................................................. 24
45. What is static and dynamic binding? ................................................................................................ 25
46. How many instances can be created for an abstract class? ............................................................. 25
47. Which keyword can be used for overloading? ................................................................................. 25
48. What is the default access specifier in a class definition? ................................................................ 25
49. Which OOPS concept is used as reuse mechanism?......................................................................... 25
50. Which OOPS concept exposes only necessary information to the calling functions? ...................... 25
51. What Will Be The Output Of The Following Code Snippet? ............................................................. 25
52. Which Of The Following Keyword, Enables To Modify The Data And Behavior Of A Base Class By
Replacing Its Member With A New Derived Member? .......................................................................... 26
53. Which Of The Following Is The Correct Way To Overload “+” Operator? ........................................ 26
54. What Will Be The Correct Order Of Execution Of Function Func1(), Func2() & Func3() In The Given
Code Snippet? ......................................................................................................................................... 26
55. Which Of The Following Statements Are Correct For C# Language?................................................ 27
56. Which Of The Following Code Needs To Be Added For The Overloaded Operator “-” In C#? ......... 27
57. Which Of The Following Is The Correct Name For Selecting An Appropriate Method From A
Number Of Overloaded Methods, By Matching The Arguments In Terms Of Their Number, Type,
Order, And Binding At Compile Time? .................................................................................................... 28
58. Which Of The Following Statements Are Correct For Run-Time Polymorphism? ............................ 29
59. What Will Be The Output Of The Following Code Snippet? ............................................................. 29
60. Which Of The Following Keywords Is Used To Refer Base Class Constructor To Subclass
Constructor? ........................................................................................................................................... 30
61. What Will Be The Output Of The Following Code Snippet? ............................................................. 30
62. What Will Be The Output Of The Following Code Snippet? ............................................................. 30
63. What Will Be The Output Of The Following Code Snippet? ............................................................. 32
64. What Will Be The Output Of The Following Code Snippet? ............................................................. 32
65. Which Of The Following Options Represents The Type Of Class Which Does Not Have Its Own
Objects But Acts As A Base Class For Its Subclass? ................................................................................. 33
66. What Will Be The Output Of The Following Code Snippet? ............................................................. 33
67. What Will Be The Output Of The Following Code Snippet? ............................................................. 34
68. Which Of The Following Represents A Class That Inherits An Abstract Class But It Does Not Define
All Of Its Functions? ................................................................................................................................ 35
69. What Will Be The Output Of The Following Code Snippet? ............................................................. 35
70. What Will Be The Output Of The Following Code Snippet? ............................................................. 36
71. Difference Between Abstraction and Encapsulation ........................................................................ 37
72. Difference Between Interface and Abstract Class ............................................................................ 38
72. Difference Between Inheritance and Polymorphism........................................................................ 41
73. Static Constructors ............................................................................................................................ 43
74. What is the return type of a constructor in C#? ............................................................................... 44
75. Can we call a normal class method inside a static class? ................................................................. 44
76. Should all the members, functions inside a static class should be static? ....................................... 45
77. Why we cannot create the Instance of the Static Class in .Net? ...................................................... 45
C# Questions ............................................................................................................................................... 47
ASP.NET Questions...................................................................................................................................... 47
ASP.NET MVC Questions ............................................................................................................................. 47
Design Patters ............................................................................................................................................. 47
Jquery .......................................................................................................................................................... 47
Angular2 ...................................................................................................................................................... 47
SQL Server ................................................................................................................................................... 47
Interview Questions Need Answers ............................................................................................................ 48
OOPS Questions

1. What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.

2. Write basic concepts of OOPS?


Following are the concepts of OOPS and are as follows:

1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.

3. What is a class?
A class is simply a representation of a type of object. It is the blueprint/ plan/ template that
describes the details of an object.

4. What is an object?
An object is an instance of a class. It has its own state, behavior, and identity.

5. What is Encapsulation?

Encapsulation is defined 'as the process of enclosing one or more items within a physical or
logical package'. Encapsulation, in object-oriented programming methodology, prevents
access to implementation details.

Encapsulation is implemented by using access specifiers. An access specifier defines the


scope and visibility of a class member. C# supports the following access specifiers −

 Public

 Private

 Protected

 Internal
 Protected internal
Public Access Specifier
Public access specifier allows a class to expose its member variables and member functions
to other functions and objects. Any public member can be accessed from outside the class.

The following example illustrates this −

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

public double length;

public double width;

public double GetArea() {

return length * width;

public void Display() {

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.length = 4.5;

r.width = 3.5;
r.Display();

Console.ReadLine();

When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75

In the preceding example, the member variables length and width are declared public, so
they can be accessed from the function Main() using an instance of the Rectangle class,
named r.

The member function Display() and GetArea() can also access these variables directly
without using any instance of the class.

The member functions Display() is also declared public, so it can also be accessed
from Main() using an instance of the Rectangle class, named r.
Private Access Specifier
Private access specifier allows a class to hide its member variables and member functions
from other functions and objects. Only functions of the same class can access its private
members. Even an instance of a class cannot access its private members.

The following example illustrates this −

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

private double length;

private double width;

public void Acceptdetails() {


Console.WriteLine("Enter Length: ");

length = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter Width: ");

width = Convert.ToDouble(Console.ReadLine());

public double GetArea() {

return length * width;

public void Display() {

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.Acceptdetails();

r.Display();

Console.ReadLine();

When the above code is compiled and executed, it produces the following result −
Enter Length:
4.4
Enter Width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52

In the preceding example, the member variables length and width are declared private, so
they cannot be accessed from the function Main(). The member
functions AcceptDetails() and Display() can access these variables. Since the member
functions AcceptDetails() and Display() are declared public, they can be accessed
from Main() using an instance of the Rectangle class, named r.
Protected Access Specifier
Protected access specifier allows a child class to access the member variables and member
functions of its base class. This way it helps in implementing inheritance. We will discuss
this in more details in the inheritance chapter.
Internal Access Specifier
Internal access specifier allows a class to expose its member variables and member
functions to other functions and objects in the current assembly. In other words, any member
with internal access specifier can be accessed from any class or method defined within the
application in which the member is defined.

The following program illustrates this −

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

internal double length;

internal double width;

double GetArea() {

return length * width;

public void Display() {

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);


Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.length = 4.5;

r.width = 3.5;

r.Display();

Console.ReadLine();

When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75

In the preceding example, notice that the member function GetArea() is not declared with
any access specifier. Then what would be the default access specifier of a class member if
we don't mention any? It is private.
Protected Internal Access Specifier
The protected internal access specifier allows a class to hide its member variables and
member functions from other class objects and functions, except a child class within the
same application. This is also used while implementing inheritance.

6. What is Polymorphism?

Polymorphism means one name many forms. Polymorphism means one object behaving as
multiple forms. One function behaves in different forms. In other words, "Many forms of a
single object is called Polymorphism."
Real World Example of Polymorphism
Example 1
 A Teacher behaves with student.
 A Teacher behaves with his/her seniors.
 Here teacher is an object but the attitude is different in different situations.
Example 2
 Person behaves as a SON in house, at the same time that person behaves like an
EMPLOYEE in the office.
Example 3
Your mobile phone, one name but many forms:

 As phone
 As camera
 As mp3 player
 As radio

With polymorphism, the same method or property can perform different actions depending
on the run-time type of the instance that invokes it.

There are two types of polymorphism:


1. Static or compile time polymorphism
2. Dynamic or runtime polymorphism

 Static or Compile Time Polymorphism

In static polymorphism, the decision is made at compile time.


 Which method is to be called is decided at compile-time only.
 Method overloading is an example of this.
 Compile time polymorphism is method overloading, where the compiler knows which
overloaded method it is going to call.

Method overloading is a concept where a class can have more than one method with
the same name and different parameters.
Compiler checks the type and number of parameters passed on to the method and decides
which method to call at compile time and it will give an error if there are no methods that
match the method signature of the method that is called at compile time.

Example
Hide Shrink Copy Code
namespace MethodOverloadingByManishAgrahari
{
class Program
{
public class TestOverloading
{

public void Add(string a1, string a2)


{
Console.WriteLine("Adding Two String :" + a1 + a2);
}

public void Add(int a1, int a2)


{
Console.WriteLine("Adding Two Integer :" + a1 + a2);
}

static void Main(string[] args)


{
TestOverloading obj = new TestOverloading();

obj.Add("Manish " , "Agrahari");

obj.Add(5, 10);

Console.ReadLine();
}
}
}

 Dynamic or Runtime Polymorphism


Run-time polymorphism is achieved by method overriding.
Method overriding allows us to have methods in the base and derived classes with the
same name and the same parameters.

By runtime polymorphism, we can point to any derived class from the object of the base
class at runtime that shows the ability of runtime binding.

Through the reference variable of a base class, the determination of the method to be called
is based on the object being referred to by reference variable.
Compiler would not be aware whether the method is available for overriding the functionality
or not. So, compiler would not give any error at compile time. At runtime, it will be decided
which method to call and if there is no method at runtime, it will give an error.
See the following example:
Hide Shrink Copy Code
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public virtual void Show()


{
Console.WriteLine("Show From Base Class.");
}
}

public class Derived : Base


{
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBase;
objBase = new Base();
objBase.Show();// Output ----> Show From Base Class.

objBase = new Derived();


objBase.Show();//Output--> Show From Derived Class.

Console.ReadLine();
}
}
}

Compiler demands virtual Show() method and it compiles successfully. The right version
of Show() method cannot be determined until run-time since only at that time Base
objBase is initialized as Derived.

Virtual Keyword
According to MSDN, “The virtual keyword is used to modify a method, property,
indexer or event declaration, and allow it to be overridden in a derived class.”

Virtual Method
Virtual method is a method whose behavior can be overridden in derived class. Virtual
method allows declare a method in base class that can be redefined in each derived class.
When a virtual method is invoked, the run-time type of the object is checked for an
overriding member. The overriding member in the most derived class is called, which might
be the original member, if no derived class has overridden the member.
 By default, methods are non-virtual. You cannot override a non-virtual method.
 You cannot use the virtual modifier with
the static, abstract, private or override modifiers.
 Virtual properties behave like abstract methods, except for the differences in
declaration and invocation syntax.
 It is an error to use the virtual modifier on a static property.
 A virtual inherited property can be overridden in a derived class by including a
property declaration that uses the override modifier.

Virtual method solves the following problem:


In OOP, when a derived class inherits from a base class, an object of the derived class may
be referred to (or cast) as either being the base class type or the derived class type. If there
are base class methods overridden by the derived class, the method call behavior is
ambiguous.
In C#, polymorphism is explicit - you must have a virtual (or abstract) modifier on the base
class method (member) and an override on the derived class method, which you probably
already know.
If you don't put a modifier on a base class method, polymorphism can't ever happen. If you
then add a non-modified method to the derived class with the same signature as the non-
modified base class method, the compiler will generate a Warning message.

See the following example:


Hide Shrink Copy Code
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public void Show()


{
Console.WriteLine("Show From Base Class.");
}
}

public class Derived : Base


{
//Following line will Give an Warning
/*
'PolymorphismByManishAgrahari.Program.Derived.Show()'
hides inherited member
'PolymorphismByManishAgrahari.Program.Base.Show()'.
Use the new keyword if hiding was intended.
*/
public void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBase = new Base();
objBase.Show();// Output ----> Show From Base Class.

Derived objDerived = new Derived();


objDerived.Show();//Output--> Show From Derived Class.

Base objBaseRefToDerived = new Derived();


objBaseRefToDerived.Show();//Output--> Show From Base Class.

Console.ReadLine();
}
}
}

It means that you are hiding (re-defining) the base class method.
In other languages, take Java for instance, you have what is called "implicit" polymorphism
where just putting the method in the derived class with the same signature as a base class
method will enable polymorphism.

all methods of a class are virtual by default unless the developer decides to use
the final keyword thus preventing subclasses from overriding that method. In contrast, C#
adopts the strategy used by C++ where the developer has to use the virtual keyword for
subclasses to override the method. Thus all methods in C# are non virtual by default.
The C# approach is more explicit for the purpose of making the code safer in versioning
scenarios, i.e., you build your code based on a 3rd party library and use meaningful, but
common, method names. The 3rd party library upgrades, using the same common method
name. With implicit polymorphism the code would break, but with C#, you would receive a
compiler warning so you can double check to see if polymorphism was something you
wanted to do.
Difference between Method Overriding and Method Hiding
Method overriding allows a subclass to provide a specific implementation of a method that
is already provided by base class. The implementation in the subclass overrides (replaces)
the implementation in the base class.
The important thing to remember about overriding is that the method that is doing the
overriding is related to the method in the base class.
When a virtual method is called on a reference, the actual type of the object to which the
reference refers is used to determine which method implementation should be used. When
a method of a base class is overridden in a derived class (subclass), the version defined in
the derived class is used. This is so even should the calling application be unaware that the
object is an instance of the derived class.

namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public virtual void Show()


{
Console.WriteLine("Show From Base Class.");
}
}

public class Derived : Base


{
//the keyword "override" change the base class method.
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBaseRefToDerived = new Derived();
objBaseRefToDerived .Show();//Output--> Show From Derived Class.

Console.ReadLine();
}
}
}

Output--> Show From Derived Class

Method hiding does not have a relationship between the methods in the base class and
derived class. The method in the derived class hides the method in the base class.

namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public virtual void Show()


{
Console.WriteLine("Show From Base Class.");
}
}

public class Derived : Base


{

public new void Show()


{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBaseRefToDerived = new Derived();
objBaseRefToDerived .Show();//Output--> Show From Base Class.

Console.ReadLine();
}
}
}

Output is:? Show From Base Class.

In the preceding example, Derived.Show will be called; because, it overrides Base.Show.


The C# language specification states that "You cannot override a non-virtual method." See
the following example:
Hide Shrink Copy Code
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public void Show()


{
Console.WriteLine("This is Base Class.");
}
}

public class Derived : Base


{
//Following Line will give error.
/*
Error:- 'PolymorphismByManishAgrahari.Program.Derived.Show()'
cannot override inherited member 'PolymorphismByManishAgrahari.Program.Base.Show()'
* because it is not marked virtual, abstract, or override
*/
public override void Show()
{

Console.WriteLine("This is Derived Class.");


}
}
static void Main(string[] args)
{
Base objBase = new Base();
objBase.Show();// Output ----> This is Base Class.

Derived objDerived = new Derived();


objDerived.Show();//Output--> This is Derived Class.

Base objBaseRefToDerived = new Derived();


objBaseRefToDerived.Show();//Output--> This is Base Class.

Console.ReadLine();
}
}
}

Error: 'PolymorphismByManishAgrahari.Program.Derived.Show()' cannot override inherited


member 'PolymorphismByManishAgrahari.Program.Base.Show()' because it is not
marked virtual, abstract, or override.

Sealed Keyword
Sealed keyword can be used to stop method overriding in a derived classes.
By default, all methods are sealed, which means you can't override them, so that "sealed"
keyword is redundant in this case and compiler will show you an error when you'll try to
make sealed already sealedmethod. But if your method was marked as virtual in a base
class, by overriding and marking this method with "sealed" will prevent method overriding in
derived classes.
See the following example:
Hide Shrink Copy Code
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public sealed void Show()//This Line will give an error - "cannot


{ //be sealed because it is not an override"

Console.WriteLine("This is Base Class.");


}
}

public class Derived : Base


{
public void Show()
{

Console.WriteLine("This is Derived Class.");


}
}
static void Main(string[] args)
{
Base objBaseReference = new Derived();
objBaseReference.Show();// Output ---------> This is Base Class.

Console.ReadLine();
}
}
}

Error: 'PolymorphismByManishAgrahari.Program.Base.Show()' cannot be sealed because it


is not an override.

To remove error from the above program, use the following:


Hide Shrink Copy Code
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{

public virtual void Show()


{
Console.WriteLine("This is Base Class.");
}
}

public class Derived : Base


{
public override sealed void Show()
{

Console.WriteLine("This is Derived Class.");


}
}

static void Main(string[] args)


{
Base objBaseReference = new Derived();
objBaseReference.Show();// Output ---> This is Derived Class.

Console.ReadLine();
}
}
}

Output ---> This is Derived Class.

7. What is Inheritance?
Inheritance is a concept where one class shares the structure and behavior defined in
another class. If inheritance applied on one class is called Single Inheritance, and if it
depends on multiple classes, then it is called multiple Inheritance.

Note: Extending the existing functionalities is called inheritance. Multiple inheritance is not
working in C#, need to use Interface.

8. What is Interface?
Interfaces define properties, methods, and events, which are the members of the interface.
Interfaces contain only the declaration of the members. It is the responsibility of the deriving
class to define the members. It often helps in providing a standard structure that the deriving
classes would follow.

Abstract classes to some extent serve the same purpose, however, they are mostly used
when only few methods are to be declared by the base class and the deriving class
implements the functionalities.
Declaring Interfaces
Interfaces are declared using the interface keyword. It is similar to class declaration.
Interface statements are public by default. Following is an example of an interface
declaration −

public interface ITransactions {

// interface members

void showTransaction();

double getAmount();

9. Define a constructor?

A constructor is a method used to initialize the state of an object, and it gets invoked at the
time of object creation. Rules forconstructor are:
 Constructor Name should be same as class name.
 A constructor must have no return type.
10. Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or
destroyed. Destructor name is also same as class name but with the tilde symbol before the
name.

11. What is an Inline function?


An inline function is a technique used by the compilers and instructs to insert complete body
of the function wherever that function is used in the program source code.

12. What is a virtual function?


A virtual function is a member function of a class, and its functionality can be overridden in
its derived class. This function can be implemented by using a keyword called virtual, and it
can be given during function declaration.

A virtual function can A token in C++, and it can be achieved in C Language by using
function pointers or pointers to function.

13. What is a friend function?


A friend function is a friend of a class that is allowed to access to Public, private or
protected data in that same class. If the function is defined outside the class cannot access
such information.

Friend can be declared anywhere in the class declaration, and it cannot be affected by
access control keywords like private, public or protected.

14. What is function overloading?


Function overloading an as a normal function, but it can perform different tasks. It allows the
creation of several methods with the same name which differ from each other by the type of
input and output of the function.

Example
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);
15. What is operator overloading?
Operator overloading is a function where different operators are applied and depends on
the arguments. Operator,-,* can be used to pass through the function, and it has their own
precedence to execute
16. What is an abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not
possible with an abstract class, but it can be inherited. An abstract class can contain only
Abstract method. Java allows only abstract method in abstract class while for other
languages allow non-abstract method as well.

17. What is a ternary operator?


The ternary operator is said to be an operator which takes three arguments. Arguments and
results are of different data types, and it depends on the function. The ternary operator is
also called a conditional operator.

18. What is the use of finalize method?


Finalize method helps to perform cleanup operations on the resources which are not
currently used. Finalize method is protected, and it is accessible only through this class or
by a derived class.

19. What are different types of arguments?


A parameter is a variable used during the declaration of the function or subroutine and
arguments are passed to the an, and it should match with the parameter defined. There are
two types of Arguments.

 Call by Value – Value passed will get modified only inside the function, and it returns
the same value whatever it is passed it into the function.
 Call by Reference – Value passed will get modified in both inside and outside the
functions and it returns the same or different value.

20. What is the super keyword?


Super keyword is used to invoke the overridden method which overrides one of its
superclass methods. This keyword allows to access overridden methods and also to access
hidden members of the superclass.

It also forwards a call from a constructor to a constructor in the superclass.

21. What is method overriding?


Method overriding is a feature that allows a subclass to provide the implementation of a
method that overrides in the main class. This will overrides the implementation in the
superclass by providing the same method name, same parameter and same return type.

22. What is Private constructor?


A private constructor means that the class can't be instantiated from outside the class: the
only way to create an instance of the class is by calling a static method within the class
which creates an instance or returns a reference to an existing instance.
Generally, they are used in singleton design patterns, where the code ensures that only one
instance of a class can ever be created.

Basically, you cannot instantiate the class if the constructor is private

Then you need to create a method/property to instantiate the class in that case when you
will insatiate the class private constructor gets fired and breakpoint hits. see the example
below

public class SingletonDemo


{
private static volatile SingletonDemo instance = null;

private SingletonDemo() { }

public static SingletonDemo getInstance()


{
if (instance == null)
instance = new SingletonDemo();

return instance;
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SingletonDemo.getInstance();
}
}
}

Hint: keep breakpoint in the constructor and see it will hit


23. What is exception handling?
An exception is an event that occurs during the execution of a program. Exceptions can be
of any type – Runtime exception, Error exceptions. Those exceptions are adequately
handled through exception handling mechanism like try, catch and throw keywords.

24. What are tokens?


The token is recognized by a compiler, and it cannot be broken down into component
elements. Keywords, identifiers, constants, string literals and operators are examples of
tokens.

Even punctuation characters are also considered as tokens – Brackets, Commas, Braces
and Parentheses.

25. Difference between overloading and overriding?


Overloading is static binding whereas Overriding is dynamic binding. Overloading is nothing
but the same method with different arguments, and it may or may not return the same value
in the same class itself.

Overriding is the same method names with same arguments and return types associated
with the class and its child class.

26. Difference between class and an object?


An object is an instance of a class. Objects hold multiple information, but classes don’t have
any information. Definition of properties and functions can be done in class and can be used
by the object.

A class can have sub-classes, and an object doesn’t have sub-objects.

27. What is an abstraction?


Abstraction is a good feature of OOPS, and it shows only the necessary details to the client
of an object. Means, it shows only required details for an object, not the inner constructors,
of an object. Example – When you want to switch On television, it not necessary to show all
the functions of TV. Whatever is required to switch on TV will be showed by using abstract
class.

28. What are access modifiers?


Access modifiers determine the scope of the method or variables that can be accessed
from other various objects or classes. There are 5 types of access modifiers, and they are
as follows:

 Private.
 Protected.
 Public.
 Friend.
 Protected Friend.
29. What are sealed modifiers?
Sealed modifiers are the access modifiers where it cannot be inherited by the methods.
Sealed modifiers can also be applied to properties, events, and methods. This modifier
cannot be applied to static members.

30. How can we call the base method without creating an instance?
Yes, it is possible to call the base method without creating an instance. And that method
should be “Static method”.

Doing inheritance from that class.-Use Base Keyword from a derived class.

31. What is the difference between new and override?


The new modifier instructs the compiler to use the new implementation instead of the base
class function. Whereas, Override modifier helps to override the base class function.

32. What are the various types of constructors?


There are three various types of constructors, and they are as follows:

– Default Constructor – With no parameters.

– Parametric Constructor – With Parameters. Create a new instance of a class and also
passing arguments simultaneously.

– Copy Constructor – Which creates a new object as a copy of an existing object.

33. What is early and late binding?


Early binding refers to the assignment of values to variables during design time whereas
late binding refers to the assignment of values to variables during run time.

34. What is ‘this’ pointer?


THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which
differentiates between the current object with the global object. Basically, it refers to the
current object.

35. What is the difference between structure and a class?


Structure default access type is public , but class access type is private. A structure is used
for grouping data whereas class can be used for grouping data and methods. Structures are
exclusively used for data, and it doesn’t require strict validation , but classes are used to
encapsulates and inherit data which requires strict validation.

36. What is the default access modifier in a class?


The default access modifier of a class is Private by default.
37. What is a pure virtual function?
A pure virtual function is a function which can be overridden in the derived class but cannot
be defined. A virtual function can be declared as Pure by using the operator =0.

Example -.
1 Virtual void function1() // Virtual, Not pure
2
3 Virtual void function2() = 0 //Pure virtual
38. What are all the operators that cannot be overloaded?
Following are the operators that cannot be overloaded -.

1. Scope Resolution (:: )


2. Member Selection (.)
3. Member selection through a pointer to function (.*)

39. What is dynamic or run time polymorphism?


Dynamic or Run time polymorphism is also known as method overriding in which call to an
overridden function is resolved during run time, not at the compile time. It means having two
or more methods with the same name, same signature but with different implementation.

40. Do we require a parameter for constructors?


No, we do not require a parameter for constructors.

41. What is a copy constructor?


This is a special constructor for creating a new object as a copy of an existing object. There
will always be only one copy constructor that can be either defined by the user or the
system.

42. What does the keyword virtual represented in the method definition?
It means, we can override the method.

43. Whether static method can use nonstatic members?


False.

44. What is a base class, sub class, and super class?


The base class is the most generalized class, and it is said to be a root class.

A Sub class is a class that inherits from one or more base classes.

The superclass is the parent class from which another class inherits.
45. What is static and dynamic binding?
Binding is nothing but the association of a name with the class. Static binding is a binding in
which name can be associated with the class during compilation time, and it is also called
as early Binding.

Dynamic binding is a binding in which name can be associated with the class during
execution time, and it is also called as Late Binding.

46. How many instances can be created for an abstract class?


Zero instances will be created for an abstract class.

47. Which keyword can be used for overloading?


Operator keyword is used for overloading.

48. What is the default access specifier in a class definition?


Private access specifier is used in a class definition.

49. Which OOPS concept is used as reuse mechanism?


Inheritance is the OOPS concept that can be used as reuse mechanism.

50. Which OOPS concept exposes only necessary information to the calling functions?
Encapsulation

51. What Will Be The Output Of The Following Code Snippet?


using System;
public class emp
{
public static int age = 40;
public static int salary = 25000;

}
public class record :emp
{
new public static int salary = 50000;
static void Main(string[] args)
{
Console.WriteLine(emp.age + " " + emp.salary + " " +
salary);
}
}
a) 40, 25000, 50000
b) 40, 25000, 25000
c) 40, 50000, 50000
d) Error

52. Which Of The Following Keyword, Enables To Modify The Data And Behavior Of A
Base Class By Replacing Its Member With A New Derived Member?
a) overloads
b) overrides
c) new
d) base

53. Which Of The Following Is The Correct Way To Overload “+” Operator?
a) public sample operator + ( sample a, sample b)
b) public abstract operator + (sample a, sample b)
c) public static sample operator + (sample a, sample b)
d) All of above

54. What Will Be The Correct Order Of Execution Of Function Func1(), Func2() & Func3()
In The Given Code Snippet?
class baseclass
{
public void func1() {}
public virtual void func2() {}
public virtual void func3() {}
}
class derivedclass :baseclass
{
new public void func1() {}
public override void func2() {}
public new void func3() {}
}
class Program
{
static void Main(string[] args)
{
baseclass b = new derivedclass();
b.func1 ();
b.func2 ();
b.func3 ();
}
}
a) func1() of derived class get executed
func2() of derived class get executed
func3() of base class get executed
b) func1() of base class get executed
func2() of derived class get executed
func3() of derived class get executed
c) func1() of derived class get executed
func2() of base class get executed
func3() of base class get executed
d) func1() of base class get executed
func2() of derived class get executed
func3() of base class get executed

55. Which Of The Following Statements Are Correct For C# Language?


a) Every derived class does not define its own version of the virtual method.
b) By default, the access mode for all methods in C# is virtual.
c) If a derived class, does not define its own version of the virtual method, then the one present in
the base class gets used.
d) All of the above.
56. Which Of The Following Code Needs To Be Added For The Overloaded Operator “-” In
C#?
class example
{
int x, y, z;
public example()
{

}
public example(int a ,int b ,int c)
{
x = a;
y = b;
z = c;
}
//Add correct set of code here
public void display()
{
Console.WriteLine(x + " " + y + " " + z);
}
class program
{
static void Main(String[] args)
{
example obj1 = new example(5 ,6 ,8);
example obj2 = new example();
obj2 = - obj1;
obj2.display();
}
}
}
a)
public static example operator -(example obj1)
{
example t = new example();
t.x = obj1.x;
t.y = obj1.y;
t.z = -obj1.z;
return t;
}
b)
public static example operator -(example obj1)
{
example t = new example();
t.x = obj1.x;
t.y = obj1.y;
t.z = obj1.z;
return t;
}
c)
public static example operator -(example obj1)
{
example t = new example();
t.x = -obj1.x;
t.y = -obj1.y;
t.z = -obj1.z;
return t;
}
d) None of the above

57. Which Of The Following Is The Correct Name For Selecting An Appropriate Method
From A Number Of Overloaded Methods, By Matching The Arguments In Terms Of Their
Number, Type, Order, And Binding At Compile Time?
a) Late binding
b) Static binding.
c) Static Linking.
d) Compile time polymorphism.

58. Which Of The Following Statements Are Correct For Run-Time Polymorphism?
a) The overridden base method should be virtual,abstract or override.
b) An abstract method is implicitly a virtual method.
c) An abstract inherited property cannot be overridden in a derived class.
d) Both override method and virtual method must have same access level modifier.

59. What Will Be The Output Of The Following Code Snippet?


using System;
class sample
{
public sample()
{
Console.WriteLine("constructor 1 called");
}
public sample(int x)
{
int p = 2;
int u;
u = p + x;
Console.WriteLine("constructor 2 called");
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample(4);
sample t = new sample();
Console.ReadLine();
}
}
a) constructor 1 called
constructor 2 called
b) constructor 2 called
constructor 1 called
c) constructor 2 called
constructor 2 called
d) error
60. Which Of The Following Keywords Is Used To Refer Base Class Constructor To
Subclass Constructor?
a) this
b) static
c) base
d) extend

61. What Will Be The Output Of The Following Code Snippet?


using System;
class sample
{
int i;
public sample(int x)
{
i = x;
Console.WriteLine("Tech");
}
}
class derived : sample
{
public derived(int x) :base(x)
{
Console.WriteLine("Beamers");
}
}
class Program
{
static void Main(string[] args)
{
derived k = new derived(12);
Console.ReadLine();
}
}
a) TechBeamers
b) 12 Tech
c) Beamers 12
d) Compile time error

62. What Will Be The Output Of The Following Code Snippet?


using System;
class sample
{
int i;
public sample(int num)
{
num = 12;
int j = 12;
int val = num * j;
Console.WriteLine(val);
}
}
class sample1 : sample
{
public sample1(int a) :base(a)
{
a = 13;
int b = 13;
Console.WriteLine(a + b);
}
}
class sample2 : sample1
{
public sample2(int k) :base(k)
{
k = 24;
int o = 6 ;
Console.WriteLine(k /o);
}
}
class Program
{
public static void Main(string[] args)
{
sample2 t = new sample2(10);
Console.ReadLine();
}
}
a) 4, 26, 144
b) 26, 4, 144
c) 144, 26, 4
d) 0, 0, 0
63. What Will Be The Output Of The Following Code Snippet?
using System;
class sample
{
static sample()
{
int num = 8;
Console.WriteLine(num);
}
public sample(int a)
{
int val = 10;
Console.WriteLine(val);
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample(100);
Console.ReadLine();
}
}
a) 10, 10
b) 0, 10
c) 8, 10
d) 8, 8
64. What Will Be The Output Of The Following Code Snippet?
using System;
class sample
{
int i;
public sample(int num)
{
num = -25;
int val;
val = num > 0 ? num : num * -1;
Console.WriteLine(val);
}
}
class sample1 :sample
{
public sample1(int no) :base(no)
{
no = -1000;
Console.WriteLine((no > 0 ? no : no * -1));
}
}
class Program
{
static void Main(string[] args)
{
sample1 s = new sample1(6);
Console.ReadLine();
}
}
a) 25
1000
b) -25
-1000
c) -25
1000
d) 25
-1000
65. Which Of The Following Options Represents The Type Of Class Which Does Not Have
Its Own Objects But Acts As A Base Class For Its Subclass?
a) Static class
b) Sealed class
c) Abstract class
d) Derived class
66. What Will Be The Output Of The Following Code Snippet?
using System;
namespace TechBeamers
{
abstract class baseclass
{
public int i ;
public int j ;
public abstract void print();
}
class derived: baseclass
{
public int j = 5;
public override void print()
{
this.j = 3;
Console.WriteLine(i + " " + j);
}
}
class Program
{
static void Main(string[] args)
{
derived obj = new derived();
obj.i = 1;
obj.print();
Console.ReadLine();
}
}
}
a) 1, 5
b) 0, 5
c) 1, 0
d) 1, 3

67. What Will Be The Output Of The Following Code Snippet?


using System;
namespace TechBeamers
{
abstract class baseclass
{
public int i;
public abstract void print();
}
class derived: baseclass
{
public int j;
public int val;
public override void print()
{
val = i + j;
Console.WriteLine(+i + "\n" + +j);
Console.WriteLine("sum is:" +val);
}
}
class Program
{
static void Main(string[] args)
{
baseclass obj = new derived();
obj.i = 2;
derived obj1 = new derived();
obj1.j = 10;
obj.print();
Console.ReadLine();
}
}
}
a) 2 10
Sum is: 12
b) 0 10
Sum is: 10
c) 2 0
Sum is: 2
d) 0 0
Sum is: 0

68. Which Of The Following Represents A Class That Inherits An Abstract Class But It Does
Not Define All Of Its Functions?
a) Abstract
b) A simple class
c) Static class
d) derived class

69. What Will Be The Output Of The Following Code Snippet?


using System;
namespace TechBeamers
{
public abstract class sample
{
public int i = 7;
public abstract void print();
}
class sample1: sample
{
public int j;
public override void print()
{
Console.WriteLine(i);
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
sample1 obj = new sample1();
sample obj1 = new sample1();
obj.j = 1;
obj1.i = 8;
obj.print();
Console.ReadLine();
}
}
}
a) 0, 8
b) 1, 8
c) 1, 7
d) 7, 1
70. What Will Be The Output Of The Following Code Snippet?
using System;
namespace TechBeamers
{
class sample
{
public int i;
public void print()
{
Console.WriteLine(i);
}
}
class sample1: sample
{
public int j;
public void print()
{
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
sample1 obj = new sample1();
obj.j = 1;
obj.i = 8;
obj.print();
Console.ReadLine();
}
}
}
a) 8, 1
b) 8
c) 1
d) 1, 8

71. Difference Between Abstraction and Encapsulation

BASIS FOR
ABSTRACTION ENCAPSULATION
COMPARISON

Basic Shows, what elements are Hides the complexity of a system.

necessary to build a system.

Application During 'design level'. During 'Implementation level'.

Focus Focus is on "what" should be done Focus is on "how" it should be

done.

Achieved Achieved through encapsulation. Achieved through making the

members of a class as 'private'.


BASIS FOR
ABSTRACTION ENCAPSULATION
COMPARISON

Example The GUI of a mobile phone, it has After the icon is clicked, the end

some icons to click on, which on user has no idea regarding its

click perform the specific function. implementation details

Key Differences Between Abstraction and Encapsulation

1. Abstraction focuses on elements that are necessary to build a system


whereas, the encapsulation focuses on hiding the complexity of the
system.
2. The abstraction is performed during the design level of a system. On the
other hand, encapsulation is performed the system is being implemented.
3. Abstractions main motive is, what is to be done to build a system.
Encapsulations main motive is, how it should be done to build a system.
4. Abstraction is achieved by encapsulation whereas, the encapsulation is
achieved by making the elements of the system private.

72. Difference Between Interface and Abstract Class

BASIS FOR
INTERFACE ABSTRACT CLASS
COMPARISON

Basic When you only have the knowledge When you partially know about

of the requirements not about its the implementations you use

implementation, you use "Abstract classes".

"Interface".
BASIS FOR
INTERFACE ABSTRACT CLASS
COMPARISON

Methods Interface only contains abstract Abstract class contains abstract

methods. methods as well as concrete

methods.

Access Modifier of Interface methods are always It is not compulsory that method

Methods "Public" and "Abstract", even if we in abstract class will be public

do not declare. Hence, it can be and abstract. It can have

said as 100%, pure abstract class. concrete methods also.

Restricted An interface method can not be There are no restrictions on the

Modifier for declared with the following modifiers of the abstract class

Methods modifiers: variable.

Public: Private and Protected

Abstract: final, static, synchronized,

native, strictfp.

Access Modifier of Acess Modifier allowed for Interface The variables in abstract class

Variables variables are public, static & final need not be public, static, final.

whether we are declaring or not.


BASIS FOR
INTERFACE ABSTRACT CLASS
COMPARISON

Restricted Interface variables can not be There is no restriction on the

modifiers for declared as private, protected, modifiers of abstract class

Variables transient, volatile. variables.

Initialization of The interface variables must be It is not compulsory that

variables initialized at the time of its abstract class variables must be

declaration. initialized at the time of its

declaration.

Instance and Inside interface, you can't declare Abstract class allows an instance

static blocks an instance or static block. or static block inside it.

Constructors You can not declare constructor You can declare constructor

inside interface. inside an abstract class.

Key Differences Between Interface and Abstract Class in C#

1. When you have the knowledge of “what is required” but not of “how it
would be implemented” then interface must be used. On the other hand, if
you know what is require and partially know how it would be implemented
then use an abstract class.
2. An interface has all its methods abstract but, an abstract class has some
abstract methods and some concrete methods.
3. The methods inside an interface are public and abstract hence, it is also
called as a pure abstract class. On the other hand, the methods inside an
abstract are not restricted to be public and abstract only.
4. An interface method can never be private, protected, final, static,
synchronized, native or strictfp. On the other hand, there are no
restrictions to methods of an abstract class.
5. The variables in an interface are public and final whether we declare them
or not whereas, there is no such restriction to the variables of an abstract
class to be public and final only.
6. Variables in an interface can never be private protected transient or
volatile whereas, there is no restriction to variables in an abstract class.
7. The variable of an interface must be initialized during declaration. On the
other hand, the variables in an abstract class can be initialized at any time.
8. Inside an interface, an instance or static block can’t be declared but, you
can declare instance or static block inside an abstract class.
9. You can not define constructor inside an interface whereas, you can define
constructor inside an abstract class.

72. Difference Between Inheritance and Polymorphism


BASIS FOR
INHERITANCE POLYMORPHISM
COMPARISON

Basic Inheritance is creating a Polymorphism is basically a common

new class using the interface for multiple form.

properties of the already

existing class.

Implementation Inheritance is basically Polymorphism is basically implemented

implemented on classes. on function/methods.


BASIS FOR
INHERITANCE POLYMORPHISM
COMPARISON

Use To support the concept of Allows object to decide which form of the

reusability in OOP and function to be invoked when, at compile

reduces the length of code. time(overloading) as well as run

time(overriding).

Forms Inheritance may be a single Polymorphism may be a compile time

inheritance, multiple polymorphism (overloading) or run-time

inheritance, multilevel polymorphism (overriding).

inheritance, hierarchical

inheritance and hybrid

inheritance.

Example The class 'table' can inherit The class 'study_table' can also have

the feature of the class function 'set_color()' and a class

'furniture', as a 'table' is a 'Dining_table' can also have function

'furniture'. 'set_color()' so, which form of the

set_color() function to invoke can be

decided at both, compile time and run

time.
Key Differences Between Inheritance and Polymorphism

1. Inheritance is creating a class that derives its feature from an already


existing class. On the other hand, polymorphism is an interface that can be
defined in multiple forms.
2. Inheritance is implemented on the classes whereas, the polymorphism is
implemented on methods/functions.
3. As inheritance allows a derived class to use the elements and methods
defined in the base class, the derived class does not need to define those
elements or method it again so, we can say it increases code reusability
and hence, reduces the length of the code. On the other hand,
polymorphism makes it possible for an object to decide what form of the
method it wants to invoke at both compile time and run time.
4. The inheritance can be classified as single inheritance, multiple
inheritance, multilevel inheritance, hierarchical inheritance and hybrid
inheritance. On the other hand, polymorphism is classified as overloading
and overriding.

73. Static Constructors

Scenario: For static class if we have more than one constructor, how the compiler knows which one to
execute when we try to call a method in static class using classname.methodname?

 Static class clan have only one static method

 Static constructor must be parameter less.


 Normal class can have more than more method which includes static.
Output:
Static constructor
Normal constructor
Main Method

74. What is the return type of a constructor in C#?

It is the new operator who is responsible of allocating memory, passing a reference of the
newly allocated object to the constructor and then returning a reference to the instance.

This would mean that the constructor per se has no return type (void).

75. Can we call a normal class method inside a static class?

Yes, we can access the normal class method inside the static class.
76. Should all the members, functions inside a static class should be static?

Because all static things are not related to any object, but they are related to a class. So all
objects of same class will access same static methods.

On the other hand, non static methods are attached to a particular object of a class.

You can call static methods without creating any object of a class, but that is not the case with
non static methods.

77. Why we cannot create the Instance of the Static Class in .Net?

 Static classes do not contain any instance member properties or functions. So to


make an instance would be pointless. Static classes are used for containing
Variables, properties and functions that have the same effect all over your program.
 A static class is basically the same as a non-static class, but there is one
difference: a static class cannot be instantiated.
 In other words, you cannot use the new keyword to create a variable of the class
type. Because there is no instance variable, you access the members of a static
class by using the class name itself. For example, if you have a static class that is
named UtilityClass that has a public method named MethodA, you call the
method as shown in the following example:

UtilityClass.MethodA();
C# Questions

ASP.NET Questions

ASP.NET MVC Questions

Design Patters

Jquery

Angular2

SQL Server
Interview Questions Need Answers
Inproc and outproc session modes
Session state mgmnt
Delegate
Threading
How iis renders d request
Operator overloading
Session
Diff between data adapter, data reader, and data set
Parameterized constructor
Diff templates in grid view
Compound index
Get date result format
How to format the getdate result as per our requirements
Delegate
MVC
Entity framework
Can we have more than one Out parameter in SP and function
Can we have more than 1 input parameter in function
Get 2nd highest salary
Global.asax
WCF
Entity in SQL
Singleton design pattern
Reflection
Yield
Parameterized view
xp_cmdshell
Delegate
2 way binding
Multi threading
Why Angular
Delegates
Diff b/w ref and out
What is event

S-ar putea să vă placă și