OA Exams
What is the output of print(5 // 2) in Python?
a) 2b) 2.5c) 3d) 5
Correct Answer: a) 2
Explanation: The // operator performs floor division, which returns the largest integer less than or equal to the result of the division.
How do you convert a string to a list of characters in Python?
a) str.split()b) list(str)c) str.toList()d) str.split(”)
Correct Answer: b) list(str)
Explanation: The list() function converts a string into a list of characters.
What is the purpose of the pass statement in Python?
a) Terminates a loopb) Skips an iteration in a loopc) Does nothing and acts as a placeholderd) Returns a value from a function
Correct Answer: c) Does nothing and acts as a placeholder
Explanation: The pass statement is used as a placeholder where code is syntactically required but not yet implemented.
Which function is used to create a dictionary from two lists in Python?
a) zip()b) dict()c) map()d) enumerate()
Correct Answer: b) dict()
Explanation: The dict() function can be used in conjunction with zip() to create a dictionary from two lists (keys and values).
How do you declare a multi-line string in Python?
a) ”’ or “””b) Single quotes ‘c) Double quotes “d) Triple single quotes only
Correct Answer: a) ''' or """
Explanation: Triple quotes, either ''' or """, are used to create multi-line strings in Python.
What does print(10 % 3) return?
a) 1b) 3c) 10d) 0
Correct Answer: a) 1
Explanation: The % operator returns the remainder of the division, so 10 % 3 equals 1.
Which of the following is a valid Python variable name?
a) 1varb) var_namec) var-named) var name
Correct Answer: b) var_name
Explanation: Variable names in Python cannot start with a number and must not contain spaces or hyphens.
How do you add an element to the beginning of a list?
a) list.append()b) list.insert(0, value)c) list.add(0, value)d) list[0] = value
Correct Answer: b) list.insert(0, value)
Explanation: The insert() method inserts an element at a specified position, and 0 refers to the first position in the list.
Which method is used to remove and return the last element from a list?
a) pop()b) remove()c) delete()d) clear()
Correct Answer: a) pop()
Explanation: The pop() method removes and returns the last element of a list.
What is the output of len([1, 2, [3, 4], 5])?
a) 3b) 4c) 5d) 6
Correct Answer: b) 4
Explanation: The len() function counts the top-level elements, and [3, 4] is considered one element.
How do you concatenate two tuples in Python?
a) tuple1.append(tuple2)b) tuple1 + tuple2c) tuple1.concat(tuple2)d) tuple1.extend(tuple2)
Correct Answer: b) tuple1 + tuple2
Explanation: The + operator is used to concatenate two tuples in Python.
What does list[::-1] do?
a) Reverses the listb) Removes elements from the listc) Slices every second element from the listd) Returns a shallow copy of the list
Correct Answer: a) Reverses the list
Explanation: The slice notation [::-1] is used to reverse a list.
How do you declare a set in Python?
a) {}b) set[]c) []d) set()
Correct Answer: d) set()
Explanation: An empty set is declared using set() because {} is used to declare an empty dictionary.
Which method removes all elements from a set?
a) pop()b) clear()c) discard()d) remove()
Correct Answer: b) clear()
Explanation: The clear() method removes all elements from a set, leaving it empty.
What is the output of print(3 == 3.0)?
a) Trueb) Falsec) Noned) Error
Correct Answer: a) True
Explanation: In Python, == compares values, and 3 is considered equal to 3.0 since their numeric values are the same.
How do you convert a list to a set in Python?
a) set(list)b) toSet()c) set.convert(list)d) list.to_set()
Correct Answer: a) set(list)
Explanation: The set() function converts a list into a set, removing any duplicates in the process.
Which of the following is immutable in Python?
a) Listb) Dictionaryc) Tupled) Set
Correct Answer: c) Tuple
Explanation: A tuple is immutable, meaning that once it is created, its elements cannot be changed.
What is the purpose of the lambda keyword in Python?
a) To define a loopb) To create anonymous functionsc) To import modulesd) To create classes
Correct Answer: b) To create anonymous functions
Explanation: The lambda keyword is used to create small anonymous functions in Python.
What does the isinstance() function check?
a) If two variables have the same valueb) If an object belongs to a specified class or typec) If an object is callabled) If an object is iterable
Correct Answer: b) If an object belongs to a specified class or type
Explanation: The isinstance() function checks if an object is an instance or subclass of a class or type.
What is the output of print(‘python’.capitalize())?
a) Pythonb) pythonc) PYTHONd) PytHon
Correct Answer: a) Python
Explanation: The capitalize() method converts the first character of a string to uppercase and the rest to lowercase.