Unit 2: Request, Routing & Responses - Practice Quiz

INT221 — Mvc Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which file serves as the entry point for all requests entering a Laravel application?

A. server.php
B. routes/web.php
C. config/app.php
D. public/index.php

2 In the Laravel Request Lifecycle, after the application instance is created, where is the incoming request sent?

A. Directly to the Controller
B. To the Database Driver
C. To the View Compiler
D. To the HTTP Kernel or Console Kernel

3 Which directory contains the route definition files for a Laravel application?

A. routes
B. app/Http/Routes
C. resources/routes
D. config/routes

4 Which route file is intended for routes that interact with the user via a browser and includes the web middleware group?

A. routes/console.php
B. routes/api.php
C. routes/channels.php
D. routes/web.php

5 What is the correct syntax to define a basic GET route returning 'Hello World'?

A. Route::get('/', function () { return 'Hello World'; });
B. Route::to('/', function () { echo 'Hello World'; });
C. Router::fetch('/', 'Hello World');
D. web::get('/', 'Hello World');

6 Which method serves as a shortcut to register a route that responds to multiple HTTP verbs?

A. Route::many(['get', 'post'], '/', ...)
B. Route::match(['get', 'post'], '/', ...)
C. Route::multiple(['get', 'post'], '/', ...)
D. Route::combine(['get', 'post'], '/', ...)

7 How do you define a route parameter in Laravel?

A. By prepending a colon: :id
B. By enclosing it in curly braces: {id}
C. By using a query string: ?id=
D. By using square brackets: [id]

8 Consider the route: Route::get('/user/{id}', function ($id) { ... });. How is the $id variable populated?

A. It is injected from the Session.
B. It must be manually retrieved using request('id').
C. It takes the value of the corresponding URI segment.
D. It is injected by the Laravel Service Container.

9 What syntax is used to define an optional route parameter?

A. {id?}
B. {?id}
C. {:id}
D. [id]

10 When defining an optional parameter in a route callback, what is a requirement for the callback argument?

A. It must have a default value.
B. It must be passed by reference.
C. It must be type-hinted as null.
D. It must be the last argument.

11 How can you constrain the format of a route parameter using a regular expression?

A. Using the constrain method.
B. Using the where method.
C. Using the validate method.
D. Using the regex method.

12 Which helper function is used to return a view from a route or controller?

A. make()
B. render()
C. view()
D. template()

13 Where are view files typically stored in a Laravel application?

A. storage/views
B. app/Views
C. resources/views
D. public/views

14 What is the default file extension for Laravel Blade templates?

A. .blade.php
B. .php
C. .blade
D. .tpl

15 If a view is located at resources/views/admin/profile.blade.php, how do you reference it using the view helper?

A. view('resources.views.admin.profile')
B. view('admin/profile')
C. view('admin.profile')
D. view('admin-profile')

16 Which of the following creates a view and passes a variable named name with the value 'John'?

A. Both B and C are correct.
B. view('greeting', 'name' => 'John')
C. view('greeting')->withName('John')
D. view('greeting', ['name' => 'John'])

17 Which PHP function is commonly used to create an array containing variables and their values to pass to a view?

A. compact()
B. array_make()
C. implode()
D. extract()

18 Which method allows you to share a piece of data with all views in the application?

A. Route::share('key', 'value')
B. View::global('key', 'value')
C. Response::share('key', 'value')
D. View::share('key', 'value')

19 Where should View::share typically be called?

A. In the config/app.php file
B. In the register method of a Service Provider
C. In the boot method of a Service Provider
D. In routes/web.php

20 If you return a string from a Laravel route, what happens?

A. The string is logged to the console but nothing is displayed.
B. The browser receives the string as a JSON object.
C. Laravel automatically converts the string into a full HTTP response.
D. It throws an error because a Response object is required.

21 Which method allows you to manually create a response instance with a specific status code?

A. return view('Content', 200);
B. return response('Content', 200);
C. return new HTTP('Content', 200);
D. return make('Content', 200);

22 How do you attach a header to a response using the fluent interface?

A. return response($content)->with('Content-Type', 'text/plain');
B. return response($content)->addHeader('Content-Type', 'text/plain');
C. return response($content)->set('Content-Type', 'text/plain');
D. return response($content)->header('Content-Type', 'text/plain');

23 How can you attach a cookie to a response instance?

A. Both A and B are correct.
B. ->withCookie('name', 'value', $minutes)
C. ->addCookie('name', 'value', $minutes)
D. ->cookie('name', 'value', $minutes)

24 Which global helper function can be used to generate a Cookie instance to be attached to a response later?

A. cookie()
B. Request::cookie()
C. generate_cookie()
D. make_cookie()

25 What is the default duration of a cookie in Laravel if the minutes argument is omitted (in older versions) or set to default?

A. Forever (5 years).
B. It is a session cookie (expires when browser closes).
C. 24 hours.
D. 1 hour.

26 What happens to cookies generated by Laravel by default?

A. They are sent as plain text.
B. They are compressed using GZIP.
C. They are encrypted and signed.
D. They are stored in the database.

27 How do you return a JSON response in Laravel?

A. return json_encode($data);
B. return response($data, 'json');
C. return View::json($data);
D. return response()->json($data);

28 If you return an Eloquent model or Collection directly from a route, what does Laravel do?

A. It displays a var_dump of the object.
B. It automatically converts it to JSON.
C. It automatically casts it to a string.
D. It throws a conversion error.

29 Which method creates a redirect response?

A. redirect()
B. view()
C. return()
D. route()

30 How do you redirect the user to a specific URI path?

A. return Redirect::to('/home');
B. All of the above.
C. return redirect()->to('/home');
D. return redirect('/home');

31 Which helper function redirects the user to their previous location?

A. return back();
B. return previous();
C. return old();
D. return redirect()->reverse();

32 To name a route, which method do you chain onto the route definition?

A. ->name('profile')
B. ->label('profile')
C. ->alias('profile')
D. ->title('profile')

33 How do you redirect to a named route?

A. return route('profile');
B. return redirect()->name('profile');
C. return redirect()->route('profile');
D. return redirect('profile');

34 If a named route requires parameters, how are they passed in the redirect?

A. return redirect()->route('profile?id=1');
B. return redirect()->route('profile')->with('id', $id);
C. return redirect()->route('profile', $id);
D. return redirect()->route('profile', ['id' => 1]);

35 How do you redirect to a specific Controller Action?

A. return redirect()->call('HomeController@index');
B. return redirect()->action([HomeController::class, 'index']);
C. return redirect()->method('HomeController::index');
D. return redirect()->controller('HomeController@index');

36 When redirecting with Input data (to repopulate a form), which method is chained?

A. ->keepInput()
B. ->withInput()
C. ->withOldData()
D. ->saveInput()

37 How do you flash a session message (like a success notification) alongside a redirect?

A. return redirect('/')->flash('status', 'Success');
B. return redirect('/')->session('status', 'Success');
C. return redirect('/')->with('status', 'Success');
D. return redirect('/')->message('Success');

38 Which method is used to redirect to an external domain?

A. redirect()->away('https://google.com')
B. redirect()->external('https://google.com')
C. redirect()->out('https://google.com')
D. redirect()->to('https://google.com')

39 What is the primary difference between Route::get and Route::post?

A. There is no functional difference in Laravel.
B. Route::post does not support CSRF protection.
C. Route::get is for retrieving data, Route::post is for submitting data.
D. Route::get is faster than Route::post.

40 In a view, how do you output a variable while escaping HTML entities to prevent XSS?

A. {!! $variable !!}
B. @echo($variable)
C. {{ $variable }}
D. <?= $variable ?>

41 In a view, how do you output data without escaping HTML (e.g., rendering a bold tag)?

A. @unescaped($variable)
B. {!! $variable !!}
C. @raw($variable)
D. {{ $variable }}

42 What happens if a required route parameter {id} is missing from the URI in the browser request?

A. The parameter takes the default value.
B. Laravel redirects to the home page.
C. The controller receives null.
D. Laravel throws a 404 Not Found exception.

43 Which status code represents a permanent redirect?

A. 302
B. 500
C. 301
D. 404

44 When defining a route group, which method is commonly used to apply a prefix to all routes within the group?

A. Route::group(['prefix' => 'admin'], function() { ... });
B. Both A and B are valid.
C. Neither are valid.
D. Route::prefix('admin')->group(function () { ... });

45 What is the purpose of Route::view('/url', 'viewname')?

A. It creates a view file dynamically.
B. It defines a route that only returns a view without needing a controller or closure.
C. It checks if a view exists before routing.
D. It is a deprecated method.

46 How can you check if the current request corresponds to a specific named route inside a Blade view?

A. route()->current('name')
B. View::is('name')
C. request()->isRoute('name')
D. request()->routeIs('name')

47 Which middleware is automatically applied to web routes to verify the user's session token on POST requests?

A. Authenticate
B. VerifyCsrfToken
C. TrimStrings
D. EncryptCookies

48 When returning a response with a file download, which method is used?

A. return response()->stream($path);
B. return response()->file($path);
C. return response()->get($path);
D. return response()->download($path);

49 How do you define a route that handles a Fallback (404) page when no other route matches?

A. Route::fallback(function () { ... });
B. Route::catch(function () { ... });
C. Route::default(function () { ... });
D. Route::missing(function () { ... });

50 If you want to view the raw contents of a variable and stop execution immediately (often for debugging request data), which helper is used?

A. dd($var)
B. stop($var)
C. break($var)
D. dump($var)