Thursday, February 29, 2024
HomeC#Grasp C# Operators: High 12 Interview Questions

Grasp C# Operators: High 12 Interview Questions


Operators’ questions are fashionable for C# job interviews. Often, the interviewers ask the juniors for quizzes relating to operators, however you may also obtain these kinds of questions. So let’s begin to put together with some questions:

What might be printed?

Console.WriteLine('a'+'b'+'c');

Reply: 294, as a result of the summation of chars will provide you with a quantity. First, the chars are transformed to the ASCII code. For instance, the ASCII code of the letter a is 97. You don’t have to know the numeric code, however keep in mind that you don’t concatenate chars utilizing the signal plus.

What might be printed?

Console.WriteLine("A"+"B"+"C");

Reply: ABC

On this case, we used double quotes to cope with strings.

What might be printed?

static void AddSign(string nume)
{
    nume += "TEST";
}


static void Important()
{
    string carName = "Mercedes";
    AddSign(carName);
    Console.WriteLine(carName);
}

The above program will print Mercedes.

A string variable is a reference sort, and it’s immutable, so we go the reference once we name the tactic. Inside that methodology, we modify the reference to one thing else. On this case, the variable from the tactic will level to a different string. When the execution returns to the primary, the outdated reference shouldn’t be modified.

How do you modify the above code to print MercedesTest?

static void AddSign(string ref nume)
{
    nume += "TEST";
}


static void Important()
{
    string carName = "Mercedes";
    AddSign(carName);
    Console.WriteLine(ref carName);
}

We use the ref key phrase.

 What occurs when you sum the utmost integer worth with 1?

int x = int.MaxValue;
int y = x + 1;
Console.WriteLine(y); // it's going to print int.MinValue

It exhibits the minimal worth of the integer. The reason is that it modifications the primary bit, which specifies the signal of the quantity.

How do you repair the above code to throw an exception if the worth of the integer is exceeded?

To resolve this downside, we should always use the checked block.

checked
{
    int x = int.MaxValue; int y = x + 1; Console.WriteLine(y); // it's going to throw an OverflowException
}

What’s the distinction between the is and as operators in C#?

The is operator returns true if a given object is a specific sort, whereas the as operator returns the article if it’s the given sort.

object obj = "Howdy world";

if (obj is string)
{
    string str = (string)obj;
    Console.WriteLine(str); // Output: Howdy world
}

What does sizeof operator do?

The sizeof operator returns the storage wanted for the given sort. It may be used solely in an unsafe block.

How do you utilize the null-coalescing operator (??) in C#?

In C#, the null-coalescing operator returns the left half if the worth shouldn’t be null. In any other case, it returns the half from the fitting.

Automobile automotive = new Automobile() { Title = "BMW" };
Console.WriteLine(automotive.Title ?? "Unknown");// prints "BMW"
automotive = null;
Console.WriteLine(automotive?.Title ?? "Unknown"); // prints "Unknown"

What’s the distinction between == operator and the Equals methodology?

The == operator compares the values of two objects to see if they’re equal. In case you could have reference varieties, the == operator returns true provided that the objects level to the identical location within the reminiscence. Even when the properties inside the article are the identical, by default the == operator will evaluate the reference of the objects.

int a = 5;
int b = 5;

// Utilizing '==' operator
bool isEqualOperator = (a == b); // true

// Utilizing 'Equals' methodology
bool isEqualMethod = a.Equals(b); // true

The equal methodology is a digital methodology outlined within the Object class. This may be overridden in your lessons to offer customized equality comparability.

class Automobile
{
    public string Title { get; set; }

    public Automobile(string identify)
    {
        Title = identify;
    }
}

inner class Program
{
    static void Important(string[] args)
    {
        Automobile car1 = new Automobile("Mercedes");
        Automobile car2 = new Automobile("Mercedes");
        Console.WriteLine(car1==car2); // false
    }
}
class Store
{
    public string Title { get; set; }
    public Store(string identify)
    {
        Title = identify;
    }

    public override bool Equals(object? obj)
    {
        if (obj == null) return false;
        if (obj == this) return true;
        Store different = obj as Store;
        if (different == null) return false;
        return different.Title == Title;
    }
}

inner class Program
{
    static void Important(string[] args)
    {
        Store shop1 = new Store("Tesco");
        Store shop2 = new Store("Tesco");
        Console.WriteLine(shop1.Equals(shop2)); // true
    }
}

Within the first instance, we used the == operator. This operator compares if the objects have the identical reference. Within the second instance, we outlined the Equal methodology, which is why it returns true.

Write an instance of the ternary operator

The ternary operator is used quite a bit in lots of programming languages like JavaScript, Java, or C#. It’s a technique to create an inline if-else assertion.

int x = 2;
int y = x==2 ? 1 : x;  // this interprets to set variable y to 1 if x is the same as 2, else return x.
Console.WriteLine(y); // prints 1

Resolve workout routines utilizing bitwise & and | operators

This train is fairly easy if you know the way to transform decimal numbers to binary. Have a look right here when you don’t bear in mind.

The AND  operator will evaluate the corresponding bits for every quantity. If each are 1,  then the result’s 1, in any other case is 0.

Within the case of the OR operator(|), if the corresponding bits are 1 or if at the very least one of many bit is 0, then the result’s 1, in any other case is 0.

static void Important(string[] args)
 y); // 1011 => 11, 

Checked and unchecked key phrases in C#

Checked and unchecked key phrases are used for arithmetic operations. Have you ever ever thought of what occurs whenever you add ‘int32.MaxValue’ to 1? The result’s the worth of int32.MinValue, as a result of every int has the primary bit that mentions the signal of the quantity. If you add 1, to the utmost worth, the primary bit will change.

To make sure you don’t have one of these state of affairs, you need to use the checked key phrase. If an overflow occurs, then the code will throw an exception.

int maxValue = int.MaxValue;

checked
{
    int resultChecked = maxValue + 1; // Throws OverflowException
}

In case you are curious about extra interview questions for .NET, you may examine this text:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments