0% found this document useful (0 votes)
6 views20 pages

Advance Python Questions

The document contains a series of questions and answers related to Python programming, specifically focusing on multithreading and Django framework concepts. It includes multiple-choice questions, coding exercises, and explanations of various functions and methods. The content is structured to test knowledge and practical skills in Python threading and Django application development.

Uploaded by

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

Advance Python Questions

The document contains a series of questions and answers related to Python programming, specifically focusing on multithreading and Django framework concepts. It includes multiple-choice questions, coding exercises, and explanations of various functions and methods. The content is structured to test knowledge and practical skills in Python threading and Django application development.

Uploaded by

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

Questions

1. Which module in Python is used for multithreading?


a) threading
b) multiprocessing
c) concurrent

d) async
Answer: a

2. Which method starts a thread in Python?


a) run()
b) start()
c) execute()
d) begin()
Answer: b

3. In Python, the method that defines thread behavior is:


a) start()
b) run()

c) execute()
d) join()
Answer: b

4. Which function is used to pause a thread?


a) stop()

b) sleep()
c) join()
d) wait()

Questions 1
Answer: b

5. The default state of a thread after creation is:


a) Running
b) New (created)
c) Waiting
d) Terminated
Answer: b

6. Which method is used to wait until a thread completes execution?


a) stop()
b) join()
c) wait()
d) exit()
Answer: b

7. Why do we use synchronization in threads?


a) To speed up threads

b) To avoid race conditions


c) To stop all threads

d) To increase memory
Answer: b

8. Which library provides ThreadPoolExecutor in Python?


a) threading

b) [Link]
c) multiprocessing

d) os
Answer: b

9. Which statement is TRUE for Python threads?

a) They run in true parallelism always

Questions 2
b) They are controlled by GIL
c) They cannot be synchronized

d) They work only on Windows


Answer: b

10. Which method is used to check if a thread is still running?


a) alive()

b) isAlive()
c) is_alive()

d) status()
Answer: c

11. Django is a framework for:

a) Machine Learning
b) Web Development

c) Data Analysis
d) Desktop Apps

Answer: b

12. Django is based on which architecture?

a) MVC
b) MVT

c) MVP
d) Layered

Answer: b

13. Which is NOT a feature of Django?

a) ORM
b) Template system

c) Middleware support
d) Real-time OS scheduling

Questions 3
Answer: d

14. The default database in Django is:

a) MySQL
b) PostgreSQL

c) SQLite

d) Oracle
Answer: c

15. Which command creates a Django project?


a) django-admin startproject

b) django-admin newproject

c) python [Link] project

d) django startproject
Answer: a

16. Which file stores project settings?

a) [Link]

b) [Link]

c) [Link]
d) [Link]

Answer: a

17. To activate a virtual environment in Linux/Mac:

a) venv\Scripts\activate

b) source venv/bin/activate

c) activate venv
d) python -m venv

Answer: b

18. Command to create a new Django app:

a) python [Link] startapp

Questions 4
b) django-admin newapp

c) python [Link] appcreate

d) django startapp

Answer: a

19. The file responsible for URL mapping is:

a) [Link]

b) [Link]

c) [Link]
d) [Link]

Answer: a

20. Django’s MVT “T” stands for:

a) Template

b) Test

c) Table
d) Tag

Answer: a

21. Which function is used for defining routes?

a) url()

b) path()
c) include()

d) route()

Answer: b

22. include() is used for:

a) Static files

b) Adding other app’s URLs


c) Adding models

d) Adding templates

Questions 5
Answer: b

23. Which view function returns plain text?


a) render()

b) HttpResponse()

c) redirect()

d) template()
Answer: b

24. render() is used to:


a) Return templates with context

b) Redirect to another page

c) Delete session data

d) Handle errors
Answer: a

25. The first argument of path() is:

a) View name

b) URL pattern
c) Template

d) Model

Answer: b

26. Which symbol is used for variables in Django templates?

a) {{ }}

b) {% %}

c) (( ))
d) <>

Answer: a

27. Which tag is used for logic in Django templates?

a) {{ }}

Questions 6
b) {% %}

c) [[ ]]

d)
Answer: b

28. Template inheritance in Django is done using:

a) extends

b) include

c) inherit
d) super

Answer: a

29. {% static %} is used for:


a) Serving static files
b) Serving dynamic data

c) Serving models
d) Serving views

Answer: a

30. STATICFILES_DIRS is defined in:

a) [Link]
b) [Link]

c) [Link]
d) [Link]
Answer: c

31. Django models are defined in:


a) [Link]

b) [Link]
c) [Link]

d) templates

Questions 7
Answer: a

32. makemigrations command is used to:

a) Apply migrations
b) Create migration files
c) Delete database

d) Start server
Answer: b

33. migrate command is used to:


a) Create models

b) Apply migrations
c) Delete tables
d) Restart server

Answer: b

34. Which ORM method fetches all records?


a) get()
b) all()

c) filter()
d) fetch()
Answer: b

35. get() is used to:


a) Fetch all rows

b) Fetch one object


c) Delete records

d) Update records
Answer: b

36. filter() returns:


a) A single object

Questions 8
b) A QuerySet
c) A dictionary
d) A list only

Answer: b

37. Which method deletes objects in ORM?


a) destroy()
b) drop()

c) delete()
d) remove()
Answer: c

38. Which file registers models to admin site?

a) [Link]
b) [Link]
c) [Link]

d) [Link]
Answer: a

39. Which class helps in creating forms from models?


a) [Link]

b) [Link]
c) [Link]
d) [Link]

Answer: b

40. CRUD stands for:

a) Create, Read, Update, Delete


b) Create, Run, Upload, Delete

c) Copy, Remove, Update, Delete


d) Create, Register, Update, Display

Questions 9
Answer: a

41. Command to create superuser:


a) python [Link] createsuperuser
b) python [Link] superuser

c) django-admin superuser
d) python [Link] adduser

Answer: a

42. The decorator for restricting access to logged-in users:

a) @authenticated
b) @login_required
c) @user_login

d) @restrict
Answer: b

43. Function used to authenticate users:


a) login()

b) authenticate()
c) Both a and b

d) verify()
Answer: c

44. Default authentication system is in:


a) [Link]
b) [Link]

c) [Link]
d) [Link]
Answer: a

45. Which template tag displays logged-in username?

a) {{ [Link] }}

Questions 10
b) {{ username }}
c) {{ [Link] }}

d) {{ [Link] }}
Answer: a

46. logout() in Django:


a) Deletes user from DB

b) Clears session data


c) Restarts server

d) None
Answer: b

47. user.is_authenticated returns:


a) True if logged in
b) False if not logged in

c) Both a and b
d) None
Answer: c

48. Django stores passwords as:

a) Plain text
b) Encrypted text

c) Hashed values
d) Binary values
Answer: c

49. Default authentication backend is:


a) ModelBackend

b) TokenBackend
c) JWTBackend
d) UserBackend

Questions 11
Answer: a

50. LOGIN_REDIRECT_URL is set in:

a) [Link]
b) [Link]
c) [Link]

d) [Link]
Answer: a

1. Write a Python program to create a thread using


[Link] .
Create a thread using [Link]

import threading

def task():
print("Thread is running")

t = [Link](target=task)
[Link]()
[Link]()

2. Write a Python program with two threads: one printing even


numbers, another printing odd numbers.
Two threads: one prints even, another odd numbers

import threading

def print_even():
for i in range(0, 11, 2):
print("Even:", i)

Questions 12
def print_odd():
for i in range(1, 11, 2):
print("Odd:", i)

t1 = [Link](target=print_even)
t2 = [Link](target=print_odd)

[Link]()
[Link]()
[Link]()
[Link]()

[Link] [Link]() in threads.


Demonstrate [Link]() in threads

import threading, time

def worker():
for i in range(3):
print(f"Working... {i}")
[Link](1)

t = [Link](target=worker)
[Link]()
[Link]()

4. Create a thread class that prints numbers from 1–10.


Thread class printing numbers 1–10

import threading

class NumberPrinter([Link]):

Questions 13
def run(self):
for i in range(1, 11):
print("Number:", i)

t = NumberPrinter()
[Link]()
[Link]()

5. Show how to use join() in threads.


Using join() in threads

import threading, time

def worker(name):
print(f"{name} started")
[Link](2)
print(f"{name} finished")

t1 = [Link](target=worker, args=("Thread-1",))
t2 = [Link](target=worker, args=("Thread-2",))

[Link]()
[Link]()

[Link]() # Wait for Thread-1


[Link]() # Wait for Thread-2

print("All threads completed")

[Link] a program with multiple threads incrementing a counter


without synchronization.
Multiple threads incrementing counter (no synchronization)

Questions 14
import threading

counter = 0

def increment():
global counter
for _ in range(100000):
counter += 1

threads = []
for _ in range(5):
t = [Link](target=increment)
[Link](t)
[Link]()

for t in threads:
[Link]()

print("Final counter (no sync):", counter)

👉 Output will usually be wrong (not 500000).


7. Fix the above program using Lock for synchronization.
Fix using Lock (synchronization)

import threading

counter = 0
lock = [Link]()

def increment():
global counter
for _ in range(100000):
with lock:

Questions 15
counter += 1

threads = []
for _ in range(5):
t = [Link](target=increment)
[Link](t)
[Link]()

for t in threads:
[Link]()

print("Final counter (with sync):", counter)

👉 Output will be 500000 correctly.


8. Implement a simple producer-consumer problem using
threads.
Producer–Consumer problem

import threading, time, queue

q = [Link]()

def producer():
for i in range(5):
print("Produced:", i)
[Link](i)
[Link](1)

def consumer():
for _ in range(5):
item = [Link]()
print("Consumed:", item)
[Link](2)

Questions 16
t1 = [Link](target=producer)
t2 = [Link](target=consumer)

[Link]()
[Link]()
[Link]()
[Link]()

9. Write a program using ThreadPoolExecutor to run 5 tasks.


Using ThreadPoolExecutor

from [Link] import ThreadPoolExecutor


import time

def task(n):
print(f"Task {n} started")
[Link](1)
print(f"Task {n} finished")

with ThreadPoolExecutor(max_workers=3) as executor:


for i in range(5):
[Link](task, i)

10. Show the output of thread states in Python.


Show thread states
Python threads don’t expose states like Java, but you can check if they are alive:

import threading, time

def worker():
print("Worker running")
[Link](2)
print("Worker done")

Questions 17
t = [Link](target=worker)
print("Before start:", t.is_alive())

[Link]()
print("After start:", t.is_alive())

[Link]()
print("After join:", t.is_alive())

👉 Example Output:
Before start: False
After start: True
Worker running
Worker done
After join: False

Django Basics
1. Write the steps to create a virtual environment in Python.

2. Install Django and show its version using code.

3. Create a project named mysite .

4. Create an app named blog .

5. Add blog app to INSTALLED_APPS .

URL Routing & Views


1. Write [Link] code to map /home to home_view .

2. Create a function-based view returning "Hello Django" .

3. Write a view returning an HTML template using render() .

Questions 18
4. Show how to use include() in [Link].

5. Redirect / to /home using a view.

Templates
1. Create [Link] and extend it in [Link] .

2. Write a template displaying {{ name }} variable.

3. Write a template loop to print student names.

4. Show how to add a static CSS file in template.

5. Configure STATICFILES_DIRS in [Link].

Models & ORM


1. Create a Student model with fields: name, age, email.

2. Run makemigrations and migrate.

3. Insert a student record using ORM.

4. Fetch all students.

5. Fetch student with id=1.

6. Update student age.

7. Delete a student.

8. Register Student in [Link].

9. Create a ModelForm for Student.

10. Display all students in template.

Authentication
1. Create a superuser.

2. Write view for user registration.

3. Write view for user login.

4. Write view for user logout.

Questions 19
5. Protect a view using @login_required .

6. Display logged-in user in template.

7. Add LOGIN_REDIRECT_URL in [Link].

8. Check if user is authenticated in a view.

9. Create a registration form with username, email, password.

10. Hash a password using make_password .

Advanced / Integration
1. Write CRUD operations for Student model in views.

2. Write ORM query to filter students older than 18.

3. Count total students using ORM.

4. Render students in HTML table.

5. Create a Book model and display all books in template.

Questions 20

You might also like