Unit 2 - Practice Quiz

INT221 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. public/index.php
D. config/app.php

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

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

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/console.php
C. routes/channels.php
D. routes/api.php

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

A. web::get('/', 'Hello World');
B. Route::to('/', function () { echo 'Hello World'; });
C. Router::fetch('/', '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::many(['get', 'post'], '/', ...)
B. Route::multiple(['get', 'post'], '/', ...)
C. Route::match(['get', 'post'], '/', ...)
D. Route::combine(['get', 'post'], '/', ...)

7 How do you define a route parameter in Laravel?

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

8 Consider the route: Route::get('/user/{id}', function (id variable populated?

A. It takes the value of the corresponding URI segment.
B. It is injected from the Session.
C. It is injected by the Laravel Service Container.
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 be the last argument.
B. It must have a default value.
C. It must be type-hinted as null.
D. It must be passed by reference.

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. render()
B. make()
C. template()
D. view()

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

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

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

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

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. view('greeting')->withName('John')
B. view('greeting', 'name' => 'John')
C. view('greeting', ['name' => 'John'])
D. Both B and C are correct.

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

A. extract()
B. implode()
C. compact()
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. Route::share('key', 'value')
D. Response::share('key', 'value')

19 Where should View::share typically be called?

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

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

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

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

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

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

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

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

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

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

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

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 View::json($data);
C. return json_encode($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 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. redirect()
B. route()
C. view()
D. return()

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

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

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

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

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

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

33 How do you redirect to a named route?

A. return route('profile');
B. return redirect()->route('profile');
C. return redirect('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')->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()->method('HomeController::index');
B. return redirect()->action([HomeController::class, 'index']);
C. return redirect()->call('HomeController@index');
D. return redirect()->controller('HomeController@index');

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

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

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

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

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

A. redirect()->external('https://google.com')
B. redirect()->away('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. Route::get is for retrieving data, Route::post is for submitting data.
B. There is no functional difference in Laravel.
C. Route::get is faster than Route::post.
D. Route::post does not support CSRF protection.

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. {{ $variable }}
B. {!! $variable !!}
C. @raw($variable)
D. @unescaped($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. 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. Both A and B are valid.
C. Route::group(['prefix' => 'admin'], function() { ... });
D. Neither 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 checks if a view exists before routing.
C. It creates a view file dynamically.
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. request()->isRoute('name')
B. request()->routeIs('name')
C. route()->current('name')
D. View::is('name')

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

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

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

A. return response()->file($path);
B. return response()->stream($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::default(function () { ... });
B. Route::missing(function () { ... });
C. Route::catch(function () { ... });
D. Route::fallback(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. dump($var)
B. dd($var)
C. break($var)
D. stop($var)