This Python Basics guide features essential interview questions and answers designed to help beginners and professionals excel in Python training and coding interviews. At Softloom IT Training, we empower you with the knowledge and skills to master Python programming and succeed in the tech world.
1. What is the correct file extension for Python files?
A) .pyth
B) .pyt
C) .py
D) .pt
Answer: C) .py
2. How do you output text in Python?
A) echo “Hello, World!”
B) print(“Hello, World!”)
C) cout << “Hello, World!”
D) Console.WriteLine(“Hello, World!”)
Answer: B) print(“Hello, World!”)
3. What will be the output of the following code?
print(type(10))
A) <class ‘int’>
B) <class ‘float’>
C) <class ‘str’>
D) <class ‘bool’>
Answer: A) <class ‘int’>
4. How do you take user input in Python?
A) input()
B) read()
C) scan()
D) gets()
Answer: A) input()
5. What is the correct way to declare a variable in Python?
A) var x = 10
B) int x = 10
C) x = 10
D) let x = 10
Answer: C) x = 10
6. What is the output of the following code?
print(2 ** 3)
A) 6
B) 8
C) 9
D) 16
Answer: B) 8
7. Which of the following is a valid list declaration in Python?
A) list = {1, 2, 3}
B) list = (1, 2, 3)
C) list = [1, 2, 3]
D) list = <1, 2, 3>
Answer: C) list = [1, 2, 3]
8. How do you check the length of a list in Python?
A) size(list)
B) length(list)
C) len(list)
D) count(list)
Answer: C) len(list)
9. What is the output of bool([]) in Python?
A) True
B) False
C) None
D) Error
Answer: B) False
10. How do you write a comment in Python?
A) // This is a comment
B) <!– This is a comment –>
C) # This is a comment
D) /* This is a comment */
Answer: C) # This is a comment
11. What is the output of print(3 == 3.0)?
A) True
B) False
C) Error
D) None
Answer: A) True
12. Which data type is mutable in Python?
A) Tuple
B) String
C) List
D) Integer
Answer: C) List
13. What is the purpose of the break statement in Python?
A) It stops the entire program execution
B) It exits the current loop
C) It skips the current iteration and moves to the next
D) It defines a breakpoint in debugging
Answer: B) It exits the current loop
14. What will be the output of the following code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
A) [1, 2, 3]
B) [1, 2, 3, 4]
C) Error
D) [4, 2, 3, 1]
Answer: B) [1, 2, 3, 4]
15. Which function is used to open a file in Python?
A) open()
B) readfile()
C) openfile()
D) file()
Answer: A) open()
16. What does the strip() method do in Python?
A) Removes all whitespace from a string
B) Removes leading and trailing whitespace from a string
C) Replaces all spaces with underscores
D) Splits a string into a list
Answer: B) Removes leading and trailing whitespace from a string
17. What will be the output of print(10 / 3) in Python?
A) 3
B) 3.3
C) 3.33
D) 3.3333333333333335
Answer: D) 3.3333333333333335
18. Which keyword is used to define a function in Python?
A) function
B) define
C) def
D) fun
Answer: C) def
19. What will be the output of print(10 // 3) in Python?
A) 3.33
B) 3
C) 4
D) 3.3
Answer: B) 3
20. What is the correct way to write a loop that iterates from 1 to 5 in Python?
A) for i in range(1, 5):
B) for i in range(1, 6):
C) for i = 1 to 5:
D) while i <= 5:
Answer: B) for i in range(1, 6):
21. Which of the following is the correct way to check if a variable x is of type int in Python?
A) x.type() == int
B) type(x) == int
C) x.isint()
D) x == int
Answer: B) type(x) == int
22. What is the correct way to create a dictionary in Python?
A) dict = [1, 2, 3]
B) dict = {1: “one”, 2: “two”}
C) dict = (1, 2, 3)
D) dict = <1: “one”, 2: “two”>
Answer: B) dict = {1: “one”, 2: “two”}
23. What is the default value of start in the range() function?
A) 0
B) 1
C) None
D) -1
Answer: A) 0
24. How do you concatenate two strings in Python?
A) string1 + string2
B) string1 & string2
C) string1.concat(string2)
D) string1.concat(string2, separator=””)
Answer: A) string1 + string2
25. Which of the following operators is used for exponentiation in Python?
A) ^
B) *
C) **
D) //
Answer: C) **
26. What is the method to remove an item from a list in Python by its value?
A) remove()
B) pop()
C) del()
D) discard()
Answer: A) remove()
27. Which of the following statements about Python tuples is correct?
A) Tuples are mutable
B) Tuples are immutable
C) Tuples can only contain integers
D) Tuples cannot store duplicates
Answer: B) Tuples are immutable
28. What will be the output of the following code?
x = “Hello, World!”
print(x[7:12])
A) Hello
B) World
C) World!
D) !
Answer: B) World
29. Which is the correct way to declare a set in Python?
A) set = {1, 2, 3}
B) set = [1, 2, 3]
C) set = (1, 2, 3)
D) set = <1, 2, 3>
Answer: A) set = {1, 2, 3}
30. What will be the output of the following code?
x = “abc”
x[1] = “d”
A) abc
B) adc
C) Error
D) ab d
Answer: C) Error
31. What is Django?
A) A database
B) A front-end framework
C) A Python web framework
D) An operating system
Answer: C) A Python web framework
32. Which pattern does Django follow?
A) ABC
B) MTV
C) MVP
D) MVVM
Answer: B) MTV
33. What is models.py used for?
A) Writing views
B) Defining database models
C) Designing templates
D) Storing static files
Answer: B) Defining database models
34. What is a Django view?
A) A database model
B) A function or class that handles requests
C) A CSS file
D) A form
Answer: B) A function or class that handles requests
35. What is the role of a template?
A) Handle URLs
B) Manage user authentication
C) Display dynamic HTML
D) Store data
Answer: C) Display dynamic HTML
36. What does urls.py do?
A) Create templates
B) Manage databases
C) Route URLs to views
D) Store session data
Answer: C) Route URLs to views
37. What is stored in settings.py?
A) Views and URLs
B) Project configurations
C) User sessions
D) Admin credentials
Answer: B) Project configurations
38. How do you start a new Django project?
A) django.startproject
B) django-admin startproject
C) createproject
D) python startproject
Answer: B) django-admin startproject
39. Which command creates a new Django app?
A) startapp
B) newapp
C) django-create
D) make migrations
Answer: A) startapp
40. What does migrate do?
A) Deletes data
B) Applies database changes
C) Renders HTML
D) Runs the server
Answer: B) Applies database changes
41. What is the expected output of the following code?
- python
- CopyEdit
- data = {}
- data[‘2’] = [1, 2]
- data[‘1’] = [3, 4]
for i in data.keys():
print(data[i][1], end=”)
Choices:
- A) 24
- B) 42
- C) 12
- D) 11
Correct Answer: A) 24
42. What is the expected output of the following code?
python
CopyEdit
data1 = (1, 2)
data2 = (3, 4)
[print(sum(x)) for x in [data1 + data2]]
Choices:
- A) 7
- B) 10
- C) 5
- D) 8
Correct Answer: B) 10
43. After execution of the following snippet, the sum of all vals elements will be equal to:
python
CopyEdit
vals = [0, 1, 2]
vals.insert(0, 1)
del vals[1]
Choices:
- A) 3
- B) 4
- C) 5
- D) 6
Correct Answer: B) 4
44. What is the expected output of the following code?
python
CopyEdit
data = set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
print(len(data))
Choices:
- A) 4
- B) 5
- C) 6
- D) 7
Correct Answer: A) 4
45. What is the output of the following snippet?
python
CopyEdit
my_list = [x * x for x in range(5)]
def fun(lst):
del lst[lst[2]]
return lst
print(fun(my_list))
Choices:
- A) [0, 1, 4, 9]
- B) [0, 1, 4]
- C) [0, 1, 9]
- D) [0, 4, 9]
Correct Answer: A) [0, 1, 4, 9]
46. What is the expected output of the following code?
python
CopyEdit
a = [1, 2, 3, 4, 5]
print(a[3:0:-1])
Choices:
- A) [5, 4, 3]
- B) [4, 3, 2]
- C) [4, 3, 2, 1]
- D) [2, 3, 4]
Correct Answer: B) [4, 3, 2]
47. Take a look at the snippet and choose the correct statement:
python
CopyEdit
nums = [10]
vals = nums[:]
vals.append(1)
print(vals)
Choices:
- A) nums and vals are of the same length
- B) nums is longer than vals
- C) vals is longer than nums
- D) nums is empty
Correct Answer: A) nums and vals are of the same length
48. What is the expected output of the following code?
python
CopyEdit
data = {‘1’: ‘0’, ‘0’: ‘1’}
for d in data.vals():
print(d, end=’ ‘)
Choices:
- A) 01
- B) 10
- C) The code is erroneous.
- D) 00
Correct Answer: C) The code is erroneous.
Explanation: data.vals() is incorrect. The correct method is data.values().
49. What will be the output of this code?
python
CopyEdit
data = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
for i in range(0, 4):
print(data[i].pop(), end=”)
Choices:
- A) 46810
- B) 1234
- C) 1236
- D) 4768
Correct Answer: A) 46810
50. What is the expected output of the following code?
python
CopyEdit
data = [[0, 1, 2, 3] for i in range(3)]
print(data)
Choices:
- A) [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
- B) [[0, 1], [0, 1], [0, 1]]
- C) [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
- D) [[0, 1], [2, 3], [4, 5]]
Correct Answer: A) [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
51. What is the output of the following code?
python
CopyEdit
x = 1
x = x == x
print(x)
Choices:
- A) 1
- B) 0
- C) True
- D) False
Correct Answer: C) True
52. What is the expected output of the following code?
python
CopyEdit
a = 10
b = 20
c = a > b
print(not(c))
Choices:
- A) False
- B) True
- C) 1
- D) 0
Correct Answer: B) True
53. What is the expected output of the following code?
python
CopyEdit
x = 1 + 1 // 2 + 1 / 2 + 2
print(x)
Choices:
- A) 3
- B) 3.5
- C) 4
- D) 2.5
Correct Answer: B) 3.5
54. What is the expected output of the following code?
python
CopyEdit
str = ‘x’
str += “y”
print(str)
Choices:
- A) xy
- B) x
- C) y
- D) None
Correct Answer: A) xy
55. What is the expected output of the following code?
python
CopyEdit
print(9 // 2)
Choices:
- A) 4
- B) 5
- C) 3
- D) 2
Correct Answer: A) 4
56. What is Django and what are its key features?
Expected Answer: Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Key features include an ORM, built-in admin interface, scalability, security, and reusable components.
57. Explain the MVT architecture in Django.
Expected Answer: MVT stands for Model-View-Template.
-
- Model handles the data and database structure.
- View contains the business logic and interacts with the model.
- Template is the HTML part that is rendered to the user.
58. What is the purpose of manage.py in Django?
Expected Answer: It is a command-line utility that helps with administrative tasks like running the development server, migrating databases, creating apps, and running tests.
59. How does Django handle database migrations?
Expected Answer: Django uses makemigrations to create migration files and migrate to apply those changes to the database schema. These tools manage changes to models over time.
60. What are Django models?
Expected Answer: Models are Python classes that define the structure of your database tables. Each model maps to a single table in the database using Django’s ORM.