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 encrypt form data before transmission
B. To protect the application from Cross-Site Request Forgery attacks
C. To validate the HTTP method used in the form
D. To automatically populate form fields with old data

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

A. @token
B. @csrf
C. @method
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. GET and POST only
B. GET, POST, PUT, and DELETE
C. POST and PUT only
D. GET, POST, and PATCH

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

A. @spoof
B. @http
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. _action
D. _verb

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

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

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. filled
B. present
C. required
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. same
B. match
C. confirmed
D. equal

13 What does the unique:users validation rule check?

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

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

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

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

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

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

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

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

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

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

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

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

A. It specifies the validation rule
B. It specifies the input type
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 make:validation RuleName
B. php artisan create:rule RuleName
C. php artisan make:rule 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. validate()
B. check()
C. passes()
D. verify()

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

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

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

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

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

A. 'photo' => 'filetype:image'
B. 'photo' => 'image'
C. 'photo' => 'type:jpg,png'
D. 'photo' => 'is_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 third argument: ['email.required' => 'We need your email!']
B. Pass an array as the second argument: ['email.required' => 'We need your email!']
C. Use the setMessage method
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. Second
C. Third
D. Fourth

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

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

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

A. The number must be less than or equal to 255
B. The number must have at most 255 digits
C. The string length representation must be 255 characters
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 kilobytes
C. Max 1024 megabytes
D. Max 1024 characters in filename

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

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

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

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

A. Return false
B. Throw an Exception
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 handle HTML form rendering
B. To encapsulate validation and authorization logic in a dedicated class
C. To replace the Controller entirely
D. To manage database migrations for forms

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 logs the user in
D. It sets the validation error messages

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

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

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

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

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

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

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

42 Why might you get a TokenMismatchException?

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

43 How can you exclude specific URIs from CSRF protection?

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

44 The accepted validation rule is commonly used for:

A. Email validation
B. Password confirmation
C. Terms of Service acceptance (checkbox)
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. database
B. has
C. exists
D. contains

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

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

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

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

50 Which validation rule is the opposite of accepted?

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