0% found this document useful (0 votes)
4 views3 pages

Python Coding Challenges and Solutions

The document contains a series of questions and answers related to Python programming concepts, including string compression, decorators, list comprehensions, exception handling, sorting, OOP magic methods, socket handling, memory management, CPU profiling, secrets management, and FastAPI user creation and file processing. Each question is followed by multiple-choice options with the correct answer indicated. The content serves as a quiz or review material for Python developers.

Uploaded by

abhishek.kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Coding Challenges and Solutions

The document contains a series of questions and answers related to Python programming concepts, including string compression, decorators, list comprehensions, exception handling, sorting, OOP magic methods, socket handling, memory management, CPU profiling, secrets management, and FastAPI user creation and file processing. Each question is followed by multiple-choice options with the correct answer indicated. The content serves as a quiz or review material for Python developers.

Uploaded by

abhishek.kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1.

String Compression
Which of the following outputs correctly compresses "aaabccddd" using counts of repeated
characters (only when count > 1)?
A. "aaabccddd"
B. "a3b1c2d3"
C. "a3bc2d3"
D. "abc3d3"
Answer: B
Q2. Execution Timer Decorator
What is the main purpose of a Python decorator like @timer?

A. To change the function name


B. To convert the function into a class
C. To measure and print the function’s execution time
D. To increase memory efficiency of the function
Answer: C
Q3. List Comprehension Challenge
Which one-liner correctly finds numbers 1–100 divisible by 7 or 11 but not both?
A. [x for x in range(1, 101) if x % 7 == 0 or x % 11 == 0]
B. [x for x in range(1, 101) if (x % 7 == 0) ^ (x % 11 == 0)]
C. [x for x in range(1, 101) if x % 7 == 0 and x % 11 == 0]
D. [x for x in range(1, 101) if x % 7 != 0 and x % 11 != 0]
Answer: A
Q4. Custom Exception Handling
Which of the following raises a custom exception if denominator is 0?
A. raise ZeroDivisionError
B. raise DivideByZeroError
C. print("Error")
D. exit()
Answer: A
Q5. File Parsing and Sorting
Which approach correctly sorts a list of dictionaries by 'score' in descending order?

A. sorted(data, key=lambda x: x['score'])


B. sorted(data, key=lambda x: x['score'], reverse=True)
C. [Link](key=lambda x: x['score'])
D. [Link]()
Answer: B

Q6. OOP – Magic Methods


Which magic method is used to compare objects based on area?
A. __init__()
B. __str__()
C. __eq__()
D. __repr__()
Answer: D
Q7. Efficient Socket Handling
Which approach is most efficient to handle multiple socket connections (10,000 clients) in Python?
A. New thread per client
B. asyncio or select
C. Use [Link]() in a loop
D. Blocking sockets
Answer: B
Q8. Stack vs Heap Memory
Which statement correctly describes stack and heap memory in Python?
A. Recursive function calls use heap memory
B. Local variables use heap memory
C. Stack stores function call frames, heap stores objects
D. Stack memory is unlimited
Answer: A
Q9. CPU Profiling
Which Python module is commonly used to identify CPU-intensive parts of a program?
A. memory_profiler
B. traceback
C. cProfile
D. Timeit
Answer: D
Q10. Secrets Management
What is the safest way to manage API keys, passwords, or secret tokens in Python projects?
A. Hardcode them in source code
B. Store them in Git repository
C. Store in environment variables
D. Store in plaintext files
Answer: C

Q11. FastAPI User Creation


Which statement correctly validates JSON data using a Pydantic model for /create_user
endpoint?
A. name: str, email: str, age: int with email validation and age > 18
B. Accepts any JSON without validation
C. Only checks name is string
D. Only validates email
Answer: A
Q12. FastAPI File Processing with Background Task
Which is the correct approach to process a file asynchronously and optionally log to a database in
the background?
A. Accept UploadFile, process in endpoint synchronously
B. Accept UploadFile, use BackgroundTasks for processing
C. Accept file, process after returning JSON synchronously
D. Use async def endpoint but process file in main thread
Answer: B

You might also like