Thursday, September 6, 2012

OOPs Reasoning FAQs-2

OOPs Reasoning FAQs-2

S.No
Questions
Answers
1
Can you create an instance of a static class ?
No, we cannot create an instance of a static class.
2
Are Value types sealed ?


Yes, Value types are sealed.
3
Can a static class contain non static members?
No, a static class can contain only static members
4
Can a struct inherit from another struct or class in C#?
No, a struct cannot inherit from another struct or class, and it cannot be the base of a class.
5
Can you instantiate a struct without using a new operator in C#?
Yes, we can instantiate a struct without using a new operator
6
Can a struct have a default constructor (a constructor without parameters) or a destructor in C#?
No
7
Do structs support inheritance?
No, structs do not support inheritance, but they can implement interfaces.
8
Can a sealed class be used as a base class?
No, sealed class cannot be used as a base class. A compile time error will be generated.
9
Can you create an instance of an abstract class?
No, abstract classes are incomplete and you cannot create an instance of an abstract class.
10
Can destructors have access modifiers?
No, destructors cannot have access modifiers.

OOPs Reasoning FAQs-1

OOPs Reasoning FAQs-1

S.No
Questions
Answers
1
Does c# support multiple-inheritance ?
No. Multiple inheritance is not possible in .NET. This means it is not possible for one class to inherit from multiple classes. However, a class may implement multiple interfaces. We may also declare objects of different classes in a class. This way, the encapsulated class may be instantiated in other classes.
2
Do events have return type ?
Events do not have return type.
3
Can you override private virtual methods ?
No, we cannot access private methods in inherited classes.
4
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.


5
Can we have shared events ?


Yes, we can have shared event’s note only shared methods can raise shared events.
6
Can you write a class without specifying namespace ? which namespace does it belong to by default ?


Yes, we can, then the class belongs to global namespace which has no name. For commercial products, naturally, we would not want global namespace.
7
Can you prevent a class from overriding ?
Yes.If we define a class as “Sealed” in C# and “NotInheritable” in VB.NET we can not
inherit the class any further.
8
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.
9
Can you declare the override method static while the original method is non-static?


No, we can't, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.


10
Can a constructors that is declared within a base class, inherited by subclasses ?


No, constructors that are declared within a base class cannot be inherited by sub-classes. If we have not declared any constructor in our class then a default constructor is provided.