12.Can we call a base class method without creating instance?
Its possible If its a static method.
Its possible by inheriting from that class also.
Its possible from derived classes using base keyword.
13.What are Sealed Classes in C#?
The sealed modifier is used to prevent derivation from a class. A compile-time error
occurs if a sealed class is specified as the base class of another class. (A sealed class
cannot also be an abstract class)
14.In which Scenario you will go for Interface or Abstract 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. Even though class inheritance allows your classes to
inherit implementation from a base class, it also forces you to make most of your design
decisions when the class is first published.Abstract classes are useful when creating
components because they allow you specify an invariant level of functionality in some
methods, but leave the implementation of other methods until a specific implementation
of that class is needed. They also version well, because if additional functionality is
needed in derived classes, it can be added to the base class without breaking code.
15.What are the access-specifiers available in c#?
Private, Protected, Public, Internal, Protected Internal.
16.Explain about Protected and protected internal, “internal” access-specifier?
protected - Access is limited to the containing class or types derived from the
containing class.
•
internal - Access is limited to the current assembly.
•
protected internal - Access is limited to the current assembly or types derived
from the containing class.
17.Difference between type constructor and instance constructor? What is static
constructor, when it will be fired? And what is its use?
(Class constructor method is also known as type constructor or type initializer)Instance
constructor is executed when a new instance of type is created and the class constructor is
executed after the type is loaded and before any one of the type members is accessed. (It
will get executed only 1st time, when we call any static methods/fields in the same class.)
Class constructors are used for static field initialization. Only one class constructor per
type is permitted, and it cannot use the vararg (variable argument) calling convention.A
static constructor is used to initialize a class. It is called automatically to initialize the
class before the first instance is created or any static members are referenced.
18.What is Private Constructor? and it’s use? Can you create instance of a class
which has Private Constructor?
When a class declares only private instance constructors, it is not possible for classes
outside the program to derive from the class or to directly create instances of it. (Except
Nested classes)
Make a constructor private if:- You want it to be available only to the class itself. For
example, you might have a special constructor used only in the implementation of your
class' Clone method.- You do not want instances of your component to be created. For
example, you may have a class containing nothing but Shared utility functions, and no
instance data. Creating instances of the class would waste memory.
19.What are Destructor and finalize?
Generally in C++ the destructor is called when objects gets destroyed. And one can
explicitly call the destructors in C++. And also the objects are destroyed in reverse order
that they are created in. So in C++ you have control over the destructors.In C# you can
never call them, the reason is one cannot destroy an object. So who has the control over
the destructor (in C#)? it's the .Net frameworks Garbage Collector (GC). GC destroys the
objects only when necessary. Some situations of necessity are memory is exhausted or
user explicitly calls System.GC.Collect() method.Points to remember:
•
Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
•
Destructors are invoked automatically, and cannot be invoked explicitly
•
Destructors are not inherited. Thus, a class has no destructors other than the one,
which may be declared in it.
•
Destructors cannot be used with structs. They are only used with classes.
•
An instance becomes eligible for destruction when it is no longer possible for any
code to use the instance.
•
Execution of the destructor for the instance may occur at any time after the
instance becomes eligible for destruction.
•
When an instance is destructed, the destructors in its inheritance chain are called,
in order, from most derived to least derived.
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpconfinalizemethodscdestructors.asp
20.What is the difference between Finalize and Dispose (Garbage collection)?
Class instances often encapsulate control over resources that are not managed by the
runtime, such as window handles (HWND), database connections, and so on. Therefore,
you should provide both an explicit and an implicit way to free those resources. Provide
implicit control by implementing the protected Finalize Method on an object (destructor
syntax in C# and the Managed Extensions for C++). The garbage collector calls this
method at some point after there are no longer any valid references to the object.In some
cases, you might want to provide programmers using an object with the ability to
explicitly release these external resources before the garbage collector frees the object. If
an external resource is scarce or expensive, better performance can be achieved if the
programmer explicitly releases resources when they are no longer being used. To provide
explicit control, implement the Dispose method provided by the IDisposable Interface.
The consumer of the object should call this method when it is done using the object.
Dispose can be called even if other references to the object are alive.Note that even when
you provide explicit control by way of Dispose, you should provide implicit cleanup
using the Finalize method. Finalize provides a backup to prevent resources from
permanently leaking if the programmer fails to call Dispose
21.What is boxing & unboxing?
Boxing:http://www.codersource.net/csharp_boxing_unboxing.html
22.Is goto statement supported in C#?
Gotos are supported in C#to the fullest.
23.What’s different about switch statements in C#?
No fall-throughs allowed. Unlike the C++ switch statement, C# does not support an
explicit fall through from one case label to another. If you want, you can use goto a
switch-case, or goto default.
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
Tuesday, March 10, 2009
Interview Question Part III
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment