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

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

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

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. back()
B. down()
C. reverse()
D. rollback()

6 Which Artisan command executes all outstanding migrations?

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

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

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

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

A. Query Builder does not prevent SQL injection
B. By using the raw() method only
C. By manually escaping strings
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. A boolean value confirming existence
B. An Eloquent Model instance
C. A single generic stdClass object representing the record
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. $guarded
D. $fillable

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

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

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

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

13 What is the purpose of Database Seeding?

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

14 Which Artisan command creates a new Seeder class?

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

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

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

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. MongoDB\Model
D. Jenssegers\Mongodb\Eloquent\Model

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

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

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

A. Migrations are impossible in MongoDB.
B. You must define all columns exactly like SQL.
C. Migrations are only used for creating indexes and unique constraints.
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. getOrFail()
B. firstOrFail()
C. find()
D. findOrFail()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. User::destroy(1);
B. User::delete(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. Python syntax
B. C# LINQ syntax
C. SQL standard operators only
D. MongoDB specific operators like 'in'

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

A. date_created and date_modified columns
B. timestamp column
C. created_at and updated_at columns
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. Microtime
D. ISO 8601

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

A. $this->call(Class::class);
B. $this->run(Class::class);
C. $this->execute(Class::class);
D. $this->seed(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 forces the column to be an integer.
C. It automatically determines the referenced table and column based on conventions.
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. return null;
C. response()->json(['error' => 'Not Found'], 500)
D. response()->json(['error' => 'Not Found'], 404)

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

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

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

A. chunk()
B. limit()
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. They cannot be accessed
C. Using XML parsing
D. As separate tables via JOINs

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

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

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

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

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

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

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

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

A. php artisan db:alter users
B. php artisan make:migration add_votes_to_users_table --table=users
C. php artisan modify:table 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/?', ...)
C. Route::get('/user/{id}', ...)
D. Route::get('/user/:id', ...)

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

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

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

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

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 MySQL Timestamp
C. An Integer
D. A string

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

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