C# Exam, 25 Questions

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By P.kusuma
P
P.kusuma
Community Contributor
Quizzes Created: 1 | Total Attempts: 333
Questions: 25 | Attempts: 340

SettingsSettingsSettings
C# Exam, 25 Questions - Quiz


C# exam as preparation for deal with the real C# exam that conducted by our beloved customer.
Prepared by: R&D Dept.


Questions and Answers
  • 1. 

    What will be the output of the C#.NET code snippet given below?int a = 10, b = 20, c = 30; int res = a < b ? a < c ? c : a : b; Console.WriteLine(res);

    • A.

      30

    • B.

      Compile Error / Syntax Error

    • C.

      10

    • D.

      20

    • E.

      None of the above

    Correct Answer
    A. 30
    Explanation
    The code snippet uses the ternary operator to compare the values of variables a, b, and c. It first checks if a is less than b, if true, it then checks if a is less than c. If both conditions are true, it assigns the value of c to the variable res. Since a is less than b and c, the value of res will be 30.

    Rate this question:

  • 2. 

    Which of the following are the correct ways to declare a delegate for calling the function func() defined in the sample class given below? class Sample { public int func(int i, Single j) { /* Add code here. */ } }

    • A.

      Delegate d(int i, Single j);

    • B.

      Delegate void d(int, Single);

    • C.

      Delegate int d(int i, Single j);

    • D.

      Delegate void (int i, Single j);

    • E.

      Delegate int sample.func(int i, Single j);

    Correct Answer
    C. Delegate int d(int i, Single j);
    Explanation
    The correct answer is "delegate int d(int i, Single j)". This is the correct way to declare a delegate for calling the function func() defined in the Sample class. The delegate declaration starts with the keyword "delegate" followed by the return type of the function, which in this case is "int". Then, the delegate name "d" is specified, along with the parameters of the function, which are "int i" and "Single j".

    Rate this question:

  • 3. 

    A derived class can stop virtual inheritance by declaring an override as

    • A.

      Extends

    • B.

      Inherits

    • C.

      Sealed

    • D.

      Inheritable

    Correct Answer
    C. Sealed
    Explanation
    In object-oriented programming, when a class is declared as "sealed", it means that it cannot be inherited by any other class. In the context of the given question, if a derived class declares a method as "sealed", it indicates that this method cannot be overridden by any further derived classes. Therefore, the correct answer is "sealed" as it stops the virtual inheritance by preventing further overriding of the method in derived classes.

    Rate this question:

  • 4. 

    For the code snippet shown below, which of the following statements are valid? public class Generic<T> { public T Field; public void TestSub() { T i = Field + 1; } } class MyProgram { static void Main(string[] args) { Generic<int> gen = new Generic<int>(); gen.TestSub(); } }

    • A.

      Program will generate run-time exception.

    • B.

      Compiler will report an error: Operator '+' is not defined for types T and int.

    • C.

      Result of addition is system-dependent.

    • D.

      Addition will produce result 1.

    Correct Answer
    B. Compiler will report an error: Operator '+' is not defined for types T and int.
    Explanation
    The code snippet defines a generic class Generic with a public field of type T and a method TestSub() which tries to perform addition on the field. In the Main method, an instance of Generic is created and the TestSub() method is called. Since the type T is not constrained to be a type that supports addition, the compiler will report an error stating that the operator '+' is not defined for types T and int.

    Rate this question:

  • 5. 

    For the code snippet shown below, which of the following statements are valid?   public class TestIndiaBix { public void TestSub (M arg) { Console.Write(arg); } } class MyProgram { static void Main(string[] args) { TestIndiaBix bix = new TestIndiaBix(); bix.TestSub("IndonesiaBIX "); bix.TestSub(4.2f); } }

    • A.

      Program will compile and on execution will print: IndonesiaBIX 4.2

    • B.

      Compiler will generate an error.

    • C.

      A non generic class Hello cannot have generic subroutine.

    • D.

      None of the above.

    Correct Answer
    B. Compiler will generate an error.
    Explanation
    The TestSub method in the TestIndiaBix class is expecting a parameter of type M, but no definition of type M is provided in the given code snippet.
    When calling the TestSub method with arguments "IndonesiaBIX " and 4.2f, the compiler will not be able to find a suitable overload of the TestSub method that accepts arguments of type string and float, respectively.
    Therefore, the compiler will generate an error indicating that there is no suitable method overload for the provided arguments.

    Rate this question:

  • 6. 

    Which of the following statements are correct about an ArrayList collection that implements the IEnumerable interface?
    1. The ArrayList class contains an inner class that implements the IEnumeratorinterface.
    2. An ArrayList Collection cannot be accessed simultaneously by different threads.
    3. The inner class of ArrayList can access ArrayList class's members.
    4. To access members of ArrayList from the inner class, it is necessary to passArrayList class's reference to it.
    5. Enumerator's of ArrayList Collection can manipulate the array.

    • A.

      2 and 5 only

    • B.

      1 and 2 only

    • C.

      1 and 3 and 4 only

    • D.

      All of the above

    Correct Answer
    C. 1 and 3 and 4 only
    Explanation
    The given correct answer is 1 and 3 and 4 only. This means that the statements 1, 3, and 4 are correct about an ArrayList collection that implements the IEnumerable interface. Statement 1 is correct because the ArrayList class contains an inner class that implements the IEnumerator interface. Statement 3 is correct because the inner class of ArrayList can access ArrayList class's members. Statement 4 is correct because to access members of ArrayList from the inner class, it is necessary to pass ArrayList class's reference to it.

    Rate this question:

  • 7. 

    In which of the following collections is the Input/Output index-based? 
    1. Stack
    2. Queue
    3. BitArray
    4. ArrayList
    5. HashTable
     

    • A.

      5 only

    • B.

      1, 2 and 5 only

    • C.

      3 and 4 only

    • D.

      1 and 2 only

    Correct Answer
    C. 3 and 4 only
    Explanation
    The correct answer is 3 and 4 only. A BitArray is a collection that allows access to its elements using an index, similar to an array. A HashTable, on the other hand, is not index-based but instead uses keys to access its elements. The other options, Stack, Queue, and ArrayList, also do not support index-based access.

    Rate this question:

  • 8. 

    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 }

    • A.

      Variables can be assigned only to the first enum element.

    • B.

      Variables cannot be assigned to enum elements.

    • C.

      Variables can be assigned to any one of the enum elements.

    • D.

      Values assigned to enum elements must always begin with 0.

    Correct Answer
    B. Variables cannot be assigned to enum elements.
    Explanation
    The given C#.NET code snippet is attempting to assign variables a, b, and c to the enum elements red, green, and blue respectively. However, this is not allowed in C#.NET. Enum elements must be assigned constant values at compile-time, and variables are not considered constant values. Therefore, the correct statement is that variables cannot be assigned to enum elements.

    Rate this question:

  • 9. 

    Which of the following statements is correct about the C#.NET code snippet given below? switch (id) { case 6: grp = "Grp B"; break; case 13: grp = "Grp D"; break; case 1: grp = "Grp A"; break; case ls > 20: grp = "Grp E"; break ; case Else: grp = "Grp F"; break; }

    • A.

      There is no error in this switch case statement.

    • B.

      Compiler will report an error in case ls > 20 as well as in case Else.

    • C.

      Compiler will report an error only in case Else.

    • D.

      Compiler will report an error as there is no default case.

    • E.

      The order of the first three cases should be case 1, case 6, case 13 (ascending).

    Correct Answer
    B. Compiler will report an error in case ls > 20 as well as in case Else.
    Explanation
    The given C#.NET code snippet contains an error in the switch case statement. The cases "case ls > 20" and "case Else" are not valid. The switch statement only accepts constant values as cases, not expressions or conditions. Therefore, the compiler will report an error in both of these cases.

    Rate this question:

  • 10. 

    What will be the output of the C#.NET code snippet given below? int i = 2, j = i; if (Convert.ToBoolean((i | j & 5) & (j - 25 * 1))) Console.WriteLine(1); else Console.WriteLine(0);

    • A.

      1

    • B.

      0

    • C.

      Run time Error

    • D.

      Compile Error

    Correct Answer
    B. 0
    Explanation
    The code snippet initializes two variables, i and j, with the value of 2. It then evaluates the condition inside the if statement. The condition is a combination of bitwise OR, bitwise AND, and arithmetic operations.

    First, the bitwise OR operation is performed on i and j with the number 5. This results in 7.

    Next, the bitwise AND operation is performed on the previous result and the subtraction of j from 25 multiplied by 1. Since j is equal to i, which is 2, the subtraction is 0. Therefore, the bitwise AND operation results in 0.

    Finally, the Convert.ToBoolean() method is called on the previous result, which is 0. Since 0 is considered as false, the else block is executed and the output is 0.

    Rate this question:

  • 11. 

    Classes and ObjectsWhich of the following will be the correct output for the C#.NET program given below?namespace IndiabixConsoleApplication { class Sample { int i; Single j; public void SetData(int i, Single j) { this.i = i; this.j = j; } public void Display() { Console.WriteLine(i + " " + j); } } class MyProgram { static void Main(string[ ] args) { Sample s1 = new Sample(); s1.SetData(36, 5.4f); s1.Display(); } } }

    • A.

      36 5.4

    • B.

      36 5.400000

    • C.

      0 0.0

    • D.

      36 5

    • E.

      None of the above

    Correct Answer
    A. 36 5.4
    Explanation
    The correct output for the given C#.NET program will be "36 5.4". This is because the SetData method of the Sample class is called with the arguments 36 and 5.4f, which sets the values of the variables i and j respectively. The Display method then prints the values of i and j, resulting in the output "36 5.4".

    Rate this question:

  • 12. 

    Array. Which of the following is the correct output of the C#.NET code snippet given below? int[ , , ] a = new int[ 3, 2, 3 ]; Console.WriteLine(a.Length);

    • A.

      20

    • B.

      18

    • C.

      4

    • D.

      5

    • E.

      10

    Correct Answer
    B. 18
    Explanation
    The code snippet creates a three-dimensional array with dimensions 3, 2, and 3. The Length property returns the total number of elements in the array, which is calculated by multiplying the lengths of all dimensions together. In this case, the length of the first dimension is 3, the length of the second dimension is 2, and the length of the third dimension is 3. Therefore, the total number of elements in the array is 3 * 2 * 3 = 18.

    Rate this question:

  • 13. 

     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.

    • A.

      2, 4, 5

    • B.

      1, 3

    • C.

      3, 5

    • D.

      4 only

    Correct Answer
    A. 2, 4, 5
    Explanation
    The given answer is correct because in C#.NET, classes must belong to a namespace, whereas structures do not necessarily need to belong to a namespace. Additionally, if a namespace is not mentioned, it takes the name of the current project. Finally, a namespace needs to be imported in order to use the elements within it.

    Rate this question:

  • 14. 

    If ListBox is class present in System.Windows.Forms namespace, then which of the following statements are the correct way to create an object of ListBox Class?  
    • using System.Windows.Forms; ListBox lb = new ListBox();
    • using LBControl = System.Windows.Forms; LBControl lb = new LBControl();
    • System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
    • using LBControl lb = new System.Windows.Forms.ListBox;
    • using LBControl = System.Windows.Forms.ListBox; LBControl lb = new LBControl();

    • A.

      1, 3

    • B.

      1, 3, 5

    • C.

      5 only

    • D.

      2, 4, 5

    Correct Answer
    B. 1, 3, 5
    Explanation
    The correct way to create an object of ListBox class is by using the following statements:

    1. ListBox lb = new ListBox();
    3. System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
    5. LBControl lb = new LBControl();

    These statements correctly import the necessary namespace (System.Windows.Forms) and create an object of the ListBox class using the appropriate syntax.

    Rate this question:

  • 15. 

      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();  

    • A.

      Ss will be created on the stack.

    • B.

      Both ss and z will be created on the heap.

    • C.

      Z will be created on the heap. 

    • D.

      Trial object referred by z will be created on the stack.

    Correct Answer
    D. Trial object referred by z will be created on the stack.
    Explanation
    Trial is a class, and instances of classes are typically created on the heap. However, when a class object is a member of a struct, as in the case of Trial z; within the Sample struct, the memory for that object is part of the struct's memory allocation.
    When an instance of the Sample struct is created (Sample ss = new Sample();), it will be allocated on the stack.
    The Trial object referred to by z will be part of the Sample struct on the stack, and its memory will be managed along with the struct.
    Therefore, the Trial object referred to by z is created on the stack, while the entire Sample struct (ss) is created on the stack.

    Rate this question:

  • 16. 

      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;

    • A.

      Address stored in a will get copied into b.

    • B.

      Once assignment is over a will get garbage collected.

    • C.

      All elements of a will get copied into corresponding elements of b.

    • D.

      Once assignment is over a will go out of scope, hence will die.

    Correct Answer
    C. All elements of a will get copied into corresponding elements of b.
    Explanation
    When a struct is assigned to another struct variable in C#, a member-wise copy of the values of all fields in 'a' is made into 'b'. Therefore, all elements (fields) of struct 'a' will be copied into corresponding elements of struct 'b', resulting in 'b' containing the same values as 'a'. This process does not involve garbage collection or scoping as structs are value types and do not rely on memory allocation on the heap like reference types.

    Rate this question:

  • 17. 

     What will be the output of the C#.NET code snippet given below?namespace IndiabixConsoleApplication { class Baseclass { public void fun() { Console.Write("Base class" + " "); } } class Derived1: Baseclass { new void fun() { Console.Write("Derived1 class" + " "); } } class Derived2: Derived1 { new void fun() { Console.Write("Derived2 class" + " "); } } class Program { public static void Main(string[ ] args) { Derived2 d = new Derived2(); d.fun(); } } }

    • A.

      Derived2 class

    • B.

      Base class

    • C.

      Derived1 class

    • D.

      Base class Derived1 class Derived2 class

    Correct Answer
    C. Derived1 class
    Explanation
    The output of the code will be "Derived1 class". This is because the Main method creates an instance of the Derived2 class and calls the fun() method on that instance. However, the fun() method in the Derived2 class is marked with the "new" keyword, which hides the fun() method in the Baseclass. Since the instance is of type Derived2, the fun() method in the Derived2 class is called, resulting in the output "Derived1 class".

    Rate this question:

  • 18. 

    In an inheritance chain which of the following members of base class are accessible to the derived class members?
    1. static
    2. protected
    3. private
    4. shared
    5. public

    • A.

      2, 5

    • B.

      3, 4

    • C.

      4, 5

    • D.

      1, 3

    Correct Answer
    A. 2, 5
    Explanation
    In an inheritance chain, the derived class can access the static and public members of the base class. Therefore, the correct answer is 2 and 5.

    Rate this question:

  • 19. 

    Which of the following statements are correct?
    1. An argument passed to a ref parameter need not be initialized first.
    2. Variables passed as out arguments need to be initialized prior to being passed.
    3. Argument that uses params keyword must be the last argument of variable argument list of a method.
    4. Pass by reference eliminates the overhead of copying large data items.
    5. To use a ref parameter only the calling method must explicitly use the refkeyword.

    • A.

      3, 4

    • B.

      4, 5

    • C.

      2, 3

    • D.

      1, 2

    Correct Answer
    A. 3, 4
    Explanation
    The third statement is correct because an argument that uses the params keyword must be the last argument of a variable argument list of a method. The fourth statement is correct because pass by reference eliminates the overhead of copying large data items.

    Rate this question:

  • 20. 

    Which of the following is the correct ways to set a value 3.14 in a variable pi such that it cannot be modified?

    • A.

      Const float pi = 3.14F;

    • B.

      #define pi 3.14F;

    • C.

      Const float pi = 3.14F;

    • D.

      Const float pi; pi = 3.14F;

    Correct Answer
    A. Const float pi = 3.14F;
    Explanation
    The correct answer is "const float pi = 3.14F;". This is because using the "const" keyword before declaring a variable makes it a constant, meaning its value cannot be modified once it is assigned. In this case, the variable "pi" is declared as a constant float with a value of 3.14F, ensuring that its value cannot be changed.

    Rate this question:

  • 21. 

    Which of the following keyword is used to overload user-defined types by defining static member functions?

    • A.

      Operatoroverload

    • B.

      Op

    • C.

      Opoverload

    • D.

      Operator

    Correct Answer
    D. Operator
    Explanation
    The keyword "operator" is used to overload user-defined types by defining static member functions. Overloading allows us to define multiple functions with the same name but different parameters, and it can be used to redefine the behavior of an operator when applied to user-defined types. By defining a static member function with the keyword "operator", we can specify how an operator should behave when used with a specific user-defined type.

    Rate this question:

  • 22. 

    A Student class has a property called rollNo and stu is a reference to a Studentobject and we want the statement stu.RollNo = 28 to fail. Which of the following options will ensure this functionality?

    • A.

      Declare rollNo property with get, set and normal accessors.

    • B.

      Declare rollNo property with only get accessor.

    • C.

      Declare rollNo property with both get and set accessors.

    • D.

      None of the above

    Correct Answer
    B. Declare rollNo property with only get accessor.
    Explanation
    If we declare the rollNo property with only a get accessor, it means that we can only retrieve the value of the rollNo property but cannot set a new value to it. Therefore, the statement stu.RollNo = 28 will fail because we are trying to assign a new value to the rollNo property, but it is not allowed due to the absence of a set accessor.

    Rate this question:

  • 23. 

    Which of the following will be the correct output for the C#.NET code snippet given below?String s1 = "Five Star"; String s2 = "FIVE STAR"; int c; c = s1.CompareTo(s2); Console.WriteLine(c);

    • A.

      0

    • B.

      -1

    • C.

      1

    • D.

      2

    Correct Answer
    B. -1
    Explanation
    The code snippet compares two strings, "Five Star" and "FIVE STAR", using the CompareTo method. The CompareTo method returns an integer value that indicates the lexicographic relationship between the two strings. In this case, since "Five Star" comes before "FIVE STAR" in alphabetical order, the CompareTo method will return a negative value. Therefore, the correct output for this code will be -1.

    Rate this question:

  • 24. 

    Which of the following ways to create an object of the Sample class given below will work correctly? class Sample { int i; Single j; double k; public Sample (int ii, Single jj, double kk) { i = ii; j = jj; k = kk; } }

    • A.

      Sample s2 = new Sample(10, 1.2f);

    • B.

      Sample s3 = new Sample(10, 1.2f, 2.4);

    • C.

      Sample s1 = new Sample();

    • D.

      Sample s1 = new Sample(, , 2.5);

    Correct Answer
    B. Sample s3 = new Sample(10, 1.2f, 2.4);
    Explanation
    The Sample class has a constructor that takes three parameters: an int, a Single, and a double. When creating an object of the Sample class, we need to provide values for all three parameters. Therefore, the correct way to create the object is by passing values for all three parameters in the constructor: new Sample(10, 1.2f, 2.4);.

    Rate this question:

  • 25. 

    Which of the following is the root of the .NET type hierarchy?

    • A.

      System.Type

    • B.

      System.Object

    • C.

      System.Base

    • D.

      System.Root

    Correct Answer
    B. System.Object
    Explanation
    The root of the .NET type hierarchy is System.Object. In .NET, all types inherit from the System.Object class, which is the base class for all other classes. This means that every class in .NET is a descendant of System.Object and inherits its methods and properties.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 14, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Sep 29, 2014
    Quiz Created by
    P.kusuma
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.