Explanation: The len() function returns the number of characters in the string, which is 6 in this case.
Question 02
Which method is used to add an element to the end of a list?
a) insert() b) append() c) extend() d) pop()
Correct Answer: b) append()
Explanation: The append() method adds an element to the end of a list.
Question 03
What is the result of 5 // 2 in Python?
a) 2.5 b) 3 c) 2 d) 1
Correct Answer: c) 2
Explanation: The // operator performs floor division and returns the largest integer less than or equal to the result.
Question 04
Which operator is used to compare if two values are equal in Python?
a) = b) == c) != d) <>
Correct Answer: b) ==
Explanation: The == operator compares two values for equality in Python.
Question 05
What does the str() function do in Python?
a) Converts a string to an integer b) Converts an integer to a string c) Converts a list to a string d) Converts a float to an integer
Correct Answer: b) Converts an integer to a string
Explanation: The str() function converts an integer or any other data type to a string.
Question 06
How do you start a comment in Python?
a) # b) /* c) <!– d) //
Correct Answer: a) #
Explanation: Comments in Python start with the # symbol.
Question 07
What is the output of print(“hello”.capitalize())?
a) Hello b) HELLO c) hello d) hELLO
Correct Answer: a) Hello
Explanation: The capitalize() method returns a copy of the string with the first character capitalized and the rest lowercased.
Question 08
How do you remove the last element from a list in Python?
a) remove() b) delete() c) pop() d) clear()
Correct Answer: c) pop()
Explanation: The pop() method removes and returns the last element of a list.
Question 09
What is the correct way to create a dictionary in Python?
a) my_dict = [1, 2, 3] b) my_dict = (1, 2, 3) c) my_dict = {‘a’: 1, ‘b’: 2} d) my_dict = 1: ‘a’, 2: ‘b’
Correct Answer: c) my_dict = {'a': 1, 'b': 2}
Explanation: A dictionary is created using curly braces {} with key-value pairs.
Question 10
What is the result of 10 % 4?
a) 0 b) 1 c) 2 d) 3
Correct Answer: c) 2
Explanation: The % operator returns the remainder of the division, so 10 % 4 equals 2.
Question 11
What does the input() function do in Python?
a) Outputs data to the console b) Receives user input from the keyboard c) Converts a string to an integer d) Ends a program
Correct Answer: b) Receives user input from the keyboard
Explanation: The input() function is used to take input from the user as a string.
Question 12
Which method can be used to convert a list into a set in Python?
a) list() b) dict() c) set() d) tuple()
Correct Answer: c) set()
Explanation: The set() function converts a list into a set, which is an unordered collection of unique elements.
Question 13
What is the output of print(‘Python’.lower())?
a) PYTHON b) python c) Python d) pYTHON
Correct Answer: b) python
Explanation: The lower() method converts all characters in a string to lowercase.
Question 14
How do you define a tuple in Python?
a) my_tuple = [1, 2, 3] b) my_tuple = {1, 2, 3} c) my_tuple = (1, 2, 3) d) my_tuple = <1, 2, 3>
Correct Answer: c) my_tuple = (1, 2, 3)
Explanation: A tuple is created using parentheses, like (1, 2, 3).
Question 15
What does the isalpha() method do?
a) Checks if all characters in a string are alphabetic b) Checks if all characters in a string are numeric c) Checks if a string contains special characters d) Checks if a string is empty
Correct Answer: a) Checks if all characters in a string are alphabetic
Explanation: The isalpha() method returns True if all characters in the string are alphabetic and False otherwise.
Question 16
How do you create an empty set in Python?
a) set = () b) set = {} c) set = [] d) set = set()
Correct Answer: d) set = set()
Explanation: An empty set is created using the set() function. Using {} creates an empty dictionary, not a set.
Question 17
What does the split() method do?
a) Joins elements of a list into a string b) Splits a string into a list based on a delimiter c) Removes elements from a list d) Sorts a list alphabetically
Correct Answer: b) Splits a string into a list based on a delimiter
Explanation: The split() method divides a string into a list, breaking at the specified delimiter.
Question 18
What will be the result of 3 + 2 * 4?
a) 20 b) 14 c) 11 d) 10
Correct Answer: c) 11
Explanation: In Python, multiplication has a higher precedence than addition, so 2 * 4 is evaluated first, followed by 3 + 8.
Question 19
What is a list comprehension in Python?
a) A method for defining functions b) A way to create lists based on existing lists c) A way to iterate through a list d) A way to convert a string to a list
Correct Answer: b) A way to create lists based on existing lists
Explanation: List comprehensions provide a concise way to generate lists from existing lists or other iterables.
Question 20
How do you create a single-line comment in Python?
a) /* This is a comment */ b) <!– This is a comment –> c) // This is a comment d) # This is a comment
Correct Answer: d) # This is a comment
Explanation: Single-line comments in Python start with the # symbol.