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 encrypt form data before transmission
D. To validate the HTTP method used in the form

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

A. @token
B. @method
C. @csrf
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. 404 Not Found
B. 500 Internal Server Error
C. 419 Page Expired
D. 403 Forbidden

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

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

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

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

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

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

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

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

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

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

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

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

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. Comma-separated string
C. PHP Array
D. Linked List

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

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

13 What does the unique:users validation rule check?

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

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. sometimes
C. optional
D. allow_empty

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

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

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->has())
D. @if($errors->any())

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

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

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

A. @check('email')
B. @error('email')
C. @haserror('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. An array of all errors for the 'title' field
B. The validation error message for the 'title' field
C. The validation rule that failed
D. The name of the field ('title')

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

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

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

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

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

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

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

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

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 return a success message
D. To send an email notification

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

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

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

A. 'photo' => 'is_image'
B. 'photo' => 'type:jpg,png'
C. 'photo' => 'image'
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. Pass an array as the second argument: ['email.required' => 'We need your email!']
B. Define it in the config/app.php file
C. Pass an array as the third argument: ['email.required' => 'We need your email!']
D. Use the setMessage method

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

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

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

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

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

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

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

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

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. website
B. http
C. link
D. url

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

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

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

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

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

A. To encapsulate validation and authorization logic in a dedicated class
B. To manage database migrations for forms
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 validates the form data
B. It checks if the user has permission to make the request
C. It sets the validation error messages
D. It logs the user in

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

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

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

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

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. date_min:tomorrow
B. later_than:tomorrow
C. future:true
D. after:tomorrow

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

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

42 Why might you get a TokenMismatchException?

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

43 How can you exclude specific URIs from CSRF protection?

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

44 The accepted validation rule is commonly used for:

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

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

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

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

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

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

A. The input is disabled
B. The input retains the user's entry after a validation error redirect
C. The email is automatically validated
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. bool
B. boolean
C. is_bool
D. true_false

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

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

50 Which validation rule is the opposite of accepted?

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