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 make:model ModelName
B. php artisan new:model ModelName
C. php artisan generate:model ModelName
D. php artisan create:model ModelName

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

A. tbl_flights
B. flight_table
C. flight
D. 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. private $db_table
B. protected $table
C. public $tableName
D. protected $source

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

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

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

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

6 Which Artisan command executes all outstanding migrations?

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

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

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

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

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

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

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

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

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. $protected
B. $blocked
C. $fillable
D. $guarded

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

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

13 What is the purpose of Database Seeding?

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

14 Which Artisan command creates a new Seeder class?

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

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

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

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. key
C. uuid
D. id

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. php artisan migrate:refresh
B. php artisan migrate:rollback
C. php artisan migrate:reset
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::text()
D. DB::sql()

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

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

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

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

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

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

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

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

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

A. Y-m-d H:i:s (String)
B. Unix Timestamp (Integer)
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 adds a unique index.
B. It automatically determines the referenced table and column based on conventions.
C. It forces the column to be an integer.
D. It prevents null values.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. One-to-One
B. Many-to-Many
C. One-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::run()
C. DB::statement()
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 db:alter users
C. php artisan modify:table users
D. php artisan make:migration add_votes_to_users_table --table=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. A recursive database error.
B. When a query fails N+1 times.
C. When pagination creates too many pages.
D. When the loop executes 1 query to fetch parents and N queries to fetch children.

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

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

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

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

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

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