C# Flashcards

1
Q
Statement to convert list of Chars to String
like List charLst=new List(){'a','b','c'}; to 
string str='abc'
A
https://stackoverflow.com/questions/58524222/c-converting-list-of-chars-to-string
using string constructor
var myString = new string(charList.ToArray());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Time complexity of String(Char[] arr)

A

https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=netcore-3.1
O(n) Initializes the new instance to the value indicated by an array of Unicode characters. This constructor copies Unicode characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
HashSet> hs=new HashSet>();	
	List lst=new List(){1,2,3};
	List lst2=new List(){1,2,3};	
	hs.Add(lst);
	hs.Add(lst2);
what will be in hs?
A

hashSet will contain
{1,2,3}
{1,2,3}
because both of them having difference reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Given a dictionary 
var dict= new Dictionary< int , int > ();
What will dictionary contain at 0 index?
dict[0]=1;
dict[0]=2;
A

First statement dict[0]
will add an entry in dictionary so dictionary will contain {0:1}
Second statement dict[0]=2
will override an existing entry in dictionary so dictionary will still contain
{0:2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

C# statement to convert char to Numeric value

A

Char.GetNumericValue(char)—> returns double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Syntax to declare tuple in C# 7.0 of double and int and initialize them with 4.5 and 3.0?

A

(double Sum, int Count) t2 = (4.5, 3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Syntax to deconstruct a tuple instance in separate variable?

A
var t = ("post office", 3.6);
(string destination, double distance) = t;
Console.WriteLine($"Distance to {destination} is {distance} kilometers.");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Initialize two variables in one statement?

A

int a,b;

a=b=10;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What will result statement will contain below?
string s="sands";
var results=s.Substring(5);
A

empty string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given a url , how we can extract host from it. For example http://google.com/news how to extract google.com from it?

A
string url="http://google.com/news"
string host=new Uri(url).Host;  // google.com
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to access element in dictionary using index?

A

dictionary.ElementAt(0).Value will give us element at index 0 its value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to calculate the remainder for the negative number?

A

int remainder=number%K;
if(remainder<0)
remainder=remainder+K;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to generate random value from 0 to n ?

A

Math.Random(n+1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to sort an array in decreasing order rather than increasing order using Array.Sort?

A
There are different methods available 
1. var sortedArray=arr.OrderByDescending();
2. Comparer comparer=Comparer.Create((x,y)=>
	{
	     return y.CompareTo(x);	  
	});
3.  Create custom comparer class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly