MCQ Related “C#”

C# Basic MCQs

  1. What is the correct file extension for a C# program file?
    • a) .cs
    • b) .csharp
    • c) .c
    • d) .cpp
  2. Which of the following is a valid way to declare a variable in C#?
    • a) int x;
    • b) int x = 10;
    • c) x = 10;
    • d) Both a and b
  3. Which of the following types can hold a decimal number in C#?
    • a) int
    • b) float
    • c) double
    • d) Both b and c
  4. What keyword is used to create a new class in C#?
    • a) class
    • b) create
    • c) new
    • d) define
  5. Which of the following statements is true about arrays in C#?
    • a) Arrays can hold multiple data types.
    • b) The size of an array is dynamic.
    • c) Array index starts from 0.
    • d) Arrays cannot be resized.
  6. How do you declare a constant in C#?
    • a) const int MY_CONSTANT = 10;
    • b) int const MY_CONSTANT = 10;
    • c) constant int MY_CONSTANT = 10;
    • d) final int MY_CONSTANT = 10;
  7. What is the output of the following code: Console.WriteLine(5 + 10);?
    • a) 15
    • b) 510
    • c) Error
    • d) 5 and 10
  8. Which of the following is used to handle exceptions in C#?
    • a) try-catch
    • b) except
    • c) finally
    • d) handle
  9. In C#, which keyword is used for inheritance?
    • a) extends
    • b) implements
    • c) inherits
    • d) :
  10. What is the default value of a boolean variable in C#?
    • a) true
    • b) false
    • c) 0
    • d) null
  11. Which of the following is not a C# access modifier?
    • a) public
    • b) private
    • c) static
    • d) protected
  12. How do you create a single-line comment in C#?
    • a) // This is a comment
    • b) /* This is a comment */
    • c) # This is a comment
    • d) — This is a comment
  13. What does the static keyword indicate in a method declaration?
    • a) The method can only be called from another class.
    • b) The method belongs to the class itself rather than instances of the class.
    • c) The method will not return any value.
    • d) None of the above.
  14. Which of the following is a correct way to declare a method in C#?
    • a) void MyMethod() { }
    • b) MyMethod void() { }
    • c) method void MyMethod() { }
    • d) function void MyMethod() { }
  15. What does the new keyword do in C#?
    • a) It declares a variable.
    • b) It initializes an object.
    • c) It creates a new class.
    • d) All of the above.
  16. What is the purpose of the using statement in C#?
    • a) To include namespaces.
    • b) To create classes.
    • c) To define variables.
    • d) To handle exceptions.
  17. Which of the following statements is used to exit a loop in C#?
    • a) exit
    • b) break
    • c) stop
    • d) return
  18. What is the size of an int variable in C#?
    • a) 2 bytes
    • b) 4 bytes
    • c) 8 bytes
    • d) Depends on the system
  19. In C#, how do you check if a string is empty?
    • a) String.IsNullOrEmpty(myString);
    • b) myString.IsEmpty();
    • c) myString.Length == 0;
    • d) Both a and c.
  20. Which operator is used to assign a value to a variable in C#?
    • a) =
    • b) ==
    • c) :=
    • d) =>
  21. What is the valid way to create an interface in C#?
    • a) interface IMyInterface { }
    • b) interface MyInterface { }
    • c) class IMyInterface { }
    • d) interface: IMyInterface { }
  22. Which of the following collections allows duplicate elements in C#?
    • a) HashSet
    • b) Dictionary
    • c) List
    • d) Queue
  23. What method is called when an object is created in a class?
    • a) Main
    • b) Constructor
    • c) Method
    • d) Init
  24. How can you convert an integer to a string in C#?
    • a) Convert.ToString(myInt);
    • b) myInt.ToString();
    • c) string(myInt);
    • d) Both a and b.
  25. What is the purpose of the override keyword in C#?
    • a) To create a new method.
    • b) To change the implementation of a base class method.
    • c) To prevent a method from being overridden.
    • d) None of the above.
  26. What is the correct way to declare a nullable type in C#?
    • a) int? x;
    • b) Nullable<int> x;
    • c) Both a and b.
    • d) int x?;
  27. Which of the following is a valid tuple declaration in C#?
    • a) var myTuple = (1, “Hello”);
    • b) var myTuple: (int, string) = (1, “Hello”);
    • c) Tuple<int, string> myTuple = (1, “Hello”);
    • d) myTuple = (1, “Hello”);
  28. What operator is used for string concatenation in C#?
    • a) +
    • b) &
    • c) .
    • d) *
  29. Which of the following is a primitive data type in C#?
    • a) String
    • b) List
    • c) int
    • d) Dictionary
  30. How do you prevent a class from being inherited in C#?
    • a) sealed
    • b) private
    • c) protected
    • d) final
  31. What is the purpose of a delegate in C#?
    • a) To create an object.
    • b) To hold a reference to a method.
    • c) To manage memory.
    • d) To define a class.
  32. Which of the following is used to define an enum in C#?
    • a) enum Colors { Red, Green, Blue }
    • b) enum Colors: { Red, Green, Blue }
    • c) Colors { Red, Green, Blue }
    • d) def enum Colors { Red, Green, Blue }
  33. How can you make a method generic in C#?
    • a) public void MyMethod<T>() { }
    • b) public void MyMethod(T) { }
    • c) generic void MyMethod() { }
    • d) public MyMethod<T>() { }
  34. What does the params keyword in C# signify?
    • a) It’s used for exceptions.
    • b) It allows passing a variable number of arguments to a method.
    • c) It’s for array declarations.
    • d) None of the above.
  35. Which of the following allows multiple methods with the same name in C#?
    • a) Abstraction
    • b) Polymorphism
    • c) Inheritance
    • d) Encapsulation
  36. Which of the following is the correct way to declare a property in C#?
    • a) public int Property { get; set; }
    • b) public int Property() { }
    • c) public int Property: get; set;
    • d) int Property { }
  37. How do you create a read-only property in C#?
    • a) public int Property { get; }
    • b) public int Property() { return 0; }
    • c) public int Property: get;
    • d) int Property { return 0; }
  38. What does the ref keyword mean when passed as a parameter in C#?
    • a) It creates a copy of the argument.
    • b) It allows the method to modify the passed argument.
    • c) It prevents modification of the argument.
    • d) None of the above.
  39. Which of the following is not a valid loop statement in C#?
    • a) for
    • b) while
    • c) repeat
    • d) do-while
  40. What is the purpose of the finally block in C#?
    • a) To execute code when an exception is caught.
    • b) To execute code regardless of whether an exception occurred.
    • c) To define a method.
    • d) To create a loop.
  41. In C#, which of the following is used to import namespaces?
    • a) include
    • b) import
    • c) using
    • d) require
  42. What does the async keyword signify in a method in C#?
    • a) The method is synchronous.
    • b) The method runs on a separate thread.
    • c) The method can contain await statements.
    • d) Both b and c.
  43. Which method is called when the application starts in C#?
    • a) Run
    • b) Start
    • c) Main
    • d) Init
  44. What does LINQ stand for in C#?
    • a) Language Integrated Query
    • b) Language Interfaced Queue
    • c) Language Interactive Query
    • d) None of the above.
  45. What is a namespace in C# used for?
    • a) To organize code.
    • b) To define class attributes.
    • c) To create methods.
    • d) None of the above.
  46. How do you create an instance of a class in C#?
    • a) MyClass myObj = new MyClass();
    • b) var myObj = new MyClass();
    • c) Both a and b.
    • d) Class MyClass = new myObj();
  47. What does the override keyword do when applied to a method?
    • a) Defines a new method.
    • b) Changes the accessibility of a method.
    • c) Modifies a base class’s method implementation.
    • d) Prevents a method from being overridden.
  48. Which of the following is the correct syntax for an if statement in C#?
    • a) if (condition) { }
    • b) if condition { }
    • c) if (condition) then { }
    • d) if [condition] { }
  49. How do you define a list in C#?
    • a) List<int> myList = new List<int>();
    • b) int[] myList = new List<int>();
    • c) myList = new List<int>();
    • d) List myList = new List<int>();
  50. What is the purpose of readonly in C#?
    • a) To declare variables that can be changed.
    • b) To declare properties that can only be set in the constructor.
    • c) To create constants.