Unit 6: Getting started with databases - Practice Quiz

INT221 — Mvc Programming 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 create:model ModelName
D. php artisan generate:model ModelName

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

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

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

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

A. To serve as version control for the database schema
B. To encrypt database connections
C. To optimize SQL queries automatically
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. rollback()
B. down()
C. back()
D. reverse()

6 Which Artisan command executes all outstanding migrations?

A. php artisan db:migrate
B. php artisan schema:update
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. get()
B. all()
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 single generic stdClass object representing the record
B. An Eloquent Model instance
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. $guarded
D. $fillable

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::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 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 create:seed Name
B. php artisan make:seeder Name
C. php artisan new: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. illuminate/nosql
C. laravel/mongo-connector
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. Illuminate\Database\NoSQL\Model
C. Jenssegers\Mongodb\Eloquent\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. MongoDB automatically creates migration files.
B. Migrations are only used for creating indexes and unique constraints.
C. You must define all columns exactly like SQL.
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. getOrFail()
C. find()
D. findOrFail()

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

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

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

A. 202 Accepted
B. 204 No Content
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. Middleware
B. API Resources
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/api.php
D. routes/web.php

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

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

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

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

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

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

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::find(1)->kill();
B. User::remove(1);
C. User::delete(1);
D. User::destroy(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 '$gt', '$in'
B. SQL standard operators only
C. Python syntax
D. C# LINQ syntax

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. Microtime
B. ISO 8601
C. Unix Timestamp (Integer)
D. Y-m-d H:i:s (String)

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

A. $this->execute(Class::class);
B. $this->run(Class::class);
C. $this->seed(Class::class);
D. $this->call(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 prevents null values.
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. return null;
B. response()->json(['error' => 'Not Found'], 500)
C. response()->json(['error' => 'Not Found'], 404)
D. redirect('/home')

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

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

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. As separate tables via JOINs
B. They cannot be accessed
C. Using XML parsing
D. As PHP arrays or objects accessed via dot notation

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

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

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

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

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. Creates a new resource at the top
C. Updates the first record
D. Retrieves the resource headers without the body

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

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

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

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

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

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

A. A MySQL Timestamp
B. A UTC DateTime (BSON Date)
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 Model
B. It implements Serializable
C. It implements ArrayAccess
D. It extends JsonResource