Top 50 MCQs on Pydantic
1. What is the primary purpose of Pydantic?
A. Creating GUIs
B. Data validation using Python type hints
C. Web scraping
D. Database management
Answer: B โ
2. Which class do you typically inherit from to create a Pydantic model?
A. Model
B. BaseSchema
C. BaseModel
D. DataClass
Answer: C โ
3. Which module is BaseModel imported from?
A. typing
B. dataclasses
C. pydantic
D. fastapi
Answer: C โ
4. What happens if a field receives data of an incompatible type?
A. It is ignored
B. Pydantic attempts validation and conversion if possible
C. The program always crashes
D. The field becomes None
Answer: B โ
5. Which exception is raised when validation fails?
A. TypeError
B. ValidationError
C. ValueError
D. RuntimeError
Answer: B โ
6. Which function is commonly used to define constraints and metadata for fields?
A. Constraint()
B. Meta()
C. Field()
D. Schema()
Answer: C โ
7. Which of the following is a valid field declaration?
A. name = str
B. name: str
C. name => str
D. string name
Answer: B โ
8. Which method converts a Pydantic model into a Python dictionary?
A. to_dict()
B. dict() (v1) / model_dump() (v2)
C. serialize()
D. export()
Answer: B โ
9. Which method converts a model into JSON in Pydantic v2?
A. jsonify()
B. model_dump_json()
C. to_json()
D. dump_json()
Answer: B โ
10. Which decorator is used for field validation in Pydantic v2?
A. @validate
B. @validator
C. @field_validator
D. @check
Answer: C โ
11. Which decorator validates the entire model?
A. @root_validator
B. @model_validator
C. @validator
D. @field_validator
Answer: B โ
12. Which keyword is used to make a field optional?
A. Optional[str]
B. Nullable[str]
C. Maybe[str]
D. Empty[str]
Answer: A โ
13. Which library heavily relies on Pydantic for request and response validation?
A. Flask
B. Django
C. FastAPI
D. Streamlit
Answer: C โ
14. Which method validates an existing Python dictionary against a model?
A. validate_dict()
B. model_validate()
C. parse_dict()
D. load()
Answer: B โ
15. What is the default behavior for extra fields in a Pydantic model?
A. Always raise an error
B. Ignore them
C. Convert them to strings
D. Store them automatically
Answer: B โ
16. Which module provides EmailStr?
A. typing
B. pydantic
C. email
D. validators
Answer: B โ
17. Can Pydantic models contain nested models?
A. No
B. Only with dictionaries
C. Yes
D. Only in FastAPI
Answer: C โ
18. Which method replaces parse_obj() in Pydantic v2?
A. validate_model()
B. model_validate()
C. parse_data()
D. load_model()
Answer: B โ
19. Which feature allows automatic type conversion?
A. Serialization
B. Type coercion
C. Reflection
D. Inheritance
Answer: B โ
20. Why is Pydantic popular in modern Python applications?
A. It replaces SQL databases
B. It validates data using type hints with minimal code
C. It automatically builds websites
D. It eliminates Python classes
Answer: B โ
21. Which argument in Field() specifies a default value?
A. value
B. default
C. initial
D. assign
Answer: B โ
22. Which constraint ensures a string has a minimum length?
A. min_size
B. min_length
C. length_min
D. min
Answer: B โ
23. Which constraint limits the maximum value of an integer?
A. max_value
B. le
C. lt
D. maximum
Answer: B โ
24. What does gt=0 mean in Field()?
A. Greater than or equal to 0
B. Less than 0
C. Greater than 0
D. Equal to 0
Answer: C โ
25. Which constraint ensures a number is less than 100?
A. lt=100
B. le=100
C. max=100
D. limit=100
Answer: A โ
26. Which type is used to validate email addresses?
A. Email
B. EmailField
C. EmailStr
D. MailStr
Answer: C โ
27. Which type validates URLs?
A. Url
B. HttpUrl
C. URLField
D. WebURL
Answer: B โ
28. Which type validates UUID values?
A. UUID
B. GUID
C. UniqueID
D. UUIDStr
Answer: A โ
29. Which Python module provides the UUID type?
A. uuid
B. random
C. hashlib
D. secrets
Answer: A โ
30. Which type is commonly used for date validation?
A. datetime.date
B. Date
C. Time
D. Calendar
Answer: A โ
31. Which method creates a copy of a Pydantic model?
A. clone()
B. copy()
C. model_copy()
D. duplicate()
Answer: C โ
32. Which method returns the model as a Python dictionary?
A. to_dict()
B. model_dump()
C. serialize()
D. dump()
Answer: B โ
33. Which method serializes a model directly to JSON?
A. json()
B. model_json()
C. model_dump_json()
D. dump_json()
Answer: C โ
34. Which decorator is used to validate a single field in Pydantic v2?
A. @validator
B. @field_validator
C. @validate_field
D. @field_check
Answer: B โ
35. Which validator mode runs before standard validation?
A. before
B. early
C. pre
D. first
Answer: A โ
36. Which validator mode runs after validation?
A. late
B. after
C. post
D. end
Answer: B โ
37. Which configuration class in Pydantic v2 replaces the old Config class?
A. ConfigData
B. Settings
C. ConfigDict
D. ModelSettings
Answer: C โ
38. Which configuration option forbids extra fields?
A. extra="allow"
B. extra="ignore"
C. extra="forbid"
D. extra="remove"
Answer: C โ
39. Which configuration option allows extra fields?
A. extra="allow"
B. extra="accept"
C. extra="extra"
D. extra="true"
Answer: A โ
40. Which type hint represents a list of integers?
A. list<int>
B. List[int]
C. array[int]
D. int[]
Answer: B โ
41. Which type hint represents a dictionary with string keys and integer values?
A. dict(str, int)
B. Dict[str, int]
C. Map[str, int]
D. Dictionary<int>
Answer: B โ
42. Which module provides List and Dict in Python versions before 3.9?
A. collections
B. typing
C. types
D. pydantic
Answer: B โ
43. Which type validates IP addresses?
A. IPAddress
B. IPvAnyAddress
C. IPStr
D. IPField
Answer: B โ
44. Which method validates JSON data directly?
A. model_validate_json()
B. validate_json()
C. json_validate()
D. parse_json()
Answer: A โ
45. Pydantic is primarily based on which Python feature?
A. Decorators
B. Type Hints
C. Lambda Functions
D. Generators
Answer: B โ
46. Which statement about Pydantic is correct?
A. It only works with FastAPI.
B. It is a standalone Python library.
C. It requires Django.
D. It only validates JSON.
Answer: B โ
47. Which built-in type is commonly used for boolean fields?
A. Boolean
B. Bool
C. bool
D. logical
Answer: C โ
48. Which of the following is a valid nested model in Pydantic?
A.
class Address(BaseModel):
city: str
class User(BaseModel):
address: Address
B.
address = Address()
C.
address: city
D.
address = str
Answer: A โ
49. Which of the following is NOT a benefit of Pydantic?
A. Automatic data validation
B. Automatic type conversion
C. Runtime SQL optimization
D. Easy serialization
Answer: C โ
50. Why is Pydantic widely used in AI and backend development?
A. It automatically trains machine learning models.
B. It validates and structures data reliably using Python type hints.
C. It replaces NumPy.
D. It eliminates the need for APIs.
Answer: B โ
Final Conclusion on Top 20 MCQs on Pydantic
We really hope that you all have liked this article really very much. Kindly, please share with your friends and family too.