Python MCQ questions. Just select the answer you think is correct and write why!
---
1. Which of the following is used to define a function in Python?
- A) func
- B) def
- C) function
- D) lambda
Answer – b) def, def is used in python the define a function as “def function-name:”.
2. What will be the output of this code: `print(type([]))`?
- A) `<class 'list'>`
- B) `<class 'tuple'>`
- C) `<class 'dict'>`
- D) `<class 'set'>`
Answer – A) `<class 'list'>`, as this brackets ‘[ ]’ are used to define list
3. How do you insert comments in Python code?
- A) `//`
- B) `#`
- C) `/*`
- D) `**`
Answer – B) `#`- hash is used to insert comments in python, but this only comment one line at a time, “”” “”” this is used for multi line comment
4. What is the output of `3**2` in Python?
- A) 6
- B) 9
- C) 8
- D) 3.2
Answer – B) 9 – as 3**2 indicates 2 to the power of 3
5. Which data type is mutable in Python?
- A) Tuple
- B) List
- C) String
- D) Integer
Answer – B) List – all other are immutable, only list can be modified after its creation
6. What will be the output of the following code: `print("Hello " + "World!")`?
- A) HelloWorld!
- B) Hello World!
- C) Error
- D) "Hello " + "World!"
Answer – A) HelloWorld! – the ‘+’ sign will combine two words by removing the space that python gives by default if we use comma(.).
7. Which of the following is used to take input from the user in Python?
- A) input()
- B) get()
- C) read()
- D) fetch()
Answer – A) input()
8. What is the output of `print(bool(0))`?
- A) True
- B) False
- C) 1
- D) 0
Answer – B) False – false because the value is 0
9. Which keyword is used to create a class in Python?
- A) class
- B) def
- C) type
- D) object
Answer – A) class
10. What does `len()` function do in Python?
- A) Returns the size of the object
- B) Returns the number of elements in a list or string
- C) Counts the number of spaces
- D) Checks the object type
Answer – B) Returns the number of elements in a list or string
11. What will `range(5)` produce?
- A) A list of 5 numbers
- B) Numbers 0 to 5
- C) Numbers 1 to 5
- D) A sequence of numbers from 0 to 4
Answer – D) A sequence of numbers from 0 to 4- it will print 5 values starting from 0,1,2,3,4
12. How do you access the first element of a list named `my_list`?
- A) `my_list[1]`
- B) `my_list(0)`
- C) `my_list[0]`
- D) `first(my_list)`
Answer – C) `my_list[0]`- as the indexing starts from 0, to fetch first element we have to call for the 0 th index
13. Which operator is used for floor division in Python?
- A) /
- B) //
- C) %
- D) **
Answer – B) // - // is used to fetch floor value in python
14. What is the correct way to define a dictionary in Python?
- A) `{ "key": "value" }`
- B) `[ "key": "value" ]`
- C) `( "key": "value" )`
- D) `< "key": "value" >`
Answer – A) `{ "key": "value" }`- curly brackets are you to define dictionary
15. What will be the output of this code: `print(2 == 2.0)`?
- A) True
- B) False
- C) Error
- D) None
Answer – A) True – as we are comparing the 2 values which are equal to each other, it will return as true and will get printed in the terminal
16. Which method is used to add an item to the end of a list in Python?
- A) add()
- B) append()
- C) insert()
- D) push()
Answer – B) append() – append() is used to add an item to the end of a list in Python
17. In Python, which keyword is used to handle exceptions?
- A) error
- B) exception
- C) try
- D) catch
Answer – C) try – try and except is used for exception handling in pyhton
18. What is the output of this code: `print("Hello"[0])`?
- A) H
- B) e
- C) Hello
- D) Error
Answer – A) H – as we have defined the index value as 0th element which is H
19. Which of the following is a correct syntax to create a set in Python?
- A) `{1, 2, 3}`
- B) `[1, 2, 3]`
- C) `(1, 2, 3)`
- D) `<1, 2, 3>`
Answer – A) `{1, 2, 3}`- curly brackets are used to define set
20. What does the `pop()` method do in a list?
- A) Adds an item
- B) Removes and returns the last item
- C) Removes an item without returning it
- D) Sorts the list
Answer – B) Removes and returns the last item – pop() method removes the item and also stores it, which can be called is need be