Explore topic-wise InterviewSolutions in Current Affairs.

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(' ');  }


By using StringBuilder here, you also de-stress the memory allocator.  

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. 

In C#, a constant is a number, string, NULL reference, or boolean values. 

For ex:

class IB { // Constant fields public const int xvar = 20; public const string str = "InterviewBit"; // Main METHOD static public void Main() { // Display the value of Constant fields Console.WriteLine("The value of xvar: {0}", xvar); Console.WriteLine("The value of str: {0}", str); }}Output:The value of xvar is 20.The value of string is INTERVIEW Bit

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 60

Constants are static by default while readonly should have a value assigned when the constructor is declared. 
Constants can be declared within FUNCTIONS while readonly modifiers can be used with reference types.  

3.

What is Reflection in C#?

Answer»

REFLECTION in C# extracts metadata from the datatypes during RUNTIME

To add reflection in the .NET framework, SIMPLY use System.Refelction namespace in your program to retrieve the TYPE which can be ANYTHING from:

  • Assembly
  • Module
  • Enum
  • MethodInfo
  • ConstructorInfo
  • MemberInfo
  • ParameterInfo
  • Type
  • FieldInfo
  • EventInfo
  • PropertyInfo
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#, 

  • Change the NUMBER of parameters in a method, or
  • Change the order of parameters in a method, or
  • Use different data types for parameters
    In these ways, you can overload a method multiple times.

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:TrueTrue

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

Equals() method: Equals method is used to compare the values CARRIED by the objects. int x=10, int y=10. If x==y is compared then, the values carried by x and y are compared which is equal and therefore they return true. 

Equality operator: Compares by reference

Equals(): Compares by value 

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. 

For ex. An array of tea Atea[4]: [green tea, chamomile tea, BLACK tea, lemon tea]. The length of the array defines how many elements are present in the array. 

In C#, the memory allocations for the elements of the array happen dynamically.  This is how values are stored in an array sequentially.

Arrays in C#

A few pointers for arrays in C#:

  • The memory ALLOCATION is DYNAMIC.
  • Arrays in C# are treated as objects.
  • The length of the array is easy to find by DETECTING the number of members in the array.
  • The members in the array are ordered and begin with the INDEX value=0.
  • The array types are reference types derived from the base array type.

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. 

For ex: one function calculateBill() will calculate bills of premium CUSTOMERS, basic customers, and semi-premium customers based on their policies differently. The calculation for all the customer objects is done differently using the same function which is called polymorphism. 

When an object is assigned to an object variable in C#, the .NET framework performs the binding. 

When the binding function happens at compile-time, it is called early binding. It investigates and checks the methods and properties of the STATIC objects. With early binding, the number of run-time errors decreases substantially and it executes pretty quickly. 

But when the binding happens at runtime, it is called late binding. Late binding happens when the objects are dynamic (decided based on the data they HOLD) at run-time. It is slower as it looks through during run-time.

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. 

The values can be easily ASSIGNED using get and set methods, also known as accessors. While the get method extracts the value, the set method assigns the value to the variables. 

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; // Boxing

UNBOXING: 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
Previous Next