Q1. Which file extension is used for C# source files?
C# source files use a short extension starting with c.
Answer: .cs — C# source code files conventionally use the .cs extension.
Q2. Which keyword declares a class in C#?
Used to define reference types.
Answer: class — use `class MyClass { }` to define a class in C#.
Q3. Which method is the program entry point in a C# console app?
Must be static and named Main.
Answer: static void Main(string[] args) — the runtime looks for Main as the entry point; signatures may vary (return int, no args).
Q4. Which type represents a 32-bit integer in C#?
Alias for System.Int32.
Answer: int — `int` is an alias for System.Int32 (32-bit signed integer).
Q5. Fill: Declare a read-only field in a class using the keyword
Field can be assigned only at declaration or in constructor.
Answer: readonly — `public readonly int x;` makes x assignable only at declaration or in constructor.
Q6. Which keyword is used to inherit from a base class?
C# uses a punctuation symbol after the class name.
Answer: : — `class Child : Parent` declares that Child inherits from Parent.
Q7. Which access modifier makes a member visible only within its class?
Default for class members if no modifier is specified.
Answer: private — private members are accessible only within the declaring type.
Q8. Which keyword declares an interface?
Interfaces declare method signatures without implementation.
Answer: interface — `public interface IMy { void M(); }`.
Q9. Which collection type preserves insertion order and allows duplicates?
Dynamic array-like collection in System.Collections.Generic.
Answer: List<T> — preserves insertion order and allows duplicate elements.
Q10. Which keyword marks a method that can be overridden in derived classes?
Declare in base class to allow overrides.
Answer: virtual — `public virtual void M() { }` allows derived classes to override.
Q11. Which keyword is used in derived class to override a virtual method?
Used in the derived method declaration.
Answer: override — `public override void M() { }` overrides a virtual base method.
Q12. Which keyword prevents further overriding of a virtual method?
Used together with override.
Answer: sealed — `public sealed override void M() { }` prevents further overrides in subclasses.
Q13. Which type is immutable and represents text?
Use StringBuilder for mutable text operations.
Answer: string — strings are immutable; StringBuilder is used for efficient mutations.
Q14. Fill: Convert string "123" to int using
Method on System.Int32 to parse string.
Answer: int.Parse — `int.Parse("123")` returns integer 123; TryParse is safer for invalid input.
Q15. Which method safely tries to parse a string to int without throwing?
Returns bool indicating success.
Answer: int.TryParse — `int.TryParse(s, out var n)` returns true/false and sets n on success.
Q16. Which statement creates a dictionary mapping string to int?
Generic dictionary in System.Collections.Generic.
Answer: new Dictionary<string,int>() — creates a generic dictionary for key-value pairs.
Q17. Which LINQ method filters a sequence by a predicate?
Used to keep elements matching a condition.
Answer: Where() — `collection.Where(x => x > 0)` filters elements by predicate.
Q18. Which LINQ method projects each element into a new form?
Transforms elements, similar to map in other languages.
Answer: Select() — `items.Select(x => x.Property)` projects elements to a new form.
Q19. Which keyword declares an asynchronous method?
Used before the return type.
Answer: async — `public async Task DoAsync()` marks a method as asynchronous; use await inside it.
Q20. Which keyword is used to wait for an asynchronous operation inside an async method?
Used only inside async methods.
Answer: await — suspends execution until the awaited Task completes without blocking the thread.
Q21. Which type represents a task that returns a value asynchronously?
Generic Task holds a result type.
Answer: Task<T> — represents an asynchronous operation that produces a result of type T.
Q22. Which keyword declares a value type that is allocated on the stack?
Value types are copied on assignment.
Answer: struct — structs are value types (allocated on stack or inline) and are copied by value.
Q23. Which C# feature provides immutable data containers with value-based equality?
Introduced in C# 9 for concise immutable types.
Answer: record — records provide concise syntax for immutable data and value-based equality semantics.
Q24. Which keyword declares a generic type parameter?
Used after type or method name.
Answer: <T> — `class MyClass<T> { }` declares a generic type parameter T.
Q25. Which exception is thrown for null references when dereferenced?
Occurs when accessing members on a null object reference.
Answer: NullReferenceException — thrown when code attempts to use a null object reference.
Q26. Which construct is used to handle exceptions?
Use catch to handle thrown exceptions.
Answer: try / catch / finally — use try to run code, catch to handle exceptions, finally for cleanup.
Q27. Which operator is used for null-coalescing to provide a fallback value?
Returns left operand if not null, otherwise right operand.
Answer: ?? — `var x = a ?? b;` uses b when a is null.
Q28. Which operator checks for null before accessing a member (safe navigation)?
Returns null if left-hand side is null instead of throwing.
Answer: ?. — `obj?.Property` safely accesses Property if obj is not null.
Q29. Which delegate type represents a method with no return value?
Generic delegate for void-returning methods.
Answer: Action — `Action` and `Action<T>` represent methods that return void.
Q30. Which delegate type represents a method that returns a value?
Func delegates return a value; generic parameters include input types and result.
Answer: Func<TResult> — `Func<int>` returns an int; `Func<T1,TResult>` takes args and returns TResult.
Q31. Which event pattern is common in .NET for event handlers?
Standard EventHandler signature.
Answer: object sender, EventArgs e — `void Handler(object sender, EventArgs e)` is the conventional pattern.
Q32. Which keyword creates an anonymous method using lambda syntax?
Used in expressions like x => x * 2.
Answer: => — lambda operator creates anonymous functions: `x => x * 2`.
Q33. Which statement declares a nullable int variable?
Nullable shorthand and generic form are equivalent.
Answer: Both A and B — `int? x` is shorthand for `Nullable<int> x`.
Q34. Which operator is used to perform string interpolation?
Prefix string with $ and use {expr} inside.
Answer: $" — `var s = $"Hello {name}";` uses interpolation to embed expressions.
Q35. Which keyword prevents a class from being inherited?
Used on class declaration to stop inheritance.
Answer: sealed — `public sealed class C { }` cannot be used as a base class.
Q36. Which operator is used to access a member of an object?
Dot operator is used for member access.
Answer: . — `obj.Member` accesses a member of obj.
Q37. Which method adds an item to a List<T>?
Standard method on List<T>.
Answer: Add() — `myList.Add(item)` appends an item to the list.
Q38. Which namespace contains common collection types like List and Dictionary?
Generic collections live here.
Answer: System.Collections.Generic — contains List<T>, Dictionary<K,V>, HashSet<T>, etc.
Q39. Which operator is used to cast an object to a specific type and throws on failure?
Direct cast syntax throws InvalidCastException if incompatible.
Answer: (T)obj — direct cast throws if obj is not compatible with T; `as` returns null on failure for reference types.
Q40. Which operator checks if an object is of a given type without casting?
Returns true if object is compatible with the type.
Answer: is — `if (obj is MyType) { ... }` checks type compatibility.
Q41. Which method reads all text from a file path?
Convenience method in System.IO.File.
Answer: File.ReadAllText(path) — reads entire file contents as a string.
Q42. Which attribute marks a method as obsolete and issues a compile-time warning?
Can include a message and error flag.
Answer: [Obsolete] — `[Obsolete("msg")]` produces a warning; `[Obsolete("msg", true)]` causes an error.
Q43. Which feature allows pattern matching with types and values (C# 7+)?
Used in switch and is expressions.
Answer: pattern matching — e.g., `if (obj is Person p) { ... }` or `case int n when n>0:` in switch.
Q44. Which keyword declares a read-only property with expression-bodied getter?
Used for concise property or method bodies.
Answer: => — `public int X => _value;` defines a read-only property with expression body.
Q45. Which modifier allows property initialization only during object creation (C# 9)?
Allows immutable-like initialization with object initializers.
Answer: init — `public int X { get; init; }` can be set during object creation but not afterwards.
Q46. Which operator concatenates strings?
Use + or String.Concat for concatenation.
Answer: + — `s1 + s2` concatenates strings; `string.Concat` is an alternative.
Q47. Which method returns the number of elements in an IEnumerable via LINQ?
LINQ extension method in System.Linq.
Answer: Count() — `collection.Count()` returns the number of elements (requires System.Linq for IEnumerable).
Q48. Which keyword indicates a method or property is part of an explicit interface implementation?
Syntax places interface name before member in implementation.
Answer: InterfaceName.Member — `void IFoo.Bar() { }` is explicit implementation and not accessible via class reference.
Q49. Which operator returns the runtime type of an expression?
typeof returns compile-time Type; GetType is instance method.
Answer: GetType() — `obj.GetType()` returns the runtime Type; `typeof(T)` returns the Type object for compile-time type T.
Q50. Which practice is recommended for disposing unmanaged resources?
Use deterministic disposal patterns.
Answer: Implement IDisposable and use using statement — ensures deterministic cleanup of unmanaged resources; finalizers are a fallback.
Your Score: 0 / 50
0 Comments