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. routes/web.php
B. server.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. app/Http/Routes
B. routes
C. config/routes
D. resources/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/web.php
B. routes/api.php
C. routes/channels.php
D. routes/console.php

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

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

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

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

7 How do you define a route parameter in Laravel?

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

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

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

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 where method.
B. Using the regex method.
C. Using the validate method.
D. Using the constrain method.

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

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

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

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

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

A. .php
B. .blade.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('admin.profile')
B. view('admin/profile')
C. view('admin-profile')
D. view('resources.views.admin.profile')

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

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

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

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

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

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

19 Where should View::share typically be called?

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

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

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

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

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

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

A. return response($content)->header('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)->with('Content-Type', 'text/plain');

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

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

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

A. Request::cookie()
B. make_cookie()
C. cookie()
D. generate_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. 24 hours.
B. It is a session cookie (expires when browser closes).
C. 1 hour.
D. Forever (5 years).

26 What happens to cookies generated by Laravel by default?

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

27 How do you return a JSON response in Laravel?

A. return response($data, 'json');
B. return json_encode($data);
C. return response()->json($data);
D. return View::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 casts it to a string.
C. It automatically converts it to JSON.
D. It throws a conversion error.

29 Which method creates a redirect response?

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

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

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

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

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

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

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

33 How do you redirect to a named route?

A. return redirect()->route('profile');
B. return redirect('profile');
C. return route('profile');
D. return redirect()->name('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', ['id' => 1]);
C. return redirect()->route('profile')->with('id', $id);
D. return redirect()->route('profile', $id);

35 How do you redirect to a specific Controller Action?

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

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

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

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

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

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

A. redirect()->external('https://google.com')
B. redirect()->out('https://google.com')
C. redirect()->to('https://google.com')
D. redirect()->away('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::get is for retrieving data, Route::post is for submitting data.
C. Route::post does not support CSRF protection.
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. @raw($variable)
B. {{ $variable }}
C. @unescaped($variable)
D. {!! $variable !!}

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

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

43 Which status code represents a permanent redirect?

A. 301
B. 500
C. 302
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::prefix('admin')->group(function () { ... });
B. Neither are valid.
C. Route::group(['prefix' => 'admin'], function() { ... });
D. Both A and B are valid.

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

A. It defines a route that only returns a view without needing a controller or closure.
B. It creates a view file dynamically.
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. request()->routeIs('name')
C. View::is('name')
D. request()->isRoute('name')

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

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

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::missing(function () { ... });
C. Route::default(function () { ... });
D. Route::catch(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. break($var)
C. dump($var)
D. stop($var)