Unit 6 - Practice Quiz

INT221 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which Artisan command is used to generate a new Eloquent model in Laravel?

A. php artisan create:model ModelName
B. php artisan make:model ModelName
C. php artisan new:model ModelName
D. php artisan generate:model ModelName

2 By default, what table name will Laravel assume for a model named Flight?

A. flight
B. flights
C. flight_table
D. tbl_flights

3 If your database table does not follow the default naming convention, which property must be defined in the Model to specify the table name?

A. protected $table
B. public $tableName
C. private $db_table
D. protected $source

4 What is the primary purpose of Database Migrations in an MVC framework like Laravel?

A. To seed the database with dummy data
B. To serve as version control for the database schema
C. To optimize SQL queries automatically
D. To encrypt database connections

5 Which method in a migration file is used to reverse the operations performed by the up method?

A. back()
B. reverse()
C. down()
D. rollback()

6 Which Artisan command executes all outstanding migrations?

A. php artisan migrate:run
B. php artisan db:migrate
C. php artisan migrate
D. php artisan schema:update

7 In the context of the Query Builder, which method is used to retrieve all rows from a table?

A. all()
B. get()
C. fetch()
D. retrieve()

8 When using Query Builder, how do you prevent SQL injection when adding a WHERE clause?

A. By manually escaping strings
B. By using Parameter Binding provided by the builder
C. By using the raw() method only
D. Query Builder does not prevent SQL injection

9 What is the result of the following Query Builder code?

$user = DB::table('users')->where('name', 'John')->first();

A. A collection of all users named John
B. A single generic stdClass object representing the record
C. A boolean value confirming existence
D. An Eloquent Model instance

10 Which property in an Eloquent model allows you to specify which attributes are mass assignable?

A. $guarded
B. $fillable
C. $visible
D. $hidden

11 Conversely, which property acts as a blacklist for mass assignment in Eloquent?

A. $guarded
B. $fillable
C. $protected
D. $blocked

12 Which of the following describes the syntax to create a new record using Eloquent Mass Assignment?

A. User::add(['name' => 'John']);
B. User::create(['name' => 'John']);
C. User::insert(['name' => 'John']);
D. User::make(['name' => 'John'])->save();

13 What is the purpose of Database Seeding?

A. To create the database structure
B. To populate the database with test or initial data
C. To index the database tables
D. To backup the database

14 Which Artisan command creates a new Seeder class?

A. php artisan make:seeder Name
B. php artisan create:seed Name
C. php artisan db:seed Name
D. php artisan new:seeder Name

15 When using Laravel with MongoDB, which library is commonly used to provide Eloquent-like functionality?

A. laravel/mongo-connector
B. jenssegers/mongodb
C. doctrine/mongodb
D. illuminate/nosql

16 In a MongoDB Eloquent model, what class should the Model extend instead of Illuminate\Database\Eloquent\Model?

A. MongoDB\Model
B. Jenssegers\Mongodb\Eloquent\Model
C. Illuminate\Database\NoSQL\Model
D. Mongo\Eloquent

17 What is the default primary key field name in MongoDB?

A. id
B. _id
C. uuid
D. key

18 How do you handle migrations for MongoDB in Laravel given it is schema-less?

A. Migrations are impossible in MongoDB.
B. Migrations are only used for creating indexes and unique constraints.
C. You must define all columns exactly like SQL.
D. MongoDB automatically creates migration files.

19 Which Eloquent method is used to retrieve a model by its primary key or throw an exception if not found?

A. find()
B. firstOrFail()
C. findOrFail()
D. getOrFail()

20 In a REST API implemented with Laravel, which HTTP method corresponds to the update action?

A. GET
B. POST
C. PUT/PATCH
D. DELETE

21 When building an API, what is the standard HTTP status code for a successful resource creation?

A. 200 OK
B. 201 Created
C. 202 Accepted
D. 204 No Content

22 To transform Eloquent models into a specific JSON format for an API, what Laravel feature is recommended?

A. API Resources
B. Blade Templates
C. View Composers
D. Middleware

23 Which file is specifically designated for defining API routes in a standard Laravel installation?

A. routes/web.php
B. routes/api.php
C. routes/console.php
D. app/Http/routes.php

24 In Eloquent, how do you define a Soft Delete?

A. Use the SoftDeletes trait in the model.
B. Set $softDelete = true.
C. Add a deleted = 1 column manually.
D. Use the delete:soft command.

25 What is the equivalent of an SQL JOIN in Eloquent ORM?

A. Merged Models
B. Relationships
C. Unions
D. Links

26 Which command would you use to rollback the very last migration operation?

A. php artisan migrate:reset
B. php artisan migrate:refresh
C. php artisan migrate:rollback
D. php artisan db:undo

27 When using the Query Builder, which method creates a raw expression that is injected into the query as-is?

A. DB::raw()
B. DB::inject()
C. DB::sql()
D. DB::text()

28 In an API Controller, what method is conventionally used to show a single specific resource?

A. index()
B. store()
C. show($id)
D. create()

29 Which of the following is correct to delete a user with ID 1 using Eloquent?

A. User::delete(1);
B. User::destroy(1);
C. User::remove(1);
D. User::find(1)->kill();

30 If you are using MongoDB with Laravel, which operator is often used within where clauses for complex comparisons?

A. SQL standard operators only
B. MongoDB specific operators like 'in'
C. C# LINQ syntax
D. Python syntax

31 What does the timestamps() method create in a migration file?

A. date_created and date_modified columns
B. created_at and updated_at columns
C. timestamp column
D. start and end columns

32 In Eloquent, what is the default format for the $timestamps properties?

A. Unix Timestamp (Integer)
B. Y-m-d H:i:s (String)
C. ISO 8601
D. Microtime

33 Which method enables you to call a seeder from within another seeder (e.g., inside DatabaseSeeder)?

A. $this->seed(Class::class);
B. $this->call(Class::class);
C. $this->run(Class::class);
D. $this->execute(Class::class);

34 When defining a foreign key in a migration, what does constrained() do in modern Laravel syntax?
Example: $table->foreignId('user_id')->constrained();

A. It forces the column to be an integer.
B. It automatically determines the referenced table and column based on conventions.
C. It adds a unique index.
D. It prevents null values.

35 Which return response is most appropriate for an API endpoint if a requested resource is not found?

A. response()->json(['error' => 'Not Found'], 500)
B. response()->json(['error' => 'Not Found'], 404)
C. redirect('/home')
D. return null;

36 What function is used to convert an Eloquent collection to a JSON string directly?

A. toJson()
B. stringify()
C. encode()
D. serialize()

37 Consider the equation for pagination offset: . Which Query Builder method handles this calculation automatically?

A. limit()
B. chunk()
C. paginate()
D. offset()

38 In a MongoDB Laravel model, how are embedded documents usually accessed?

A. As separate tables via JOINs
B. As PHP arrays or objects accessed via dot notation
C. Using XML parsing
D. They cannot be accessed

39 What is the purpose of Model Factories in the context of Seeding?

A. To build the database schema
B. To define a blueprint for generating fake data for models
C. To compile the PHP code
D. To create controller logic

40 Which query builder method is used to update an existing record?

A. insert()
B. replace()
C. update()
D. change()

41 To use a specific database connection for a model other than the default, which property is used?

A. $connection
B. $database
C. $link
D. $driver

42 In a REST API, what does the HEAD method do?

A. Deletes the header of the table
B. Retrieves the resource headers without the body
C. Updates the first record
D. Creates a new resource at the top

43 When defining an Eloquent relationship, return $this->hasMany(Comment::class); implies what relationship structure?

A. One-to-One
B. One-to-Many
C. Many-to-Many
D. Polymorphic

44 If you want to run a raw SQL query using the facade but do not need a return value (e.g., deleting), which method is used?

A. DB::select()
B. DB::statement()
C. DB::run()
D. DB::exec()

45 Which Artisan command generates a migration file specifically to modify an existing table?

A. php artisan make:migration create_users_table
B. php artisan make:migration add_votes_to_users_table --table=users
C. php artisan modify:table users
D. php artisan db:alter users

46 How do you define a route parameter in Laravel (often used for REST APIs)?

A. Route::get('/user/?', ...)
B. Route::get('/user/{id}', ...)
C. Route::get('/user/:id', ...)
D. Route::get('/user/$id', ...)

47 In Eloquent, what is the 'N+1 query problem'?

A. When a query fails N+1 times.
B. When the loop executes 1 query to fetch parents and N queries to fetch children.
C. When pagination creates too many pages.
D. A recursive database error.

48 How is the N+1 query problem solved in Eloquent?

A. Using all()
B. Using Eager Loading with with()
C. Increasing memory limit
D. Using raw SQL

49 When using MongoDB with Laravel, if you want to store a date, it is stored as:

A. A string
B. A MySQL Timestamp
C. A UTC DateTime (BSON Date)
D. An Integer

50 Which interface must a resource class implement to define how a model maps to JSON?

A. It extends JsonResource
B. It implements Serializable
C. It extends Model
D. It implements ArrayAccess