You are on page 1of 10

9/15/2019 15 C# Interview Questions Beginners Should not Miss

🏠 Python ▾ Selenium QA Agile Angular SQL MySQL Java Linux C C# Web PHP Android

15 C# Interview Questions Every Programmer Should Know Example: Python sockets


SPONSORED SEARCHES

general interview questions programming for beginners

sample interview questions and answers learn c#

C# Interview | By Meenakshi Agarwal

Today, we’ve outlined 15 C# interview questions that every beginner should read once. We’ve mostly
covered the fundamentals of C# programming and touched upon a few advanced concepts.

As per the latest TIOBE index, C# stands at the seventh position among the best programming
language to learn first. So you probably took the right decision to start learning the language that can
fetch you one of the highest paying jobs in the IT industry.

Looking at the popularity and the volume of job openings, we’ve picked some of the essential C#
interview questions that could increase your chances of getting hired. You must remember that a
successful candidate is the one who can think a step ahead than the interviewer.

Before moving on to the questions and answers section, let’s read a few interesting facts about C#
programming.

Let’s Prepare For C# Programming Interview


History Matters
As the world knows that C# took its first breath in 2001 at Microsoft labs. And soon, it went through
several transformations under the supervision of Anders Hejlsberg. Hejlsberg who missed his graduation in
engineering from the Technical University of Denmark. But later went on to authoring notable programming
languages like Turbo Pascal, Delphi and currently the man behind the development of C#.

Now, let’s hear a few fun facts about the C# language.

C# Fun Facts

It puts away all global variables and functions. You must declare them strictly inside a class.
Local variables can’t push back the variables of the enclosing block.
C# presents a native boolean type i.e. bool. Unlike C/C++, you can’t translate it to/from an integer.
You can use pointers only from the blocks marked as unsafe. You won’t be able to dereference any
pointer defined outside the unsafe block.
C# brought in GC (Garbage Collector) to manage memory and avoid memory leaks.
It fixed the well-known diamond problem by casting out the ability that could lead to multiple
inheritance issues.
C# allows using Enumeration within the boundary of their namespaces.
It fully supports type reflection and discovery.

15 C# Interview Questions And Answers

https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 1/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss

C# Interview Questions

Q-1. What Are The Essential Features Of C# Language That Made It The Fourth Most
Used Language?

Ans. Following is the list of features behind the popularity of the C# language:

1. Boolean Conditions
2. Automatic Garbage Collection
3. Standard Library
4. Assembly Versioning
5. Properties and Events
6. Delegates and Events Management
7. Easy-to-use Generics
8. Indexers
9. Conditional Compilation
10. Simple Multithreading
11. LINQ and Lambda Expressions
12. Integration with Windows

Q-2. What Are The Different Access Modifiers C# Language Supports?

Ans. Access modifiers define the scope in which an object and its members are accessible. All types and
type members in C# support them.

There are following five types of access specifiers available in C#.

A. Public :

It is the most common access specifier in C#. There is no restriction on accessing public members. The
scope of its accessibility is inside as well as outside the class.

B. Private :

A private member is accessible from within the body of the class or the struct which defines it. Thus it
cannot be accessed from outside the class. It is the default access modifier type if there is no explicit
declaration.

C. Protected :

The scope of accessibility limits to within the class or struct and the class derived ( or Inherited) from this
class.

D. Internal :

The internal access modifiers can access within the program that contains its declarations and also
access within the same assembly level but not from another assembly.

E. Protected Internal :

It is the same as protected and internal access levels. Its accessibility scope is anywhere in the same
assembly, in the same class and also the classes inherited from that class.

Q-3. What Is A Constructor And How Is It Different In C# Than C++?

Ans. As usual, a constructor is a special method of a class that gets invoked automatically on
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 2/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
, p g y
instantiating that class.

The primary purpose of the constructors is to initialize the private members while creating the objects of
the class. If a class doesn’t have an explicit constructor, then the compiler automatically creates the
default one. It initializes all numeric fields in the class to zero and all string and object fields to null.

Advertise on LinkedIn
Reach a community of
55M+ professionals in India

Since there is no way a constructor can return values, so it doesn’t have a return type.

A constructor can call another constructor by using “this” keyword. The “this” keyword represents the
current instance of a class.

Syntax:

You can define a constructor with the same class name and without mentioning any return type as:

[Access Modifier] className([Parameters])


{
public className() {
// write the code here
}
}

Q-4. What Are Different Types Of Constructor Available In C#?

Ans. We can classify the constructors into the following five types:

1. Default Constructor:

A constructor without any parameters is the default Constructor. A major drawback of default Constructor
is that all instances of the class get initialized with the same values. It is not possible to initialize them
with different values.

2. Parameterized Constructor:

A Constructor with at least one parameter is Parameterized Constructor. Its advantage is that it enables
to initialize each instance of the class to different values.

3. Copy Constructor:

A Copy Constructor is a parameterized constructor that contains, parameters of same class type. Its
objective is to initialize a new instance to the values of an existing one.

4. Static Constructor:

We create a Static Constructor by declaring a Constructor as static. It gets invoked only once for any
number of instances of the class.

5. Private Constructor:

https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 3/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss

It is not possible to instantiate a class that contains a Private Constructor. Thus we can create a Private
Constructor in a class which has only static members.

Q-5. What Is A Jagged Array? Explain With Examples.

Ans. A jagged array is a sequential collection of elements which themselves are arrays. It may constitute
elements of different dimensions and sizes.

Programmers often call it as an “array of arrays” where the length of each array index can differ.

Example:

Declaring the array of four elements:


int[][] jagArray = new int[4][];

In the above declaration, the rows are fixed in size. However, columns can vary as they are not
specified.

Initializing the elements in a jagged array:

jagArray[0] = new int[2] { 7, 9 };


jagArray[1] = new int[4] { 12, 42, 26, 38 };
jagArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
jagArray[3] = new int[3] { 4, 6, 8 };

Q-6. How Does C# Differentiate Between A Struct And A Class?

Ans. Class and struct both are the user-defined data types. However, there are some differences between
them, which are as follows:

Struct:

1. It is value type in C# and inherits from System.Value Type.


2. C# stores the Struct value in the stack memory.
3. The Struct uses the array type. And it’s good to use the Struct for read-only and light-weight objects.
4. A Struct doesn’t support inheritance. So it can’t fit as the base type for a class or a Struct.
5. A Struct can only inherit the interfaces.
6. A Struct can have the constructor only but not the destructor.
7. Using the “new” keyword is not required to instantiate a Struct.
8. The Struct cannot have the default constructor.
9. The Struct is by default sealed class. Hence it will not allow inheritance. It cannot use the abstract,
sealed, and base keywords.
10. The Struct cannot use the protected or protected internal access modifier.
11. We cannot initialize a Struct at the time of declaration.

Class:

1. It is a reference type in C# and inherits from System.Object Type.


2. C# stores the Class object in the heap memory. The GC automatically removes them when needed.
3. The Class uses the collection object type and thus supports operations for complex data type storage.
4. The Class can inherit another Class, interface and it can be the base Class for another Class.
5. The Class can inherit the interfaces and abstract classes.
6. The Class can have both the constructor and the destructor.
7. It is mandatory to use the “new” keyword to create the object for the Class.
8. The Class will have the default constructor.
9. A Class can be declared as an abstract and sealed class.
10. A Class can use all the available access modifiers.
11 A Class can have the object initializer fields
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 4/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
11. A Class can have the object initializer fields.

Q-7. What Is The Use Of Abstract And Sealed Classes In C#?

Ans.

The abstract keyword allows creating classes and class members that are incomplete. Implementation of
such classes transpires in the derived class.

However, the sealed keyword stops a class (marked as virtual) or a particular class member from being
inherited.

Q-8. What Are The Possible Ways Of Passing Parameters To A Function In C#?

Ans. We can use the following approaches to pass parameters in C#.

1. Value Parameters:

It makes a copy of the original value of the arguments, into the functional parameters. In this case, any
change made to the parameter inside the function has no effect on the actual argument.

2. Reference Parameters:

This method copies the address of the memory location of the argument into the formal parameter. Thus,
any change in the value of the parameter affects the actual argument.

3. Output Parameters:

This method allows a function to return multiple values.

Q-9. What Is The Difference Between Ref And Out Keywords?

Ans. Following are the key differences between ref and out keywords:

Ref:

1. The parameter or the argument must be initialized first before passing it to Ref.
2. It is not mandatory for the called method to initialize or assign values to parameters (passed by
reference) before control returns to the calling function.
3. Passing a parameter value by Ref is useful when its value has to be modified by the called function
also.
4. It is not compulsory to assign a value to a parameter before using it in the calling method.
5. Ref supports the bi-directional passing of data.

Out:

1. It is not compulsory to initialize a parameter or argument before passing it to an Out.


2. It is mandatory for the called method to assign or initialize values to the parameters before the control
returns to the calling method.
3. Declaring a parameter to an Out method is useful when a function returns multiple values.
4. It is compulsory to initialize a parameter value within the calling method before its use.
5. Out supports the passing of data in a uni-directional way only.

Q-10. Is It Feasible To Use “This” Within A Static Method?

Ans. It is not possible to use “this” in the static method. Because ‘this’ keyword returns a reference to
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 5/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss

the current instance of the class that contains it.

Static methods (or any static member) do not belong to a particular instance. They exist even if you do
not create an instance of the class and we call it using the name of the class.

So we can’t use “this” keyword in the body of Static Methods. However, there is a situation known as the
Extension Method which does allow to use “this” keyword in static methods.

It is relatively a new concept introduced in C# 3.0. It enables a class to extend without modifying its
code.

Its primary purpose is to extend the capabilities of an existing class.


To add new methods, first of all, declare them inside a static class. Then, bind them with the target
class.
Every such method must have its first parameter beginning with the class name prefixed with this
keyword. The “this” keyword would only act as a binding parameter and will get ignored while calling
the method.

Q-11. What Is A Namespace? And What Is Its Use?

Ans. The namespace keyword is used to declare a scope that contains a set of related objects. You can
use a namespace to organize code elements and to create globally unique types. In other words, it
provides a way to keep one set of names separate from another.

For example, let’s create a class with the name Console and place it in its own namespace. The
explanation for doing this is that it ensures, there is no confusion about when the System.Console class
gets used and when the user created class gets used.

Though, it is bad coding practice to create a class with the name as Console. However, there are
situations where the name of our class could conflict with the names in the Dot Net library or a third
party library. In such cases, namespace helps you avoid the problems that identical class names would
cause. In such scenarios, namespace helps you avoid the problems that identical class names would
cause.

This implies that the class names declared in one namespace does not conflict with the same class
names declared in another.

The following program demonstrates the use of namespaces.

C# Example

using System;
namespace one
{
class myClass
{
public void func()
{
Console.WriteLine("Inside namespace one");
}
}
}

namespace two
{
class myClass
{
public void func()
{
Console.WriteLine("Inside namespace two");
}
}
}
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 6/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
}

class TestClass
{
static void Main(string[] args)
{
one.myClass obj = new one.myClass();
two.myClass itm = new two.myClass();
obj.func();
itm.func();
Console.ReadKey();
}
}

Q-12. What Is The Purpose Of The “Using” Directive In C#?

Ans. The “using” keyword states that the program is using the names in the given namespace.

Let’s take the following example.

C# Example.

// Namespace Declaration
using System;
using techbeamers;

// techbeamers Namespace
namespace techbeamers
{
class myExample
{
public static void myPrint()
{
Console.WriteLine("Example of using directive.");
}
}
}

// Program start class


class UsingDirective
{
// Main begins program execution.
public static void Main()
{
// Call namespace member
myExample.myPrint();
}
}

Explanation.

If we want to call methods without typing their fully-qualified name, we implement the using directive. In
the above example, we have declared two using directives. The first, using System directive allows us to
type the method names of members of the System namespace without typing the word System every
time. In myPrint() method above, Console is a class member of the System namespace with the method
WriteLine(). Its fully qualified name is <System.Console.WriteLine(…)>.

Similarly, the <using techbeamers> directive enables us to call the members of the techbeamers
namespace without typing the fully-qualified name. This is why we can type <myExample.myPrint()>
instead of <techbeamers.myExample.myPrint()> every time we want to call that method.

Q-13. What Is The Concept Of Boxing And Unboxing In C#?

Ans. Boxing and Unboxing both are used for type conversion but have some differences:
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 7/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
g g yp

1. Boxing:

It is the process of converting a value type to an object or to an interface data type. When boxing of
CLR happens, it means that CLR is converting a value type to an Object Type. It wraps the value inside
a <System.Object> and stores it in the heap area in an application domain.

C# Example:

public void myExample()


{
int num=10;
object obj = num; //implicit boxing
Console.WriteLine(obj);
}

2. Unboxing:

It is a process that gets utilized when extracting the value type from the object or any implemented
interface type. Boxing is done implicitly, while unboxing has to be explicit by code.

C# Example:

public void myExample()


{
object obj=10;
int num = int (obj); //explicit Unboxing
Console.WriteLine(num);
}

Q-14. Which Class Acts As A Base Class For All The Data Types In C#?

Ans. The Object type is the base class of all data types in C# Common Type System (CTS). It stays at
the root of the hierarchy and works as an alias for the classes.

We can assign Object type to any of the value or reference types, predefined or user-defined
types. However, before assigning values, type conversion is mandatory.

Q-15. What Is An Enum And How Does C# Implement It?

Ans. An enum is a value type with a set of related named constants often referred to as an enumerator
list. You can define it with the enum keyword. It’s a user-defined primitive data type.

Usually, an enum is of an integer type. Though, you can use other types e.g. float, byte, double.
However, you need to cast while using it.

An enum helps to define numeric constants in C#. All of its members are of the enum type. There must
be a numeric value for each enum type.
The default underlying type of the enumeration element is int. By default, the first enumerator has the
value 0, and the value of each successive enumerator is increased by 1.
Given below is the example of the enum declaration:

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

Following Are Some Important Points About Enum:

1. They are strongly-typed constants. Thus an enum of one type cannot be implicitly assigned to another
enum type, even though their members have the same values.
2 The enum values are same as constants You can print them like a string and process like an integer
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 8/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
2. The enum values are same as constants. You can print them like a string and process like an integer.
3. Its default type is int, and the approved types are as follows.
byte, sbyte, short, ushort, uint, long, and ulong .
4. All enum type automatically derives from System.Enum and its methods work on enums.
5. Enums are value types constructed on the stack and not on the heap.

Summary – C# Interview Questions And Answers For Beginners


We intend you to succeed in all your endeavors. And we are hopeful the above C# interview questions
would help you do that.

In our next posts on C# programming, we’ll add more questions for experienced and bring a few quizzes
for you to practice your skills.

If you have any feedback or like to share some idea, then speak it out in the comment box.

Check out more CSharp Q&A on various programming topics:

1. 50 CSharp Coding Interview Questions


2. 15 CSharp Programming Interview Questions
3. 15 CSharp Interview Questions Every Programmer
4. 15 CSharp Questions on For, While and If Else
5. 15 CSharp Programming Questions on Classes
6. 20 CSharp OOPS Interview Questions Set-1
7. 20 CSharp OOPS Interview Questions Set-2
8. 25 CSharp OOPs Interview Questions Set-3
9. 35 CSharp Exception Handling Questions

All the Best,

TechBeamers

RELATED
C# Exception Handling – 35 20 C# Programming Questions
Questions Programmers Should On Object Oriented Concepts
Not Miss

Tutorials Interviews Questions Web Development Quick Info

https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 9/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
Python Tutorial Python Interview Questions AngularJS Questions ☛ About Us
☛ Privacy Policy
Selenium Python Tutorial SQL Interview Questions JavaScript Questions
☛ Disclaimer
Selenium Webdriver Tutorial Selenium Interview Questions Web Developer Questions ☛ Contact Us

Selenium Demo Websites QA Interview Questions NodeJS Interview Questions

Python Multithreading Manual Testing Questions HTML Interview Questions

Java Multithreading Rest API Interview Questions PHP Interview Questions-1

Python Tips & Tricks Linux Interview Questions PHP Interview Questions-2

© 2019 TechBeamers.com

https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 10/10

You might also like