Multiple choice practice

ASP.NET Web API MCQ questions

Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.

Questions
123
From lessons
103
Your last test
—

Practice test

ASP.NET Web API practice testMultiple choice Question 1 of 10

What does “stateless” mean for a REST API?

Score so far: 0/0

All questions with answers

  1. Which base class do Web API 2 controllers inherit from?

    • AController
    • BApiController
    • CControllerBase
    • DHttpController
    Show answer

    Answer: B. ApiController

    System.Web.Http.ApiController.

  2. Where is Web API routing configured?

    • ARouteConfig.cs
    • BWebApiConfig.Register
    • CProgram.cs
    • DStartup.Configure only
    Show answer

    Answer: B. WebApiConfig.Register

    App_Start/WebApiConfig.cs is called from Global.asax.

  3. What is the default Web API route template?

    • A{controller}/{action}/{id}
    • Bapi/{controller}/{id}
    • Capi/{action}
    • D{controller}/{id}
    Show answer

    Answer: B. api/{controller}/{id}

    Actions are chosen by HTTP method, so the template has no {action}.

  4. Which return type lets an action return different HTTP results?

    • Astring
    • BIHttpActionResult
    • Cvoid
    • Dobject
    Show answer

    Answer: B. IHttpActionResult

    Helpers like Ok(), NotFound() and BadRequest() return IHttpActionResult.

  5. Which helper returns 201 with a Location header?

    • AOk()
    • BCreatedAtRoute("DefaultApi", new { id }, dto)
    • CRedirect()
    • DContent()
    Show answer

    Answer: B. CreatedAtRoute("DefaultApi", new { id }, dto)

    CreatedAtRoute builds the location from a named route.

  6. By default, where does Web API bind an int parameter from?

    • AThe body
    • BThe URI
    • CHeaders
    • DCookies
    Show answer

    Answer: B. The URI

    Simple types come from the route or query string.

  7. Which attribute forces a simple type to be read from the body?

    • A[FromUri]
    • B[FromBody]
    • C[FromQuery]
    • D[FromForm]
    Show answer

    Answer: B. [FromBody]

    Only one parameter per action can use [FromBody].

  8. What chooses JSON or XML for the response?

    • AModel binding
    • BContent negotiation using the Accept header
    • CRouting
    • DIIS
    Show answer

    Answer: B. Content negotiation using the Accept header

    Formatters are chosen from the Accept header and media types.

  9. Which class handles requests before they reach controllers?

    • AActionFilterAttribute
    • BDelegatingHandler
    • CModelBinder
    • DMediaTypeFormatter
    Show answer

    Answer: B. DelegatingHandler

    Message handlers sit at the start of the pipeline.

  10. Which interface implements a global exception logger?

    • AIExceptionLogger
    • BIErrorFilter
    • CILogger<T>
    • DIHttpModule
    Show answer

    Answer: A. IExceptionLogger

    IExceptionLogger logs; IExceptionHandler shapes the response.

  11. Which package adds Swagger to Web API 2?

    • ASwashbuckle
    • BNSwag.AspNetCore
    • CScalar
    • DMicrosoft.AspNetCore.OpenApi
    Show answer

    Answer: A. Swashbuckle

    Swashbuckle (the classic package) generates Swagger for Web API 2.

  12. Which attribute protects an action with bearer tokens?

    • A[Authorize]
    • B[RequireToken]
    • C[Secure]
    • D[ApiKey]
    Show answer

    Answer: A. [Authorize]

    With OWIN bearer middleware configured, [Authorize] checks the token.

  13. Which call enables CORS attributes in Web API 2?

    • Aapp.UseCors()
    • Bconfig.EnableCors()
    • Cservices.AddCors()
    • Droutes.EnableCors()
    Show answer

    Answer: B. config.EnableCors()

    Then apply [EnableCors] to controllers or globally.

  14. What is wrong with this action?

    C# Illustrative
    public IHttpActionResult Get(int id)
    {
        var product = _service.GetAsync(id).Result;
        return Ok(product);
    }
    
    • ANothing
    • BIt blocks a thread with .Result instead of awaiting
    • CAsync actions are not supported
    • DIt must return void
    Show answer

    Answer: B. It blocks a thread with .Result instead of awaiting

    Make the action async and await the call; .Result can deadlock in ASP.NET.

  15. Which ADO.NET object executes a stored procedure?

    • ASqlConnection
    • BSqlCommand with CommandType.StoredProcedure
    • CDataSet
    • DSqlBulkCopy
    Show answer

    Answer: B. SqlCommand with CommandType.StoredProcedure

    Add parameters and call ExecuteReaderAsync or ExecuteNonQueryAsync.

  16. Which EF6 method saves changes asynchronously?

    • ASaveChangesAsync()
    • BCommitAsync()
    • CFlushAsync()
    • DUpdateAsync()
    Show answer

    Answer: A. SaveChangesAsync()

    SaveChangesAsync writes tracked changes in a transaction.

  17. Which setting raises the IIS request size limit?

    • AmaxAllowedContentLength in requestFiltering
    • BmaxJsonLength
    • CexecutionTimeout
    • DmaxUrlLength
    Show answer

    Answer: A. maxAllowedContentLength in requestFiltering

    Also raise httpRuntime maxRequestLength (in KB).

  18. Which status code is correct for an unsupported Content-Type?

    • A400
    • B406
    • C415
    • D422
    Show answer

    Answer: C. 415

    415 Unsupported Media Type; 406 is for an unsatisfiable Accept header.

  19. What is the main reason to migrate to ASP.NET Core?

    • AWeb API 2 cannot return JSON
    • BCross-platform hosting, performance and active development
    • CCore does not need HTTPS
    • DWeb API 2 has no routing
    Show answer

    Answer: B. Cross-platform hosting, performance and active development

    .NET Framework only receives maintenance; ASP.NET Core is where new features land.

  20. Which pagination response is most helpful?

    • AOnly the items
    • BItems plus page, pageSize and totalCount
    • COnly totalCount
    • DA redirect to the next page
    Show answer

    Answer: B. Items plus page, pageSize and totalCount

    Metadata lets clients build pagers.

  21. Which base class does a Web API 2 controller inherit?

    • AController
    • BApiController
    • CControllerBase
    • DHttpHandler
    Show answer

    Answer: B. ApiController

    Web API 2 controllers inherit System.Web.Http.ApiController.

    From the lesson: Introduction to Web APIs
  22. Where is WebApiConfig.Register called from?

    • AProgram.cs
    • BGlobal.asax Application_Start
    • Cweb.config
    • DEach controller
    Show answer

    Answer: B. Global.asax Application_Start

    GlobalConfiguration.Configure(WebApiConfig.Register) runs in Application_Start.

    From the lesson: Introduction to Web APIs
  23. ASP.NET Web API 2 runs on Linux without changes.

    • ATrue
    • BFalse
    Show answer

    Answer: B. False

    It requires .NET Framework, which is Windows-only.

    From the lesson: Introduction to Web APIs
  24. Which URL follows REST conventions to delete product 7?

    • APOST /api/deleteProduct?id=7
    • BGET /api/products/7/delete
    • CDELETE /api/products/7
    • DDELETE /api/product/remove/7
    Show answer

    Answer: C. DELETE /api/products/7

    The resource is in the URL; the action is the HTTP method.

    From the lesson: REST Principles
  25. What does “stateless” mean for a REST API?

    • AIt has no database
    • BEach request contains all the information needed to process it
    • CIt never returns errors
    • DIt does not use HTTP headers
    Show answer

    Answer: B. Each request contains all the information needed to process it

    The server keeps no client session between requests.

    From the lesson: REST Principles