-
web.groovymark@gmail.com
- November 29, 2024
Question 21
What is the result of list(“hello”) in Python?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [hello]
d) [‘h e l l o’]
Correct Answer: a) ['h', 'e', 'l', 'l', 'o']
Explanation: The list() function converts a string into a list of individual characters.
Question 22
How do you retrieve all the keys from a dictionary in Python?
a) dict.keys()
b) dict.values()
c) dict.items()
d) dict.get()
Correct Answer: a) dict.keys()
Explanation: The keys() method returns a view object containing all the keys of a dictionary.
Question 23
Which operator is used to get the remainder of a division in Python?
a) /
b) //
c) %
d) **
Correct Answer: c) %
Explanation: The % operator returns the remainder of a division
Question 24
What will be the output of print(3 ** 2)?
a) 6
b) 9
c) 8
d) Error
Correct Answer: b) 9
Explanation: The ** operator is used for exponentiation, and 3 ** 2 equals 9.
Question 25
How do you convert a list of strings into a single string separated by commas?
a) ”.join(list)
b) ‘,’.join(list)
c) list.join(‘,’)
d) string(list)
Correct Answer: b) ','.join(list)
Explanation: The join() method is used to concatenate list elements with a specified separator.
Question 26
How do you find the minimum value in a list in Python?
a) min()
b) max()
c) sum()
d) reduce()
Correct Answer: a) min()
Explanation: The min() function returns the smallest element in a list.
Question 27
What is the result of len(“Hello World”)?
a) 10
b) 11
c) 12
d) Error
Correct Answer: b) 11
Explanation: The len() function counts all characters in the string, including spaces, making it 11 characters long.
Question 28
What is the default value of a global variable in Python?
a) 0
b) None
c) “”
d) False
Correct Answer: b) None
Explanation: Global variables are initialized to None by default if not assigned a value.
Question 29
Which of the following is not a valid list method in Python?
a) append()
b) extend()
c) insert()
d) add()
Correct Answer: d) add()
Explanation: The add() method is used for sets, not lists.
Question 30
What is the output of print(type([])) in Python?
a) set
b) tuple
c) list
d) dict
Correct Answer: c) list
Explanation: An empty pair of square brackets [] creates an empty list in Python.
Question 31
How do you remove the last element from a list in Python?
a) list.pop()
b) list.remove()
c) list.clear()
d) list.delete()
Correct Answer: a) list.pop()
Explanation: The pop() method removes and returns the last element of a list.
Question 32
What will print(range(5)) output in Python 3?
a) [0, 1, 2, 3, 4]
b) range(0, 5)
c) (0, 1, 2, 3, 4)
d) Error
Correct Answer: b) range(0, 5)
Explanation: In Python 3, range() returns a range object, not a list.
Question 33
How do you raise an exception in Python?
a) raise Exception(“Error”)
b) throw Exception(“Error”)
c) except Exception(“Error”)
d) assert Exception(“Error”)
Correct Answer: a) raise Exception("Error")
Explanation: The raise statement is used to trigger an exception.
Question 34
What is the output of print(“Hello”.replace(“l”, “L”))?
a) Hello
b) HeLLo
c) HeLL
d) HELLO
Correct Answer: b) HeLLo
Explanation: The replace() method replaces all occurrences of the substring "l" with "L".
Question 35
How do you declare a global variable inside a function in Python?
a) global x
b) x = global
c) var x
d) global(x)
Correct Answer: a) global x
Explanation: The global keyword is used to declare a global variable inside a function.
Question 36
What is the result of print(len([]))?
a) 0
b) None
c) Error
d) 1
Correct Answer: a) 0
Explanation: An empty list has a length of 0.
Question 37
Which keyword is used to handle exceptions in Python?
a) except
b) try
c) catch
d) finally
Correct Answer: b) try
Explanation: The try block lets you test a block of code for exceptions.
Question 38
What is the output of print(“A” in “Apple”)?
a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation: The in operator checks if a substring exists within another string, and A is in "Apple".
Question 39
What is the result of int(“42.5”)?
a) 42
b) 42.5
c) Error
d) 43
Correct Answer: c) Error
Explanation: You cannot directly convert a string with a decimal point to an integer.
Question 40
How do you concatenate two tuples in Python?
a) +
b) append()
c) extend()
d) insert()
Correct Answer: a) +
Explanation: The + operator concatenates two tuples.