🧩 C# Programming MCQs and Exam Questions

Practice 151 approved 🧩 C# Programming MCQs and exam questions from the 💻 Programming subject.

151 solved MCQs

Which of the following statements are correct about exception handling in C#.NET? 1. If an exception occurs then the program terminates abruptly without getting any chance to recover from the exception. 2. No matter whether an exception occurs or not, the statements in the finally clause (if present) will get executed. 3. A program can contain multiple finally clauses. 4. A finally clause is written outside the try block. 5. finally clause is used to perform clean up operations like closing the network/database connections.Answer: 2 and 5 only Which of the following statements are correct about exception handling in C#.NET? 1. If our program does not catch an exception then the .NET CLR catches it. 2. It is possible to create user-defined exceptions. 3. All types of exceptions can be caught using the Exception class. 4. CLRExceptions is the base class for all exception classes. 5. For every try block there must be a corresponding finally block.Answer: 1, 2 and 3 only Which of the following statements are correct about the exception reported below? Unhandled Exception: System.lndexOutOfRangeException: Index was outside the bounds of the array: at IndiabixConsoleApplication.MyProgram.SetVal(Int32 index, Int32 val) in D:\Sample\IndiabixConsoleApplication\MyProgram.cs:line 26 at IndiabixConsoleApplication.MyProgram.Main(String[] args) in D:\Sample\IndiabixConsoleApplication\MyProgram.cs:line 20 1. The CLR failed to handle the exception. 2. The class MyProgram belongs to the namespace MyProgram. 3. The function SetVal() was called from Main() in line number 20. 4. The exception occurred in line number 26 in the function SetVal() 5. The runtime exception occurred in the project IndiabixConsoleApplication.Answer: 3, 4 and 5 only Which of the following is the Object Oriented way of handling run-time errors?Answer: Exceptions Which of the following statements is correct about the C#.NET program given below if a value "6" is input to it? using System; namespace IndiabixConsoleApplication { class MyProgram { static void Main(string[] args) { int index; int val = 44; int[] a = new int[5]; try { Console.Write("Enter a number:"); index = Convert.Tolnt32(Console.ReadLine()); a[index] = val; } catch(FormatException e) { Console.Write("Bad Format"); } catch(IndexOutOfRangeException e) { Console.Write("Index out of bounds"); } Console.Write("Remaining program"); } } }Answer: It will output: Index out of bounds Remaining program Which of the following statements are correct about the exception reported below? Unhandled Exception: System.lndexOutOfRangeException: Index was outside the bounds of the array. at IndiabixConsoleApplication.Program.Main(String[] args) in D:\ConsoleApplication\Program.cs:line 14 1. The program did not handle an exception called IndexOutOfRangeException. 2. The program execution continued after the exception occurred. 3. The exception occurred in line number 14. 4. In line number 14, the program attempted to access an array element which was beyond the bounds of the array. 5. The CLR could not handle the exception.Answer: 1, 3 and 4 only Which of the following statements are correct about exception handling in C#.NET? 1. try blocks cannot be nested. 2. In one function, there can be only one try block. 3. An exception must be caught in the same function in which it is thrown. 4. All values set up in the exception object are available in the catch block. 5. While throwing a user-defined exception multiple values can be set in the exception, object.Answer: 4 and 5 only Which of the following statements is correct about the C#.NET program given below if a value "6" is input to it? using System; namespace IndiabixConsoleApplication { class MyProgram { static void Main (string[] args) { int index; int val = 66; int[] a = new int[5]; try { Consote.Write("Enter a number: "); index = Convert.ToInt32(Console.ReadLine()); a[index] = val; } catch(Exception e) { Console.Write("Exception occurred "); } Console.Write("Remaining program "); } } }Answer: It will output: Exception occurred Remaining program Which of the following statements is correct about the C#.NET program given below if a value "ABCD" is input to it? using System; namespace IndiabixConsoleApplication { class MyProgram { static void Main(string[] args) { int index; int val = 55; int[] a = new int[5]; try { Console.Write("Enter a number: "); index = Convert.ToInt32(Console.ReadLine()); a[index] = val; } catch(FormatException e) { Console.Write("Bad Format "); } catch(IndexOutOfRangeException e) { Console.Write("Index out of bounds "); } Console.Write("Remaining program "); } } }Answer: It will output: Bad Format Remaining program Which of the following is NOT an Exception?Answer: Incorrect Arithmetic Expression If a namespace is present in a library then which of the following is the correct way to use the elements of the namespace?Answer: Add Reference of the namespace. Import the namespace. Use the elements of the namespace. Which of the following is NOT a namespace in the .NET Framework Class Library?Answer: System.Process Which of the following statements is correct about a namespace in C#.NET?Answer: Namespaces help us to control the visibility of the elements present in it. Which of the following is absolutely neccessary to use a class Point present in namespace Graph stored in library?Answer: Add Reference of the library before using the Point class. Which of the followings are NOT a .NET namespace? 1. System.Web 2. System.Process 3. System.Data 4. System.Drawing2D 5. System.Drawing3DAnswer: 2, 4, 5 Which of the following statements is correct about namespaces in C#.NET?Answer: There is no limit on the number of levels while nesting namespaces. Which of the following statements is correct about the using statement used in C#.NET?Answer: A C#.NET source code file can contain any number of using statement. Which of the following statements are correct about a namespace used in C#.NET? 1. Classes must belong to a namespace, whereas structures need not. 2. Every class, struct, enum, delegate and interlace has to belong to some or the other namespace. 3. All elements of the namespace have to belong to one file. 4. If not mentioned, a namespace takes the name of the current project. 5. The namespace should be imported to be able to use the elements in it.Answer: 2, 4, 5 Which of the following CANNOT belong to a C#.NET Namespace?Answer: Data Which of the following statements is correct about a namespace used in C#.NET?Answer: Nested namespaces are allowed. A property can be declared inside a class, struct, Interface.Answer: True Which of the following statements is correct about properties used in C#.NET?Answer: A property can be either read only or write only. A Student class has a property called rollNo and stu is a reference to a Student object and we want the statement stu.RollNo = 28 to fail. Which of the following options will ensure this functionality?Answer: Declare rollNo property with only get accessor. Which of the following statements are correct? 1. The signature of an indexer consists of the number and types of its formal parameters. 2. Indexers are similar to properties except that their accessors take parameters. 3. Accessors of interface indexers use modifiers. 4. The type of an indexer and the type of its parameters must be at least as accessible as the indexer itself. 5. An interface accessor contains a body.Answer: 1, 2, 4 Which of the following is the correct way to implement a write only property Length in a Sample class?Answer: class Sample { int len; public int Length { set { len = value; } } } If Sample class has a Length property with set accessor then which of the following statements will work correctly?Answer: Sample m = new Sample(); m.Length = 10; If Sample class has a Length property with get accessor then which of the following statements will work correctly?Answer: Sample m = new Sample(); int l; l = m.Length; An Account class has a property called accountNo and acc is a reference to a bank object and we want the C#.NET code snippet given below to work. Which of the following options will ensure this functionality? acc.accountNo = 10; Console.WriteLine(acc.accountNo);Answer: Declare accountNo property with both get and set accessors. Which of the following statements is correct about properties used in C#.NET?Answer: Properties of a class are actually methods that work like data members. Which of the following is the correct way to implement a read only property Length in a Sample class?Answer: class Sample { int len; public int Length { get { return len; } } }