C# Language Basic Interview Questions and Answers

C# Language Basic Interview Questions and Answers


C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.
C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages to be used on different computer platforms and architectures.




What is C#?
 
C# (pronounced "C sharp") is a simple, modern, object-oriented, and type-safe programming language.

It will immediately be familiar to C and C++ programmers.

C# combines the high productivity of Rapid Application Development (RAD) languages.


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


Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.



2.Data Types in C#?

bool, byte , char, decimal , double, float, int, long, sbyte , short, uint, ulong, ushort.

3. Explain about C# Language.


C# is a OOPs language, .net framework use to compiled it, to generate machine code.


4.Top reason to use C# language?


 Modern, general-purpose programming language
Object oriented.
Component oriented.
Easy to learn.
Structured language.
It produces efficient programs.
It can be compiled on a variety of computer platforms.
Part of .Net Framework.


5.Feature of C# language?


Boolean Conditions
Automatic Garbage Collection
Standard Library
Assembly Versioning
Properties and Events
Delegates and Events Management
Easy-to-use Generics
Indexers
Conditional Compilation
Simple Multithreading
LINQ and Lambda Expressions
Integration with Windows


6. Types of comments in C#?



Single line comments


// for single line comments

Multiple line comments

/* for multi line comments */

XML tags comments

/// XML tags displayed in a code comment




7.What is a Class?

a set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.



8. What is object?

Objects are created from Classes, in C#, is an instance of a class that is created dynamically. Object is also a keyword that is an alias for the predefined type System.



9.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])
{
}


EX



class Consc
{
public Consc()
{
Console.WriteLine("Simple Constructor");
}
}

10.Types of Constructors


1.Basically constructors are 5 types those are
2.Default Constructor
3.Parameterized Constructor
4.Copy Constructor
5.Static Constructor
6.Private Constructor




11.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.

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.


12.Explain use of Abstract and Sealed Classes in C#?

The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.


 13.Different between method overriding and  method overloading?

In Overriding methods it will create two or more methods with same name and same parameter in different classes.

while Overloading it will create more then one method with same name but different parameter in same class.



14.What is Static Classes?

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.




 Explain Static Class Members.

A non-static class can contain static methods, fields, properties, or events.

The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.


15. index value of the first element in an array?

First element is 0 (zero). In a Array.



16. What is Jagged Arrays?

The array which has elements of type array is called jagged array. The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.


17. What is the difference between ref & out parameters?

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method.


18. What are value types and reference types?  

Value types are stored in the Stack whereas reference types stored on heap.
Value types
bool, byte, chat, decimal, double, enum , float, int, long, sbyte, short, strut, uint, ulong, ushort.
Reference types:
class, delegate, interface, object, string.


19.What is delegates in c# and uses of delegates?

Basically delegates in c# are type safe objects which are used to hold reference of one or more methods in c#.net
Delegates concept will match with pointer concept of c language.  

multiple methods with same signature (return type & number of parameters) and want to call all the methods with single object then we can go for delegates.

Delegates are two types


   Single Cast Deligates

   Multi Cast Deligates




 Multi Cast Deligates

Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=

Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list




20.Polymorphism



Polymorphism means many forms (ability to take more than one form).

In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.



In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes.


Polymorphism has ability to provide different implementation of methods that are implemented with same name.



In Polymorphism we have 2 different types those are




    Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)



     Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)


     Compile Time Polymorphism


    Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

    Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

public class Class1
{
public void Add(int x, int y)
{
Console.WriteLine(x + y);
}
public void Add(int x, int y, int z)
{
Console.WriteLine(x + y + z);
}
}


Run Time Polymorphism

Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.

21. What are the namespaces used in C#.NET?
 
Namespace is a logical grouping of class.

using System;

using System.Collections.Generic;

using System.Windows.Forms;




22. What are the characteristics of C#?

 

There are several characteristics of C# are :



Simple

Type safe

Flexible

Object oriented

Compatible

Consistent
Interoperable
Modern


23. What are the basic concepts of object oriented programming?
 
It is necessary to understand some of the concepts used extensively in object oriented programming.These include:

Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message passing.


24. What are the different categories of inheritance?

The method of constructing one class from another is called Inheritance.The derived class inherits all the properties and methods from the base class and it can add its own methods also. 

-Inheritance is deriving the new class from the already existing one.
-Inheritance is Extending Functionality of Existing Class
-Re-usablity


Inheritance in Object Oriented Programming is of four types:

Single inheritance: Contains one base class and one derived class.

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Multilevel inheritance: Contains a class derived from a derived class.

Multiple inheritance: Contains several base classes and a derived class.


25.Advantages of Inheritance.


a)Code re-usability-Public and protected methods can be used in derived classes
b)Extensibility- base class can be extended as per business logic in derived           Classes.
c)Polymorphism.



26.why c# does not support inheritance by structure?

Because structures are mainly used for light weight process. 
And if they are allowed to be inherited then they have to act as base class which is not possible as they are value types.




27.Can you inherit multiple interfaces?



Yes. Multiple interfaces may be inherited in C#.




28.Does structs inherit from interfaces?


Yes structs can inherit only from interface.






29.How do you prevent a class from being inherited ?


a)Make the class as sealed.
b)Add private constructors to the class.




30.What do you mean by sealed keyword ?

If you mark a class as sealed it means that you cannot inherit from it but you can create objects of that class.



31.can you mark method as sealed ?

Yes.But for a method to be marked as sealed you need to have override keyword also.


32.what do you mean by upcasting and downcasting ?

class DerivedClass :BaseClass

Upcasting--assigning a derived class object to a base class.

This is implicit.BaseClass b= new DerivedClass.


Downcasting--assigning baseclass object to derived class. 

This is explicit and it throws run time error.


33. What is the difference between public, static and void?
 
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.


34. What are the modifiers in C#?
 
Abstract
Sealed
Virtual
Const
Event
Extern
Override
Readonly
Static
New



Implicit conversion of value type to reference type of a variable is known as BOXING, for example integer to object type conversion. 

Conversion of reference type variable back to value type is called as UnBoxing.


36. Where are the types of arrays in C#?
 
Single-Dimensional
Multidimensional
Jagged arrays.


37.What is Destructor?

Whereas destructors get invoked when life of an object ends. Main purpose of destructor is cleaning class resources (unmanaged resources).

Destructor Symbol ~


38. What is the use of enumerated data type?

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.


39. What is encapsulation?

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.

 It is a technique to hide the properties and behaviours of an object. 
-The access is provided only as required. 
-It prevents other objects from altering or accessing the properties of an encapsulated object.

Private is the Encaptulation



 class Employee{


 private void AccountInfo()

{

  Console.WriteLine("Displaying Account Details");

 } 

}


40. Explain: a.) Static binding b.) Dynamic binding

a.) Static binding- 

It is a binding in which the name of the class can be associated with it during compilation. Also referred to as early binding.

b.) Dynamic binding – 

It is a binding in which the name of the class can be associated with it during execution time. Also referred to as late binding.



41.Does c# support multiple inheritance?

No,its impossible which accepts MultiLevel Inheritance.


42. What is an abstract base class?

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.

The abstract keyword can be used with classes, methods, properties, indexers and events. 

Abstract classes to avoid repetetion of code.

Abstract Class Are Inherited

Abstract Class used  When We Want to Share Common Functionality Parent child Relationship

EX:

Public abstract Class Demo
{
}
Public Class D1:Demo
{
}
Public Class D2:Demo
{
}


43.What is an Interface

An interface is not a class
 An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.

A class may inherit several interfaces.
An interface cannot provide any code, just the signature.
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public.

EX:

interface Demo
{
Void Add();
}
Public Class D1:Demo
{
Public Void Add()
{
    // Some Coding
}
}
Public Class D2:Demo
{
Public Void Add()
{


}
}


44.What is serialization?

Serialization is the process of converting an object into a stream of bytes.

De-serialization is the opposite process of creating an object from a stream of

 bytes.


45. What are the difference between Structure and Class?
 
Structures are value type and Classes are reference type

Structures can not have contractors or destructors.

Classes can have both contractors and destructors.

Structures do not support Inheritance, while Classes support Inheritance.


46. What is Authentication and Authorization?

Authentication is the process of identifying users. 
Authentication is identifying/validating the user against the credentials (username and password).

Authorization performs after authentication. Authorization is the process of granting access to those users based on identity. 


47. What is difference between constants, readonly and, static ?
 
Constants: The value can’t be changed.

Read-only: The value will be initialized only once from the constructor of the class.

Static: Value can be initialized once.


48. What are the different types of statements supported in C#?
 
C# supports several different kinds of statements are

Block statements
Declaration statements
Expression statements
Selection statements
Iteration statements
Jump statements
Try catch statements
Checked and unchecked
Lock statement


49. What are the two data types available in C#?
 
1.Value type
2.Reference type


50. What are the different types of Caching?
 
There are three types of Caching:

Output Caching: stores the responses from an asp.net page.

Fragment Caching: Only caches/stores the portion of page (User Control)

Data Caching: is Programmatic way to Cache objects for performance.


51. What is methods?

A method is a member that implements a computation or action that can be performed by an object or class. 
Static methods are accessed through the class. Instance methods are accessed through instances of the class.


52. 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.

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 files.


53. What is fields?

A field is a variable that is associated with a class or with an instance of a class.


54. What is events?

An event is a member that enables a class or object to provide notifications.

An event is declared like a field except that the declaration includes an event keyword and the type must be a delegate type.



55. What is literals and their types?
 
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


56. What are the types of errors?
 
Syntax error
Logic error
Runtime error


57. What is the difference between break and continue statement?

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.


58. What are sealed classes in C#?

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.


59. What is a code group?

A code group is a set of assemblies that share a security context.


60. What is the difference between static and instance methods?

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.

A method declared without a static modifier is an instance method. 
An instance method operates on a specific instance and can access both static and instance members. 
The instance on which an instance method was invoked can be explicitly accessed as this. It is an error to refer to this in a static method.


61. What are the different types of variables in C#?
 
Different types of variables used in C# are :

static variables
instance variable
value parameters
reference parameters
array elements
output parameters
local variables


62. What is parameters?

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, 
parameter arrays.


63. Is C# is object oriented?

Yes, C# is an Object Oriented  Langauge in the tradition of Java and C++.



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.


65 What is meant by operators in c#?

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, 
 conversion operators. 

All operators must be declared as public and static.


66. What are the special operators in C#?
 
C# supports the following special operators.

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)


67. What is the use of goto statement?

 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.


68. What is the difference between console and window application?
 
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.


69. What is the use of return statement?
 
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 :

to return immediately to the caller of the currently executed code

to return some value to the caller of the currently executed code.


70. Can you override private virtual methods?

No, private methods are not accessible outside the class.


71. Do events have return type?

No, events do not have return type.


72. How does C# differ from C++?
 
C# does not support #include statement. It uses only using statement.

In C# , class definition does not use a semicolon at the end.

C# does not support multiple code inheritance.

Casting in C# is much safer than in c++.

In C# switch can also be used on string values.

Command line parameters array behave differently in C# as compared to C++.


73. What is nested class?
 
A Nested classes are classes within classes.

A nested class is any class whose declaration occurs within the body of another class or interface.


74. Is String is Value Type or Reference Type in C#?

String is an object(Reference Type).


75. Does C# provide copy constructor?

No, C# does not provide copy constructor.


76. Can you create an instance of an interface?

No, you cannot create an instance of an interface.


77. Can an Interface contain fields?

No, an Interface cannot contain fields.


78. What is the main use of delegates in C#?

Delegates are mainly used to define call back methods.


79.Can events have access modifiers?

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.


80.Why is the virtual keyword used in code?

The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.


81. What is Generic?
 
Generic help us to create flexible strong type collection.

Generic basically seperate the logic from the datatype in order maintain better reusability, better maintainability etc.


82.What is the difference between compile time polymorphism and run time polymorphism?
 
Compile time Polymorphism

Compile time Polymorphism also known as method overloading.

Method overloading means having two or more methods with the same name but with different signatures.

Run time Polymorphism

Run time Polymorphism also known as method overriding.

Method overriding means having two or more methods with the same name , same signature but with different implementation.


 83.Can we declare a block as static in c#?

No, because c# doesnot support static block, but it supports static method.



84.Can we declare a method as sealed?

In C# a method can't be declared as sealed. However when we override a method in a derived class, we can declare the overridden method as sealed. 
By declaring it as sealed, we can avoid further overriding of this method.


85.What Command is used to implement properties in C#?

get & set access modifiers are used to implement properties in c#.


86.What is static member?

The member defined as static which can be invoked directly from the class level, rather than from its instance.


87.What is the syntax to inherit from a class in C#?
 
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.

The syntax to inherit a class from another class In C# is as follows:

class MyNewClass : MyBaseClass


88.What is the main difference between a subprocedure and a function?

Subprocedures do not return a value, while functions do.


89.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.


90.What are sealed classes in c#?
 
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.


91.What are the advantages of get and set properties in C#?
 
The get property accessor is used to return the property value.

The set property accessor is used to assign a new value.


92.What is Static 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.


93.What is a New modifier?

The new modifier hides a member of the base class. C# supports only hide by signature.


94.What is the difference between Shadowing and Overriding?
 
Overriding redefines only the implementation while shadowing redefines the whole element.

In overriding derived classes can refer the parent class element by using "ME" keyword, but in shadowing you can access it by "MYBASE".


95.Can you create an instance of an interface?

No, you cannot create an instance of an interface.


96.What are the different ways a method can be overloaded?

 Different parameter data types,
 Different number of parameters,
 Different order of parameters.


97.What does a break statement do in switch statements?
 
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.


98.Which is an exclusive feature of C#?

Xml documentation. Using Xml in Web.Config.


99.What is the difference between private and public keyword?
 
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.


100.Define scope?

Scope refers to the region of code in which a variable may be accessed.



101.Difference between Encapsulation and Abstraction in OOPS

Abstraction and Encapsulation are two important Object Oriented Programming (OOPS) concepts. Encapsulation and Abstraction both are interrelated terms. 

Encapsulate means to hide. Encapsulation is also called data hiding.You can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. 

1) Encapsulation --- By applying access specifier (Private,public)
-- By making method access specifier public
-- use the Java Beans Getter/setter method instead of constructor



Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.


102.What is a multi cast delegates?

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.



103.Difference between "throw" and "throw ex" in .NET


  •  "throw" statement, it preserve original error stack information.
  •  "throw ex" statement, stack trace of the exception will be replaced with a stack trace starting at the re-throw point.
it is very important to just use the throw statement, rather than throw ex because it will give you more accurate error stack information.


104.Difference between IS and AS operators in C#


The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false.
The is operator will never throw an exception.

An is expression evaluates to true if both of the following conditions are met: 
expression is not null
expression can be cast to type
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:

Employee e = obj as Employee;

is equivalent to:

 Employee e = obj is  Employee ? ( Employee)obj : ( Employee)null;


105.What is a COPY Constructor?
  • C# does not provide a copy constructor.
  • A copy constructor is a special constructor used to create a new object as a copy of an existing object.
  • This constructor takes a single argument: a reference to the object to be copied.
  • It is a great convenience to create copy constructor for C# classes using Reflection.
  • If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.




0 comments:

Post a Comment