OA Exams
What method would you use to add an element to the end of a list in Python?
a) append()b) insert()c) remove()d) pop()
Correct Answer: a) append()
Explanation: The append() method adds an element to the end of a list.
What is the correct syntax to import the math module in Python?
a) import Mathb) import math()c) from math import *d) import math
Correct Answer: d) import math
Explanation: The correct syntax to import the math module is import math.
What does the abs() function return?
a) The average of a list of numbersb) The largest number in a listc) The absolute value of a numberd) The sum of two numbers
Correct Answer: c) The absolute value of a number
Explanation: The abs() function returns the absolute (non-negative) value of a given number.
Which Python data type is used to store key-value pairs?
a) Listb) Tuplec) Setd) Dictionary
Correct Answer: d) Dictionary
Explanation: A dictionary is a mutable container in Python that stores key-value pairs.
How can you remove an element from a list in Python?
a) pop()b) insert()c) append()d) find()
Correct Answer: a) pop()
Explanation: The pop() method removes and returns the last element in a list.
Which of the following is a valid Python variable name?
a) 3valueb) _var_namec) ifd) var-name
Correct Answer: b) _var_name
Explanation: A valid Python variable name starts with a letter or underscore and contains letters, digits, or underscores.
What does x = [1, 2, 3] represent in Python?
a) A tupleb) A listc) A setd) A dictionary
Correct Answer: b) A list
Explanation: A list in Python is created using square brackets, such as [1, 2, 3].
Which operator is used for floor division in Python?
a) /b) //c) %d) **
Correct Answer: b) //
Explanation: The // operator performs floor division, which rounds the result down to the nearest whole number.
Which method is used to reverse a list in Python?
a) reverse()b) append()c) sort()d) remove()
Correct Answer: a) reverse()
Explanation: The reverse() method reverses the order of the elements in a list.
What does the random.randint() function do?
a) Generates a random float numberb) Returns a random integer between a specified rangec) Sorts a list randomlyd) Repeats a block of code
Correct Answer: b) Returns a random integer between a specified range
Explanation: The random.randint() function generates a random integer within a specified range.
Which function can be used to generate a random float number between 0 and 1 in Python?
a) randint()b) uniform()c) random()d) choice()
Correct Answer: c) random()
Explanation: The random() function generates a random float number between 0.0 and 1.0.
What is the default value returned by a Python function if no return statement is specified?
a) 0b) Falsec) Noned) True
Correct Answer: c) None
Explanation: If a Python function does not have a return statement, it returns None by default.
How do you create a tuple in Python?
a) Using curly braces {}b) Using parentheses ()c) Using square brackets []d) Using angle brackets <>
Correct Answer: b) Using parentheses ()
Explanation: A tuple is created by placing values inside parentheses, such as (1, 2, 3).
Which method is used to find the position of a character in a string?
a) find()b) count()c) index()d) position()
Correct Answer: a) find()
Explanation: The find() method returns the index of the first occurrence of a substring in a string.
What is the output of the following code: print(2 ** 3)?
a) 6b) 9c) 8d) 5
Correct Answer: c) 8
Explanation: The ** operator raises a number to a power, so 2 ** 3 equals 8.
Which function returns the type of an object in Python?
a) type()b) isinstance()c) id()d) str()
Correct Answer: a) type()
Explanation: The type() function returns the type of an object, such as int, float, or str.
Which method removes whitespace from the beginning and end of a string in Python?
a) strip()b) split()c) remove()d) trim()
Correct Answer: a) strip()
Explanation: The strip() method removes leading and trailing whitespace from a string.
What is the purpose of the continue statement in a loop?
a) To exit the loopb) To skip the current iteration and continue with the nextc) To restart the loopd) To stop the program
Correct Answer: b) To skip the current iteration and continue with the next
Explanation: The continue statement causes the loop to skip the current iteration and move on to the next one.
What type of data does the input() function return?
a) Integerb) Floatc) Stringd) List
Correct Answer: c) String
Explanation: The input() function always returns user input as a string.
What is a comment in Python denoted by?
a) #b) @c) //d) /* */
Correct Answer: a) #
Explanation: A comment in Python is denoted by the # symbol.