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

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

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

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

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

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

A. down()
B. reverse()
C. back()
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. fetch()
B. all()
C. retrieve()
D. get()

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

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

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. $hidden
B. $visible
C. $fillable
D. $guarded

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

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

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

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

13 What is the purpose of Database Seeding?

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

14 Which Artisan command creates a new Seeder class?

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

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

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

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

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

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

A. key
B. _id
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. MongoDB automatically creates migration files.
C. Migrations are only used for creating indexes and unique constraints.
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. findOrFail()
B. getOrFail()
C. find()
D. firstOrFail()

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. 201 Created
B. 202 Accepted
C. 204 No Content
D. 200 OK

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

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

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

A. routes/console.php
B. app/Http/routes.php
C. routes/web.php
D. routes/api.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 db:undo
B. php artisan migrate:rollback
C. php artisan migrate:refresh
D. php artisan migrate:reset

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::sql()
C. DB::text()
D. DB::inject()

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

A. index()
B. show($id)
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::remove(1);
C. User::destroy(1);
D. User::delete(1);

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

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

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

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

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

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

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

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

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 PHP arrays or objects accessed via dot notation
B. Using XML parsing
C. As separate tables via JOINs
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. replace()
B. insert()
C. change()
D. update()

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. Updates the first record
B. Retrieves the resource headers without the body
C. Deletes the header of the table
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-Many
B. Polymorphic
C. Many-to-Many
D. One-to-One

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::statement()
B. DB::select()
C. DB::exec()
D. DB::run()

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

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

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

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

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

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

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

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

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

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

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

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