Unit 5 - Practice Quiz

INT221 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 What is the primary purpose of the CSRF token in Laravel forms?

A. To automatically populate form fields with old data
B. To protect the application from Cross-Site Request Forgery attacks
C. To validate the HTTP method used in the form
D. To encrypt form data before transmission

2 Which Blade directive is used to generate the hidden CSRF token input field inside a form?

A. @method
B. @csrf
C. @token
D. @secure

3 If a POST request is made to a Laravel route without a valid CSRF token, which HTTP status code is typically returned?

A. 419 Page Expired
B. 403 Forbidden
C. 404 Not Found
D. 500 Internal Server Error

4 HTML forms natively support which of the following HTTP verbs?

A. POST and PUT only
B. GET, POST, and PATCH
C. GET and POST only
D. GET, POST, PUT, and DELETE

5 Which Blade directive helps in performing 'Method Spoofing' to simulate PUT or DELETE requests?

A. @http
B. @method
C. @spoof
D. @verb

6 What is the name of the hidden input field used by Laravel to determine the spoofed HTTP method?

A. _method
B. _action
C. _verb
D. _token

7 In a controller, which method is most commonly called on the $request object to validate incoming data?

A. $request->verify()
B. $request->sanitize()
C. $request->validate()
D. $request->check()

8 What happens automatically when the $request->validate() method fails?

A. It returns a JSON response with the errors immediately
B. It stops execution and logs the error without redirecting
C. It redirects the user back to the previous location with errors and input flashed to the session
D. It throws a 500 error

9 Which validation rule ensures that a field must be present in the input data and not empty?

A. filled
B. required
C. present
D. not_null

10 How can you specify multiple validation rules for a single field using the pipe syntax?

A. 'title' => 'required|unique:posts|max:255'
B. 'title' => 'required, unique:posts, max:255'
C. 'title' => 'required/unique:posts/max:255'
D. 'title' => 'required & unique:posts & max:255'

11 Which data structure can be used as an alternative to the pipe syntax for defining rules?

A. JSON Object
B. PHP Array
C. Comma-separated string
D. Linked List

12 Which validation rule checks if two fields (e.g., password and password_confirmation) have the same value?

A. confirmed
B. equal
C. same
D. match

13 What does the unique:users validation rule check?

A. That the user is unique in the session
B. That the input value does not already exist in the users database table
C. That the input value is unique within the form submission
D. That the input value is unique across all tables

14 If you want a field to be optional but validated if it is present, which rule should typically accompany specific constraints?

A. nullable
B. allow_empty
C. sometimes
D. optional

15 Which variable is automatically shared with all views to display validation errors?

A. $errors
B. $messages
C. $exceptions
D. $validation

16 How do you check if there are any validation errors in a Blade view?

A. @if(!empty($errors))
B. @if($errors->count() > 0)
C. @if($errors->any())
D. @if($errors->has())

17 Which method retrieves the first error message for a specific field?

A. $errors->one('field_name')
B. $errors->get('field_name')
C. $errors->first('field_name')
D. $errors->start('field_name')

18 Which Blade directive allows you to quickly check for validation errors on a specific field and display them?

A. @error('email')
B. @haserror('email')
C. @check('email')
D. @validation('email')

19 Consider the following code inside a Blade view:

blade
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror

What does the $message variable contain?

A. The name of the field ('title')
B. The validation error message for the 'title' field
C. An array of all errors for the 'title' field
D. The validation rule that failed

20 To repopulate a form field with its previous value after a validation failure, which helper function is used?

A. back()
B. previous()
C. input()
D. old()

21 What is the second argument of the old() helper function used for? e.g. old('email', $user->email)

A. It specifies the input type
B. It specifies the validation rule
C. It sets a default value if the old input is not found
D. It forces the value to overwrite the old input

22 Which Artisan command creates a new custom validation rule class?

A. php artisan rule:make RuleName
B. php artisan create:rule RuleName
C. php artisan make:rule RuleName
D. php artisan make:validation RuleName

23 In a custom Rule class implementing Illuminate\Contracts\Validation\Rule, which method contains the validation logic?

A. verify()
B. passes()
C. validate()
D. check()

24 In a custom Rule class, what is the purpose of the message() method?

A. To return the validation error message if validation fails
B. To log the error to a file
C. To send an email notification
D. To return a success message

25 Which validation rule stops the validation process for a field immediately after the first failure?

A. break
B. halt
C. stop
D. bail

26 How would you validate that an uploaded file is an image?

A. 'photo' => 'is_image'
B. 'photo' => 'image'
C. 'photo' => 'type:jpg,png'
D. 'photo' => 'filetype:image'

27 What is the correct syntax to define a custom error message for a specific rule on a specific attribute in the validate method?

A. Use the setMessage method
B. Pass an array as the second argument: ['email.required' => 'We need your email!']
C. Pass an array as the third argument: ['email.required' => 'We need your email!']
D. Define it in the config/app.php file

28 Correction on previous logic: In rules, $messages), the messages array is passed as which argument?

A. First
B. Fourth
C. Third
D. Second

29 Which rule checks that a value is within a given list of values?

A. in
B. list
C. array
D. contains

30 What does the max:255 rule implies when validating a numeric field?

A. The number must have at most 255 digits
B. The file size must be 255 kilobytes
C. The string length representation must be 255 characters
D. The number must be less than or equal to 255

31 What does max:1024 imply when validating a file upload?

A. Max 1024 kilobytes
B. Max 1024 characters in filename
C. Max 1024 megabytes
D. Max 1024 bytes

32 How can you validate nested array input, for example, checking the name of every person in a people array?

A. 'people.*.name' => 'required'
B. 'people.name' => 'required'
C. 'people[].name' => 'required'
D. 'people:name' => 'required'

33 Which rule ensures a string is a valid URL?

A. link
B. website
C. url
D. http

34 Which of the following is NOT a valid way to specify a custom validation rule?

A. Extending the Validator via a Service Provider
B. Using a Closure
C. Using a Rule Object
D. Using a standard PHP if statement inside the rules array

35 When using a Closure for custom validation, how do you signal a failure?

A. Return false
B. Return a string
C. Throw an Exception
D. Call the $fail callback with the error message

36 What is the purpose of the FormRequest class in Laravel?

A. To manage database migrations for forms
B. To encapsulate validation and authorization logic in a dedicated class
C. To handle HTML form rendering
D. To replace the Controller entirely

37 In a FormRequest class, what does the authorize() method do?

A. It sets the validation error messages
B. It checks if the user has permission to make the request
C. It validates the form data
D. It logs the user in

38 Which rule validates that the field under validation matches a given regular expression?

A. pattern
B. match
C. regexp
D. regex

39 To use the regex rule inside a pipe-delimited string, what must you be careful about?

A. Regex rules must always be last
B. You cannot use regex in pipe strings
C. You must escape the pipe character if it is part of the regex
D. You must use an array instead of a pipe string to avoid conflict

40 If you want to validate a date and ensure it is after a specific date (e.g., tomorrow), which rule would you use?

A. after:tomorrow
B. later_than:tomorrow
C. date_min:tomorrow
D. future:true

41 Which method is used to retrieve validated data from a FormRequest instance?

A. $request->checked()
B. $request->validated()
C. $request->all()
D. $request->input()

42 Why might you get a TokenMismatchException?

A. The CSRF token is missing or invalid
B. The validation rules failed
C. The database connection failed
D. The user password is incorrect

43 How can you exclude specific URIs from CSRF protection?

A. It is not possible to exclude URIs
B. Add URIs to the $except array in the VerifyCsrfToken middleware
C. Use @without_csrf in the blade file
D. Delete the VerifyCsrfToken middleware

44 The accepted validation rule is commonly used for:

A. Password confirmation
B. Email validation
C. File uploads
D. Terms of Service acceptance (checkbox)

45 What is the key difference between size rule and min/max rules?

A. There is no difference
B. size is only for files
C. size validates an exact amount (characters, number, or file size), whereas min/max validate a range
D. min/max is only for numbers

46 Which rule validates that the value must exist in a given database table?

A. contains
B. database
C. has
D. exists

47 When repopulating forms, what does <input type="text" name="email" value="{{ old('email') }}"> ensure?

A. The input retains the user's entry after a validation error redirect
B. The email is automatically validated
C. The input is disabled
D. The input shows the email currently stored in the database

48 If you need to validate a boolean value, which rule should be used?

A. is_bool
B. true_false
C. boolean
D. bool

49 In a Blade template, how is the $errors variable passed to the view?

A. It is a global PHP variable
B. It is created by the @csrf directive
C. You must manually pass it from the controller
D. It is injected via the ShareErrorsFromSession middleware

50 Which validation rule is the opposite of accepted?

A. rejected
B. denied
C. refused
D. declined