Monday , December 10 2018
Home / C# Fresher Interview Questions

C# Fresher Interview Questions

Here I am posting the c# Fresher Interview Questions.  have searched for so many websites and gathered information from my friends to answer the questions perfectly.i think these questions are very helpful for the people who are trying to get the job on .NET  The most common question for fresher.

1. What is object-oriented programming (OOP)?

1. What is object-oriented programming (OOP)?

OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object is created in the program to represent a class. Therefore, an object encapsulates all the features, such as data and behavior that are associated to a class. OOP allows developers to develop modular programs and assemble them as software. Objects are used to access data and behaviors of different software modules, such as classes, namespaces, and sharable assemblies. .NET Framework supports only OOP languages, such as Visual Basic .NET, Visual C#, and Visual C++.

2.What is a class??

2.What is a class??

A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type, which represents a blue print of objects. It is a template of object.

A class can be defined as the primary building block of OOP. It also serves as a template that describes the properties, state, and behaviors common to a particular group of objects.

A class contains data and behavior of an entity. For example, the aircraft class can contain data, such as model number, category, and color and behavior, such as duration of flight, speed, and number of passengers. A class inherits the data members and behaviors of other classes by extending from them.

3.What is an object?

3.What is an object?

They are instance of classes. It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Attributes and behavior of an object are defined by the class definition.

4.What is the relationship between a class and an object?

4.What is the relationship between a class and an object?

A class acts as a blue-print that defines the properties, states, and behaviors that are common to a number of objects. An object is an instance of the class. For example, you have a class called Vehicle and Car is the object of that class. You can create any number of objects for the class named Vehicle, such as Van, Truck, and Auto.

The new operator is used to create an object of a class. When an object of a class is instantiated, the system allocates memory for every data member that is present in the class.

5.Explain the basic features of OOPs?

5.Explain the basic features of OOPs?

The following are the four basic features of OOP:

  • Abstraction– Refers to the process of exposing only the relevant and essential data to the users without showing unnecessary information.
  • Polymorphism– Allows you to use an entity in multiple forms.
  • Encapsulation– Prevents the data from unwanted access by binding of code and data in a single unit called object.
  • Inheritance– Promotes the re usability of code and eliminates the use of redundant code. It is the property through which a child class obtains all the features defined in its parent class. When a class inherits the common properties of another class, the class inheriting the properties is called a derived class and the class that allows inheritance of its common properties is called a base class.

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

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

Class:

  • A class is a reference type.
  • While instantiating a class, CLR allocates memory for its instance in heap.
  • Classes support inheritance.
  • Variables of a class can be assigned as null.
  • Class can contain constructor/destructor.

Structure:

  • A structure is a value type.
  • In structure, memory is allocated on stack.
  • Structures do not support inheritance.
  • Structure members cannot have null values.
  • Structure does not require constructor/destructor and members can be initialiazed automatically.

7.Which are Access Modifiers available in C#?

7.Which are Access Modifiers available in C#?

All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies.

You can use the following access modifiers to specify the accessibility of a type or member when you declare it:
public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private: The type or member can be accessed only by code in the same class or struct.
protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

8.What is Constructors, Explain with syntax?

8.What is Constructors, Explain with syntax?

A is special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

Constructors are mainly used to initialize private fields of the class while creating an instance for the class.

When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null.

Syntax.
[Access Modifier] ClassName([Parameters])
{

}

9.What is Static Classes?

9.What is Static Classes?

C# provides the important feature to create static classes, there are two main features of a static class, one is no object of static class can be created and another is, a static class must contain only static members, then it is important that what is the main benefit to create a static class, the main benefit of making static class, we do not need to make any instance of this class ,all members can be accessible with its own name.

Declaration:

A static class is created by using keyword ‘Static’ as shown here:

Static class Classname
{
//C#
}

One more thing that is notable-within static class, all members must be explicitly specified as static, static class does not automatically make its members static. Static class can contain a collection of static methods.

10.Types of classes in C#.Net?

10.Types of classes in C#.Net?

• Abstract Class (somtimes called a Pure Virtual Class)

• Partial Class

• Sealed Class

• Static Class

Abstract Class:
An Abstract Class means that, no object of this class can be instantiated, but can make derivation of this. It can serve the purpose of base class only as no object of this class can be created.
Abstract Class is denoted by the keyword abstract.

Example:

abstract class myClass
{
public myClass()
{
// code to initialize the class…
}

abstract public void anyMethod_01();
abstract public void anyMethod_02(int anyVariable);
abstract public int anyMethod_03 (int anyvariable);
}

abstract class myclass
{
public void nonAbstractMethod ()
{
// code…
}
}

Partial Class:
This special type of class called “Partial Class” is introduced with .Net Framework 2.0. Partial Class allows its members – method, properties, and events – to be divided into multiple source files (.cs). At compile time these files get combined into a single class.
Partial Class is denoted by the keyword partial.
Some do’s and don’ts about partial class:-
• All the parts of a partial class must be prefixed with the keyword partial.
• Accessibility, signature etc. must be same in all parts of the partial class.
• You cannot sealed one part of the partial class. In that case entire class in sealed.
• If you define any part of the partial class abstract, entire class will become abstract.
• Inheritance cannot be applied to a part of partial class. If you do so, it applies to entire class.

Example:

public partial class myPartialClass
{
public void firstMethod()
{
// code…
}
}

public partial class myPartialClass
{
public void secondMethod()
{
// code…
}
}

Sealed Class:
A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed. It is the last class in hierarchy. To access the members of a sealed class, you must create objects of that class.
Sealed Class is denoted by the keyword sealed.

Example:

sealed class mySealedClass
{
int a;
int b;
}

Class mainClass
{
public static void Main()
{
mySealedClass obj = new mySealedClass();
obj.a = 5;
obj.b = 7;
Console.WriteLine(“a = {0}, b = {1}”, obj.a, obj.b);
}
}

Static Class:
A Static Class is one which cannot be instantiated. The keyword new cannot be used with static classes as members of such class can be called directly by using the class name itself.
Following are the main characteristics of a static class:-
• A Static Class can only have static members.
• A Static Class cannot be instantiated.
• A Static Class is sealed, so cannot be inherited.
• A Static Class cannot have a constructor (except static constructor).
Static Class is denoted by the keyword static.

 

Example:

// static class definition…
public static class myclass
{
public static int addNumbers(int a, int b)
{
return (a + b);
}
}

// to use it, we call directly on the class…
Console.WriteLine(“The addition of 5 and 7 is: ” + myClass.addNumbers(5, 7));

 

Pages ( 1 of 6 ): 1 23 ... 6Next »