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

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. public $tableName
C. private $db_table
D. protected $table

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

6 Which Artisan command executes all outstanding migrations?

A. php artisan migrate
B. php artisan db:migrate
C. php artisan migrate:run
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. retrieve()
B. all()
C. get()
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. By manually escaping strings
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 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. $visible
B. $fillable
C. $guarded
D. $hidden

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

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

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

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

13 What is the purpose of Database Seeding?

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

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. 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. Mongo\Eloquent
B. MongoDB\Model
C. Jenssegers\Mongodb\Eloquent\Model
D. Illuminate\Database\NoSQL\Model

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

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

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

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

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

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

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

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

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

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

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

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

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

A. routes/console.php
B. routes/api.php
C. routes/web.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. Links
B. Relationships
C. Unions
D. Merged Models

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

A. php artisan db:undo
B. php artisan migrate:refresh
C. php artisan migrate:rollback
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. create()
B. show($id)
C. index()
D. store()

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::find(1)->kill();
D. User::remove(1);

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

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

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

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

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. redirect('/home')
C. response()->json(['error' => 'Not Found'], 404)
D. return null;

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

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

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

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

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

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

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

A. To define a blueprint for generating fake data for models
B. To compile the PHP code
C. To build the database schema
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. $driver
C. $link
D. $database

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

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

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

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

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::run()
B. DB::statement()
C. DB::exec()
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 create_users_table
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/$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 a query fails N+1 times.
B. A recursive database error.
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. Using raw SQL
C. Using all()
D. Increasing memory limit

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 string
D. A UTC DateTime (BSON Date)

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

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