Interview Tips Interview Tips, Interview Questions and Answers

29Aug/110

When should I use WPF instead of DirectX?

DirectX is definitely not dead and is still more appropriate than WPF for advanced developers writing hard-core “twitch games” or applications with complex 3D models where you need maximum performance. That said, it’s easy to write a naive DirectX application that performs far worse than a similar WPF application. DirectX is a low-level interface to the graphics hardware that exposes all of the quirks of whatever GPU a particular computer has. DirectX can be thought of as assembly language in the world of graphics: You can do anything the GPU supports. WPF provides a high-level abstraction that takes a description of your scene and figures out the best way to render it, given the hardware resources available. Internally, this might involve using Shader Model 3.0, or the fixed-function pipeline, or software. (Don’t worry if you’re not familiar with these terms, but take it as a sign that you should be using WPF!). The downside of choosing DirectX over WPF is a potentially astronomical increase in development cost. A large part of this cost is the requirement to test your application on each driver/GPU combination you intend to support. One of the major benefits of building on top of WPF is that Microsoft has already done this testing for you! You can instead focus your testing on low-end hardware for measuring performance. The fact that WPF applications can even leverage the client GPU over Remote Desktop or in a partial-trust environment is also a compelling differentiator.

20May/110

Class Questions in C#

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

2.Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

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

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

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

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

7.Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public, and are therefore public by default.

8.Can you inherit multiple interfaces?
Yes. .NET does support multiple interfaces.

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

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

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

27Jan/100

What do you mean by Explicit Interface Implimentation? What’s Interface Mapping?

A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface
When class implement interface user mandatory needs to specify interface method body. Such implementation is called as Exlicit
implementation. All the signature of interface API must be compatible with interface API. Dervice class can also define their
own API.

   1: interface I1

   2: {

   3: void someMethod();

   4: }

   5: Interface I1

   6: {

   7: void someMethod();

   8: }

   9:  

  10: class someClass:I1 I2

  11: {

  12: void I1.someMethod()

  13: {

  14: //This is called explicit implementation as i am telling the compiler to implement someMethod of interface I1

  15: }

  16: void I2.someMethod()

  17: {

  18: //This is called explicit implementation of someMethod of I2 interface

  19: }

  20: }

19Jan/100

Design Pattern – proxy pattern

Proxy fundamentally is a class functioning as in interface which points towards the actual class which has data. This actual data can be a huge image or an object data which very large and can not be duplicated. So you can create multiple proxies and point towards the huge memory consuming object and perform operations. This avoids duplication of the object and thus saving memory. Proxies are references which points towards the actual object.

Figure ‘Proxy and actual object’ shows how we have created an interface which is implemented by the actual class. So the interface ‘IImageProxy’ forms the proxy and the class with implementation i.e. ‘clsActualImage’ class forms the actual object. You can see in the client code how the interface points towards the actual object.

Figure: - Proxy and actual object

The advantages of using proxy are security and avoiding duplicating objects which are of huge sizes. Rather than shipping the code we can ship the proxy, thus avoiding the need of installing the actual code at the client side. With only the proxy at the client end we ensure more security. Second point is when we have huge objects it can be very memory consuming to move to those large objects in a network or some other domain. So rather than moving those large objects we just move the proxy which leads to better performance.

6Jan/100

10 Frequently asked C# questions, Class Questions

    1. 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
    2. 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. 
    3. Why can’t you specify the accessibility modifier for methods inside the interface?
      They all must be public, and are therefore public by default.
    4. 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.
    5. 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.
    6. Can you prevent your class from being inherited by another class?
      Yes. The keyword “sealed” will prevent the class from being inherited. 
    7. 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.
    8. 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.
    9. 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.
    10. Can you inherit multiple interfaces?
      Yes.  .NET does support multiple interfaces. 
22Dec/090

C# Interview Questions:Interfaces Topic

In our example we created IBankCustomer interface. The interface declares 2 methods.
1. void DepositMoney();
2. void WithdrawMoney();
Notice that method declarations does not have access modifiers like public, private, etc. By default all interface members are public. It is a compile time error to use access modifiers on interface member declarations. Also notice that the interface methods have only declarations and not implementation. It is a compile time error to provide implementation for any interface member. In our example as the Demo class is inherited from the IBankCustomer interface, the Demo class has to provide the implementation for both the methods (WithdrawMoney() and DepositMoney()) that is inherited from the interface. If the class fails to provide implementation for any of the inherited interface member, a compile time error will be generated. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. When a class or a struct inherits an interface, the class or struct must provide implementation for all of the members declared in the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation.
Can an Interface contain fields?
No, an Interface cannot contain fields.

Explain what is an Interface in C#?
An Interface in C# is created using the interface keyword. An example is shown below.
using System;
namespace Interfaces
{
interface IBankCustomer
{
void DepositMoney();
void WithdrawMoney();
}
public class Demo : IBankCustomer
{
public void DepositMoney()
{
Console.WriteLine("Deposit Money");
}
public void WithdrawMoney()
{
Console.WriteLine("Withdraw Money");
}
public static void Main()
{
Demo DemoObject = new Demo();
DemoObject.DepositMoney();
DemoObject.WithdrawMoney();
}
}
}
Can an interface inherit from another interface?
Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.
Can you create an instance of an interface?
No, you cannot create an instance of an interface.
If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inheirted from the interface.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
class BaseClass1 : Interface1
{
public void Interface1Method()
{
Console.WriteLine("Interface1 Method");
}
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
}
Option 2: If the class does not wish to provide Implementation for all the members inheirted from the interface, then the class has to be marked as abstract.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
abstract class BaseClass1 : Interface1
{
abstract public void Interface1Method();
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
}
A class inherits from 2 interfaces and both the interfaces have the same method name as shown below. How should the class implement the drive method for both Car and Bus interface?
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
//How to implement the Drive() Method inherited from Bus and Car
}
}
To implement the Drive() method use the fully qualified name as shown in the example below. To call the respective interface drive method type cast the demo object to the respective interface and then call the drive method.
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}
static void Main()
{
Demo DemoObject = new Demo();
((Car)DemoObject).Drive();
((Bus)DemoObject).Drive();
}
}
}
What do you mean by "Explicitly Implemeting an Interface". Give an example?
If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is "Explicitly Implemeting an Interface member". The disadvantage of Explicitly Implemeting an Interface member is that, the class object has to be type casted to the interface type to invoke the interface member.