Unit 2 - Practice Quiz
1 Which file serves as the entry point for all requests entering a Laravel application?
server.php
public/index.php
routes/web.php
config/app.php
2 In the Laravel Request Lifecycle, after the application instance is created, where is the incoming request sent?
3 Which directory contains the route definition files for a Laravel application?
app/Http/Routes
resources/routes
routes
config/routes
4
Which route file is intended for routes that interact with the user via a browser and includes the web middleware group?
routes/api.php
routes/web.php
routes/channels.php
routes/console.php
5 What is the correct syntax to define a basic GET route returning 'Hello World'?
Route::get('/', function () { return 'Hello World'; });
Router::fetch('/', 'Hello World');
Route::to('/', function () { echo 'Hello World'; });
web::get('/', 'Hello World');
6 Which method serves as a shortcut to register a route that responds to multiple HTTP verbs?
Route::many(['get', 'post'], '/', ...)
Route::match(['get', 'post'], '/', ...)
Route::multiple(['get', 'post'], '/', ...)
Route::combine(['get', 'post'], '/', ...)
7 How do you define a route parameter in Laravel?
:id
{id}
?id=
[id]
8
Consider the route: Route::get('/user/{id}', function (id variable populated?
request('id').
9 What syntax is used to define an optional route parameter?
{id?}
{?id}
[id]
{:id}
10 When defining an optional parameter in a route callback, what is a requirement for the callback argument?
11 How can you constrain the format of a route parameter using a regular expression?
validate method.
where method.
regex method.
constrain method.
12 Which helper function is used to return a view from a route or controller?
render()
template()
make()
view()
13 Where are view files typically stored in a Laravel application?
public/views
app/Views
resources/views
storage/views
14 What is the default file extension for Laravel Blade templates?
.php
.blade
.blade.php
.tpl
15
If a view is located at resources/views/admin/profile.blade.php, how do you reference it using the view helper?
view('admin/profile')
view('admin.profile')
view('admin-profile')
view('resources.views.admin.profile')
16
Which of the following creates a view and passes a variable named name with the value 'John'?
view('greeting', 'name' => 'John')
view('greeting')->withName('John')
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?
extract()
compact()
implode()
array_make()
18 Which method allows you to share a piece of data with all views in the application?
View::global('key', 'value')
View::share('key', 'value')
Route::share('key', 'value')
Response::share('key', 'value')
19
Where should View::share typically be called?
routes/web.php
boot method of a Service Provider
register method of a Service Provider
config/app.php file
20 If you return a string from a Laravel route, what happens?
21 Which method allows you to manually create a response instance with a specific status code?
return response('Content', 200);
return make('Content', 200);
return new HTTP('Content', 200);
return view('Content', 200);
22 How do you attach a header to a response using the fluent interface?
return response($content)->addHeader('Content-Type', 'text/plain');
return response($content)->header('Content-Type', 'text/plain');
return response($content)->with('Content-Type', 'text/plain');
return response($content)->set('Content-Type', 'text/plain');
23 How can you attach a cookie to a response instance?
->withCookie('name', 'value', $minutes)
->cookie('name', 'value', $minutes)
->addCookie('name', 'value', $minutes)
24 Which global helper function can be used to generate a Cookie instance to be attached to a response later?
make_cookie()
cookie()
generate_cookie()
Request::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?
26 What happens to cookies generated by Laravel by default?
27 How do you return a JSON response in Laravel?
return json_encode($data);
return response()->json($data);
return View::json($data);
return response($data, 'json');
28 If you return an Eloquent model or Collection directly from a route, what does Laravel do?
29 Which method creates a redirect response?
view()
redirect()
return()
route()
30 How do you redirect the user to a specific URI path?
return redirect('/home');
return redirect()->to('/home');
return Redirect::to('/home');
31 Which helper function redirects the user to their previous location?
return back();
return previous();
return redirect()->reverse();
return old();
32 To name a route, which method do you chain onto the route definition?
->title('profile')
->label('profile')
->name('profile')
->alias('profile')
33 How do you redirect to a named route?
return redirect()->route('profile');
return redirect()->name('profile');
return route('profile');
return redirect('profile');
34 If a named route requires parameters, how are they passed in the redirect?
return redirect()->route('profile', $id);
return redirect()->route('profile')->with('id', $id);
return redirect()->route('profile', ['id' => 1]);
return redirect()->route('profile?id=1');
35 How do you redirect to a specific Controller Action?
return redirect()->controller('HomeController@index');
return redirect()->action([HomeController::class, 'index']);
return redirect()->method('HomeController::index');
return redirect()->call('HomeController@index');
36 When redirecting with Input data (to repopulate a form), which method is chained?
->withInput()
->withOldData()
->keepInput()
->saveInput()
37 How do you flash a session message (like a success notification) alongside a redirect?
return redirect('/')->flash('status', 'Success');
return redirect('/')->with('status', 'Success');
return redirect('/')->session('status', 'Success');
return redirect('/')->message('Success');
38 Which method is used to redirect to an external domain?
redirect()->external('https://google.com')
redirect()->away('https://google.com')
redirect()->out('https://google.com')
redirect()->to('https://google.com')
39
What is the primary difference between Route::get and Route::post?
Route::get is for retrieving data, Route::post is for submitting data.
Route::get is faster than Route::post.
Route::post does not support CSRF protection.
40 In a view, how do you output a variable while escaping HTML entities to prevent XSS?
{!! $variable !!}
{{ $variable }}
<?= $variable ?>
@echo($variable)
41 In a view, how do you output data without escaping HTML (e.g., rendering a bold tag)?
{!! $variable !!}
{{ $variable }}
@raw($variable)
@unescaped($variable)
42
What happens if a required route parameter {id} is missing from the URI in the browser request?
null.
43 Which status code represents a permanent redirect?
44 When defining a route group, which method is commonly used to apply a prefix to all routes within the group?
Route::group(['prefix' => 'admin'], function() { ... });
Route::prefix('admin')->group(function () { ... });
45
What is the purpose of Route::view('/url', 'viewname')?
46 How can you check if the current request corresponds to a specific named route inside a Blade view?
request()->isRoute('name')
route()->current('name')
request()->routeIs('name')
View::is('name')
47
Which middleware is automatically applied to web routes to verify the user's session token on POST requests?
Authenticate
VerifyCsrfToken
TrimStrings
EncryptCookies
48 When returning a response with a file download, which method is used?
return response()->file($path);
return response()->download($path);
return response()->get($path);
return response()->stream($path);
49 How do you define a route that handles a Fallback (404) page when no other route matches?
Route::fallback(function () { ... });
Route::catch(function () { ... });
Route::missing(function () { ... });
Route::default(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?
stop($var)
dd($var)
dump($var)
break($var)