Create New Post

Laravel - MCQ

1. What does Laravel use as its default templating engine?

  • A) Twig
  • B) Blade (Correct Answer)
  • C) Smarty
  • D) Handlebars

2. In Laravel, what is the purpose of Eloquent?

  • A) View rendering
  • B) Database query builder (Correct Answer)
  • C) Authentication
  • D) Routing

3. How do you define a route with parameters in Laravel?

  • A) /route/{param}
  • B) /route/{param?}
  • C) /route/param
  • D) /route/{param} (Correct Answer)

4. What is the purpose of the composer.json file in a Laravel project?

  • A) Configuration settings
  • B) Dependency management (Correct Answer)
  • C) Route definitions
  • D) Database migrations

5. Which Artisan command is used to create a new controller in Laravel?

  • A) make:model
  • B) create:controller
  • C) generate:controller
  • D) make:controller (Correct Answer)

6. How can you secure routes in Laravel to only allow authenticated users?

  • A) Using middleware (Correct Answer)
  • B) Changing the .env file
  • C) Configuring the routes/web.php file
  • D) Using Eloquent models

7. What is the purpose of the fillable property in an Eloquent model?

  • A) Sets the table name
  • B) Defines primary key
  • C) Specifies columns that can be mass-assigned (Correct Answer)
  • D) Determines relationships

8. Which of the following is the correct way to define a one-to-many relationship in Eloquent?

  • A) hasOne
  • B) belongsTo
  • C) hasMany (Correct Answer)
  • D) belongsToMany

9. In Laravel, what is the purpose of database migrations?

  • A) Seeding the database
  • B) Querying the database
  • C) Managing database schema changes over time (Correct Answer)
  • D) Creating database backups

10. What command is used to run Laravel's built-in development server?

  • A) php artisan serve (Correct Answer)
  • B) php artisan run
  • C) artisan start
  • D) php start

11. What is the purpose of Laravel's composer command?

  • A) Manage database migrations
  • B) Dependency management (Correct Answer)
  • C) Artisan command creation
  • D) HTTP request handling

12. How do you create a new model along with its associated migration and controller in Laravel using Artisan?

  • A) php artisan make:model MyModel
  • B) php artisan generate:model MyModel
  • C) php artisan scaffold MyModel
  • D) php artisan make:model MyModel -m -c (Correct Answer)

13. Which of the following is a valid way to define a named route in Laravel?

  • A) Route::get('path', 'Controller@method')->name('myRoute'); (Correct Answer)
  • B) Route::name('myRoute')->get('path', 'Controller@method');
  • C) Route::get('path', 'Controller@method', 'myRoute');
  • D) Route::named('myRoute')->get('path', 'Controller@method');

14. In Laravel, what is the purpose of the with method when flashing data to the session?

  • A) Store data in the session permanently
  • B) Attach data to the next request (Correct Answer)
  • C) Share data across all views
  • D) Create a global variable

15. How do you define a custom primary key in an Eloquent model in Laravel?

  • A) Override the primary_key property
  • B) Use the key method
  • C) Set the $primaryKey property (Correct Answer)
  • D) Specify it in the database migration file

16. Which of the following is a valid way to include a view partial in Blade?

  • A) @insert('partial.view')
  • B) @component('partial.view')
  • C) @include('partial.view') (Correct Answer)
  • D) @partial('partial.view')

17. What is the purpose of Laravel's event broadcasting?

  • A) Broadcasting live video streams
  • B) Sending notifications to mobile devices
  • C) Real-time communication between server and client (Correct Answer)
  • D) Broadcasting radio signals

18. Which Artisan command is used to create a new middleware in Laravel?

  • A) php artisan generate:middleware
  • B) php artisan make:middleware (Correct Answer)
  • C) php artisan new:middleware
  • D) php artisan create:middleware

19. In Laravel, what is the purpose of the env function?

  • A) Fetch the current environment
  • B) Access environment variables (Correct Answer)
  • C) Execute a system command
  • D) Define a new environment

20. What is the purpose of the compact function in Laravel?

  • A) Create a compressed file
  • B) Reduce image file size
  • C) Create an array by compacting variables (Correct Answer)
  • D) Minimize database queries

21. What is the purpose of the artisan tinker command in Laravel?

  • A) Generate a new database migration
  • B) Interact with the Laravel application using a REPL (Read-Eval-Print Loop) (Correct Answer)
  • C) Run all PHPUnit tests
  • D) Clear the application cache

22. How can you implement method injection in a Laravel controller?

  • A) Use the request function
  • B) Utilize the input method
  • C) Type-hint the dependencies in the controller method signature (Correct Answer)
  • D) Access the data using the get method

23. What is the purpose of Laravel's task scheduling feature?

  • A) Run scheduled tasks at specific intervals (Correct Answer)
  • B) Manage background jobs
  • C) Execute one-time tasks immediately
  • D) Synchronize data between multiple databases

24. How do you define a named middleware in Laravel?

  • A) php artisan make:middleware MyMiddleware
  • B) Register the middleware in the Kernel.php file (Correct Answer)
  • C) Use the middleware method in the controller
  • D) Define it in the .env file

25. What is the purpose of the app function in Laravel?

  • A) Retrieve an instance of the application container (Correct Answer)
  • B) Define a new route
  • C) Access the application configuration
  • D) Create a new instance of a model

26. How can you customize the primary key column name in an Eloquent model?

  • A) Use the $primaryKey property (Correct Answer)
  • B) Override the primary_key method
  • C) Set it in the database migration file
  • D) Specify it in the .env file

27. In Laravel, what is the purpose of the hasManyThrough relationship?

  • A) Define a many-to-many relationship
  • B) Establish a one-to-one relationship
  • C) Define a one-to-many relationship through an intermediate table
  • D) Define a one-to-many relationship through another Eloquent model (Correct Answer)

28. Which of the following is a valid way to define a route parameter with a regular expression in Laravel?

  • A) {param:regex(\d+)}
  • B) {param:numeric}
  • C) {param} (Correct Answer)
  • D) {param:integer}

29. How do you retrieve the last inserted ID after saving a new Eloquent model instance?

  • A) Access the lastInsertId property
  • B) Use the insertGetId method
  • C) Retrieve it from the $id property of the model instance (Correct Answer)
  • D) Execute a raw SQL query

30. What is the purpose of the php artisan optimize command in Laravel?

  • A) Optimize and compress image assets
  • B) Minimize CSS and JavaScript files
  • C) Optimize the application for production (Correct Answer)
  • D) Run database migrations

1. What is Laravel?

  • A) A JavaScript library
  • B) A PHP framework (Correct Answer)
  • C) A database management system
  • D) An operating system

2. What is the default database connection in Laravel?

  • A) SQLite
  • B) MySQL
  • C) PostgreSQL
  • D) MySQL (Correct Answer)

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

  • A) composer create-project laravel/laravel (Correct Answer)
  • B) laravel new project
  • C) php artisan make:project
  • D) artisan create laravel

4. What is the purpose of the artisan command in Laravel?

  • A) To generate controllers
  • B) To manage dependencies
  • C) To interact with the application's console (Correct Answer)
  • D) To create database tables

5. In Laravel, where are the configuration files located?

  • A) /config directory (Correct Answer)
  • B) /resources/config
  • C) /app/config
  • D) /configuration

6. What is the purpose of the routes/web.php file in Laravel?

  • A) Define routes for web middleware (Correct Answer)
  • B) Configure web server settings
  • C) Create web templates
  • D) Define web-based Artisan commands

7. Which Eloquent method is used to retrieve all records from a table?

  • A) all (Correct Answer)
  • B) get
  • C) find
  • D) fetch

8. What is the purpose of the composer dump-autoload command?

  • A) Optimize Composer packages
  • B) Clear the cache
  • C) Reload the Composer autoloader (Correct Answer)
  • D) Update Laravel dependencies

9. How do you define a middleware in Laravel?

  • A) php artisan make:middleware MyMiddleware
  • B) Create a class in the /app/Middleware directory (Correct Answer)
  • C) Configure it in the .env file
  • D) Use the middleware method in routes

10. What is the primary role of Eloquent in Laravel?

  • A) HTML templating
  • B) Database query builder (Correct Answer)
  • C) Asset compilation
  • D) Code optimization

11. Which Eloquent relationship represents a many-to-many association?

  • A) hasMany
  • B) belongsTo
  • C) belongsToMany (Correct Answer)
  • D) hasOneThrough

12. How do you create a migration file for a new database table?

  • A) php artisan make:migration create_table_name
  • B) php artisan migrate:make create_table_name
  • C) php artisan make:migration create_table_name --create
  • D) php artisan make:migration create_table_name --table (Correct Answer)

13. What is the purpose of the with method in Eloquent?

  • A) Eager loading relationships
  • B) Include related models in a query (Correct Answer)
  • C) Define global scope
  • D) Create a new instance of a model

14. Which command is used to roll back the last database migration in Laravel?

  • A) php artisan migrate:rollback
  • B) php artisan migrate:reset
  • C) php artisan migrate:undo
  • D) php artisan migrate:rollback (Correct Answer)

15. In Laravel, what does CSRF stand for?

  • A) Cross-Site Request Forgery (Correct Answer)
  • B) Cross-Site Request Formatting
  • C) Cryptographic Security for Requests
  • D) Codeigniter Security Request Framework

16. How can you access session data in Laravel?

  • A) $this->session
  • B) Session::get() (Correct Answer)
  • C) $request->session()
  • D) session()->fetch()

17. Which Blade directive is used for including a sub-view?

  • A) @import
  • B) @include (Correct Answer)
  • C) @extends
  • D) @yield

18. What is the purpose of the php artisan serve command?

  • A) Run unit tests
  • B) Serve the application on the PHP built-in server (Correct Answer)
  • C) Start a queue worker
  • D) Optimize the application

19. Which of the following is a valid route parameter constraint in Laravel?

  • A) {id:numeric}
  • B) {id:int}
  • C) {id:integer} (Correct Answer)
  • D) {id:alpha}

20. In Laravel, what is the purpose of the php artisan make:controller command?

  • A) Create a new model
  • B) Generate a controller class (Correct Answer)
  • C) Run a controller method
  • D) Register a controller route

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

52349