Python Basics guide featuring 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 of the following 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