Hello All, We are going to start new batch from next week. message/call or mail us for more details.

C Sharp Interview Questions/Answers Part-2

What is .NET Remoting?
.NET Remoting allows objects to interact with one another across application domains.

What are the 2 message encoding formats supported by .NET Remoting and when do you choose one over the other?
Message Encoding Formats:
1. Binary encoding.
2. XML encoding.

Applications can use binary encoding where performance is critical, or XML encoding where interoperability with other remoting frameworks is essential.

What are the two types of .NET remote objects?
1. Client-activated objects - Client-activated objects are under the control of a lease-based lifetime manager that ensures that the object is garbage collected when its lease expires.
2. Server-activated objects - In the case of server-activated objects, developers have a choice of selecting either a "single call" or "singleton" model. The lifetime of singletons are also controlled by lease-based lifetime.

What is considered as Remote Object?
Any object outside the application domain of the calling appication is considered remote object, even if the objects are executing on the same machine.

Can you treat every object as a remote object?
Objects that cannot be serialized cannot be passed to a different application domain and are therefore nonremotable.

What are the ways in which an object can be serialized?
1. Mark your class with serializable attribute.
2. Make your class implement ISerializable interface.

How can you change an object into a remote object?
Any object can be changed into a remote object by deriving it from MarshalByRefObject. 

What happens when a client activates a remote object?
When a client activates a remote object, it receives a proxy to the remote object. All operations on this proxy are appropriately indirected to enable the remoting infrastructure to intercept and forward the calls appropriately.

What are proxy objects and what is the use of these proxy objects?
Proxy objects are created when a client activates a remote object. The proxy object acts as a representative of the remote object and ensures that all calls made on the proxy are forwarded to the correct remote object instance.

What is the difference between method parameters and method arguments. Give an example?
In the example below FirstNumber and SecondNumber are method parameters where as FN and LN are method arguments. The method definition specifies the names and types of any parameters that are required. When calling code calls the method, it provides concrete values called arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method.

Code: [Select all]
using System;
namespace Demo
{
class Program
{
  public static void Main()
  {
   int FN = 10;
   int SN = 20;
   //FN and LN are method arguments
   int Total = Sum(FN, SN);
   Console.WriteLine(Total);
  }
  //FirstNumber and SecondNumber are method parameters
  public static int Sum(int FirstNumber, int SecondNumber)
  {
   int Result = FirstNumber + SecondNumber;
   return Result;
  }
}
}

Can you pass value types by reference to a method?
Yes, we can pass value types by by reference to a method. An example is shown below.

Code: [Select all]
using System;
namespace Demo
{
class Program
{
  public static void Main()
  {
   int I = 10;
   Console.WriteLine("Value of I before passing to the method = " + I);
   Function(ref I);
   Console.WriteLine("Value of I after passing to the method by reference= " + I);
  }
  public static void Function(ref int Number)
  {
   Number = Number + 5;
  }
}
}

What is the difference between static class and class with static methods? In which case I should use either of them?
If your class has only static members you never need an instance of that class so you should make the class itself static but if your class has instance members (non static) then you have to make your class an instance class to access its instance members via instances of your class.

If a method's return type is void, can you use a return keyword in the method?
Yes, Even though a method's return type is void, you can use the return keyword to stop the execution of the method as shown in the example below.
Code: [Select all]
using System;
namespace Demo
{
class Program
{
  public static void Main()
  {
   SayHi();
  }
  public static void SayHi()
  {
   Console.WriteLine("Hi");
   return;
   Console.WriteLine("This statement will never be executed");
  }
}
}

How do you create empty strings in C#? 
Using string.empty as shown in the example below.
string EmptyString = string.empty;

What is the difference between System.Text.StringBuilder and System.String?
1. Objects of type StringBuilder are mutable where as objects of type System.String are immutable. 
2. As StringBuilder objects are mutable, they offer better performance than string objects of type System.String.
3. StringBuilder class is present in System.Text namespace where String class is present in System namespace.

What is the difference between int.Parse and int.TryParse methods? 
Parse method throws an exception if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse.

What is the difference between string keyword and System.String class? 
string keyword is an alias for Syste.String class. Therefore, System.String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings.

Are string objects mutable or immutable? 
String objects are immutable.

What do you mean by String objects are immutable?
String objects are immutable means, they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

Code: [Select all]
string s1 = "First String ";
string s2 = "Second String";

// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;

System.Console.WriteLine(s1);
// Output: First String Second String

What is an array? 
An array is a data structure that contains several variables of the same type.

What are the 3 different types of arrays?
1. Single-Dimensional
2. Multidimensional
3. Jagged

What is Jagged Array? 
A jagged array is an array of arrays.

Are arrays value types or reference types?
Arrays are reference types.

What is the base class for Array types? 
System.Array

Can you use foreach iteration on arrays in C#?
Yes,Since array type implements IEnumerable, you can use foreach iteration on all arrays in C#.

What is Boxing and Unboxing? 
Boxing - Converting a value type to reference type is called boxing. An example is shown below.
Code: [Select all]
int i = 101;
object obj = (object)i; // Boxing
Unboxing - Converting a reference type to a value typpe is called unboxing. An example is shown below.
Code: [Select all]
obj = 101;
i = (int)obj; // Unboxing

Is boxing an implicit conversion?
Yes, boxing happens implicitly.

Is unboxing an implicit conversion? 
No, unboxing is an explicit conversion.

What happens during the process of boxing?
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.

What are the 4 pillars of any object oriented programming language?
1. Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism

Do structs support inheritance?
No, structs do not support inheritance, but they can implement interfaces.

What is the main advantage of using inheritance? 
Code reuse

Does C# support multiple class inheritance?
No, C# supports single class inheritance only. However classes can implement multiple interfaces at the same time.

What is an abstract class? 
An abstract class is an incomplete class and must be implemented in a derived class.

Can you create an instance of an abstract class?
No, abstract classes are incomplete and you cannot create an instance of an abstract class.

What is a sealed class? 
A sealed class is a class that cannot be inherited from. This means, If you have a class called Customer that is marked as sealed. No other class can inherit from Customer class. For example, the below code generates a compile time error "MainClass cannot derive from sealed type Customer.
Code: [Select all]
using System;
public sealed class Customer
{
}
public class MainClass : Customer
{
public static void Main()
{
}
}
What are abstract methods? 
Abstract methods are methods that only the declaration of the method and no implementation.

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.

Explain polymorphism in C# with a simple example? 
Polymorphism allows you to invoke derived class methods through a base class reference during run-time. An example is shown below.
Code: [Select all]
using System;
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I am a drawing object.");
}
}
public class Triangle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Triangle.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Circle.");
}
}
public class Rectangle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Rectangle.");
}
}
public class DrawDemo
{
public static void Main()
{
DrawingObject[] DrawObj = new DrawingObject[4];

DrawObj[0] = new Triangle();
DrawObj[1] = new Circle();
DrawObj[2] = new Rectangle();
DrawObj[3] = new DrawingObject();

foreach (DrawingObject drawObj in DrawObj)
{
drawObj.Draw();
}
}
}
When can a derived class override a base class member? 
A derived class can override a base class member only if the base class member is declared as virtual or abstract.

What is the difference between a virtual method and an abstract method? 
A virtual method must have a body where as an abstract method should not have a body.

Can fields inside a class be virtual?
No, Fields inside a class cannot be virtua. Only methods, properties, events and indexers can be virtual.

What are Access Modifiers in C#? 
In C# there are 5 different types of Access Modifiers.
Public 
The public type or member can be accessed by any other code in the same assembly or another assembly that references it.

Private
The type or member can only be accessed by code in the same class or struct.

Protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.

Internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

Protected Internal 
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

What are Access Modifiers used for?
Access Modifiers are used to control the accessibilty of types and members with in the types.

Can you use all access modifiers for all types? 
No, Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type.

Can derived classes have greater accessibility than their base types?
No, Derived classes cannot have greater accessibility than their base types. For example the following code is illegal.
Code: [Select all]
using System;
internal class InternalBaseClass
{
   public void Print()
   {
      Console.WriteLine("I am a Base Class Method");
   }
}
public class PublicDerivedClass : InternalBaseClass
{
   public static void Main()
   {
      Console.WriteLine("I am a Public Derived Class Method");
   }
}
When you compile the above code an error will be generated stating "Inconsistent accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this simple, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

Can you declare struct members as protected? 
No, struct members cannot be declared protected. This is because structs do not support inheritance.

Can the accessibility of a type member be greater than the accessibility of its containing type?
No, the accessibility of a type member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal class has only internal accessibility.

Can destructors have access modifiers? 
No, destructors cannot have access modifiers.

What does protected internal access modifier mean?
The protected internal access means protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

What is the default access modifier for a class,struct and an interface declared directly with a namespace? 
internal

Can you specify an access modifier for an enumeration? 
Enumeration members are always public, and no access modifiers can be specified.

14 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It very interesting and helpful. Can u plz post the oops and .net like this simple language...

    ReplyDelete
  3. Thanks for sharing use interview questions on .Net technology. While preparing for my job interview, your article helped me a lot to sharpen my skills and do well in my interview.

    ReplyDelete
  4. Data warehousing Training in Chennai
    I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

    ReplyDelete
  5. Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
    Unix Training In Chennai

    ReplyDelete
  6. can create partial class as a sealed class?????

    ReplyDelete
  7. Really it is an amazing article I had ever read. I hope it will help a lot for all. Can you more read now visit Dot Net Certification Training Institute in Delhi

    ReplyDelete
  8. This is very nice blog,and it is helps for student's.Thanks for info
    Dot Net Online Training Bangalore

    ReplyDelete
  9. I am glad to read this. Thank you for this beautiful content, Keep it up. Techavera is the best Automation Testing training course in Noida. Visit us For Quality Learning.Thank you

    ReplyDelete
  10. Thank you.Well it was nice post and very helpful information on AngularJS Online Training

    ReplyDelete
  11. Good Post. I like this. Your posts are great.

    If you want to logo designing services than visit - Logo Designing Services Jaipur | Logo Designer in Jaipur | Logo maker in Jaipur

    ReplyDelete
  12. The business leads are followed adequately by the Salesforce advisors and accordingly decrease the requirement for organizations to oversee more noteworthy leads.
    Salesforce training with job support in Noida

    ReplyDelete