Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

OOPS Interview Question With Answer--C#

enforce a call from an inherited constructor to an arbitrary base constructor?

Q) 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.

What do you understand by the term "immutable"?

Immutable means you can't change the currrent data,but if you perform some operation on that data, a new copy is created. The operation doesn't change the data itself.

For example let's say you have a string object having "hello" value. Now if you say
temp = temp + "new value" new object is created, and values is saved in that. The temp object is immutable, can't be changed. An object qualifies as being called immutable if its value cannot be modified once it has been created.

For example, methods that appear to modify a String actually return a new String containing the modification. Developers are modifying strings all the time in their code. This may appear to the developer as mutable - but it is not.

What actually happens is your string variable/object has been changed to reference a new string value containing the results of your new string value. For this very reason .NET has the System.Text.StringBuilder class.

If you find it necessary to modify the actual contents of a string-like object heavily, such as in a for or foreach loop, use the System.Text.StringBuilder class.

What is the difference between abstract class and an interface?

An abstract class and Interface both have method only but not have body of method.The difference between Abstract class and An Interface is that if u call Abstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not necessary that u call all method of that particular interface.Method Over Loading:-Return type, Parameter type, parameter and body of method number may be different.Method Overriding:- Return type, Parameter type, Parameter Number all must be same . Only body of method can change.

Note : This is one of the most commonly asked question in Interviews

When should a struct be used instead of a class?

They should be used for types that contain just a few data members..

How do class property members work in C#?

A property may have a get and/or a set associated with it. Within a set accessor block, C# automatically provides a variable called 'value' which holds the new value to which the property can be set.

A property with a get and set accessor for the int variable age would be defined as follows:

To set the value of age in object instance p to the new value of 34, :

public int Age

{
get { return age; }
set { age = value; }
}

What is the difference between an event and a delegate?

An event is just a wrapper for a multicast delegate. Adding a public event to a class is almost the same as adding a public multicast delegate field. In both cases, subscriber objects can register for notifications, and in both cases the publisher object can send notifications to the subscribers.

However, a public multicast delegate has the undesirable property that external objects can invoke the delegate, something we'd normally want to restrict to the publisher. Hence events - an event adds public methods to the containing class to add and remove receivers, but does not make the invocation mechanism public.

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 prevent a class from overriding ?

Yes, by defining a class as “Sealed” in C# and “NotInheritable” in VB.Net we can prevent a class from overriding.

More discussions related to this at http://www.dotnetinterviews.co.nr

What’s the difference between const and readonly?

You can initialize readonly variables to some runtime values. Let’s say your program uses current date and time as one of the values that won’t change. This way you declarepublic readonly string

DateT = new DateTime().ToString().

What is the difference between the value-type variables and reference-type variables in terms of garbage collection?

The value-type variables are not garbage-collected, they just fall off the stack when they fall out of scope, the reference-type objects are picked up by GC when their references go null.

Does Main() always have to be public in C#?

No not necessarily

More discussions related to this at http://www.dotnetinterviews.co.nr/

What are valid signatures for the Main function in C#?

1) public static void Main()
2) public static int Main()
3) public static void Main( string[] args )
4) public static int Main(string[] args )

What’s the difference between struct and class in C#?

1 Structs cannot be inherited.
2 Structs are passed by value, not by reference.
3 Struct is stored on the stack, not the heap.

What’s a multicast delegate?

A multicast delegate is a delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.

What are sealed classes

Classes marked sealed cannot be inherited further. The keyword “sealed” will prevent the class from being inherited.

When do you use virutal keyword?

We use virtual when we need to override a method of the base class from the sub class, then we give the virtual keyword in the base class method. This makes the method in the base class to be overridable.

What is encapsulation ? Give an example?

Encapsulation is the ability to hide the internal workings of an object's behavior and its data.

For instance, let's say we have a object named Car and this object has a method named run(). When you create an instance of a Car object and call its run() method you are not worried about what happens to accomplish this, you just want to make sure the state of the Car is changed to 'running' afterwards.

This kind of behavior hiding is encapsulation and it makes programming much easier.

What is the Difference between Int and Int32 in .Net?

There is no difference in these two.

Is it possible to use multipe inheritance in C# / VB.Net?

Multiple Inheritance is an ability to inherit from more than one base class i.e. ability of a class to have more than one superclass, by inheriting from different sources and thus combine separately-defined behaviors in a single class.

There are two types of multiple inheritance: multiple type/interface inheritance and multiple implementation inheritance.

C# & VB.NET supports only multiple type/interface inheritance, i.e.you can derive an class/interface from multiple interfaces.

There is no support for multiple implementation inheritance in .NET. That means a class can only derived from one class.

What is the difference between structures and enumeration?

Unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data. They are derived from System.ValueType class.

Enum->An enum type is a distinct type that declares a set of named constants.They are strongly typed constants. They are unique types that allow to declare symbolic names to integral values. Enums are value types, which means they contain their own value, can't inherit or be inherited from and assignment copies the value of one enum to another.
public enum Levels
{
One, Two, Three
}

.NET C# OOPS

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.

Core Java and OOP's Interview questions

1. What is JVM (Java Virtual Machine)?
2. What is JIT (Just-in-Time) Compilation?
3. What is Object Oriented Programming?
4. What’s a Class?
5. What’s an Object?
6. What’s the relation between Classes and Objects?
7. What are different properties provided by Object-oriented systems?
8. How can we implement polymorphism in Java?
9. What’s an interface and how will you go about implementing an interface?
10. What is an Abstract class?
11. What are Abstract methods?
12. What’s the difference between “Abstract” classes and “Interfaces”?
13. What’s difference between Static and Non-Static fields of a class?
14. What are inner classes and what’s the practical implementation of inner classes?
15. What are packages?
16. What is a constructor in class?
17. Can constructors be parameterized?
18. What is the use if “instanceof” keyword?
19. What are Native methods in Java?
20. How do refer to a current instance of object?
21. Explain in depth Garbage collector?
22. How does the garbage collector determine that the object has to be marked for deletion?
23. Can you explain “finalize()” method?
24. How can we force the garbage collector to run?
25. What’s the main difference between “Switch” and “If” comparison?
26. What’s the use of JAVAP tool?
27. What are applets?
28. In which package is the applet class located?
29. What are native interfaces in Java?
30. what are Class loader’s?
31. what is Bootstrap, Extension and System Class loader?
32. Can you explain the flow between bootstrap, extension and system class loader?
33. Can you explain how can you practically do dynamic loading?
34. What is Reflection API in Java ?
35. What’s the difference between static and dynamic class loading ?
36. How can you copy one array in to a different array?
37. Can you explain the core collection interfaces?
38. What’s the difference between standard JAVA array and ArrayList class?
39. What’s the use of “ensureCapacity” in ArrayList class?
40. How can we obtain an array from an ArrayList class?
41. What is “LinkedList” class for?
42. Can you explain HashSet class in collections?
43. what is LinkedHashSet class?
44. what is a TreeSet class?
45. what’s the use of Comparator Interface?
46. How can we access elements of a collection?
47. What is Map and SortedMap Interface?
48. Have you used any collection algorithm?
49. Why do we use collections when we had traditional ways for collection?
50. Can you name the legacy classes and interface for collections?
51. What is Enumeration Interface?
52. what’s the main difference between ArrayList / HashMap and Vector / Hashtable?
53. Are String object Immutable, Can you explain the concept?
54. what is a StringBuffer class and how does it differs from String class?
55. what is the difference between StringBuilder and StringBuffer class?
56. What is Pass by Value and Pass by reference? How does JAVA handle the same?
57. What are access modifiers?
58. what is Assertion?
59. Can you explain the fundamentals of deep and shallow Cloning?
60. How do we implement Shallow cloning?
61. How do we implement deep cloning?
62. What’s the impact of private constructor?
63. What are the situations you will need a constructor to be private?
64. Can you explain final modifier?
65. What are static Initializers?
66. If we have multiple static initializer blocks how is the sequence handled?
67. Define casting? What are the different types of Casting?
68. Can you explain Widening conversion and Narrowing conversion?
69. Can we assign parent object to child objects?
70. Define exceptions?
71. Can you explain in short how JAVA exception handling works?
72. Can you explain different exception types?
73. Can you explain checked and unchecked exceptions?
74. Can we create our own exception class?
75. What are chained exceptions?
76. What is serialization?
77. How do we implement serialization actually?
78. What’s the use of Externalizable Interface?
79. What is JAVAdoc utility?
80. what are JAVAdoc doclets?
81. What is Auto boxing and unboxing?
82. How much subclasses you can maximum in Inheritance?
83. Can you explain transient and volatile modifiers?

Note:- if you want to Answer please send me mail
nivedita2104@gmail
with question
number