.NET Core interview questions

28 questions from the .NET Core: Build, Test and Ship Web APIs course, each with the correct answer and an explanation. Try to answer before you open the answer.

Getting Started with .NET

1. Which piece must a build machine have to compile C# apps?

  1. Only the runtime
  2. Only a web browser
  3. The .NET SDK
  4. .NET Framework 4.8
Show answer

Answer: The .NET SDK

The SDK includes the compiler and CLI. The runtime alone can run apps but not build them.

Lesson: What .NET Is

2. Which command builds the project and then starts it?

  1. dotnet new
  2. dotnet run
  3. dotnet add
  4. dotnet restore
Show answer

Answer: dotnet run

`dotnet run` restores, builds and launches the app in one step.

Lesson: Your First App with the CLI

3. What does the obj folder contain?

  1. Your hand-written source code
  2. Intermediate build files
  3. User settings secrets
  4. Database migrations
Show answer

Answer: Intermediate build files

It holds generated intermediate files and restore results, safe to delete and regenerate.

Lesson: Inside a Project Folder

C# Essentials for .NET

4. What does `string?` mean with Nullable enabled?

  1. The string is always empty
  2. The string may be null
  3. The string is read-only
  4. The string is a number
Show answer

Answer: The string may be null

The `?` marks a reference type as allowed to be null, so the compiler makes you handle that case.

Lesson: Types, var and Nullability

5. Which LINQ method keeps only items matching a condition?

  1. Select
  2. OrderBy
  3. GroupBy
  4. Where
Show answer

Answer: Where

`Where` filters; `Select` transforms each item into something else.

Lesson: Collections and LINQ

6. Which keyword makes a modified copy of a record?

  1. copy
  2. with
  3. clone
  4. new
Show answer

Answer: with

`with { ... }` creates a new record with the listed properties changed.

Lesson: Classes and Records

7. What should an async method that returns no value return?

  1. void
  2. int
  3. Task
  4. object
Show answer

Answer: Task

Return `Task` so callers can await it and catch exceptions. `async void` hides failures.

Lesson: async and await

Projects, Packages and Solutions

8. Why choose an LTS release for production?

  1. It receives fixes for a longer time
  2. It is always faster
  3. It needs no SDK
  4. It is the only one that runs on Linux
Show answer

Answer: It receives fixes for a longer time

LTS releases are supported longer, which reduces forced upgrades.

Lesson: Target Framework and Language Version

9. Which command lists packages with known vulnerabilities?

  1. dotnet run --safe
  2. dotnet new package
  3. dotnet clean
  4. dotnet list package --vulnerable
Show answer

Answer: dotnet list package --vulnerable

`dotnet list package --vulnerable` checks your references against published advisories.

Lesson: NuGet Packages

10. What does `dotnet sln add` do?

  1. Adds a NuGet package
  2. Adds a project to the solution file
  3. Publishes the solution
  4. Adds a database
Show answer

Answer: Adds a project to the solution file

It registers existing projects in the `.sln` so they build together.

Lesson: Solutions and Project References

Building APIs with ASP.NET Core

11. Which call maps a GET endpoint in a minimal API?

  1. app.UseGet
  2. app.MapGet
  3. builder.AddGet
  4. app.RunGet
Show answer

Answer: app.MapGet

`MapGet(pattern, handler)` registers a handler for GET requests on a route.

Lesson: Your First Minimal API

12. Which status code should a successful POST that creates a resource return?

  1. 200 OK only
  2. 204 No Content
  3. 404 Not Found
  4. 201 Created
Show answer

Answer: 201 Created

201 signals that a new resource exists, ideally with a Location header pointing to it.

Lesson: Routing and Model Binding

13. What happens if a middleware never calls next()?

  1. The request is short-circuited there
  2. The server restarts
  3. The next middleware runs twice
  4. The app fails to compile
Show answer

Answer: The request is short-circuited there

Not calling `next()` short-circuits the pipeline; later middleware and the endpoint never run.

Lesson: The Middleware Pipeline

14. Which source wins when the same key is set in appsettings.json and an environment variable?

  1. appsettings.json
  2. The first one the app reads
  3. Neither; it throws an error
  4. The environment variable
Show answer

Answer: The environment variable

Environment variables are added after the JSON files, so they override them.

Lesson: Configuration and Environments

Dependency Injection and Services

15. Why depend on an interface instead of a concrete class?

  1. Interfaces are faster at runtime
  2. It lets you swap implementations, such as a fake in tests
  3. The compiler requires it
  4. It removes the need for a container
Show answer

Answer: It lets you swap implementations, such as a fake in tests

Depending on an abstraction lets you replace the implementation without changing the caller.

Lesson: Dependency Injection Basics

16. How many instances of a scoped service exist during one HTTP request?

  1. A new one per use
  2. Zero
  3. One
  4. One per thread forever
Show answer

Answer: One

Scoped services are created once per request scope and shared within it.

Lesson: Service Lifetimes

17. Why prefer `{PageSize}` placeholders over `$"{pageSize}"` in log calls?

  1. They compile faster
  2. They are required by C#
  3. Values are kept as searchable properties
  4. They hide values from logs
Show answer

Answer: Values are kept as searchable properties

Message templates keep the value separate from the text, so tools can query and filter on it.

Lesson: Options and Logging

Data with Entity Framework Core

18. What does a `DbSet<Book>` property usually represent?

  1. A database server
  2. A table of books
  3. A migration file
  4. A connection string
Show answer

Answer: A table of books

Each `DbSet<T>` maps to a table (or view) holding rows for the entity type.

Lesson: Entities and DbContext

19. Which command applies pending migrations to a database?

  1. dotnet ef database update
  2. dotnet ef migrations add
  3. dotnet ef dbcontext scaffold
  4. dotnet build
Show answer

Answer: dotnet ef database update

`database update` runs migrations that have not yet been applied. `migrations add` only creates the file.

Lesson: Migrations

20. What does AsNoTracking() do?

  1. Deletes the rows
  2. Encrypts the query
  3. Disables caching of the table
  4. Skips change tracking for a faster read-only query
Show answer

Answer: Skips change tracking for a faster read-only query

Returned entities are not tracked, which saves memory and time when you will not update them.

Lesson: Querying and Saving Data

Errors, Security and Testing

21. Which status code fits "your JSON has an invalid field"?

  1. 500 Internal Server Error
  2. 200 OK
  3. 400 Bad Request
  4. 301 Moved Permanently
Show answer

Answer: 400 Bad Request

400 means the client sent something invalid. 500 is reserved for server faults.

Lesson: Validation and Error Responses

22. Which call makes an endpoint require a signed-in user?

  1. .AllowAnonymous()
  2. .WithName()
  3. .RequireAuthorization()
  4. .Produces()
Show answer

Answer: .RequireAuthorization()

`RequireAuthorization()` makes the endpoint reject requests that are not authenticated and authorized.

Lesson: Authentication and Authorization with JWT

23. What does `WebApplicationFactory<Program>` provide?

  1. An in-memory test server running your app
  2. A production load balancer
  3. A database schema generator
  4. A code formatter
Show answer

Answer: An in-memory test server running your app

It hosts your real pipeline in the test process so you can call it with an `HttpClient`.

Lesson: Testing with xUnit

Shipping, Performance and Revision

24. Why use a multi-stage Dockerfile?

  1. The final image contains only what is needed to run
  2. It makes C# run faster
  3. Docker requires exactly two stages
  4. It removes the need to publish
Show answer

Answer: The final image contains only what is needed to run

The SDK stays in the build stage; only the compiled output ships in the smaller runtime image.

Lesson: Publish and Docker

25. What should you do before adding caching to a slow endpoint?

  1. Rewrite it in another language
  2. Turn off logging
  3. Add more middleware
  4. Measure to find the real bottleneck
Show answer

Answer: Measure to find the real bottleneck

Profiling or query logs usually show the real cause, such as an N+1 query, which caching would only hide.

Lesson: Performance Habits

26. A singleton service needs a DbContext. What is the right fix?

  1. Make DbContext a static field
  2. Inject IServiceScopeFactory or DbContextFactory and create a scope per use
  3. Register everything as singleton
  4. Disable scope validation
Show answer

Answer: Inject IServiceScopeFactory or DbContextFactory and create a scope per use

Creating a short-lived scope or context per operation avoids the captive dependency and keeps DbContext non-shared.

Lesson: Revision: Cheat Sheet and Self-Check

27. Your API returns 500 for bad user input. What is the best change?

  1. Catch the exception and return 200
  2. Log nothing
  3. Validate input and return 400 with ProblemDetails
  4. Restart the server
Show answer

Answer: Validate input and return 400 with ProblemDetails

Client mistakes are 4xx responses; a 500 should mean a real server fault.

Lesson: Revision: Cheat Sheet and Self-Check

28. Which statement about async/await is correct?

  1. It creates a new thread for every await
  2. It lets a thread serve other work while waiting for I/O
  3. It makes CPU-bound code faster
  4. It must be avoided in web apps
Show answer

Answer: It lets a thread serve other work while waiting for I/O

`await` releases the thread during I/O waits. It does not speed up CPU-bound work.

Lesson: Revision: Cheat Sheet and Self-Check

← Back to the course