Wednesday, 3 December 2014

C# interview Questions

I have add this post for .net programmers how want to appear in their first .Net interview. These   questions are very compulsory if you want to be selected in .Net interview. 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1: What is IL?
(IL)Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. This IL is then converted to machine code at the point where the software is installed, or at run-time by a Just-In-Time (JIT) compiler.
2: What is the CLR?
Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework. All Languages have runtime and it’s the responsibility of the runtime to take care of the code execution of the program. IL (Intermediate language)-to-native translators and optimizer’s:- CLR uses JIT and compiles the IL code to machine code and then executes. CLR also determines depending on platform what is optimized way of running the IL code.
3: What is the CTS?
In order that two languages communicate smoothly CLR has CTS (Common Type System). Example in VB you have “Integer” and in C++ you have “long” these data types are not compatible so the interfacing between them is very complicated. In order that two different languages can communicate Microsoft introduced Common Type System. So “Integer” data type in VB6 and “int” data type in C++ will convert it to System.int32 which is data type of CTS.
4: What is CLS (Common Language Specification)?
Microsoft has defined CLS which are nothing but guidelines that language to follow so that it can communicate with other .NET languages in a seamless manner.
5: What is Assembly?
Assembly is unit of deployment like EXE or a DLL.
An assembly consists of one or more files (dlls, exe’s, html files etc.), and represents a group of resources, type definitions, and implementations of those types. An assembly may also contain references to other assemblies. These resources, types and references are described in a block of data called a manifest. The manifest is part of the assembly, thus making the assembly self-describing.
6: What is Namespace?
The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.
Namespace logically group types. Example System.Web.UI logically groups our UI related features. In Object Oriented world May times it’s possible that programmers will use the same class name. By qualifying namespace with class name this collision can be removed.
7: What is garbage collection?
Garbage collection is a CLR feature which automatically manages memory. Programmers forget to release the objects while coding. Laziness (Remember in VB6 where one of the good practices is to set object to nothing). CLR automatically releases objects when they are no longer referenced and in use.
8: What are Value types and Reference types?
C# has two varieties of data types: value and reference. When a variable is declared using one of the basic, built-in data types or a user defined structure, it is a value type. An exception is the string data type, which is a reference type.
A data type is a value type if it holds the data within its own memory allocation. Value types include the following: (All numeric data types, BooleanChar, and Date, All structures, even if their members are reference types)
A reference type contains a pointer to another memory location that holds the data. Reference types include the following: (String, All arrays, even if their elements are value types, Class types, such as Form, Delegates).
 9: What is concept of boxing and unboxing?
Boxing
Unboxing
Definition:
Boxing is the process of converting a value type to the reference type.
(value type to the type object)
Unboxing is the process of converting
a reference type to value type
Type of Conversion:
Implicit Conversion
Explicit Conversion
Example:
int i = 221;
object obj = i; //boxing
object obj = 213;
i = (int)obj ; // unboxing
10: What’s difference between VB.NET and C#.NET?
11: What are exceptions?     (Try, Catch, Finally, Throw)
12: What is a Thread?
A thread is the basic unit to which the operating system allocates processor time. Threading enables your Visual Basic or C# program to perform concurrent processing so that you can do more than one operation at a time. For example, you can use threading to monitor input from the user, perform background tasks, and handle simultaneous streams of input.
Threading solves problems with throughput and responsiveness, but it can also introduce resource-sharing issues such as deadlocks and race conditions.
Threads have the following properties:
·        Threads enable your program to perform concurrent processing.
·        The .NET Framework System.Threading namespace makes using threads easier.
·        Threads share the application's resources.

13: What is Multi-threading?
Multi-threading forms subset of Multi-tasking instead of having to switch between programs this feature switches between different parts of the same program. Example you are writing in word and at the same time word is doing a spell check in background.
14: Which namespace has threading?  (Systems.Threading)
15: Can you explain in brief how can we implement threading?
16: What is a Web Service?
Web Services are business logic components which provide functionality via the Internet using standard protocols such as HTTP. Web Services uses Simple Object Access Protocol (SOAP) in order to expose the business functionality. SOAP defines a standardized format in XML which can be exchanged between two entities over standard protocols such as HTTP.
17: What is the extension of web service and how to call a web service?

Extension: .asmx

 To call a Web service programmatically

·        Use the Web reference name (or server name) as the namespace, and the name of its .WSDL file (or the service name) as the proxy class.
18: What’s a Class?
A class describes all the attributes of objects of that class, as well as the methods that implement the behavior of member objects. It’s a comprehensive data type which represent a blue print of objects. It’s a template of object.
19: What’s an Object?
It’s a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class, and the attributes and behavior of an object are defined by the class definition.
20: Abstraction?
It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world. It’s a model of real world or concept.
21: Encapsulation?
The process of hiding all the internal details of an object from the outside world.
22: What is Association?
This is the simplest relationship between objects. Example every customer has sales. So Customer object and sales object have an association relation between them. Computer uses keyboard as input device. Employee uses Bus Service for transportation. (Class A uses Class B.)
23: What is Aggregation & Composition?
The main difference between an aggregation and a composition is the property of your "has-a" relationship. The aggregation “has-a" relationship is of "weak-type". Example in order to make a “Accounts” class it has use other objects example “Voucher”, “Journal” and “Cash” objects. So accounts class is aggregation of these three objects. (Class A is owns Class B). The composition "has-a" relationship of "strong-type”.  Example Body consists of Arm, Head, and Legs. (Class A contains Class B).
24: What is Inheritance?
Hierarchy is used to define more specialized classes based on a preexisting generalized class. Example we have VEHICLE class and we can inherit this class make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. Its shows more of a parent-child relationship. This kind of hierarchy is called inheritance.
25: What is Polymorphism?
When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class (Generalized class).The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name if the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.

26: What are Access Specifiers and why we use?

 

Private

Protected

Public

 

27: Can we create private constructor & use of private constructor ?
Yes. A private constructor is a special instance constructor. It is generally used in classes that contain static members only. The declaration of the empty constructor prevents the automatic generation of a default constructor. Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.
 class Sample
{
    // Private Constructor: 
    private Sample() { }

    public static double e = Math.E;  //2.71828...
}

28: Can constructor inherit?
Constructors are not inherited. They are called implicitly or explicitly by the child constructor. The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.
class A
{
public: 
    explicit A(int x) {}
};
 
class B: public A
{
public:
    explicit B(int x) : A(x) { }
};

29: What is function overloading and overriding?
Function overloading is same name function but different arguments. Function over riding means same name function and same as arguments.
30: What are abstract classes?
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. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
31: What’s an Interface?
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
32: What is difference between abstract classes and interfaces?

OOPs interface vs abstract class

Interface
Abstract class
Interface support multiple inheritance
Abstract class does not support multiple inheritance
Interface doesn’t Contains Data Member
Abstract class contains Data Member
Interface doesn’t contains Constructors
Abstract class contains Constructors
An interface Contains only incomplete member (signature of member)
An abstract class Contains both incomplete (abstract) and complete member
An interface cannot have access modifiers by default everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Member of interface cannot be Static
Only Complete Member of abstract class can be Static

33: Difference between Virtual / Abstract methods?
Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and forces the derived classes to override the method.
Abstract
Virtual
No Keyword
Can have implementation? 
No
Yes
Yes
Can override?
Must
Can but not a must
You can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete class?
override
override
No keyword needed
If an object is created of the base class type, which method will be executed?
Concrete implementation
The parent implementation will be called only if no implementation is provided in the concrete class
Parent implementation
If an object is created of the concrete class type, which method will be executed?
Concrete implementation
Concrete implementation
The parent implementation will be called only if no implementation is provided in the concrete class
34: Difference between Classes and Structs?
The struct default access type is public. A struct should typically be used for grouping data. The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data. In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance. (instances of classes) may be passed by reference and structures may be passed by value. Technically there are only two differences between classes and structures: classes are declared using the keyword class while structures are declared using the keyword struct.

35: What is Static Variables?
A variable that retains the same data throughout the execution of a program. In contrast, a dynamic variable can have different values during the course of a program.
36: What is Waterfall Model?
37: What is a delegate?
Delegate is a class that can hold a reference to a method or a function. Delegate class has a signature and it can only reference those methods whose signature is compliant with the class. Delegates are type-safe functions pointers or callbacks.
38: What are events?
As compares to delegates event works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to all of its listener when needed. One source can have multiple listeners.
39: Do events have return type?
No events do not have return type.
40: What does virtual keyword mean?
That method and property can be overridden.
41: What is ENUM?
It’s used to define constants.
42: What is Array List?
43: What are queues and stacks?
Queue is for first-in, first-out (FIFO) structures. Stack is for last-in, first-out (LIFO) structures.
44: What are static classes and variables and why we use?
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.
45: What is dataset?
Dataset is a collection of Data Tables. We use the Dataset type to store many Data Tables in a single collection. Conceptually, the Dataset acts as a set of Data Table instances. This simplifies programs that use many Data Tables.
46: What are the types of dataset?
There are 2 types of dataset available.
1.     Strongly Typed DataSets
2.     Annotating Typed DataSets              
47: What is MVC pattern?
The main purpose using MVC pattern is to decouple the GUI from the Data. It also gives the ability to provide multiple views for the same Data. MVC pattern separates objects in to three important sections:-
Model: - This section is specially for maintaining data. It is actually where your business logic, querying database, database connection etc. is actually implemented.
Views: - Displaying all or some portion of data, or probably different view of data. View is responsible for look and feel, Sorting, formatting etc.
Controller: - They are event handling section which affects either the model or the view. Controller responds to the mouse or keyboard input to command model and view to change. Controllers are associated with views. User interaction triggers the events to change the model, which in turn calls some methods of model to update its state to notify other registered views to refresh their display.
48: What is ADO.NET?
Microsoft ActiveX® Data Objects (ADO). ADO.NET is an object-oriented set of libraries that allows you to interact with data sources. Commonly, the data source is a database, but it could also be a text file, an Excel spreadsheet, or an XML file. ADO.NET defines Dataset and Data Table objects which are optimized for moving disconnected sets of data across intranets and Internets, including through firewalls.
49: What is normalization?

50: What is transitive dependency?
51: How to select data from database using ado.net? Write code
52: How to insert and update data from database using ado.net?
53: What is connection string?
The connection string contains the information that the provider needs to know to be able to establish a connection to the database or the data file.


56: Different Data Access Technologies in .NET?
v ADO.NET
v  ADO
v  OLE DB. 
57: What are indexers in c# (Types of indexes)?
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.
Types of Indexes:-

1)    Clustered Index (CI)

2)    Non-Clustered Index (NCI)

3)    Covering Index

4)    Unique Index


58: Difference between Primary key and unique key?
Primary Key:
i) Can be only one in a table
ii) It never allows null values
iii) Primary Key is unique key identifier and cannot be null and must be unique.
Unique Key:
i) Can be more than one unique key in one table.
ii) Unique key can have null values.
iii) It can’t be candidate key.
iv) Unique key can be null and may not be unique.

60: What is the difference between char, nchar, varchar, and nvarchar in SQL Server?
·        nchar and nvarchar can store Unicode characters.
·        char and varchar cannot store Unicode characters.
·        char and nchar are fixed-length which will reserve storage space for number of characters you specify even if you don't use up all that space.
·        varchar and nvarchar are variable-length which will only use up spaces for the characters you store. It will not reserve storage like char or nchar.
·        nchar and nvarchar will take up twice as much storage space, so it may be wise to use them only if you need Unicode support.

61: What is thread, background thread and foreground thread?
62: Why anonymous types are better than tuples?
63: What is the difference between convert.tostring and tostring ()?
Convert.ToString can handle null value. Object.ToString() will throw an error if the object is null. So its alway best to use Convert.Tostring() when you are converting reference type to string. Value type cannot be null so you can use .ToString() for value types.
64: What is circular dependency?
65: What is the difference between Service and Component?
A service can be made up of several components. Usually a service provides one complete feature that is made up by combining different components.
The service's user don't need to know anything about the underlying components. User will deal only directly with the service while service internally will be interacting with the components.





69: What are Generics?
Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.
70: Response. Redirect (False vs True)?
71: Prove that only 1 instance of the object is created for static classes?
72: What is Difference between == VS .Equals ()?
The equals ( ) method and the == operator perform two different operations. The equals ( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal.


75: What is the use of checked and unchecked keyword?
C# statements can execute in either checked or unchecked context. In a checked context, arithmetic overflow raises an exception. In an unchecked context, arithmetic overflow is ignored and the result is truncated.
·        checked   Specify checked context.
·        unchecked   Specify unchecked context.



78: Flow of web application in asp.net?
1:Page request.
2:Start
3: Initialization
4:Load
5: validation
6:Event handling 
7:Rendering
8:Unload

79: Difference b/w primary and unique key?

Primary Key


Unique Key


v  Primary key does not accept null values.
v  There is only one primary key in a table.
v  Clustered index is created in Primary key.
v  Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist.
v  Using Unique key, Null values are accepted.
v  More than one unique key can be defined  there in a table.
v  Non-Clustered index is created in unique key.
v   Unique key constraint is used to avoid the duplication of key values within the rows of a table.

80: Difference b/w primary & foreign key?

Primary Key


Foreign Key


v Primary key uniquely identify a record in the table.
v Primary Key can't accept null values.
v By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
v We can have only one Primary key in a table.
v Foreign key is a field in the table that is primary key in another table.
v Foreign key can accept multiple null value.
v Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
v We can have more than one foreign key in a table.

80: what is Get () and Post ()?
POST METHOD: The POST method generates a FORM collection, which is sent as a HTTP request body. All the values typed in the form will be stored in the FORM collection.
GET METHOD: The GET method sends information by appending it to the URL (with a question mark) and stored as A Query string collection. The Query string collection is passed to the server as name/value pair. The length of the URL should be less than 255 characters.
An HTTP GET is a request from the client to the server, asking for a resource.
An HTTP POST is an upload of data (form information, image data, whatever) from the client to the server.
 
81:  What are joins?
Joins are use in database to get the data from multiple table of database.
There are many types of joins.
1:Inner join
2:Outer join
3:Self join
82: What is Managed Code & Unmanaged Code?
Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a (hopefully!) secure framework which handles dangerous things like memory and threads for you. In modern usage this frequently means .NET but does not have to.
Unmanaged code is compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like .dlls
Native code is often synonymous with Unmanaged, but is not identical.

83:  what is Asp.net Session & View state?
84: Difference String and String Builder (important)?
String
String builder
v  It’s a class used to handle strings.
v  Here concatenation is used to combine two strings.
v  String object is used to concatenate two strings.
v  The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted
v  Strings are immutable. 
v  system. String is non-updatable. 
v  Slower
v  This is also the class used to handle strings.
v  Here Append method is used.
v  Here, Stringbuilder object is used.
v  Insertion is done on the existing string.
v  Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed.
v  system.stringbuilder is updatable. 
v  String builder is faster than the string object. 
v  String builder is mutable means we can able to re size the memory size 

85: Difference b/w sql server and Oracle?

86: What are the differences between pointer variable and reference variable in C++?


A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
Pointers can point nowhere (NULL), whereas reference always refer to an object.
You can't take the address of a reference like you can with pointers.
There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5)




1 comment: