For Part 1 of C# Interview Questions, please refer to the below link.
Let’s look at some more questions.
- Which attribute of the client-side document object do we use to get a reference to the root node of the DOM tree?
rootElement
- What is the output of the below code?
- try {
- try {
- int i = 1;
- Console.WriteLine(i / 0);
- } catch (Exception e) {
- Console.WriteLine(e.GetType() + " : First Exception");
- throw;
- }
- } catch (DivideByZeroException e) {
- Console.WriteLine(e.GetType() + " : Second Exception");
- Console.ReadLine();
- }
Output
System.DivideByZeroException : First Exception
System.DivideByZeroException : Second Exception
- What is the difference between throw and throw ex?
Throw will rethrow the original exception, whereas throw ex will create a new exception which, in turn, changes the stack trace.
- Which catch block will get caught?
- try {
- try {
- int i = 1;
- Console.WriteLine(i / 0);
- } catch (InvalidCastException e) {
- Console.WriteLine(e.GetType() + " : First Exception");
- throw;
- }
- } catch (NullReferenceException e) {
- Console.WriteLine(e.GetType() + " : Second Exception");
- Console.ReadLine();
- }
None of the catch block will get caught. We will get an error at Console.WriteLine(i/0);
- What are custom/user defined exceptions?
As the name hints, the exceptions which are defined by users are termed as user-defined exceptions.
Sample Code
- static void Main(string[] args) {
- FileClass file = new FileClass();
- try {
- file.openFile();
- } catch (FileNotFoundException e) {
- Console.WriteLine("File not found: {0}", e.Message);
- }
- Console.ReadKey();
- }
- public class FileNotFoundException: Exception {
- public FileNotFoundException(string message): base(message) {}
- }
- public class FileClass {
- string filepath = "D:\\temp\\123.txt";
- public void openFile() {
- if (!File.Exists(filepath)) {
- throw (new FileNotFoundException("No File Exists"));
- } else {
- Console.WriteLine("File not Found: {0}", filepath);
- }
- }
- }
- }
Output
File not found: No File Exists.
- What are Tracers?
Tracer is a class used to produce the messages that monitor the code execution.
- What is Tuple?
Tuple is a data structure that represents a single set of data.
Sample
- var member = new Tuple<string, string, int>("Sridhar", "Sharma", 2006);
- Console.WriteLine(member.Item1+" "+ member.Item2 +" is a member of csharpcorner since "+ member.Item3);
- Console.ReadLine();
Output
Sridhar Sharma is a member of csharpcorner since 2006
- What are Resource Files? Or, How will you generate Messages in languages other than English?
A Resource file is an XML file that can contain strings and other resources, such as image file paths. Resource files are typically used to store user interface strings like custom messages that must be translated into other languages.
Let us have an example to demonstrate use of Resources.
- Add a Resource file by right clicking on project and name it as CustomMessages.
- Add some sample values in it.
- Add another Resource file and rename it as hi-IN.
Note
hi-IN stands for hindi language from India (http://www.localeplanet.com/dotnet/hi-IN/index.html)
- Let’s add some data to it.
- Add a Windows form and place two buttons. One button click will display a message in English and the other one will display the message in Hindi.
- private void button1_Click(object sender, EventArgs e) {
- Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
- MessageBox.Show(CustomMessages.HelloWorld);
- }
- private void button2_Click(object sender, EventArgs e) {
- Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("hi-IN");
- MessageBox.Show(CustomMessages.HelloWorld);
- }
Let us run the code and see how the messages would be displayed.
Let us click "Hindi".
That’s it.
- How to retrieve 3rd highest salaried employee from a list of employees?
- var employee = employees
- .OrderByDescending(e => e.Salary)
- .Skip(2).First();
- What is an Array?
An array is a collection of items of the same data type.
- int[] rollnumbers = new int[] { 1291, 1292, 1293, 1294 };
- What is the difference between Parse and TryParse?
Parsing is the process of converting a value from one data type to another.
The Prase() method throws three different types of exceptions depending upon the data provided - if the input is null, or in other format, or out of range.
- If the parameter value is null, then it will throw ArgumentNullException.
- If the parameter value is other than specified data type, it will throw FormatException.
- If the parameter value is out of range, then it will throw OverflowException.
- string number = "1234";
- string n = null;
- try {
- Console.WriteLine(int.Parse(number));
- Console.WriteLine(int.Parse(n));
- } catch (Exception e) {
- Console.WriteLine(e);
- Console.ReadLine();
- throw;
- }
When we run the above code, we will get the below output.
1234
System.ArgumentNullException: Value cannot be null.
Parameter name: String
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at LambdaExpressionSample.Program.Main(String[] args) in D:\Projects\SampleInterview\LambdaExpressionSample\Program.cs:line 28
In order to come out of these runtime issues, it is better to use TryParse().
Let us modify the above code and see how it will behave.
- string number = "1234";
- string n = null;
- int num;
- bool bCheck = int.TryParse(n, out num);
- try {
- Console.WriteLine(int.Parse(number));
- if (bCheck) {
- Console.WriteLine(int.Parse(n));
- }
- Console.ReadLine();
- } catch (Exception e) {
- Console.WriteLine(e);
- Console.ReadLine();
- throw;
- }
When we run the above code, we will get the below output.
1234