Марафон вопросов и ответов C# in English

Опубликовано Mar 16, 2012 в Языки программирования | 10 коммент.


Марафон вопросов и ответов C# in English

Предлагаем одновременно проверить знания английского языка и технологии DotNet.

Ниже приведен список вопросов по DotNet на английском языке. 

Рекомендуем пробежаться перед прохождение собеседования.

Обратите внимание на четкость и краткость ответов

General and Collections Questions

 1) Is String is Value Type or Reference Type in C#?

String is an object (Reference Type).

2) What is garbage collection?

Garbage collection is a mechanism that allows the computer to detect when an object can no longer be accessed. It then automatically releases the memory used by that object (as well as calling a clean-up routine, called a “finalizer,” which is written by the user). Some garbage collectors, like the one used by .NET, compact memory and therefore decrease your program’s working set.

3) What does assert() method do?

In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.

4) How and why to use Using statement in C#?

Using statement is used to work with an object in C# that inherits IDisposable interface. IDisposable interface has one public method called Dispose that is used to dispose off the object. When we use Using statement, we don’t need to explicitly dispose the object in the code, the using statement takes care of it. Using statement make the code more readable and compact. When we use above block, internally the code is generated like this

 try {
finally {  // calls the dispose method

5) Difference between Static and ReadOnly?

Static: A variable that retains the same data throughout the execution of a program.
ReadOnly: you can’t change its value.

6) Which class is at the top of .NET class hierarchy?


7) What does the term immutable mean?

The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.

8) What’s the difference between System.String and System.Text.StringBuilder classes?

System.String is immutable.

System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed. StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.

10) Can you store multiple data types in System.Array?

11) What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identical object.

12) How can you sort the elements of the array in descending order?

By calling Sort() and then Reverse() methods.

By calling Sort() with proper comparison predicate/delegate. Using LINQ and orderby with desc.

13) What’s the .NET collection class that allows an element to be accessed using a unique key?


14) What class is underneath the SortedList class?

A sorted HashTable.

15) Will the finally block get executed if an exception has not occurred?


16) What’s the C# syntax to catch any possible exception?

A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.

17) Can multiple catch blocks be executed for a single try statement?

No. Once the proper catch block processed, control is transferred to the finally block .

18) Explain the three services model commonly know as a three-tier application?

Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources).

19)  How to loop through all rows of the DataTable?

It can be done in more than one way but foreach loop is much better than any other way in terms of cleanliness of the code or performance.

20)  What is an Array?

An array is a collection of related instance either value or reference types. Array posses an immutable structure in which the number of dimensions and size of the array are fixed at instantiation.

C# Supports Single, Multi-dimensional and Jagged Array.

  • Single Dimensional Array: it is sometimes called vector array consists of single row.
  • Multi-Dimensional Array: are rectangular & consists of rows and columns.
  • Jagged Array: also consists of rows & columns but in irregular shaped (like row 1 has 3 column and row 2 has 5 column)

21)  What is an ArrayList?

ArrayList is a dynamic array. Elements can be added & removed from an arraylist at the runtime. In this elements are not automatically sorted.

22) What is the use of var keyword in C#?

This enable us to declare a variable whose type is implicitly inferred from the expression used to initialize the variable. There are few limitation of the var type of variables:

  • They can’t be initialized as null.
  • They need to be declared and initialized in the same statement.
  • They can’t be used as a member of the class.

23) What is anonymous type in C#?

This enable us to create a type/class on-the-fly at compile time. This is especially useful when we want to receive data from other object or iterate through a collection and set values and do not want to create a class just to hold the data. Note that anonymous types are just a placeholder, we can’t customize its behavior or add methods inside it.

24) What is BitArray?

The BitArray collection is a composite of bit values. It stores 1 or 0 where 1 is true and 0 is false. This collection provides an efficient means of storing and retrieving bit values.

25) What is HashTable?

A Hashtable is a collection of key-value pairs. Entries in this are instance of DictionaryEntry type. It implements IDictionary, ISerilizable, IDeserializable collback interface.

26) What is Queue?

This is a collection that abstracts FIFO (First In First Out) data structure. The initial capacity is 32 elements. It is ideal for messaging components.

27) What is Stack?

This is a collection that abstracts LIFO (Last In First Out) data structure in which initial capacity is 32.

28) What is SortedList?

This is a collection and it is a combination of key/value entries and an ArrayList collection. Where the collection is sorted by key.

29) What is a collection?

A collection serves as a container for instances of other classes. All classes implement ICollection interface which intern implement IEnumerable interface.

30) What will be the output for the following:

  String str1 = "Hello";
  String str2 = str1;
  str1 = str1 + "C#";

31) A try block having 4 catch block will fire all catch block or not?

No, A try having more than one catch block will fire the first relevant catch block after that cursor will be moved to the finally block (if exists) leaving all remaining catch blocks. So in all cases only one catch block will fire.

32) Can we throw exception from catch block ?

Yes. The exceptions which cant be handled in the defined catch block are thrown to its caller.

33) Give Some Examples of Generic Classes?


34) Give Some Examples of Generic thread-safe collections?

SynchronizedCollection<T>, SynchronizedKeyedCollection<K, T>, SynchronizedReadOnlyCollection<T>.

35) What is the difference between a.Equals(b) and a == b?

For value types : “==” and Equals() works same way : Compare two objects by VALUE. For reference types : both works differently : “==” compares reference – returns true if and only if both references point to the SAME object while “Equals” method compares object by VALUE and it will return true if the references refers object which are equivalent.

36)What is structs?

Structs are similar to a class.Some of the points listed below:

• A struct is a value type.

• The structs doesn’t support inheritance other than implicitly deriving from object.

• A struct can’t have the members like parameterless constructor, a finalizer, virtual members etc.

37) What is enum?

A enum is nothing but a special value type which specifies a group of named numeric constants.

 Class and Interface Questions

 1) Does C# support multiple-inheritance?

No. But you can use Interfaces.

2) Where is a protected class-level variable available?

It is available to any sub-class derived from base class

3) Are private class-level variables inherited?

Yes, but they are not accessible.

4) Describe the accessibility modifier “protected internal”.

The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

 5) Where is a protected class-level variable available?

It is available to any sub-class derived from base class

6) Are private class-level variables inherited?

Yes, but they are not accessible.

7) Describe the accessibility modifier “protected internal”.

It is available to classes that are within the same assembly and derived from the specified base class.

8) 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

9) Can you prevent your class from being inherited by another class?

Yes. The keyword “sealed” will prevent the class from being inherited. Another way to achieve this is to declare base class constructor as Private we can avoid that class being inherited by another class. When we create the object for the derived class the base class constructor called automatically and executed. Here it cannot be executed due to its protection level.

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

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

12) When do you absolutely have to declare a class as abstract?

  • When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
  • When at least one of the methods in the class is abstract.

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

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

15) Can you inherit multiple interfaces?
Yes. .NET does support multiple interfaces.

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

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

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

19) In which cases you use override and new base ?

You can use the new modifier to explicitly hide a member inherited from a base class.
To hide an inherited member, you have to declare it in the derived class using the same name, and modify it with the new modifier.

20) Is overriding of a function possible in the same class?


21)  Is it namespace.class or class.namespace?

The .NET class library is organized into namespaces. Each namespace contains a functionally related group of classes so natural namespace comes first.

21)  Are private class-level variables inherited?
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited.

22)  Describe the accessibility modifier protected internal.?
It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

 Method and Property Questions

 1)  What’s the implicit name of the parameter that gets passed into the set method/property of a class?

Value. The data type of the value parameter is defined by whatever data type the property is declared .

2)  What does the keyword “virtual” declare for a method or property?

The method or property can be overridden.

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

4) Can you declare an override method to be static if the original method is not static?

No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override)

5)  What are the different ways a method can be overloaded?

Different parameter data types, different number of parameters, different order of parameters.

6)  If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific 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.

7) What is extension method in C#?

 Extension methods allow existing classes to be extended without relying on inheritance or having to change the class’s source code. This means that if you want to add some methods into the existing String class you can do it quite easily.
Here’s a couple of rules to consider when deciding on whether or not to use extension methods:

  • Extension methods cannot be used to override existing methods.
  • An extension method with the same name and signature as an instance method will not be called.
  • The concept of extension methods cannot be applied to fields, properties or events.

8) What are the different method parameter modifiers in C#?

out, ref, params.

9)  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?

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.

10)  When you inherit a protected class-level variable, who is it available to?
Classes in the same namespace.

11)  How’s method overriding different from overloading?

When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.

12)  What does the keyword virtual mean in the method definition?
The method can be over-ridden.

13)  Can you declare the override method static while the original method is non-static?
No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

14)  Can you override private virtual methods?
No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.

16)  Can you allow class to be inherited, but prevent the method from being over-ridden?

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

17) What is the use of param keyword in C#?

In C# param parameter allows us to create a method that may be sent to a set of identically typed arguments as a single logical parameter.

18)  Why can’t you specify the accessibility modifier for methods inside the interface?

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.

 Events and Delegates

 1)  What’s a delegate?

A delegate object encapsulates a reference to a method.

2)  What’s a multicast delegate?

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

3) What are anonymous methods?

Anonymous methods are another way to declare delegates with inline code except named methods.

4) C# delegate keyword is derived from which namespace?

C# delegate keyword is derived from System.MulticastDelegate.

5) What is the use of GetInvocationList() in C# delegates?

GetInvocationList() returns an array of System.Delegate types, each representing a particular method that may be invoked.

6) Which class defines different events for controls in C#?

The “Control” class defines a number of events that are common to many controls.

7) When TimerCallback delegate is used?

Many application have the need to call a specific method during regular intervals. For such situations we can use the System.Threading.Timer type in conjunction with a related delegate named TimerCallback.

8) What are the different compiler generated methods when a .NET delegate is compiled?

Compilation of a .NET delegate results in a sealed class with three compiler generated methods whose parameters and return values are dependent on the delegate declaration. The methods are Invoke(), BeginInvoke() and EndInvoke().

9) What’s the difference between Delegate.Invoke(), Delegate.BeginInvoke(),Control.Invoke(), Control.BeginInvoke()?

Delegate.Invoke: Executes synchronously, on the same thread. Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread. Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing. Control.BeginInvoke : Executes on the UI thread, and calling thread doesn’t wait for completion.

Автор публикации: