Skip to content

adiiaditya/Essential-Csharp-Interview-Questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 

Repository files navigation

Essential-Csharp-Interview-Questions

Question 1

Difference between Array and ArrayList?

1. Array is strongly typed and can store only same type of data while ArrayList can store any type of data.

2. Array stores fixed number of elements while ArrayList can store any number of elements as it grows dynamically.

3. In Array no need to cast elements while retrieving whereas in ArrayList items need to be cast to appropriate data type while retrieving.

Question 2

Difference between String and StringBuilder?

1. String is immutable i.e. if we create a string object then we can't modify it & it always creates new object of string type in memory while StringBuilder is mutable i.e. if we create StringBuilder object then we can perform any operation like insert, replace or append without creating new instance every time.

2. String belongs to System class while StringBuilder belongs to System.Text class.

Question 3

Difference between ArrayList and HashTable?

1. ArrayList stores only the value at a particular index while HashTable stores data as Key-Value pair.

2. To access value from ArrayList, index number should be passed while to access value from HashTable key should be passed.

Question 4

Difference between while and for loop?

Both loops are used when a unit of code needs to execute repeatedly.

For loop is used when you know how many times you need to iterate through the code whereas while loop is used when you need to repeat something until a given statement is true.

Question 5

Define Boxing and Unboxing?

Boxing is converting a value type to an object type.

Unboxing is extracting the value type from the object type.

```C# int i =13; object myObject = i; //Boxing-implicit i = (int)myObject; //Unboxing-explicit ```

Question 6

Difference between Constants and Read-Only variables?

1. Constants are evaluated compile-time while Read-Only variables are evaluated at runtime.

2. Constants only support value-type variables while Read-Only variables can hold reference type variables.

3. Constants should be used when the value won't change during runtime while Read-Only variables are used mostly when their actual value is unknown before the runtime.

Question 7

Difference between == and Equals()?

1. == is the comparison operator while Equals() compares the contents of a String.

2. == compares the reference identity as well as the contents while Equals() compares only the contents.

Question 8

Difference between Finalize and Dispose?

1. Finalize is used to free unmanaged resources like files, database connections, etc held by an object before that object is destroyed. Internally, it is called by Garbage Collector & can't be called by user code while Dispose is used to free unmanaged resources at any time. Explicitally, it is called by user code.

2. Finalize belongs to the Object class while Dispose belongs to IDisposable interface and the class which is implementing Dispose method must implement this interface.

Question 9

What is the default access for a class & the default access for members?

The default access for a class is Internal & the default access for members is Private.

Question 10

Can multiple catch blocks be executed in c#?

No. We can use multiple catch blocks with every try but when an Exception is thrown by the debugger, it tries matching the exception type with their signature and catch the exception by the particular catch block, so we can use multiple catch blocks but only one catch can be executed at once.

```C# try { Console.WriteLine("Sample Data"); } catch (NullPointerException ex) { Console.WriteLine("NullPointerException"); } catch (Exception e) { Console.WriteLine("Exception"); } ```

Question 11

Can 'this' keyword be used within a static method?

'this' keyword can't be used in static methods because it returns a reference to the current instance of the class containing it. Static methods can't be instantiated, so they are called by the name of a class and not by instance. Therefore, we can’t use 'this' keyword in the body of static methods.

By definition, 'this' keyword is a special type of reference variable that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.

License

This book is released under a Creative Commons Attribution-Noncommercial- No Derivative Works 3.0 United States License.

What this means is that it is free to read and use, but the license does not permit commercial use of the material (i.e you can freely print out the questions for your own use, but you can't sell it). I'm trying to help so I would greatly appreciate it if you would respect these terms.

Copyright Aditya Kumar, 2017.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published