🧩 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 folowing does an indexer allow to index in the same way as an array? 1. A class 2. A property 3. A struct 4. A function 5. An interfaceAnswer: 1, 3, 5 An Employee class has a property called age and emp is reference to a Employee object and we want the statement Console.WriteLine(emp.age) to fail. Which of the following options will ensure this functionality?Answer: Declare age property with only set accessor. Which of the following statements are correct about an enum used inC#.NET? 1.By default the first enumerator has the value equal to the number of elements present in the list. 2. The value of each successive enumerator is decreased by 1. 3. An enumerator contains white space in its name. 4. A variable cannot be assigned to an enum element. 5. Values of enum elements cannot be populated from a database.Answer: 4, 5 Which of the following statements is correct about the C#.NET code snippet given below? int a = 10; int b = 20; int c = 30; enum color: byte { red = a, green = b, blue = c }Answer: Variables cannot be assigned to enum elements. Which of the following statements is true about an enum used in C#.NET?Answer: An enum variable can be defined inside a class or a namespace. Which of the following is the correct output for the C#.NET code snippet given below? enum color { red, green, blue } color c; c = color.red; Console.WriteLine(c);Answer: red Which of the following statements are correct about an enum used inC#.NET? 1. To use the keyword enum, we should either use [enum] or System.Enum. 2. enum is a keyword. 3. Enum is class declared in System.Type namespace. 4. Enum is a class declared in the current project's root namespace. 5. Enum is a class declared in System namespace.Answer: 2, 5 Which of the following will be the correct output for the C#.NET code snippet given below? enum color : int { red = -3, green, blue } Console.Write( (int) color.red + ", "); Console.Write( (int) color.green + ", "); Console.Write( (int) color.blue );Answer: -3, -2, -1 An enum that is declared inside a class, struct, namespace or interface is treated as public.Answer: True Which of the following statements is correct about the C#.NET code snippet given below? enum per { married, unmarried, divorced, spinster } per.married = 10; Console.WriteLine(per.unmarried);Answer: The program will report an error since an enum element cannot be assigned a value outside the enum declaration. Which of the following is the correct output for the C#.NET code snippet given below? enum color: int { red, green, blue = 5, cyan, magenta = 10, yellow } Console.Write( (int) color.green + ", " ); Console.Write( (int) color.yellow );Answer: 1, 11 An enum can be declared inside a class, struct, namespace or interface.Answer: True Which of the following CANNOT be used as an underlying datatype for an enum in C#.NET?Answer: float Which of the following statements are correct about enum used in C#.NET? 1. Every enum is derived from an Object class. 2. Every enum is a value type. 3. There does not exist a way to print an element of an enum as a string. 4. Every enum is a reference type. 5. The default underlying datatype of an enum is int.Answer: 1, 2, 5 Which of the following statements is correct about the C#.NET code snippet given below? enum color : byte { red = 500, green = 1000, blue = 1500 }Answer: Since 500, 1000, 1500 exceed the valid range of byte compiler will report an error. Which of the following is the correct output for the C#.NET code snippet given below? enum color { red, green, blue } color c = color.red; Type t; t = c.GetType(); string[ ]str; str = Enum.GetNames(t); Console.WriteLine(str[ 0 ]);Answer: red Which of the following statements is correct about an enum used in C#.NET?Answer: enum is a value type. Which of the following statements are correct about an enum used in C#.NET? 1. An enum can be declared inside a class. 2. An enum can take Single, Double or Decimal values. 3. An enum can be declared outside a class. 4. An enum can be declared inside/outside a namespace. 5. An object can be assigned to an enum variable.Answer: 1, 3, 4 The space required for structure variables is allocated on stack.Answer: True Creating empty structures is allowed in C#.NET.Answer: False Which of the following will be the correct output for the C#.NET program given below? namespace IndiabixConsoleApplication { struct Sample { public int i; } class MyProgram { static void Main() { Sample x = new Sample(); x.i = 10; fun(x); Console.Write(x.i + " "); } static void fun(Sample y) { y.i = 20; Console.Write(y.i + " "); } } }Answer: 20 10 Which of the following is the correct way of setting values into the structure variable e defined below? struct Emp { public String name; public int age; public Single sal; } Emp e = new Emp();Answer: e.name = "Amol"; e.age = 25; e.sal = 5500; Which of the following is the correct way to define a variable of the type struct Emp declared below? struct Emp { private String name; private int age; private Single sal; } 1. Emp e(); e = new Emp(); 2. Emp e = new Emp; 3. Emp e; e = new Emp; 4. Emp e = new Emp(); 5. Emp e;Answer: 4, 5 Which of the following statements is correct about the C#.NET code snippet given below? class Trial { int i; Decimal d; } struct Sample { private int x; private Single y; private Trial z; } Sample ss = new Sample();Answer: ss will be created on the stack. How many bytes will the structure variable samp occupy in memory if it is defined as shown below? class Trial { int i; Decimal d; } struct Sample { private int x; private Single y; private Trial z; } Sample samp = new Sample();Answer: 12 bytes Which of the following will be the correct result of the statement b = a in the C#.NET code snippet given below? struct Address { private int plotno; private String city; } Address a = new Address(); Address b; b = a;Answer: All elements of a will get copied into corresponding elements of b. Which of the following statements are correct? 1. A struct can contain properties. 2. A struct can contain constructors. 3. A struct can contain protected data members. 4. A struct cannot contain methods. 5. A struct cannot contain constants.Answer: 1, 2 C#.NET structures are always value types.Answer: True When would a structure variable get destroyed?Answer: When it goes out of scope. Once a structure variable has been defined, its members can be accessed using something called the:Answer: Dot operator