MCQ s of Class 27: Advanced Laravel Concepts

Rashmi Mishra
0

 MCQ s of Class 27: Advanced Laravel Concepts

Here are 100 multiple-choice questions (MCQs) based on advanced Laravel concepts, focusing on routing, middleware, authentication, and authorization.

Multiple Choice Questions

1.  What is the default directory for Laravel routes?

o    A) /app/Http/Controllers

o    B) /routes

o    C) /app/Models

o    D) /resources/views

o    Answer: B

2.  Which method is used to create a named route in Laravel?

o    A) route()

o    B) name()

o    C) as()

o    D) setName()

o    Answer: A

3.  In Laravel, which command is used to create a middleware?

o    A) php artisan make:route

o    B) php artisan make:middleware

o    C) php artisan create:middleware

o    D) php artisan generate:middleware

o    Answer: B

4.  What does middleware do in Laravel?

o    A) Redirects users

o    B) Processes requests before reaching the controller

o    C) Connects to the database

o    D) Handles routing

o    Answer: B

5.  Which method is used to redirect a user to a named route?

o    A) redirect()->route()

o    B) redirectTo()

o    C) goToRoute()

o    D) returnTo()

o    Answer: A

6.  What type of response does the abort() function return in Laravel?

o    A) JSON response

o    B) HTML response

o    C) HTTP exception

o    D) Plain text

o    Answer: C

7.  In Laravel, which command is used to cache the routes?

o    A) php artisan route:cache

o    B) php artisan cache:routes

o    C) php artisan optimize:routes

o    D) php artisan routes:cache

o    Answer: A

8.  Which of the following is NOT a built-in middleware in Laravel?

o    A) Authenticate

o    B) CheckAdmin

o    C) TrimStrings

o    D) RedirectIfAuthenticated

o    Answer: B

9.  What does CSRF stand for in Laravel?

o    A) Cross-Site Request Framework

o    B) Cross-Site Request Forgery

o    C) Cross-Site Resource Filter

o    D) Cross-Site Reference Form

o    Answer: B

10.                   Which file is responsible for defining route middleware in Laravel?

o    A) app/Http/Kernel.php

o    B) routes/web.php

o    C) config/middleware.php

o    D) app/Http/Routes.php

o    Answer: A

11.                   How do you check if a user is authenticated in Laravel?

o    A) if (user())

o    B) if (auth()->check())

o    C) if (Auth::isLoggedIn())

o    D) if (Auth::checkUser())

o    Answer: B

12.                   What is the purpose of policies in Laravel?

o    A) To handle routing

o    B) To manage views

o    C) To define authorization logic

o    D) To manage middleware

o    Answer: C

13.                   Which command is used to generate a policy in Laravel?

o    A) php artisan make:policy

o    B) php artisan create:policy

o    C) php artisan policy:create

o    D) php artisan generate:policy

o    Answer: A

14.                   What is the purpose of the auth() helper function in Laravel?

o    A) To access the authentication service

o    B) To redirect users

o    C) To retrieve user input

o    D) To handle session management

o    Answer: A

15.                   In Laravel, which file do you typically modify to set up user authentication routes?

o    A) routes/api.php

o    B) routes/web.php

o    C) app/Http/Kernel.php

o    D) app/Providers/RouteServiceProvider.php

o    Answer: B

16.                   What is the default method used by Laravel for storing user sessions?

o    A) Database

o    B) File

o    C) Cookie

o    D) In-memory

o    Answer: B

17.                   Which Laravel command is used to create a new controller?

o    A) php artisan make:controller

o    B) php artisan create:controller

o    C) php artisan generate:controller

o    D) php artisan new:controller

o    Answer: A

18.                   How do you register a new route in Laravel?

o    A) Route::register()

o    B) Route::add()

o    C) Route::create()

o    D) Route::get()

o    Answer: D

19.                   What does the middleware method do in a route definition?

o    A) Defines the HTTP method

o    B) Sets the route name

o    C) Applies middleware to a route

o    D) Specifies the response type

o    Answer: C

20.                   Which of the following is a method of the Request class in Laravel?

o    A) get()

o    B) retrieve()

o    C) fetch()

o    D) request()

o    Answer: A

21.                   How do you retrieve the authenticated user's information in Laravel?

o    A) Auth::user()

o    B) auth()->user()

o    C) Both A and B

o    D) getUser()

o    Answer: C

22.                   What is the purpose of route model binding in Laravel?

o    A) To automatically inject model instances into routes

o    B) To manage view logic

o    C) To optimize query performance

o    D) To define middleware

o    Answer: A

23.                   Which of the following commands will create a migration file in Laravel?

o    A) php artisan make:migration

o    B) php artisan create:migration

o    C) php artisan new:migration

o    D) php artisan generate:migration

o    Answer: A

24.                   Which attribute is commonly used for route caching in Laravel?

o    A) expire

o    B) cache

o    C) name

o    D) method

o    Answer: B

25.                   What is the purpose of the Request facade in Laravel?

o    A) To create HTTP requests

o    B) To handle incoming HTTP requests

o    C) To manage response headers

o    D) To authenticate users

o    Answer: B

26.                   Which Laravel feature allows you to define different behaviors based on user roles?

o    A) Middleware

o    B) Policies

o    C) Gates

o    D) All of the above

o    Answer: D

27.                   What type of middleware is auth in Laravel?

o    A) Global middleware

o    B) Route middleware

o    C) Application middleware

o    D) Session middleware

o    Answer: B

28.                   How do you define a route that responds to both GET and POST requests in Laravel?

o    A) Route::match()

o    B) Route::both()

o    C) Route::any()

o    D) Route::all()

o    Answer: A

29.                   Which command is used to create a new Laravel project?

o    A) laravel new project

o    B) composer create-project

o    C) php artisan new project

o    D) php artisan create project

o    Answer: B

30.                   In Laravel, which method is used to validate incoming requests?

o    A) validate()

o    B) check()

o    C) request()

o    D) authorize()

o    Answer: A

31.                   Which of the following will create a resource controller in Laravel?

o    A) php artisan make:controller --resource

o    B) php artisan create:controller --resource

o    C) php artisan generate:controller --resource

o    D) php artisan make:resource controller

o    Answer: A

32.                   What is the function of the web middleware group in Laravel?

o    A) To handle API requests

o    B) To enable session state and CSRF protection

o    C) To manage error handling

o    D) To optimize query performance

o    Answer: B

33.                   Which Laravel method is used to define a fallback route?

o    A) Route::fallback()

o    B) Route::default()

o    C) Route::notFound()

o    D) Route::catchAll()

o    Answer: A

34.                   How can you change the authentication guard in Laravel?

o    A) Modify config/auth.php

o    B) Change the guard in the controller

o    C) Update the route definition

o    D) Change middleware settings

o    Answer: A

35.                   Which of the following is used to create a new user in Laravel's default authentication?

o    A) Auth::register()

o    B) User::create()

o    C) Auth::createUser()

o    D) User::new()

o    Answer: B

36.                   What does the Route::group() method do?

o    A) Groups middleware

o    B) Groups routes under a prefix

o    C) Defines shared attributes for multiple routes

o    D) Both B and C

o    Answer: D

37.                   What is the purpose of the RedirectIfAuthenticated middleware?

o    A) To redirect unauthenticated users

o    B) To redirect authenticated users from login routes

o    C) To validate user roles

o    D) To manage session state

o    Answer: B

38.                   How do you secure routes for authenticated users in Laravel?

o    A) Use auth middleware

o    B) Use guest middleware

o    C) Use csrf middleware

o    D) Use session middleware

o    Answer: A

39.                   What is the purpose of the throttle middleware?

o    A) To limit the number of requests

o    B) To manage session state

o    C) To redirect users

o    D) To authenticate users

o    Answer: A

40.                   Which method is used to validate the presence of a field in a request?

o    A) required()

o    B) exists()

o    C) notEmpty()

o    D) mandatory()

o    Answer: A

41.                   How can you apply multiple middleware to a single route in Laravel?

o    A) Route::middleware(['auth', 'verified'])->get(...)

o    B) Route::apply(['auth', 'verified'])->get(...)

o    C) Route::with(['auth', 'verified'])->get(...)

o    D) Route::setMiddleware(['auth', 'verified'])->get(...)

o    Answer: A

42.                   In Laravel, how do you define a one-to-many relationship in a model?

o    A) public function posts() { return $this->hasMany(Post::class); }

o    B) public function posts() { return $this->belongsTo(Post::class); }

o    C) public function posts() { return $this->hasOne(Post::class); }

o    D) public function posts() { return $this->belongsToMany(Post::class); }

o    Answer: A

43.                   Which of the following commands can be used to run database migrations in Laravel?

o    A) php artisan migrate

o    B) php artisan db:migrate

o    C) php artisan run:migrations

o    D) php artisan migrate:run

o    Answer: A

44.                   What is the purpose of the @csrf directive in Laravel Blade templates?

o    A) To validate user inputs

o    B) To protect against CSRF attacks

o    C) To manage sessions

o    D) To authenticate users

o    Answer: B

45.                   Which method is used to log a user out in Laravel?

o    A) Auth::logout()

o    B) auth()->logout()

o    C) Both A and B

o    D) logout()

o    Answer: C

46.                   What does the route() function do in Laravel?

o    A) Generates a URL for a named route

o    B) Defines a route

o    C) Handles requests

o    D) Redirects to a route

o    Answer: A

47.                   In Laravel, what type of authentication does Passport provide?

o    A) Session-based

o    B) Token-based

o    C) Cookie-based

o    D) OAuth-based

o    Answer: D

48.                   Which method is used to define custom validation rules in Laravel?

o    A) Validator::custom()

o    B) request()->validate()

o    C) Validator::extend()

o    D) Validation::define()

o    Answer: C

49.                   What is the primary purpose of artisan commands in Laravel?

o    A) To manage database migrations

o    B) To automate common tasks

o    C) To handle routing

o    D) To manage views

o    Answer: B

50.                   Which Laravel feature allows you to authenticate users via social networks?

o    A) Socialite

o    B) Passport

o    C) Sanctum

o    D) Auth

o    Answer: A

51.                   How can you retrieve the current route name in Laravel?

o    A) Route::currentRouteName()

o    B) request()->route()->getName()

o    C) Route::name()

o    D) current_route_name()

o    Answer: A

52.                   What is the default storage location for session files in Laravel?

o    A) /storage/session

o    B) /storage/framework/sessions

o    C) /storage/logs

o    D) /app/sessions

o    Answer: B

53.                   What does the @can directive do in Blade templates?

o    A) Displays content based on user roles

o    B) Checks if the user can perform an action

o    C) Validates user inputs

o    D) Redirects users

o    Answer: B

54.                   Which command will create a new seeder class in Laravel?

o    A) php artisan make:seeder

o    B) php artisan create:seeder

o    C) php artisan generate:seeder

o    D) php artisan new:seeder

o    Answer: A

55.                   Which file contains the application’s authentication settings in Laravel?

o    A) config/auth.php

o    B) config/session.php

o    C) config/middleware.php

o    D) config/routes.php

o    Answer: A

56.                   What is the purpose of the dispatch() function in Laravel?

o    A) To handle HTTP requests

o    B) To send emails

o    C) To dispatch jobs to the queue

o    D) To create new routes

o    Answer: C

57.                   Which method is used to attach a policy to a model in Laravel?

o    A) Gate::define()

o    B) Policy::attach()

o    C) Auth::registerPolicy()

o    D) authorize()

o    Answer: A

58.                   How do you access the session data in Laravel?

o    A) session()->get()

o    B) Session::get()

o    C) Both A and B

o    D) getSession()

o    Answer: C

59.                   Which Laravel feature allows you to handle user permissions?

o    A) Middleware

o    B) Policies

o    C) Gates

o    D) All of the above

o    Answer: D

60.                   What is the primary function of the route middleware?

o    A) To manage user sessions

o    B) To handle incoming requests

o    C) To restrict access to routes

o    D) To perform data validation

o    Answer: C

61.                   Which Laravel command is used to rollback the last migration?

o    A) php artisan migrate:rollback

o    B) php artisan rollback:migration

o    C) php artisan down:migration

o    D) php artisan revert:migration

o    Answer: A

62.                   What does the with() method do in a controller?

o    A) Attaches middleware to a route

o    B) Passes data to a view

o    C) Validates user input

o    D) Redirects to another route

o    Answer: B

63.                   Which command is used to create a new migration file with timestamps?

o    A) php artisan make:migration --timestamp

o    B) php artisan make:migration

o    C) php artisan new:migration --time

o    D) php artisan create:migration --time

o    Answer: B

64.                   How do you secure API routes in Laravel?

o    A) Use web middleware

o    B) Use auth

middleware

o    C) Use guest middleware

o    D) Use session middleware

o    Answer: B

65.                   Which of the following methods is NOT part of the Request object in Laravel?

o    A) all()

o    B) only()

o    C) except()

o    D) delete()

o    Answer: D

66.                   What is the primary purpose of the @foreach directive in Blade templates?

o    A) To create a loop

o    B) To conditionally display content

o    C) To define sections

o    D) To include other templates

o    Answer: A

67.                   Which command can be used to seed the database with data in Laravel?

o    A) php artisan db:seed

o    B) php artisan seed

o    C) php artisan migrate:seed

o    D) php artisan populate

o    Answer: A

68.                   In Laravel, how do you define a many-to-many relationship?

o    A) public function roles() { return $this->hasMany(Role::class); }

o    B) public function roles() { return $this->belongsToMany(Role::class); }

o    C) public function roles() { return $this->hasOne(Role::class); }

o    D) public function roles() { return $this->belongsTo(Role::class); }

o    Answer: B

69.                   What is the purpose of the @yield directive in Blade templates?

o    A) To extend a layout

o    B) To define a section

o    C) To inject content

o    D) To include another template

o    Answer: B

70.                   How do you generate a URL for a named route in Laravel?

o    A) url()->route('route.name')

o    B) route('route.name')

o    C) generateUrl('route.name')

o    D) makeUrl('route.name')

o    Answer: B

71.                   What is the default session driver used in Laravel?

o    A) file

o    B) database

o    C) cookie

o    D) array

o    Answer: A

72.                   Which of the following is NOT a valid session driver in Laravel?

o    A) cookie

o    B) database

o    C) memory

o    D) file

o    Answer: C

73.                   How do you register a new middleware in Laravel?

o    A) Add it to the Kernel.php file

o    B) Use make:middleware

o    C) Define it in the route file

o    D) Both A and B

o    Answer: D

74.                   What is the primary purpose of the withErrors() method in Laravel?

o    A) To display validation errors

o    B) To manage user sessions

o    C) To redirect users

o    D) To log messages

o    Answer: A

75.                   Which command will create a new controller in Laravel?

o    A) php artisan make:controller

o    B) php artisan new:controller

o    C) php artisan create:controller

o    D) php artisan generate:controller

o    Answer: A

76.                   What is the primary function of the abort() helper in Laravel?

o    A) To return a 404 response

o    B) To terminate the application

o    C) To stop middleware execution

o    D) To trigger an exception

o    Answer: A

77.                   Which of the following methods is used to get the authenticated user in Laravel?

o    A) Auth::user()

o    B) User::auth()

o    C) request()->user()

o    D) Both A and C

o    Answer: D

78.                   What does the dd() function do in Laravel?

o    A) Dumps and dies the application

o    B) Debugs database connections

o    C) Displays errors

o    D) Redirects to home

o    Answer: A

79.                   How do you specify a fallback route in Laravel?

o    A) Route::fallback()

o    B) Route::catchAll()

o    C) Route::default()

o    D) Route::any('*')

o    Answer: A

80.                   What is the purpose of the store() method in a resource controller?

o    A) To update a resource

o    B) To delete a resource

o    C) To create a new resource

o    D) To display a resource

o    Answer: C

81.                   Which of the following is a way to share data across all views in Laravel?

o    A) View composers

o    B) Middleware

o    C) Routes

o    D) Controllers

o    Answer: A

82.                   What is the correct syntax to retrieve all records from a model in Laravel?

o    A) Model::all()

o    B) Model::getAll()

o    C) Model::retrieve()

o    D) Model::fetchAll()

o    Answer: A

83.                   In Laravel, how do you define a controller method that responds to multiple HTTP verbs?

o    A) Use Route::match()

o    B) Use Route::any()

o    C) Use Route::methods()

o    D) Both A and B

o    Answer: D

84.                   What is the purpose of the @extends directive in Blade templates?

o    A) To define a layout

o    B) To create a section

o    C) To include a file

o    D) To yield content

o    Answer: A

85.                   Which command is used to create a new factory in Laravel?

o    A) php artisan make:factory

o    B) php artisan generate:factory

o    C) php artisan new:factory

o    D) php artisan create:factory

o    Answer: A

86.                   What is the main purpose of the @if directive in Blade templates?

o    A) To create a loop

o    B) To display content conditionally

o    C) To define sections

o    D) To include other templates

o    Answer: B

87.                   Which Laravel feature is used for API rate limiting?

o    A) Middleware

o    B) Throttle

o    C) RateLimit

o    D) Both A and B

o    Answer: D

88.                   What does the json() method do in a Laravel controller?

o    A) Returns a JSON response

o    B) Encodes data to JSON format

o    C) Parses JSON data

o    D) None of the above

o    Answer: A

89.                   Which method is used to generate a CSRF token in Laravel?

o    A) csrf_token()

o    B) csrf()

o    C) generateCsrfToken()

o    D) makeCsrfToken()

o    Answer: A

90.                   What is the purpose of the when() method in Laravel collections?

o    A) To filter items conditionally

o    B) To sort items

o    C) To map items

o    D) To group items

o    Answer: A

91.                   How do you define a controller resource in Laravel routes?

o    A) Route::resource('photos', 'PhotoController')

o    B) Route::controllers('photos', 'PhotoController')

o    C) Route::resourceful('photos', 'PhotoController')

o    D) Route::routes('photos', 'PhotoController')

o    Answer: A

92.                   Which command is used to clear the application cache in Laravel?

o    A) php artisan cache:clear

o    B) php artisan clear:cache

o    C) php artisan flush:cache

o    D) php artisan clear-all

o    Answer: A

93.                   What is the default database connection used by Laravel?

o    A) mysql

o    B) sqlite

o    C) pgsql

o    D) sqlsrv

o    Answer: A

94.                   What is the purpose of the unique() validation rule in Laravel?

o    A) To ensure a field has a unique value in the database

o    B) To check if a field is required

o    C) To validate the length of a field

o    D) To check if a field exists

o    Answer: A

95.                   Which function is used to generate a slug in Laravel?

o    A) Str::slug()

o    B) String::makeSlug()

o    C) slugify()

o    D) generateSlug()

o    Answer: A

96.                   What is the default hashing driver used in Laravel for passwords?

o    A) bcrypt

o    B) argon

o    C) sha256

o    D) md5

o    Answer: A

97.                   How do you add a global middleware in Laravel?

o    A) Register it in app/Http/Kernel.php

o    B) Use make:middleware

o    C) Add it to routes

o    D) Both A and B

o    Answer: A

98.                   Which of the following is the correct way to display an error message in Blade templates?

o    A) {{ $errors->first('field_name') }}

o    B) {{ $errors->get('field_name') }}

o    C) {{ $errors['field_name'] }}

o    D) {{ $error->field_name }}

o    Answer: A

99.                   What is the purpose of the all() method in Laravel collections?

o    A) To retrieve all items from the collection

o    B) To convert the collection to an array

o    C) To check if the collection is empty

o    D) To count the items in the collection

o    Answer: A

100.             In Laravel, how do you send an email using the Mailable class? - A) Mail::to($email)->send(new MailableClass()) - B) Mail::send(new MailableClass()) - C) Mail::sendTo($email, new MailableClass()) - D) Mail::dispatch($email, new MailableClass()) - Answer: A


Post a Comment

0Comments

Post a Comment (0)