Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created November 24, 2021 04:43
Show Gist options
  • Save rozer007/43131e5f0e07ac79aeae1729a1686b47 to your computer and use it in GitHub Desktop.
Save rozer007/43131e5f0e07ac79aeae1729a1686b47 to your computer and use it in GitHub Desktop.
List and exception handling in c#
Q1) How to create a 2d array in list?
ans: List<List< data_type>> 2dlist = new List<List< data_type>>();
2dlist.Add(new List<data_type>())
Q2) How can we raise an exception in c#?
ans: We can raise an exception manually in c# using the throw keyword.
Q3) How to get the number of element in a nth dimension of a List?
ans: We can get the number of element by this statement list[n-1].Count;
Q4) What does finally block do?
ans: The code under the finally block will execute whether the try block raise exception or not.
Q1. What will be the output of the statement?
List<List<int>> matrix = new List<List< int>>();
matrix.Add(new List<int>());
matrix[0].AddRange(new int[] {1,2,3,4,5,6,7,8,9});
matrix.Add(new List<int>());
matrix[1].AddRange(new int[] {10,11,12,13,14,15,16,17,18,19});
Console.WriteLine(matrix.Count);
a) 10
b) 9
c) 2
d) error
ans: c) 2
Q2. what will be the output of the code?
try{
int c=10/2;
Console.WriteLine(c/0);
}
catch(DivideByZeroException ex)
{
Console.WriteLine("Don't divide by zero");
}
catch(Exception ex)
{
Console.WriteLine("There should not be any zero");
Console.WriteLine("bye");
}
finally
{
Console.WriteLine("Don't give zero");
}
a) Don't divide by zero
Don't give zero
b) Don't divide by zero
There should not be any zero
bye
Don't give zero
c) Don't divide by zero
There should not be any zero
bye
d) There should not be any zero
bye
Don't give zero
ans: a)
Q3) Select the correct option of this statement?
List<int> nn=new List<int>(new int [] {1,2,3,4,5});
Console.WriteLine(nn.Length);
a) 4
b) 5
c) List can't be assigned in this way
d) none of the above
ans: d)
Q4) Which is the correct way to insert an array in a list at a particular index.
a) list.Add(index,array)
b) list.AddRange(index,array)
c) list.Insert(index,array)
d) list.InsertRange(index,array)
ans: d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment