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. public/index.php
C. routes/web.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 View Compiler
B. To the HTTP Kernel or Console Kernel
C. Directly to the Controller
D. To the Database Driver

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

A. app/Http/Routes
B. resources/routes
C. 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/api.php
B. routes/web.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. Route::get('/', function () { return 'Hello World'; });
B. Router::fetch('/', 'Hello World');
C. Route::to('/', function () { echo '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 variable populated?

A. It is injected from the Session.
B. It is injected by the Laravel Service Container.
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 be type-hinted as null.
B. It must have a default value.
C. It must be the last argument.
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 validate method.
B. Using the where method.
C. Using the regex 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. public/views
B. app/Views
C. resources/views
D. storage/views

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

A. .php
B. .blade
C. .blade.php
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. view('greeting')->withName('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. 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::global('key', 'value')
B. View::share('key', 'value')
C. Route::share('key', 'value')
D. Response::share('key', 'value')

19 Where should View::share typically be called?

A. In routes/web.php
B. In the boot method of a Service Provider
C. In the register 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. 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 response('Content', 200);
B. return make('Content', 200);
C. return new HTTP('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)->addHeader('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)->set('Content-Type', 'text/plain');

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

A. ->withCookie('name', 'value', $minutes)
B. ->cookie('name', 'value', $minutes)
C. ->addCookie('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. make_cookie()
B. cookie()
C. generate_cookie()
D. 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?

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

26 What happens to cookies generated by Laravel by default?

A. They are sent as plain text.
B. They are encrypted and signed.
C. They are compressed using GZIP.
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()->json($data);
C. return View::json($data);
D. return response($data, 'json');

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

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

29 Which method creates a redirect response?

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

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

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

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

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

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

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

33 How do you redirect to a named route?

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

35 How do you redirect to a specific Controller Action?

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

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

A. ->withInput()
B. ->withOldData()
C. ->keepInput()
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('/')->with('status', 'Success');
C. return redirect('/')->session('status', 'Success');
D. return redirect('/')->message('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. Route::get is faster than Route::post.
C. Route::post does not support CSRF protection.
D. There is no functional difference in Laravel.

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

A. {!! $variable !!}
B. {{ $variable }}
C. <?= $variable ?>
D. @echo($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 controller receives null.
B. Laravel throws a 404 Not Found exception.
C. Laravel redirects to the home page.
D. The parameter takes the default value.

43 Which status code represents a permanent redirect?

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

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. Route::prefix('admin')->group(function () { ... });
C. Both A and B are valid.
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. route()->current('name')
C. request()->routeIs('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. Authenticate
B. VerifyCsrfToken
C. TrimStrings
D. EncryptCookies

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

A. return response()->file($path);
B. return response()->download($path);
C. return response()->get($path);
D. return response()->stream($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::missing(function () { ... });
D. 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?

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