Unit 3: Advanced C# Programming - Practice Quiz
1 What is the main purpose of LINQ in C#?
2 Which LINQ method is commonly used to filter a collection?
3 Which LINQ method is used to project each element of a collection into a new form?
4
What does the OrderBy() LINQ method do?
5 What is a delegate in C#?
6 What is an event commonly used for in C#?
7 What does an interface primarily define in C#?
8 Which keyword is used when a class implements an interface?
9 What is a main benefit of generics in C#?
10 Which notation is used to specify a generic type parameter?
{}
()
<>
[]
11 Which keyword marks a C# method as asynchronous?
12
What does the await keyword do?
13 What does the Single Responsibility Principle state?
14 Which design best follows SRP?
15 What does the Open/Closed Principle recommend?
16 What is the main idea of the Liskov Substitution Principle?
17 What does the Interface Segregation Principle encourage?
18 Which situation violates ISP?
19 What does the Dependency Inversion Principle suggest?
20 Which C# feature is commonly used to support dependency inversion?
21
What is the result of the following LINQ query?
var numbers = new[] { 2, 5, 8, 11, 14 };
var result = numbers.Where(n => n % 2 == 0).Select(n => n / 2);
Which values are produced when result is enumerated?
2, 5, 8, 11, 14
1, 4, 7
1, 4, 7, 5, 7
2, 8, 14
22
Given the collection below, which LINQ expression returns the name of the first employee whose salary is greater than 60000, or null if no employee matches?
List<Employee> employees;
employees.FirstOrDefault(e => e.Salary > 60000)?.Name
employees.Single(e => e.Salary > 60000).Name
employees.First(e => e.Salary > 60000).Name
employees.Where(e => e.Salary > 60000).First().Name
23
Why does the following query not immediately execute against the list?
var query = products.Where(p => p.Price > 100);
Where method copies the list asynchronously
Price is private
24 Which LINQ operation is most appropriate for grouping orders by their customer identifier?
Join(o => o.CustomerId)
GroupBy(o => o.CustomerId)
Select(o => o.CustomerId)
OrderBy(o => o.CustomerId)
25
A class publishes an event named DataReceived. Which declaration best follows the standard .NET event pattern?
public Func<DataReceivedEventArgs> DataReceived;
public event EventHandler<DataReceivedEventArgs> DataReceived;
public delegate void DataReceived();
public event Action DataReceived;
26 What is a key benefit of using an event instead of exposing a delegate field publicly?
27
A class implements IComparable<Product>. Which method must it provide?
int CompareTo(Product? other)
void CompareTo(object value)
Product CompareWith(Product other)
bool Compare(Product other)
28 What does interface-based programming primarily allow in a C# application?
29
Why is List<int> generally preferable to ArrayList when storing integers?
30
What is the purpose of the generic constraint in this declaration?
static T Create<T>() where T : new()
T must be an interface
T must inherit from object directly
T must be a nullable value type
T must contain a public parameterless constructor
31 Which statement about generic variance is correct for reference types?
IEnumerable<string> can be assigned to IEnumerable<object>
Action<object> can never be assigned to Action<string>
Dictionary<string, int> can be assigned to Dictionary<object, int>
List<string> can be assigned to List<object>
32
What is the main effect of applying await to an incomplete task in an asynchronous method?
33 Which code efficiently starts two independent asynchronous operations and waits for both to finish?
var a = await GetAAsync(); var b = await GetBAsync();
await Task.Run(() => GetAAsync()); await Task.Run(() => GetBAsync());
Task.WaitAll(GetAAsync(), GetBAsync());
var a = GetAAsync(); var b = GetBAsync(); await Task.WhenAll(a, b);
34
Why is calling .Result on an asynchronous operation discouraged in UI applications?
void
35 Which design most closely follows the Single Responsibility Principle?
36
A billing system uses a separate IPricingRule implementation for each discount type. New discount types can be added without modifying the billing service. Which principle does this design demonstrate?
37 Which situation is a likely violation of the Liskov Substitution Principle?
NotSupportedException for a behavior promised by its base type
38
A ReadOnlyReportViewer is forced to implement Print, Export, and Edit methods even though it only displays reports. What refactoring best applies ISP?
39 Which design best demonstrates the Dependency Inversion Principle?
SqlRepository inside its constructor
IRepository and receives an implementation through dependency injection
40 What is a practical testing benefit of injecting an interface-based dependency into a class?
41
Given IEnumerable<int> source = new[] { 1, 2, 3, 4 }; var query = source.Where(x => x % 2 == 0); source = new[] { 5, 6, 7, 8 }; var result = query.ToArray(); What is the value of result?
{ 2, 4 }
source was reassigned
{ 2, 4, 6, 8 }
{ 6, 8 }
42
Which statement best explains why IQueryable<T> queries can behave differently from IEnumerable<T> queries when the source is a remote provider?
IQueryable<T> always executes immediately
IEnumerable<T> automatically batches network requests
IQueryable<T> represents an expression tree for provider translation
IEnumerable<T> can translate expressions into SQL
43
For var values = new[] { 1, 2, 2, 3, 4 };, which expression returns the first element greater than 2, or -1 when no such element exists, without throwing for an empty match?
values.SingleOrDefault(x => x > 2, -1)
values.FirstOrDefault(x => x > 2, -1)
values.First(x => x > 2, -1)
values.DefaultIfEmpty(-1).First(x => x > 2)
44
Consider var query = items.GroupBy(x => x.Category).Select(g => new { g.Key, Count = g.Count() });. Which change is required to guarantee that categories are processed in descending order of their counts?
Distinct before GroupBy
GroupBy with ToLookup
ThenByDescending inside the grouping key
OrderByDescending(x => x.Count) after Select
45 Why should a class normally expose an event instead of exposing a public delegate field for notification callbacks?
46 A publisher raises an event while one subscriber unsubscribes itself during event handling. What is the most reliable way to ensure the current notification uses a stable handler list?
DynamicInvoke for every subscriber
47
A class implements two interfaces that both define a parameterless method named Reset, but the methods require different behavior. Which implementation resolves the collision while preserving both contracts?
new in the class
Reset method only
48 An interface evolves by adding a new abstract member. Existing third-party implementations immediately fail to compile. Which design change most directly reduces this versioning problem in modern C#?
49 Which statement about generic variance is correct for reference types?
IEnumerable<string> can be assigned to IEnumerable<object>
Func<object> can be assigned to Func<string>
Action<object> can be assigned to Action<string>
List<string> can be assigned to List<object>
50
What is the primary purpose of the constraint where T : unmanaged on a generic type parameter?
T to implement IComparable<T>
T to classes with public constructors
T to contain arbitrary reference fields
T to types without managed references
51
A method starts Task<int> a = GetValueAsync(); Task<int> b = GetValueAsync(); and then awaits both tasks. Compared with awaiting the first call before starting the second, what is the main behavioral difference?
52
Why can calling .Result on an incomplete task inside a UI synchronization context cause a deadlock?
.Result
53
Which implementation correctly preserves cancellation semantics for an asynchronous operation that supports a CancellationToken?
TimeoutException
OperationCanceledException and always return success
54
A ReportService queries sales data, formats HTML, writes files, sends email, and records audit entries. Which refactoring best applies SRP?
ReportService
55
A billing method contains a growing switch over payment types, requiring edits whenever a new payment type is introduced. Which design most directly supports OCP?
IPaymentProcessor strategy for each payment type
56
A subtype overrides Save and rejects inputs that the base type accepts, even though callers rely on the base contract. Which principle violation is most directly present?
57
A ReadOnlyFile derives from File, but inherits Write and throws NotSupportedException. What is the strongest design correction?
Write virtual with an empty default body
ReadOnlyFile
58
A Printer interface requires Print, Scan, Fax, and Staple, causing simple printers to implement unused members. Which change best follows ISP?
59
A high-level OrderService directly constructs SqlOrderRepository and SmtpEmailSender. Which change most directly applies DIP?
OrderService
60
A dependency injection composition root registers IClock with a singleton implementation that stores mutable request-specific state. What is the principal design risk?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →