Showing posts with label multiple inheritance. Show all posts
Showing posts with label multiple inheritance. Show all posts

Thursday, April 2, 2009

OOPS Concepts Interview Questions Part I

1. What are the OOPS concepts?

Ans. 1) Encapsulation: It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data.
2) Inheritance: It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.
3) Polymorphism: It is a feature that allows one interface to be used for general class of actions. The specific action is determined by the exact nature of the situation. In general polymorphism means "one interface, multiple methods", This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler's job to select the specific action (that is, method) as it applies to each situation.

2. What is the difference between a Struct and a Class?

Ans. The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.
· When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
· It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.
· It is an error to initialize an instance field in a struct.
· There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.
· A struct is a value type, while a class is a reference type.

3. Value type & reference types difference? Example from .NET. Integer & struct are value types or reference types in .NET?

Ans. Most programming languages provide built-in data types, such as integers and floating-point numbers, that are copied when they are passed as arguments (that is, they are passed by value). In the .NET Framework, these are called value types. The runtime supports two kinds of value types:
· Built-in value types
The .NET Framework defines built-in value types, such as System.Int32 and System.Boolean, which correspond and are identical to primitive data types used by programming languages.
· User-defined value types
Your language will provide ways to define your own value types, which derive from System.ValueType. If you want to define a type representing a value that is small, such as a complex number (using two floating-point numbers), you might choose to define it as a value type because you can pass the value type efficiently by value. If the type you are defining would be more efficiently passed by reference, you should define it as a class instead.
Variables of reference types, referred to as objects, store references to the actual data. This following are the reference types:
· class
· interface
· delegate
This following are the built-in reference types:
· object
· string

4. What is Inheritance, Multiple Inheritance, Shared and Repeatable Inheritance?

5. What is Method overloading?

Ans. Method overloading occurs when a class contains two methods with the same name, but different signatures.

6. What is Method Overriding? How to override a function in C#?

Ans. Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

7. Can we call a base class method without creating instance?

Ans. Its possible If its a static method.
Its possible by inheriting from that class also.
Its possible from derived classes using base keyword.

8. You have one base class virtual function how will call that function from derived class?

Ans:
class a
{
public virtual int m()
{
return 1;
}
}
class b:a
{
public int j()
{
return m();
}
}
9. In which cases you use override and new base?

Ans. Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

c# Interview Questions Part II

11. Can you declare the override method static while the original method is non-static?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. Yes, just leave the class public and make the method sealed.

15. What’s an abstract class?

Ans. 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)?

Ans. 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?

Ans. 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?

Ans . 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?

Ans. Yes, why not.

20. And if they have conflicting method names?

Ans. 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.

C# Interview Questions Part I

1. What’s the implicit name of the parameter that gets passed into the class’ set method?

Ans. Value, and its datatype depends on whatever variable we’re changing.

2. How do you inherit from a class in C#?

Ans. Place a colon and then the name of the base class. Notice that it’s double colon in C++.

3. Does C# support multiple inheritance?

Ans. No, use interfaces instead.

4. When you inherit a protected class-level variable, who is it available to?

Ans. Classes in the same namespace.

5. Are private class-level variables inherited?

Ans. 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.

Ans. 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?

Ans 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?

Ans System.Object.

9. How’s method overriding different from overloading?

Ans. 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?

Ans The method can be over-ridden.

Friday, March 27, 2009

OOPs Interview Questions Part IV

Question: What is an interface? How to implement an interface? 
Answer: An Interface is a collection of semantically related abstract members. An interface expresses through the members it defines, the behaviors that a class needs to support. An interface is defined using the keyword interface. The members defined in an interface contain only definition, no implementation. The members of an interface are all public by default, any other access specifier cannot be used. 

An interface is implemented using implements keyword. A class may implement more than one keyword. 

When a class inherits and implements at the same time, the inherited parent class name is written first, followed by the names of the interfaces to be implemented. 

Question: Is multiple inheritance possible in .NET? 
Answer: 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.