MCQs on Class 25: Introduction to PHP Frameworks
Creating 100 multiple-choice questions (MCQs) on Laravel as a PHP framework can provide a comprehensive assessment of knowledge for students. Below are the MCQs, including the correct answer for each:
Laravel MCQs
1. What is Laravel?
o A) A programming language
o B) A PHP framework
o C) A database
o D) A web server
Answer: B
2. Which command is used to create a new Laravel project?
o A) laravel new project_name
o B) composer create-project
o C) php artisan create project
o D) php create project
Answer: B
3. Which file is used for environment configuration in Laravel?
o A) config.php
o B) .env
o C) app.php
o D) settings.php
Answer: B
4. In Laravel, which command is used to run migrations?
o A) php artisan migrate
o B) php artisan run:migrations
o C) php migrate
o D) php artisan migrate:run
Answer: A
5. Which of the following is NOT a feature of Laravel?
o A) Eloquent ORM
o B) Blade templating engine
o C) Built-in security features
o D) Built-in JavaScript framework
Answer: D
6. What does the artisan command do in Laravel?
o A) Generates documentation
o B) Manages application tasks
o C) Deploys the application
o D) Connects to the database
Answer: B
7. What is Eloquent in Laravel?
o A) A type of database
o B) An ORM (Object-Relational Mapping)
o C) A templating engine
o D) A middleware
Answer: B
8. Which of the following is a valid route definition in Laravel?
o A) Route::get('/home', 'HomeController@index');
o B) get('/home', 'HomeController@index');
o C) Route::home('/', 'HomeController@index');
o D) Route('/home', 'HomeController@index');
Answer: A
9. In Laravel, where do you define your routes?
o A) app/routes.php
o B) routes/web.php
o C) config/routes.php
o D) routes/api.php
Answer: B
10. Which command is used to generate a new controller in Laravel?
o A) php artisan make:controller
o B) php artisan create:controller
o C) php artisan new:controller
o D) php controller:new
Answer: A
11. What is the default database used by Laravel?
o A) SQLite
o B) PostgreSQL
o C) MySQL
o D) MongoDB
Answer: C
12. How do you create a new migration in Laravel?
o A) php artisan make:migration
o B) php artisan migration:create
o C) php migrate:create
o D) php artisan new:migration
Answer: A
13. Which of the following is the default templating engine in Laravel?
o A) Smarty
o B) Blade
o C) Twig
o D) Mustache
Answer: B
14. Which method is used to retrieve all records from a model in Eloquent?
o A) Model::all()
o B) Model::getAll()
o C) Model::fetch()
o D) Model::retrieve()
Answer: A
15. What is the purpose of middleware in Laravel?
o A) To handle database connections
o B) To process HTTP requests
o C) To manage sessions
o D) To route requests
Answer: B
16. What command is used to start the Laravel development server?
o A) php artisan serve
o B) php serve
o C) laravel serve
o D) artisan serve
Answer: A
17. Which of the following is NOT a valid HTTP verb in Laravel routing?
o A) GET
o B) POST
o C) PUT
o D) DELETE
Answer: D
18. In Laravel, how do you pass data to a view?
o A) return view('name', data)
o B) return view('name')->with(data)
o C) view('name')->data(data)
o D) return view('name', ['data' => data])
Answer: D
19. Which of the following commands is used to run database seeders?
o A) php artisan db:seed
o B) php artisan seed:run
o C) php artisan migrate:seed
o D) php artisan seeder:run
Answer: A
20. What is the default authentication method provided by Laravel?
o A) Token-based
o B) Session-based
o C) API-based
o D) Cookie-based
Answer: B
21. In Laravel, which file contains the application configuration?
o A) .env
o B) config/app.php
o C) app/config.php
o D) settings/config.php
Answer: B
22. How do you define a one-to-many relationship in Eloquent?
o A) hasMany
o B) belongsTo
o C) hasOne
o D) manyToMany
Answer: A
23. What command is used to create a new model in Laravel?
o A) php artisan make:model
o B) php artisan new:model
o C) php model:create
o D) php artisan create:model
Answer: A
24. What is the purpose of the web.php file in Laravel?
o A) To define API routes
o B) To handle web routes
o C) To configure middleware
o D) To set environment variables
Answer: B
25. Which method is used to validate incoming request data in a controller?
o A) validate()
o B) check()
o C) assert()
o D) request() Answer: A
26. In Laravel, which method is used to return a JSON response?
o A) response()->json()
o B) json_response()
o C) response()->data()
o D) return json() Answer: A
27. What 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 reset:cache Answer: A
28. How do you use middleware in a route definition?
o A) Route::middleware('auth')->get('/dashboard', 'DashboardController@index');
o B) Route::use('auth')->get('/dashboard', 'DashboardController@index');
o C) Route::with('auth')->get('/dashboard', 'DashboardController@index');
o D) Route::apply('auth')->get('/dashboard', 'DashboardController@index'); Answer: A
29. What is the primary purpose of Laravel Mix?
o A) To manage database migrations
o B) To compile and manage assets
o C) To generate controllers
o D) To handle routing
Answer: B
30. Which of the following is a valid way to create a new request validation rule in Laravel?
o A) request()->validate(['field' => 'rule'])
o B) validate(request(), ['field' => 'rule'])
o C) request()->check(['field' => 'rule'])
o D) validate(['field' => 'rule']) Answer: A
31. In Laravel, what is the purpose of @csrf directive in Blade templates?
o A) To include a session ID
o B) To protect against CSRF attacks
o C) To validate user input
o D) To redirect to the login page
Answer: B
32. What is the default session driver in Laravel?
o A) Cookie
o B) File
o C) Database
o D) Redis
Answer: B
33. Which method would you use to redirect a user to another route in Laravel?
o A) redirectTo()
o B) redirect()
o C) route()
o D) return redirect()
Answer: D
34. What is the purpose of the route() helper function in Laravel?
o A) To define routes
o B) To generate URLs to named routes
o C) To bind controllers to routes
o D) To handle middleware
Answer: B
35. In Laravel, which command is used to create a new middleware?
o A) php artisan make:middleware
o B) php artisan new:middleware
o C) php artisan middleware:create
o D) php artisan create:middleware
Answer: A
36. What is the purpose of the app/Http/Controllers directory in Laravel?
o A) To store models
o B) To store middleware
o C) To store controllers
o D) To store views
Answer: C
37. How do you return a view with a specific layout in Laravel?
o A) return view('view_name')->layout('layout_name');
o B) return view('view_name', ['layout' => 'layout_name']);
o C) return view('layout.view_name');
o D) return view('view_name')->extends('layout_name');
Answer: D
38. Which command is used to generate a new request class in Laravel?
o A) php artisan make:request
o B) php artisan new:request
o C) php request:create
o D) php artisan create:request
Answer: A
39. What is the purpose of composer.json in a Laravel project?
o A) To define routes
o B) To manage dependencies
o C) To configure the application
o D) To manage middleware
Answer: B
40. How do you enable query logging in Laravel?
o A) DB::enableQueryLog();
o B) DB::logQueries();
o C) DB::startQueryLog();
o D) DB::recordQueries();
Answer: A
41. What does the where method do in Eloquent?
o A) Adds a condition to the query
o B) Retrieves all records
o C) Deletes a record
o D) Updates a record
Answer: A
42. Which file is responsible for registering service providers in Laravel?
o A) app.php
o B) config/app.php
o C) routes/web.php
o D) app/Providers/AppServiceProvider.php
Answer: B
43. Which of the following is a valid way to create a resourceful route in Laravel?
o A) Route::resource('photos', 'PhotoController');
o B) Route::create('photos', 'PhotoController');
o C) Route::route('photos', 'PhotoController');
o D) Route::add('photos', 'PhotoController');
Answer: A
44. What is the purpose of the storage directory in a Laravel application?
o A) To store views
o B) To store logs and cached files
o C) To store controllers
o D) To store configuration files
Answer: B
45. Which method is used to update a resource in Eloquent?
o A) update()
o B) save()
o C) edit()
o D) modify()
Answer: A
46. What is the purpose of the @yield directive in Blade templates?
o A) To define a section that can be filled by child views
o B) To include another view
o C) To return a response
o D) To create a route
Answer: A
47. In Laravel, which command is used to create a new migration file with a specific name?
o A) php artisan make:migration create_users_table
o B) php artisan new:migration create_users_table
o C) php artisan migration:create create_users_table
o D) php artisan make:migration --name=create_users_table
Answer: A
48. What is the purpose of the artisan tinker command in Laravel?
o A) To run migrations
o B) To interact with the application via a REPL
o C) To manage assets
o D) To clear cache
Answer: B
49. Which of the following statements about Laravel's Blade templating engine is TRUE?
o A) It does not support loops.
o B) It uses PHP syntax exclusively.
o C) It allows for template inheritance.
o D) It is a JavaScript framework.
Answer: C
50. How do you access configuration values in Laravel?
o A) config('app.name')
o B) app('config.name')
o C) get_config('app.name')
o D) settings('app.name')
Answer: A
51. Which method is used to delete a resource in Eloquent?
o A) delete()
o B) remove()
o C) destroy()
o D) erase()
Answer: A
52. How can you define a many-to-many relationship in Eloquent?
o A) belongsToMany
o B) hasMany
o C) hasOne
o D) manyToMany
Answer: A
53. What does the with() method do in Eloquent?
o A) Adds a condition to the query
o B) Eager loads relationships
o C) Creates a new record
o D) Deletes a record
Answer: B
54. Which method is used to retrieve a single record by its primary key in Eloquent?
o A) find()
o B) first()
o C) get()
o D) retrieve()
Answer: A
55. What is the role of the app/Models directory in Laravel?
o A) To store views
o B) To store controllers
o C) To store Eloquent models
o D) To store middleware
Answer: C
56. How do you define a route that accepts parameters in Laravel?
o A) Route::get('/user/{id}', 'UserController@show');
o B) Route::get('/user?id={id}', 'UserController@show');
o C) Route::get('/user/', 'UserController@show/{id}');
o D) Route::get('/user/<id>', 'UserController@show');
Answer: A
57. What command is used to create a new seeder class in Laravel?
o A) php artisan make:seeder
o B) php artisan new:seeder
o C) php artisan seed:create
o D) php artisan create:seeder
Answer: A
58. Which method is used to perform a soft delete in Eloquent?
o A) softDelete()
o B) delete()
o C) destroy()
o D) markAsDeleted()
Answer: B
59. What is the purpose of the cache directory in Laravel?
o A) To store database migrations
o B) To store cached data
o C) To store views
o D) To store logs
Answer: B
60. Which command is used to run a specific seeder in Laravel?
o A) php artisan db:seed --class=UserSeeder
o B) php artisan seed:run --class=UserSeeder
o C) php artisan run:seeder UserSeeder
o D) php artisan db:seed --name=UserSeeder
Answer: A
61. What does the @section directive do in Blade templates?
o A) Defines a section of content
o B) Includes another view
o C) Returns a response
o D) Creates a route
Answer: A
62. How do you define a route group with middleware in Laravel?
o A) Route::group(['middleware' => 'auth'], function() {});
o B) Route::middleware('auth')->group(function() {});
o C) Route::withMiddleware('auth')->group(function() {});
o D) Route::apply('auth')->group(function() {});
Answer: A
63. What command is used to create a new job in Laravel?
o A) php artisan make:job
o B) php artisan new:job
o C) php artisan job:create
o D) php artisan create:job
Answer: A
64. What does the save() method do in Eloquent?
o A) Updates a record
o B) Creates a new record
o C) Saves changes to a record
o D) Deletes a record
Answer: C
65. What is the purpose of the public directory in a Laravel application?
o A) To store application logic
o B) To store public assets (CSS, JS, images)
o C) To store configuration files
o D) To store database files
Answer: B
66. Which method is used to generate a URL to a named route in Laravel?
o A) route()
o B) url()
o C) link()
o D) path()
Answer: A
67. What command is used to create a new controller in Laravel?
o A) php artisan make:controller
o B) php artisan new:controller
o C) php artisan controller:create
o D) php artisan create:controller
Answer: A
68. What is the purpose of the .env file in a Laravel project?
o A) To store configuration settings
o B) To manage routes
o C) To handle migrations
o D) To manage database connections
Answer: A
69. Which of the following is NOT a valid way to retrieve all records from a model in Eloquent?
o A) Model::all()
o B) Model::get()
o C) Model::fetchAll()
o D) Model::where([])->get()
Answer: C
70. What does the @include directive do in Blade templates?
o A) Includes another view
o B) Defines a section of content
o C) Returns a response
o D) Creates a route
Answer: A
71. How do you define a one-to-many relationship in Eloquent?
o A) hasMany
o B) belongsTo
o C) hasOne
o D) oneToMany
Answer: A
72. 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 cache:flush
o D) php artisan flush:cache
Answer: A
73. What is the purpose of the config directory in a Laravel application?
o A) To store views
o B) To store configuration files
o C) To store routes
o D) To store migrations
Answer: B
74. Which method is used to create a new record in Eloquent?
o A) create()
o B) add()
o C) insert()
o D) store()
Answer: A
75. What is the purpose of the logs directory in a Laravel application?
o A) To store migrations
o B) To store log files
o C) To store views
o D) To store configuration files
Answer: B
76. What is the command 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
Answer: A
77. How can you create a new policy in Laravel?
o A) php artisan make:policy
o B) php artisan new:policy
o C) php artisan policy:create
o D) php artisan create:policy
Answer: A
78. What is the purpose of the routes/web.php file in a Laravel application?
o A) To define API routes
o B) To define web routes
o C) To manage database migrations
o D) To handle middleware
Answer: B
79. Which method is used to attach a model to a user in Laravel?
o A) attach()
o B) link()
o C) associate()
o D) add()
Answer: A
80. What is the purpose of the artisan migrate:rollback command?
o A) To create a new migration
o B) To revert the last database migration
o C) To run all migrations
o D) To clear migration history
Answer: B
81. How do you define a route that only accepts POST requests in Laravel?
o A) Route::post('/submit', 'SubmitController@store');
o B) Route::get('/submit', 'SubmitController@store');
o C) Route::request('submit', 'SubmitController@store');
o D) Route::method('POST', '/submit', 'SubmitController@store');
Answer: A
82. Which command is used to create a new test in Laravel?
o A) php artisan make:test
o B) php artisan new:test
o C) php artisan test:create
o D) php artisan create:test
Answer: A
83. What is the purpose of the app/Providers directory in Laravel?
o A) To store views
o B) To store service providers
o C) To store middleware
o D) To store routes
Answer: B
84. Which of the following is a valid way to create a new resource controller in Laravel?
o A) php artisan make:controller PhotoController --resource
o B) php artisan new:controller PhotoController --resource
o C) php artisan controller:create PhotoController --resource
o D) php artisan create:controller PhotoController --resource
Answer: A
85. What does the abort() function do in Laravel?
o A) Stops the application
o B) Triggers an HTTP exception
o C) Clears the cache
o D) Redirects the user
Answer: B
86. Which method is used to retrieve the current authenticated user in Laravel?
o A) Auth::user()
o B) Auth::currentUser()
o C) Auth::getUser()
o D) Auth::retrieve()
Answer: A
87. How do you validate incoming requests in Laravel?
o A) Using middleware
o B) Using request classes
o C) Using controllers only
o D) Using views
Answer: B
88. What is the purpose of the route::view() method in Laravel?
o A) To display a view for a route
o B) To define a route
o C) To return a JSON response
o D) To perform middleware checks
Answer: A
89. Which command is used to refresh the database by rolling back and running migrations again?
o A) php artisan migrate:refresh
o B) php artisan migrate:reset
o C) php artisan db:refresh
o D) php artisan db:migrate:refresh
Answer: A
90. What does the dd() function do in Laravel?
o A) Dumps data and dies
o B) Deletes data
o C) Returns data
o D) None of the above
Answer: A
91. How do you create a custom validation rule in Laravel?
o A) By creating a new request class
o B) By using the Validator facade
o C) By creating a new validation rule class
o D) All of the above
Answer: D
92. What is the purpose of the artisan serve command in Laravel?
o A) To run database migrations
o B) To serve the application on a development server
o C) To clear cache
o D) To run tests
Answer: B
93. Which of the following is used to handle errors in Laravel?
o A) app/Exceptions/Handler.php
o B) app/Errors/Handler.php
o C) app/Http/Errors.php
o D) app/Error/Handler.php
Answer: A
94. What is the purpose of the @csrf directive in Blade templates?
o A) To prevent SQL injection
o B) To add a CSRF token to forms
o C) To define routes
o D) To manage sessions
Answer: B
95. How do you access session data in Laravel?
o A) session('key')
o B) Session::get('key')
o C) Both A and B
o D) getSession('key')
Answer: C
96. What does the str_limit() helper function do in Laravel?
o A) Limits the length of a string
o B) Trims whitespace from a string
o C) Converts a string to uppercase
o D) None of the above
Answer: A
97. What is the command to create a new model in Laravel?
o A) php artisan make:model
o B) php artisan new:model
o C) php artisan model:create
o D) php artisan create:model
Answer: A
98. Which method is used to retrieve a record by its primary key in Eloquent?
o A) find()
o B) findOrFail()
o C) get()
o D) Both A and B
Answer: D
99. What is the purpose of the lang directory in a Laravel application?
o A) To store language files
o B) To store views
o C) To store middleware
o D) To store configuration files
Answer: A
100. What does the cache() function do in Laravel? - A) Retrieves cached data - B) Stores data in the cache - C) Both A and B - D) Clears cache data
Answer: C