Some Solutions of Hacker Rank Csharp Interview Questions
Some Solutions of Hacker Rank Csharp Interview Questions
125 Basic C# Interview Questions and Basic C# interview question and answer.
► January (16)
125 Basic C# Interview Questions and Answers
Below is the list of 125 basic C# interview questions with their answers. These C# interview
questions and answers are very simple and straight-forward which cover the basic concepts of
C# mostly related to object oriented concepts. So if you are preparing for C# interview, I will
suggest you to must go through these 125 C# basic interview questions and answers to revise
your C# concepts. Here goes the list of 125 basic C# interview questions and answers.
1. What is C#?
Simple
Type safe
Flexible
Object oriented
Compatible
Consistent
Interoperable
Modern
Hierarchical inheritance: Contains one base class and multiple derived classes of the same
base class.
hackerrankproblemsolutions.blogspot.com 1/16
6/19/2019 Some Solutions of Hacker Rank
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message passing.
8. What is inheritance?
Inheritance is deriving the new class from the already existing one.
9. Define scope?
public: The keyword public is an access modifier that tells the C# compiler that the Main
method is accessible by anyone.
static: The keyword static declares that the Main method is a global one and can be called
without creating an instance of the class. The compiler stores the address of the method as the
entry point and uses this information to begin execution before any objects are created.
void: The keyword void is a type modifier that states that the Main method does not return any
value.
Abstract
Sealed
Virtual
Const
Event
Extern
Override
Readonly
Static
New
public
protect
private
internal
internal protect
Implicit conversion of value type to reference type of a variable is known as BOXING, for
example integer to object type conversion.
An object is an instance of a class. An object is created by using operator new. A class that
creates an object in memory will contain the information about the values and behaviors (or
methods) of that specific object.
hackerrankproblemsolutions.blogspot.com 2/16
6/19/2019 Some Solutions of Hacker Rank
15. Where are the types of arrays in C#?
Single-Dimensional
Multidimensional
Jagged arrays.
An instance of a user-defined type is called an object. We can instantiate many objects from one
class.
A destructor is called for a class object when that object passes out of scope or is explicitly
deleted.A destructors as the name implies is used to destroy the objects that have been created
by a constructors.Like a constructor , the destructor is a member function whose name is the
same as the class name but is precised by a tilde.
An enumerated data type is another user defined type which provides a way for attaching
names to numbers thereby increasing comprehensibility of the code. The enum keyword
automatically enumerates a list of words by assigning them values 0,1,2, and so on.
A constructor is a member function with the same name as its class. The constructor is invoked
whenever an object of its associated class is created.It is called constructor because it
constructs the values of data members of the class.
The wrapping up of data and functions into a single unit (called class) is known as
encapsulation. Encapsulation containing and hiding information about an object, such as
internal data structures and code.
Private: The private keyword is the default access level and most restrictive among all other
access levels. It gives least permission to a type or type member. A private member is
accessible only within the body of the class in which it is declared.
Public: The public keyword is most liberal among all access levels, with no restrictions to
access what so ever. A public member is accessible not only from within, but also from outside,
and gives free access to any member declared within the body or outside the body.
Polymorphism means one name, multiple forms. It allows us to have more than one function
with the same name in a program. It allows us to have overloading of operators so that an
operation can exhibit different behaviors in different instances.
hackerrankproblemsolutions.blogspot.com 3/16
6/19/2019 Some Solutions of Hacker Rank
A jagged array is sometimes called an array–of–arrays.
An abstract class is a class that is designed to be specifically used as a base class. An abstract
class contains at least one pure virtual function.
When overriding a method, you change the behavior of the method for the derived class.
Overloading a method simply involves having another method with the same name within the
class.
An argument passed to a ref parameter must first be initialized. Compare this to an out
parameter, whose argument does not have to be explicitly initialized before being passed to an
out parameter.
The using statement is used to obtain a resource, execute a statement, and then dispose of that
resource.
31.What is serialization?
Class is logical representation of object. It is collection of data and related sub procedures with
defination.
Interface is also a class containg methods which is not having any definations.Class does not
support multiple inheritance. But interface can support.
Delegates are a type-safe, object-oriented implementation of function pointers and are used in
many situations where a component needs to call back to the component that is using it.
A class declaration may specify a base class by following the class name with a colon and the
name of the base class. omitting a base class specification is the same as deriving from type
object.
hackerrankproblemsolutions.blogspot.com 4/16
6/19/2019 Some Solutions of Hacker Rank
No ‘This’ cannot be used in a static method. As only static variables/methods can be used in a
static method.
Read-only: The value will be initialized only once from the constructor of the class.
Block statements
Declaration statements
Expression statements
Selection statements
Iteration statements
Jump statements
Try catch statements
Checked and unchecked
Lock statement
It is an abstract class with public abstract methods all of which must be implemented in the
inherited classes.
42. What is the difference between string keyword and System.String class?
String keyword is an alias for Syste.String class. Therefore, System.String and string keyword
are the same, and you can use whichever naming convention you prefer. The String class
provides many methods for safely creating, manipulating, and comparing strings.
Value type
Reference type
45. What is the difference between Custom Control and User Control?
Custom Controls are compiled code (Dlls), easier to use, difficult to create, and can be placed in
toolbox. Drag and Drop controls.
Attributes can be set visually at design time. Can be used by Multiple Applications (If Shared
Dlls), Even if Private can copy to bin directory of web application add reference and use.
Normally designed to provide common functionality independent of consuming Application.
User Controls are similar to those of ASP include files, easy to create, can not be placed in the
toolbox and dragged - dropped from it. A User Control is shared among the single application
hackerrankproblemsolutions.blogspot.com 5/16
6/19/2019 Some Solutions of Hacker Rank
files.
Literals are value constants assigned to variables in a program. C# supports several types of
literals are
Integer literals
Real literals
Boolean literals
Single character literals
String literals
Backslash character literals
50. What is the difference between value type and reference type?
Value types are stored on the stack and when a value of a variable is assigned to another
variable.
Reference types are stored on the heap, and when an assignment between two reference
variables occurs.
C# is a simple and powerful programming language for writing enterprise edition applications.
This is a hybrid of C++ and VB. It retains many C++ features in the area
statements,expressions, and operators and incorporated the productivity of VB.
C# helps the developers to easily build the web services that can be used across the Internet
through any language, on any platform.
C# helps the developers accomplishing with fewer lines of code that will lead to the fewer errors
in the code.
Syntax error
Logic error
Runtime error
The break statement is used to terminate the current enclosing loop or conditional statements in
which it appears. We have already used the break statement to come out of switch statements.
The continue statement is used to alter the sequence of execution. Instead of coming out of the
loop like the break statement did, the continue statement stops the current iteration and simply
returns control back to the top of the loop.
The namespace are known as containers which will be used to organize the hierarchical set of
.Net classes.
hackerrankproblemsolutions.blogspot.com 6/16
6/19/2019 Some Solutions of Hacker Rank
The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a
sealed class is specified as the base class of another class.
A method declared with a static modifier is a static method. A static method does not operate on
a specific instance and can only access static members.
static variables
instance variable
value parameters
reference parameters
array elements
output parameters
local variables
Method overloading permits multiple methods in the same class to have the same name as long
as they have unique signatures. When compiling an invocation of an overloaded method, the
compiler uses overload resolution to determine the specific method to invoke.
Parameters are used to pass values or variable references to methods. The parameters of a
method get their actual values from the arguments that are specified when the method is
invoked. There are four kinds of parameters: value parameters, reference parameters, output
parameters, and parameter arrays.
An array is a collection of the same type. The size of the array is fixed in its declaration. A linked
list is similar to an array but it doesn’t have a limited size.
is (relational operator)
as (relational operator)
typeof (type operator)
sizeof (size operator)
new (object creator)
.dot (member access operator)
checked (overflow checking)
unchecked (prevention of overflow checking)
An operator is a member that defines the meaning of applying a particular expression operator
to instances of a class. Three kinds of operators can be defined: unary operators, binary
operators, and conversion operators. All operators must be declared as public and static.
hackerrankproblemsolutions.blogspot.com 7/16
6/19/2019 Some Solutions of Hacker Rank
An abstract class cannot be instantiated, and it is an error to use the new operator on an
abstract class.
An abstract class is permitted (but not required) to contain abstract methods and accessors.
The modifier abstract is a keyword used with a class, to indicate that this class cannot itself
have direct instances or objects, and it is intended to be only a 'base' class to other classes.
The goto statement is also included in the C# language. This goto can be used to jump from
inside a loop to outside. But jumping from outside to inside a loop is not allowed.
A console application, which is designed to run at the command line with no user interface.
A Windows application, which is designed to run on a user’s desktop and has a user interface.
The return statement is associated with procedures (methods or functions). On executing the
return statement, the system passes the control from the called procedure to the calling
procedure. This return statement is used for two purposes :
Array is a simple sequence of numbers which are not concerned about each others positions.
they are independent of each others positions. adding,removing or modifying any array element
is very easy. Compared to arrays ,linked list is a comlicated sequence of numbers.
No, unlike Java, C# does not require the developer to specify the exceptions that a method can
throw.
Yes, uisng the params keyword. The arguments are specified as a list of arguments of a specific
type.
Each delegate object holds reference to a single method. However, it is possible for a delegate
object to hold references of and invoke multiple methods. Such delegate objects are called
multicast delegates or combinable delegates.
Xml documentation.
Yes, exceptions are the recommended error handling mechanism in .NET Framework.
hackerrankproblemsolutions.blogspot.com 8/16
6/19/2019 Some Solutions of Hacker Rank
The break statement terminates the loop in which it exists. It also changes the flow of the
execution of a program.
In switch statements, the break statement is used at the end of a case statement. The break
statement is mandatory in C# and it avoids the fall through of one case statement to another.
The cursor position is maintained when the page gets refreshed due to the server side validation
and the page gets refreshed.
Both are meant for constant values. A const field can only be initialized at the declaration of the
field. A readonly field can be initialized either at the declaration or.
No, unlike Java, C# does not require (or even allow) the developer to specify the exceptions that
a method can throw.
Different parameter data types, different number of parameters, different order of parameters.
An event is a delegate type dass member that is used by an object or a class to provide a
notification to other objects that an event has occurred.
Identifiers are nothing but names given to various entities uniquely identified in a program.
Boolean literals: True and False are literals of the Boolean type that map to the true and false
state, respectively.
Integer literals: Used to write values of types Int, ulnt, long, and ulong.
Real literals: Used to write values of types float, double, and dedmal.
Character literals: Represents a single character and usually consists of a character in quotes,
such as 'a'.
String literals: C# supports two types of string literals, regular string literal and verbatim string
literals. A regular string literal consists of zero or more characters enclosed in double quotes,
such as "116110". A verbatim string literal consists of an @ character followed by a double–
quote character, such as ©"hello".
Data encapsulation, also referred to as data hiding, is the mechanism whereby the
implementation details of a class are kept hidden from the user. The user can only perform a
hackerrankproblemsolutions.blogspot.com 9/16
6/19/2019 Some Solutions of Hacker Rank
restricted set of operations on the hidden members of the class by executing special functions
called methods.
A nested class is any class whose declaration occurs within the body of another class or
interface.
Yes, a class or a struct can have multiple constructors. Constructors in C# can be overloaded.
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.
Overriding redefines only the implementation while shadowing redefines the whole element.
hackerrankproblemsolutions.blogspot.com 10/16
6/19/2019 Some Solutions of Hacker Rank
In overriding derived classes can refer the parent class element by using "ME" keyword, but in
shadowing you can access it by "MYBASE".
Yes, you can have access modifiers in events. You can have events with the protected keyword,
which will be accessible only to inherited classes. You can have private events only for objects
in that class.
The Virtual keyword is used in code to define methods and the properties that can be overridden
in derived classes.
Each class has its own constructor and destructor and are called automatically when the
instance of a class is created or destroyed.
The constructor initializes all class members whenever you access the class and the destructor
destroys them when the objects are not required anymore.
GC.SuppressFinalize()
The arguments are specified as a list of arguments of a specific type, e.g., int. For ultimate
flexibility, the type can be object.
Start
Generic basically seperate the logic from the datatype in order maintain better reusability, better
maintainability etc.
110. What is the difference between compile time polymorphism and run time
polymorphism?
Method overloading means having two or more methods with the same name but with different
signatures.
hackerrankproblemsolutions.blogspot.com 11/16
6/19/2019 Some Solutions of Hacker Rank
Method overriding means having two or more methods with the same name , same signature
but with different implementation.
System.Threading
No, because c# doesnot support static block, but it supports static method.
get & set access modifiers are used to implement properties in c#.
The member defined as static which can be invoked directly from the class level, rather than
from its instance.
When a class is derived from another class, then the members of the base class become the
members of the derived class.
The access modifier used while accessing members of the base class specifies the access
status of the base class members inside the derived class.
117. What is a basic difference between the while loop and do while loop in C#?
The while loop tests its condition at the beginning, which means that the enclosed set of
statements run for zero or more number of times if the condition evaluates to true. The do while
loop iterates a set of statements at least once and then checks the condition at the end.
A compile time error occurs if a sealed class is specified as the base class of another class.
Abstract classes can have implementations for some of its members, but the interface can't
have implementation for any of its members.
Interfaces cannot have fields where as an abstract class can have fields.
An interface can inherit from another interface only and cannot inherit from an abstract class,
where as an abstract class can inherit from another abstract class or another interface.
A class can inherit from multiple interfaces at the same time, where as a class cannot inherit
from multiple classes at the same time.
Abstract class members can have access modifiers where as interface members cannot have
access modifiers.
121. What is the difference between an abstract method & virtual method?
hackerrankproblemsolutions.blogspot.com 12/16
6/19/2019 Some Solutions of Hacker Rank
An Abstract method does not provide an implementation and forces overriding to the deriving
class (unless the deriving class also an abstract class), where as the virtual method has an
implementation and leaves an option to override it in the deriving class. Thus Virtual method has
an implementation & provides the derived class with the option of overriding it. Abstract method
does not provide an implementation & forces the derived class to override the method.
It is possible to declare a method as Static provided that they don't attempt to access any
instance data or other instance methods.
The new modifier hides a member of the base class. C# supports only hide by signature.
124. What are the advantages of get and set properties in C#?
A const need to be declared and initialized at declaration only, while a readonly can be initialized
at declaration or by the code in the constructor.
The class which derives functionality from a base class is called a derived class. If
Class Y derives from Class X, then Class Y is a derived class.
What is an extender class?
An extender class allows you to extend the functionality of an existing control. It is
used in Windows forms applications to add properties to controls.
A demonstration of extender classes can be found over here.
What is inheritance?
Inheritance represents the relationship between two classes where one type derives
functionality from a second type and then extends it by adding new methods,
properties, events, fields and constants.
hackerrankproblemsolutions.blogspot.com 13/16
6/19/2019 Some Solutions of Hacker Rank
In VB.NET you use the NotInheritable modifier to prevent programmers from
using the class as a base class. In C#, use the sealed keyword.
When should you use inheritance?
Read this.
De ine Overriding?
Overriding is a concept where a method in a derived class uses the same name,
return type, and arguments as a method in its base class. In other words, if the
derived class contains its own implementation of the method rather than using the
method in the base class, the process is called overriding.
Can you use multiple inheritance in .NET?
.NET supports only single inheritance. However the purpose is accomplished using
multiple interfaces.
Why don’t we have multiple inheritance in .NET?
There are several reasons for this. In simple words, the efforts are more, benefits
are less. Different languages have different implementation requirements of
multiple inheritance. So in order to implement multiple inheritance, we need to
study the implementation aspects of all the languages that are CLR compliant and
then implement a common methodology of implementing it. This is too much of
efforts. Moreover multiple interface inheritance very much covers the benefits that
multiple inheritance has.
What is an Interface?
An interface is a standard or contract that contains only the signatures of methods
or events. The implementation is done in the class that inherits from this interface.
Interfaces are primarily used to set a common standard or contract.
When should you use abstract class vs interface or What is the
difference between an abstract class and interface?
I would suggest you to read this. There is a good comparison given over here.
What are events and delegates?
An event is a message sent by a control to notify the occurrence of an action.
However it is not known which object receives the event. For this reason, .NET
provides a special type called Delegate which acts as an intermediary between the
sender object and receiver object.
What is business logic?
It is the functionality which handles the exchange of information between database
and a user interface.
What is a component?
Component is a group of logically related classes and methods. A component is a
class that implements the IComponent interface or uses a class that implements
IComponent interface.
What is a control?
A control is a component that provides user-interface (UI) capabilities.
What are the differences between a control and a component?
The differences can be studied over here.
What are design patterns?
Design patterns are common solutions to common design problems.
What is a connection pool?
A connection pool is a ‘collection of connections’ which are shared between the
clients requesting one. Once the connection is closed, it returns back to the pool.
This allows the connections to be reused.
What is a lat ile?
A flat file is the name given to text, which can be read or written only sequentially.
What are functional and non-functional requirements?
Functional requirements defines the behavior of a system whereas non-functional
requirements specify how the system should behave; in other words they specify
the quality requirements and judge the behavior of a system.
E.g.
Functional - Display a chart which shows the maximum number of products sold in
a region.
Non-functional – The data presented in the chart must be updated every 5 minutes.
What is the global assembly cache (GAC)?
GAC is a machine-wide cache of assemblies that allows .NET applications to share
libraries. GAC solves some of the problems associated with dll’s (DLL Hell).
What is a stack? What is a heap? Give the differences between the
two?
Stack is a place in the memory where value types are stored. Heap is a place in the
memory where the reference types are stored.
hackerrankproblemsolutions.blogspot.com 14/16
6/19/2019 Some Solutions of Hacker Rank
What is code review?
The process of examining the source code generally through a peer, to verify it
against best practices.
What is logging?
Logging is the process of persisting information about the status of an application.
What are mock-ups?
Mock-ups are a set of designs in the form of screens, diagrams, snapshots etc., that
helps verify the design and acquire feedback about the application’s requirements
and use cases, at an early stage of the design process.
What is a Form?
A form is a representation of any window displayed in your application. Form can be
used to create standard, borderless, floating, modal windows.
What is a multiple-document interface(MDI)?
A user interface container that enables a user to work with more than one
document at a time. E.g. Microsoft Excel.
What is a single-document interface (SDI) ?
A user interface that is created to manage graphical user interfaces and controls
into single windows. E.g. Microsoft Word
What is BLOB ?
A BLOB (binary large object) is a large item such as an image or an exe
represented in binary form.
What is ClickOnce?
ClickOnce is a new deployment technology that allows you to create and publish
self-updating applications that can be installed and run with minimal user
interaction.
What is object role modeling (ORM) ?
It is a logical model for designing and querying database models. There are various
ORM tools in the market like CaseTalk, Microsoft Visio for Enterprise Architects,
Infagon etc.
What is a private assembly?
A private assembly is local to the installation directory of an application and is used
only by that application.
What is a shared assembly?
A shared assembly is kept in the global assembly cache (GAC) and can be used by
one or more applications on a machine.
What is the difference between user and custom controls?
User controls are easier to create whereas custom controls require extra effort.
User controls are used when the layout is static whereas custom controls are used
in dynamic layouts.
A user control cannot be added to the toolbox whereas a custom control can be.
A separate copy of a user control is required in every application that uses it
whereas since custom controls are stored in the GAC, only a single copy can be
used by all applications.
Where do custom controls reside?
In the global assembly cache (GAC).
What is a third-party control ?
A third-party control is one that is not created by the owners of a project. They are
usually used to save time and resources and reuse the functionality developed by
others (third-party).
What is a binary formatter?
Binary formatter is used to serialize and deserialize an object in binary format.
What is Boxing/Unboxing?
Boxing is used to convert value types to object.
E.g. int x = 1;
object obj = x ;
Unboxing is used to convert the object back to the value type.
E.g. int y = (int)obj;
Boxing/unboxing is quiet an expensive operation.
What is a COM Callable Wrapper (CCW)?
CCW is a wrapper created by the common language runtime(CLR) that enables COM
components to access .NET objects.
What is a Runtime Callable Wrapper (RCW)?
RCW is a wrapper created by the common language runtime(CLR) to enable .NET
components to call COM components.
What is a digital signature?
A digital signature is an electronic signature used to verify/gurantee the identity of
the individual who is sending the message.
What is garbage collection?
Garbage collection is the process of managing the allocation and release of memory
in your applications. Read this article for more information.
What is globalization?
Globalization is the process of customizing applications that support multiple
cultures and regions.
hackerrankproblemsolutions.blogspot.com 15/16
6/19/2019 Some Solutions of Hacker Rank
What is localization?
Localization is the process of customizing applications that support a given culture
and regions.
What is MIME?
The definition of MIME or Multipurpose Internet Mail Extensions as stated in MSDN
is “MIME is a standard that can be used to include content of various types in a
single message. MIME extends the Simple Mail Transfer Protocol (SMTP) format of
mail messages to include multiple content, both textual and non-textual. Parts of
the message may be images, audio, or text in different character sets. The MIME
standard derives from RFCs such as 2821 and 2822”
hackerrankproblemsolutions.blogspot.com 16/16