Showing posts with label C# Interview questions. Show all posts
Showing posts with label C# Interview questions. Show all posts

C# Interview questions

General Questions:
  • Does C# support multiple-inheritance? No.
  • Who is a protected class-level variable available to? It is available to any sub-class (a class inheriting this class).
  • Are private class-level variables inherited? Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.
  • Describe the accessibility modifier “protected internal”. It is available to classes that are within the same assembly and derived from the specified base class.
  • What’s the top .NET class that everything is derived from? System.Object.
  • What does the term immutable mean?The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.
  • What’s the difference between System.String and System.Text.StringBuilder classes?System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
  • What’s the advantage of using System.Text.StringBuilder over System.String?StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.
  • Can you store multiple data types in System.Array?No.
  • What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.
  • How can you sort the elements of the array in descending order?By calling Sort() and then Reverse() methods.
  • What’s the .NET collection class that allows an element to be accessed using a unique key?HashTable.
  • What class is underneath the SortedList class?A sorted HashTable.
  • Will the finally block get executed if an exception has not occurred?­Yes.
  • What’s the C# syntax to catch any possible exception?A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.
  • Can multiple catch blocks be executed for a single try statement?No. Once the proper catch block processed, control is transferred to the finally block (if there are any).
    Explain the three services model commonly know as a three-tier application.Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources).
    Class Questions
  • What is the syntax to inherit from a class in C#? Place a colon and then the name of the base class.Example: class MyNewClass : MyBaseClass
  • Can you prevent your class from being inherited by another class? Yes. The keyword “sealed” will prevent the class from being inherited.
  • Can you allow a class to be inherited, but prevent the method from being over-ridden?Yes. Just leave the class public and make the method sealed.
  • What’s an abstract class?A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.
  • When do you absolutely have to declare a class as abstract?1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden. 2. When at least one of the methods in the class is abstract.
  • What is an interface class?Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.
  • Why can’t you specify the accessibility modifier for methods inside the interface?They all must be public, and are therefore public by default.
  • Can you inherit multiple interfaces?Yes. .NET does support multiple interfaces.
  • What happens if you inherit multiple interfaces and they have conflicting method names?It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay. To Do: Investigate
  • What’s the difference between an interface and abstract class?In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers. What is the difference between a Struct and a Class?Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that structs cannot inherit.

Method and Property Questions :

  • What’s the implicit name of the parameter that gets passed into the set method/property of a class? Value. The data type of the value parameter is defined by whatever data type the property is declared as.
  • What does the keyword “virtual” declare for a method or property? The method or property can be overridden.
  • How is method overriding different from method overloading? 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.
  • Can you declare an override method to be static if the original method is not static? No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override)
  • What are the different ways a method can be overloaded? Different parameter data types, different number of parameters, different order of parameters.
  • If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific base constructor?Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

Events and Delegates:

  • What’s a delegate? A delegate object encapsulates a reference to a method.
  • What’s a multicast delegate? A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.




.Net Interview Questions

     ASP.Net Questions

Q. Explain the differences between Server-side and Client-side code?
A. Server-side code executes on the server. Client-side code executes in
     the context of the clients' browser.

Q. What are some ways to manage state in an ASP.Net application?
A. Session objects, Application objects, ViewState, cookies, hidden form fields.

Q. What does the "EnableViewState" property do? Why would I want it on or off?
A. It allows page objects to save their state in a Base64 encoded string in the page
     HTML. One should only have it enabled when needed because it adds to the
     page size and can get fairly large for complex pages with many controls.
    (It takes longer to download the page).

Q. What is the difference between Server.Transfer and Response.Redirect? Why
      would I choose one over the other?
A. Server.Transfer transfers excution directly to another page. Response.Redirect
     sends a response to the client and directs the client (the browser) to load the
    new page (it causes a roundtrip). If you don't need to execute code on the client,
     Transfer is more efficient.

Q. How can I maintain Session state in a Web Farm or Web Garden?
A. Use a State Server or SQL Server to store the session state.

Q. What base class do all Web Forms inherit from?
A. The Page class.

Q. What does WSDL stand for? What does it do?
A.(Web Services Description Language). It describes the interfaces and
     other information of a web service.

Q. Which WebForm Validator control would you use if you needed to make
      sure the values in two different WebForm controls matched?
A. CompareValidator Control

Q. What property must you set, and what method must you call in your code,
      in order to bind the data from some data source to the Repeater control?
A. You must set the DataSource property and call the DataBind method.


     C# Questions

Q. Can you explain what inheritance is and an example of when you might use it?
A. Inheritance allows us to extend the functionality of a base class. It is an "Is a"
     type of relationship rather than a "Uses" type of relationship (a dalmation IS A
     dog which IS A canine which IS A mammal - dalmations inherist from dog which
     inherits from canine which inherits from mammal). All child classes retain the
     properties and methods of their parent classes but may override them. When you
     want to inherit (use the functionality of) another class. Base Class Employee.
     A Manager class could be derived from the Employee base class.

Q. Does C# support multiple-inheritance?
A. No, use interfaces instead.

Q. Can you prevent your class from being inherited by another class?
A. Yes. The keyword “sealed” will prevent the class from being inherited.

Q. What does the keyword “virtual” declare for a method or property?
A. The method or property can be overridden.

Q. What's the top .NET class that everything is derived from?
A. System.Object.

Q. What does it mean that a String is immutable?
A. Strings cannot be altered. When you alter a string (by adding to it for example),
     you are actually creating a new string.

Q. If I have to alter a string many times, such as mutliple concatenations,
      what class should I use?
A. StringBuilder. It is not immutable and is very efficient.

Q. In a Try - Catch - Finally block, will the finally block execute if an exception
      has not occurred? If an Exception has occurred?
A. Yes and yes.

Q. Whats MSIL, and why should developers need an appreciation of it, if at all?
A. MSIL is the Microsoft Intermediate Language. All .NET compatible
      languages will get converted to MSIL.

Q. Explain the three tier or n-Tier model.
A. Presentation (UI), business (logic and underlying code) and data
     (from storage or other sources).

Q. What is SOA?
A. Service Oriented Architecture. In SOA you create an abstract layer that
     your applications use to access various "services" and can aggregate the
     services. These services could be databases, web services, message queues
     or other sources. The Service Layer provides a way to access these services
     that the applications do not need to know how the access is done. For example,
     to get a full customer record, I might need to get data from a SGL Server
     database, a web service and a message queue. The Service layer hides this from
     the calling application. All the application knows is that it asked for a full
     customer record. It doesn't know what system or systems it came from or
      how it was retrieved.

Q. What is the role of the Data Reader class in ADO.NET connections?
A. It returns a forward-only, read-only view of data from the data source when
     the command is executed.

Q. Is XML case-sensitive?
A. Yes.

Q. What is the CLR?
A. Common Language Runtime

Q. Can you explain some differences between an ADO.NET Dataset and
      an ADO Recordset? (Or describe some features of a Dataset).
A. A DataSet can represent an entire relational database in memory,
    complete with tables, relations, and views. A DataSet is designed to
    work without any continuing connection to the original data source. Data in
    a DataSet is bulk-loaded, rather than being loaded on demand. There's no
    concept of cursor types in a DataSet. DataSets have no current record pointer
    You can use For Each loops to move through the data. You can store many
    edits in a DataSet, and write them to the original data source in a single operation.
    Though the DataSet is universal, other objects in ADO.NET come in different
    versions for different data sources

Q. Name some of the Microsoft Application Blocks. Have you used any?
     Which ones?
A. Examples:
     Exception Management
     Logging
     Data Access
     User Interface
     Caching Application Block for .NET
     Asynchronous Invocation Application Block for .NET
     Configuration Management Application Block for .NET
     (there are others) We use Exception and Data Access