This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is the difference between String and StringBuilder in C#? |
|
Answer» The major difference between String and StringBuilder is that String OBJECTS are immutable while StringBuilder creates a mutable string of characters. StringBuilder will make the changes to the existing object RATHER than creating a new object. StringBuilder SIMPLIFIES the entire process of making changes to the existing string object. Since the String class is immutable, it is costlier to create a new object every time we need to make a CHANGE. So, the StringBuilder class comes into picture which can be evoked using the System.Text namespace. In case, a string object will not change throughout the entire program, then use String class or else StringBuilder. For ex: string s = string.Empty; for (i = 0; i < 1000; i++) { s += i.ToString() + " "; }Here, you’ll need to create 2001 objects out of which 2000 will be of no use. The same can be APPLIED using StringBuilder: StringBuilder sb = new StringBuilder(); for (i = 0; i < 1000; i++) { sb.Append(i); sb.Append(' '); }
|
|
| 2. |
What is the difference between constant and readonly in C#? |
|
Answer» A const keyword in C# is used to declare a constant field throughout the program. That means once a variable has been declared const, its value cannot be changed throughout the program. On the other hand, with readonly keyword, you can assign the variable only when it is declared or in a constructor of the same class in which it is declared. Ex: public readonly int xvar1; public readonly int yvar2; // Values of the readonly // variables are assigned // Using constructor public IB(int b, int c) { xvar1 = b; yvar2 = c; Console.WriteLine("The value of xvar1 {0}, "+ "and yvar2 {1}", xvar1, yvar2); } // Main method static public void Main() { IB obj1 = new IB(50, 60); }}Output:The value of xvar1 is 50, and yvar2 is 60Constants are static by default while readonly should have a value assigned when the constructor is declared. |
|
| 3. |
What is Reflection in C#? |
|
Answer» REFLECTION in C# extracts metadata from the datatypes during RUNTIME.
|
|
| 4. |
What are the different ways in which a method can be Overloaded in C#? |
|
Answer» OVERLOADING means when a method has the same name but carries different values to use in a different context. Only the main() method cannot be overloaded. In order to overload METHODS in C#,
For ex. PUBLIC class Area { public double area(double x) { double area = x * x; return area; } public double area(double a, double B) { double area = a * b; return area; }}Here, the method Area is used twice. In the first declaration, one argument is used while in the second one, there were two arguments are used. Using different parameters in the same method, we were able to overload the method area(). |
|
| 5. |
Difference between the Equality Operator (==) and Equals() Method in C#? |
|
Answer» Although both are used to compare two OBJECTS by value, still they both are used differently. For ex.: int x = 10;int y = 10;Console.WriteLine( x == y);Console.WriteLine(x.Equals(y));OUTPUT:TrueTrueEquality operator (==) is a reference type which means that if equality operator is used, it will return TRUE only if both the references point to the same object. |
|
| 6. |
What are Indexers in C#? |
|
Answer» Indexers are called smart arrays that allow access to a MEMBER variable. Indexers allow member variables using the features of an array. They are created using the Indexer keyword. Indexers are not static members. For ex. Here the indexer is defined the same WAY. <return type> this[<parameter type> index]{ get{ // return the value from the SPECIFIED index of an INTERNAL collection } set{ // set values at the specified index in an internal collection }} |
|
| 7. |
What are the Arrays in C#? |
|
Answer» When a GROUP of similar elements is clubbed together under one name, they are called arrays. A few pointers for arrays in C#:
Syntax: < Data Type > [ ] < Name_Array > |
|
| 8. |
What is the difference between late binding and early binding in C#? |
|
Answer» Late binding and early binding are examples of one of the primary concepts of OOPS: Polymorphism. |
|
| 9. |
What are partial classes in C#? |
|
Answer» Partial classes implement the functionality of a single class into multiple files. These multiple files are combined into ONE during COMPILE time. The partial class can be created using the partial keyword. PUBLIC partial Clas_name { // code}You can easily split the functionalities of methods, interfaces, or structures into multiple files. You can EVEN ADD nested partial classes. |
|
| 10. |
What are Properties in C#? |
|
Answer» Properties in C# are public members of a class where they PROVIDE the ability to access private members of a class. The basic principle of encapsulation LETS you hide some sensitive properties from the USERS by making the variables private. The private members are not accessible otherwise in a class. Therefore, by using properties in C# you can easily access the private members and set their values. |
|
| 11. |
What is Boxing and Unboxing in C#? |
|
Answer» The two functions are used for typecasting the DATA types: Boxing: Boxing CONVERTS value TYPE (int, char, etc.) to reference type (object) which is an implicit CONVERSION process using object value. Example: int num = 23; // 23 will assigned to numObject Obj = num; // BoxingUNBOXING: Unboxing converts reference type (object) to value type (int, char, etc.) using an explicit conversion process. Example: int num = 23; // value type is int and assigned value 23Object Obj = num; // Boxingint i = (int)Obj; // Unboxing |
|