Tricky C# Interview Questions

Hi Guy’s, Here is the article for tricky c# interview question and answer for beginners and experience.

Question 1 :

Code Explanation :
Array has the Elements int []a = {0 , 2, 2, 1, 3};
for (int i = 0; i < a.Length; i++)
First Iteration : i = 0, a.Length = 5
condition check 0 < 5 , True then it starts processing for loop.
a[0] = a[(a[0] + 3) %5];
a[0] = a[(0+3) % 5]
a[0] = a[3%5]
a[0] = a[3] then a[0] = 1.
Second Iteration : i = 1, a.Length = 5
condition check 1 < 5 , True then it starts processing for loop.
a[1] = a[(a[1] + 3) %5];

a[1] = a[(2+3) % 5]
a[1] = a[5%5]
a[1] = a[0] then a[1] = 1. since a[0]=1
Third Iteration : i = 2, a.Length = 5
condition check 2 < 5 , True then it starts processing for loop.
a[2] = a[(a[2] + 3) %5];
a[2] = a[(2+3) % 5]
a[2] = a[5%5]
a[2] = a[0] then a[2] = 1. since a[0]=1
so a[2] = 1 Correct Answer.

Question 2 :

Code Explanation : Correct Answer 0 because variable i is never initialized to,and will always have its default value 0. The default constructor is invoked implicitly if there is no constructor in the class, the variable i is initialized to 0.

Question 3 :

Code Details : The ASCII of i = 115 and 16 + 115 = 121 Correct Answer.Char datatypes is the sub-datatype of double.So that no error.

Question 4 :

Answer : Join(st,str,1,3), the array index start from 1. 3 that means it counts three elements. so the output return Nokia Samsung Sony. Correct Answer 4.

Question 5 :

Code Explanation : Assignment operator: It perform Shallow copying operation. The Shallow copy can be made by simply
copying the reference. str2 simply refers to the same array as str1. Clone Method.It perform Deep copying operation.
Deep copying means creating a new array and copying over the values. str2 refers to the different array as str1. Correct Answer 3.

Question 6 :

Code Explanation :
str1 = {“Pocco”, “IPhone”, “Samsung”, “Sony”};
now, str2 copy the elements of str from index 2.
so, str2[0]=”null”, str2[1]=”null”
and str2[2]=”Pocco”,str2[3]=”IPhone”
str2[4]=”Samsung” str2[5]=”Sony” rest index null.
printing str2[2] = Pocco
for clone method search in Google –> Assignment vs Clone method in C#. so , correct output 4.

7. C# interview Question and Answer.

Code Explanation ; Rank Function ; It gets the rank (number of dimensions) of the array.
For Example : one-dimension array returns rank one, two-dimension array returns
rank two and so on. Jagged Array returns rank one.
so that : arr1.rank = 1, arr2.rank =2, arr3.rank = 3 and
arr4.rank = 1 (arr4 is jagged array). 1 + 2 + 3 + 1 = 7

Question 8 :

Code Details : Program.Main(string) and Main(object) has the wrong signature to be an entry point. The Correct Signature Program.Main(string[] args) , so the output display Main Method. Correct Answer 2.

Question 9 :

Code Details : null Represents the empty string.System.String class. so, fun(null) method call fun(string s) method. then output display String Method. Correct Answer 1.

Question 10 :

Correct Answer 3. Code Details. string pool in c# ozanecare

Question 11 :

Correct Answer 2. Code Details. Search in Google. string pool in c# ozanecare.

Question 12 :

Correct Answer 4. True False because zedd and Zedd are not same. Different memory of object str1 and different memory address of str2 but same memory address of st and st2.

Question 13 :

Code Explanation : String s1 = “Tech”; String s2 =”Tech”;
Creating a String object in intern pool by directly assigning a String literal value. so if you give, Console.WriteLine(ReferenceEqual(s1,s3)); it return true. but
char[] arr = {‘T’,’e’,’c’,’h’};
String s3 = new String(arr);
so here, Dynamically creating a String object by programmatically creating array variable arr and storing elements after arr values store into String constructor. and
String s4 = new StringBuilder(“Tech”).ToString.
it also Dynamically creating a String object in strng pool.
so if you give , Console.WriteLine(ReferenceEqual(s3,s4)); it return false.
so correct Answer 4. false false.

Question 14 :

If return true and function only takes one parameter in Console class Write method. so it comes Yes and others parameters ignore. The next Console class Write method will only print Martin because Substring method string index start from 3. so correct output Yes Martin.

Question 15 :

Code Details : Object is the super class of String. Object and String has same value. so i will store values into same address. same address and same values. so if statement return true. Correct output True.

Question 16 :

Code Details : Object is the super class of String. Object and String has different value initially. so i will store values into different address. different address and same values. so if statement return false so control move to else part and print False. Correct output False.

Question 17 :

Correct Output x1 != x2.
Whenever two Different Object references are compared using == Operator, the value is always False.

Question 18 :

Correct Output x1 == x2.
Whenever two different object references are compared using == Operator, the value is always False . But here, because of the Double caching, x1 and x2 are autoboxing. so that , x1 == x2 returns True.

Question 19 :

Correct Output False True (2)
Numbers that power of 2 must be rounded to fit into small bits. so that 0.2 equal to 0.2 but with 0.3 C# compiler uses double data type to represent decimal values, 64 bits values are used that number. So that, 0.1*3 not equal to 0.3

Post Author: adama