Friday, April 10, 2009

C# interview questions Set 2



1. 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.
2. What does the keyword “virtual” declare for a method or property?
The method or property can be overridden.
3. 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.
4. Can you declare an override method to be static if the original method is non-static?
No. The signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
1. To do: Re-word the answer for better clarity.
5. Can you override private virtual methods?
No. Private methods are not accessible outside the class.
1. Original answer: No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
2. To do: Can a private method even be declared a virtual?
6. What are the different ways a method can be overloaded?
Different parameter data types, different number of parameters, different order of parameters.
7. If a base class has a number of overloaded constructors, and an inherited 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.
1. To do: question is to complex. It can be stated better.
8. Why is it a bad idea to throw your own exceptions?
Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block?
Throwing your own exceptions signifies some design flaws in the project.
9. What’s a delegate?
A delegate object encapsulates a reference to a method.
10. What’s a multicast delegate?
It’s a delegate that points to and eventually fires off several methods.
20. How do you inherit from a class in C#?
Place a colon and then the name of the base class.
21. Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.
22. 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.
23. 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.
24. When do you absolutely have to declare a class as abstract?
1. When at least one of the methods in the class is abstract.
2. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
25. What’s an interface class?
It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.
26. 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.
1. To do: Clean up this answer.
27. Can you inherit multiple interfaces?
Yes, why not.
1. To do: Need a better answer.
28. 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.
29. What’s the difference between an interface and abstract class?
In an interface class, all methods must be abstract. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed, which is ok in an abstract class.
30. 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. (questions courtesy of Eyal)

No comments: