C# interview questions and answers

  1. What’s the implicit name of the parameter that gets passed into the class’ set method? Value, and it’s datatype depends on whatever variable we’re changing.
  2. How do you inherit from a class in C#? Place a colon and then the name of the base class.
  3. Does C# support multiple inheritance? No, use interfaces instead.
  4. When you inherit a protected class-level variable, who is it available to? Classes in the same namespace.
  5. Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
  6. Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).
  7. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.
  8. What’s the top .NET class that everything is derived from? System.Object.
  9. How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.
  10. What does the keyword virtual mean in the method definition? The method can be over-ridden.
  11. Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
  12. Can you override private virtual methods? No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
  13. Can you prevent your class from being inherited and becoming a base class for some other classes? Yes, that’s what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It’s the same concept as final class in Java.
  14. Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed.
  15. What’s an abstract class? A class that cannot be instantiated. A concept in C++ known as pure virtual method. A class that must be inherited and have the methods over-ridden. Essentially, it’s a blueprint for a class without any implementation.
  16. When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
  17. What’s an interface class? It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.
  18. Why can’t you specify the accessibility modifier for methods inside the interface? They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.
  19. Can you inherit multiple interfaces? Yes, why not.
  20. And if 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.
  21. What’s the difference between an interface and abstract class? In the interface all methods must be abstract, in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
  22. How can you overload a method? Different parameter data types, different number of parameters, different order of parameters.
  23. If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary 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.
  24. What’s the difference between System.String and System.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.
  25. Is it namespace class or class namespace? The .NET class library is organized into namespaces. Each namespace contains a functionally related group of classes so natural namespace comes first.
This entry was posted in .NET. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

40 Comments on C# interview questions and answers

  1. Posted 2/9/2004 at 6:07 pm | Permalink

    Regarding #4 . “When you inherit a protected class-level variable, who is it available to? Classes in the same namespace. ”

    Wrong, the protected keyword in C# specifies that only classes that ultimately inherit from the class with a protected member can see that member.

    Quote from the C# Programmer’s Reference (MSDN):
    “protected - Access is limited to the containing class or types derived from the containing class.”

  2. Posted 2/9/2004 at 6:12 pm | Permalink

    Again in #15 “What’s an abstract class? … Essentially, it’s a blueprint for a class without any implementation.”

    Although an abstract class does not require implementations (it’s methods can be abstract) it *can* also offer implementations of methods (either virtual or not) which can be called in implementing classes.

  3. Posted 2/9/2004 at 6:14 pm | Permalink

    Right you are, Richard. Thanks, I will update.

  4. Posted 2/9/2004 at 6:16 pm | Permalink

    #18 is misleading. You CAN implement an interface explicitly without the members being declared public in your class. The difference is that if the members are not public, they will only be available when you have a reference variable of the type of the interface pointing to you class

    You implement them like so:
    void IDisposable.Dispose()
    {
    // TODO: Add Class1.Dispose implementation
    }

  5. Posted 2/9/2004 at 6:18 pm | Permalink

    Oh hi, I hope my comments were helpful - I’m not just some C# facist :) I would just hate to see a right answer disqualify a good candidate!

  6. Posted 2/9/2004 at 6:20 pm | Permalink

    #18

    If the members of the interface are not public, then even if you have an instance of that, how would you implement/override non-public members if you don’t have any access to them?

  7. Posted 2/9/2004 at 6:22 pm | Permalink

    Yeah, no problem, I always welcome suggestions and corrections. Especially this set of questions which I wrote myself, by taking notes from Microsoft’s Self-Training MCSD cert kit. Your comments make me go back to the books and online documentation. #4 and #15 I totally agree. #18 I will double check :-)

  8. Posted 2/9/2004 at 7:56 pm | Permalink

    “If the members of the interface are not public, then even if you have an instance of that, how would you implement/override non-public members if you don’t have any access to them?”

    Ah, you meant “public to references to the *interface*” so of course they must be publically visible. I read the original question as meaning “public to references to the class” which isn’t a necessity. That was my mis-read, sorry.

  9. Andrew O
    Posted 2/17/2004 at 11:26 am | Permalink

    Is it namespace class or class namespace???

    The .NET class library is organized into namespaces. Each namespace contains a functionally related group of classes so natural namespace comes first.

  10. Andrew O
    Posted 2/17/2004 at 11:27 am | Permalink

    correction: “so natural namespace comes first” should be “so naturally namespace comes first”

  11. Kishore
    Posted 2/24/2004 at 11:00 am | Permalink

    for question 7,
    C# does’t cancel the freebie constructor(parameter less constuctor).
    it is goting to keep the parameter less constructor, even if you add any number of parameterised constructors.

  12. MBearden
    Posted 3/24/2004 at 8:11 am | Permalink

    I believe that Kishore is wrong in comment number 11.
    C# behaves this way so that you can specify a class has
    *no* parameterless constructor. If Kishore were correct,
    then it would be impossible to implement a class without
    a parameterless (”default”) constructor.

  13. MBearden
    Posted 3/24/2004 at 8:13 am | Permalink

    An interesting (and difficult) question to add would be:
    “How do you specify constructors in an interface?” Answer is
    “You cannot. C# does not allow constructor signatures to
    be specified in interface.” I don’t know the reason why…
    If anyone reading this does, could you e-mail with your
    comments on why C# was designed this way? Just curious.

  14. ram
    Posted 4/28/2004 at 10:10 am | Permalink

    For Question 13:
    As far I know interfaces doesn’t have data members(fields), they will only have methods or properties, so no need for constructors. I am also new to C# so I dont know whether I am completely correct or not.

  15. Rad
    Posted 5/24/2004 at 6:59 pm | Permalink

    Follow up for #13 & #17
    Ram, you are right. An instance of an interface cannot be created. (But the interface methods can be accessed in a class implementing that interface, by casting the instance of that class to the interface type. This is sometimes referred to as creating an instance of the interface.) “The job of a constructor is to create the object specified by a class and to put it into a valid state.”[Programming C#-Jesse Liberty]. So, no need of a constructor.

    Use of property in an interface:
    An interface can have methods, properties, events and indexers. When a property in an interface is implemented in a class,
    the advantage is that, through that property, the class can give access to its private member varibles.

    Hope Iam clear.
    Thanks,RAD

  16. Tim Haughton
    Posted 10/13/2004 at 10:44 am | Permalink

    Thought I’d offer a few points on your list of questions.

    1) This question would be better stated “What is the implicit name of the parameter passed into a property’s ’set’ method?”

    2) It’s a good while since I’ve had to do any C++, but my long term memory tells me that it isn’t double colon in C++. Perhaps you’re confusing the scope resolution operator?

    3) A better answer would be that C# disallows multiple inheritance of implementation, but allows classes to implement multiple interfaces.

    4) This is just wrong. Protected members are visible only to classes that are derived from the containing class.

    15) An abstract class in C++ is a class that contains at least 1 pure virtual method.

  17. Kiran Mandhadi
    Posted 10/13/2004 at 12:51 pm | Permalink

    can anyone explain/differentiate between different database concepts(odbc,ole-db,ado.net, ado,dao,rao)?

  18. Binoj Antony
    Posted 10/27/2004 at 8:55 am | Permalink

    Here are more questions

    1. What does the readonly keyword do for a variable in c#? (hint on Question 4)

    2. Can we overload methods by specifying different return types?

    3. Can you can specify values for for variables in interfaces in c#? (In Java you can)

    4. What is the difference between const and readonly ?

    Have Fun :)

  19. Kiran Mandhadi
    Posted 11/2/2004 at 1:29 am | Permalink

    Answers to Binoj’s Questions:

    What does the readonly keyword do for a variable in c#? What is the difference between const and readonly ?
    Answer) To declare constants in C# the const keyword is used for compile time constants while the readonly keyword is used for runtime constants

    Can we overload methods by specifying different return types?
    Answer) NO. I tried it in VS.net

    Can you can specify values for for variables in interfaces in c#? (In Java you can)
    Answer) Yes! I am not sure of this answer, correct me if I am wrong.

    Would appreciate if anybody else could post more messages here
    Thanks, Kiran.

  20. Kiran Mandhadi
    Posted 11/2/2004 at 1:33 am | Permalink

    Can you can specify values for for variables in interfaces in c#? (In Java you can)
    Answer) NO. I tried this in VS.net and it gave me the following error:
    D:\VisualStudio\…\Interface.cs(10): Interfaces cannot contain fields

  21. Laura Hunt
    Posted 11/30/2005 at 1:57 pm | Permalink

    #12 The .Net compiler does not accept a virtual private method which makes sense since a virtual private method makes no sense.

    Thanks,
    Laura

  22. Ike
    Posted 12/12/2005 at 11:12 am | Permalink

    Another response to #13:

    Since you constructors are not allowed in an interface, how would you be able to use an interface as a type parameter when the type parameter is constrained by new()?

  23. surendra
    Posted 1/13/2006 at 1:23 pm | Permalink

    This is regarding question #5, if u can’t access a member in derived class, then how do we say it is inherited.To my knowledge, the fundamental concept behind inheritance is representing the parent, if a child con’t access then we can say its not inherited.Please correct me if am wrong.

  24. Andy
    Posted 3/4/2006 at 3:39 pm | Permalink

    here’s a question I was asked at an interview:

    What methods are called when a windows app is run and in what order are they called?

    1. main
    2. an instance of the form
    3. form load
    4. Initialize component
    5. Dispose

    what am I forgetting?

  25. David
    Posted 3/6/2006 at 7:22 pm | Permalink

    “Another response to #13:

    Since you constructors are not allowed in an interface, how would you be able to use an interface as a type parameter when the type parameter is constrained by new()?”

    You can’t. But, if the generic class requires the type to have a default constructor, that means it plans on creating an instance of that type at some point; and since you cant create an instance of an interface, it wouldnt make any sense to use an interface as the type parameter.

    “This is regarding question #5, if u can’t access a member in derived class, then how do we say it is inherited.To my knowledge, the fundamental concept behind inheritance is representing the parent, if a child con’t access then we can say its not inherited.Please correct me if am wrong.”

    Technically, saying that a particular item is inherited means that it still exists in the derived class. In .NET this is always the case: if I have a private field in my base class, then when I instantiate my derived class, space is reserved on the heap for the private field, even though my derived class cannot access it directly. This is somewhat confusing, and makes for a bad interview question, because it is not clear whether the interviewer is asking if the private member still exists in the derived class, or if the derived class can access the private member.

  26. Jimbobob
    Posted 5/30/2006 at 8:54 pm | Permalink

    with regards to question #7:
    The default (parameterless) constructor is not implicitly canceled by C# simply by virtue of you writing one that requires a parameter. Though for ease of use you SHOULD implement default behavior and initialisations in the default constructor, unless parameters are absolutely required for initialisation. Which would only be necessary in the event of a lack of get/set properties for private/protected member variables.

    hope that makes sense.

    Jimbobob

  27. Jimbobob
    Posted 5/30/2006 at 8:55 pm | Permalink

    oh yeah, and unless you intentionally erase the default consturctor in the code, it’ll stay there and stay functional.

  28. Phil Curran
    Posted 7/21/2006 at 8:24 am | Permalink

    On question 14, it should be noted that only overridden methods can be sealed

  29. varaprasad
    Posted 9/14/2006 at 2:34 am | Permalink

    Can a constructor be overrided? If so how? give that with an Example

  30. Essam
    Posted 10/10/2006 at 6:36 am | Permalink

    can someone send an example for using protected member , in the derived class

  31. Justin
    Posted 11/2/2006 at 2:14 am | Permalink

    Question 4 is still wrong,
    In regard to 7, yes, the default constructor, if it hasn’t been explicitly written into the class (ie you can’t see it) will be rendered inaccessible if you implement a constructor without a parameter.

    It’s very easy to test, just create this class:
    public class ConstructorTest{public ConstructorTest(int x){}}
    and try to create an instance with ConstructorTest = new ConstructorTest();
    It’ll fail to compile.

    Technically, with components the constructor is still there, because components that inherit from this one will still access it by default when they’re created (so they can call initializeComponent) but that’s a stretch.

  32. Sivakumar
    Posted 12/27/2006 at 6:13 am | Permalink

    1. How many constructors can we use?
    2. What is the difference between Varchar and Varchar2

  33. Suyash
    Posted 1/3/2007 at 4:47 am | Permalink

    To answer Andy’s Question:

    >>Andy said,
    >>here’s a question I was asked at an interview:

    >>What methods are called when a windows app is run >>and in what order are they called?

    >>1. main
    >>2. an instance of the form
    >>3. form load
    >>4. Initialize component
    >>5. Dispose

    >>what am I forgetting?

    I guess the correct sequence is:

    1. Program.Main() method which in turn calls these methods Application.EnableVisualStyles(), Application.SetCompatibleTextRenderingDefault , and Application.Run(new MainForm())
    2. MainForm()i.e. the default constructor which in turn calls the InitializeComponent() method
    3. MainForm’s Form Load method, if any

    Please correct if required!

  34. manas
    Posted 2/15/2007 at 7:42 am | Permalink

    Q-Can we overload methods by specifying different return types?
    Ans-Yes,We can overload methods with different return types.

  35. Hsen Ho
    Posted 2/21/2007 at 4:41 am | Permalink

    How to run from all these Microsoft Visual C# 2005 Express Edition
    Everything cannot how to RUN from C# Recent Projects(all cannot to write/read). HELP ME!
    How to get for C# Recent Projects working!!??
    Cannot how to work new project but the name is only from Recent Project
    How can to start C# Recent Project from Start Page.
    Only Start Page - Recent Project, Getting Started, Visual C# Developer News, but cannot work anything C#.
    All Recent Projects cannot to work, cannot run get MainClass & anything!!

  36. Doug
    Posted 2/21/2007 at 1:06 pm | Permalink

    Manas,

    You are incorrect. The question “Can we overload methods by specifiying different return types?” implies that ALL you’re changing is the return type–not parameters, etc.

    This is possible:

    public string SimpleTest() { /* code */ }
    public int SimpleTest(object arg1) { /* code */ }

    This is NOT:

    public string SimpleTest() { /* code */ }
    public int SimpleTest() { /* code */ }

  37. Tamilmaran
    Posted 12/6/2007 at 11:41 pm | Permalink

    *@#$* ANSWER ME IF YOU CAN *$#@*
    Which one takes more time?
    1)int a,b;
    a+b;

    2)float a,b;
    a+b;

    3)int a;
    float b;
    a+b;

    Qn:In the above three cases which ADDITION OPERATION takes more time than others?
    and tell me the correct reason

  38. Posted 12/7/2007 at 1:52 pm | Permalink

    @Tamilmaran:

    It depends on what you mean by “addition operation”. The third choice requires a cast from int to float, so the expression “a+b” in 3 will take more time than the same in 2. But if you are talking about the IL (or assembly) level, the cast has already occurred prior to the actual addition operation, so in that sense the addition operation of 2 and 3 will take the same amount of time.

    1 takes less time than both 2 and 3 no matter which way you look at it.

  39. sam
    Posted 1/7/2008 at 6:09 am | Permalink

    Regarding interfaces -
    1. Interface itself can have any of the 5 access modifiers
    2. No modifier is allowed on interface members - it is implicitly public. The reason is - the basic concept of interface is a contract of the class with its consumers about the basic services it is going to provide. So all members have to be public, and in the class also they have to be implemented as public. It is a compiler error to use other access specifiers for the interface member implementations.
    3. Constructors are not allowed since an interface is not meant to be instantiated. Also, constructor is meant to initialize the fields (data members) of the class - the interface cannot declare fields - so naturally it also cannot declare constructors.
    4. In short interface does not deal with anything which is ’stored’ - which is data for the class, and hence it does not deal with the initialization of the data. It talks about services to be offered to the consumers of the class.

    The following answers are wrong (or partially wrong)
    1. Can you override private virtual methods? No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
    Correct answer is - It is a compilation error to declare a private method / property to be abstract or virtual.

    2. Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed.
    Correct answer is - You CANNOT declare a method to be sealed, unless you are overriding a base class method. ’sealed’ keyword is only allowed along with ‘override’ keyword. A topmost base class cannot declare a method as sealed.

  40. Mohit Jethva
    Posted 3/25/2008 at 10:38 am | Permalink

    Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed. For this question which answer you given that is wrong can you plz check it !

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*