The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Which method is correct to get 3 elements randomly from the list = [3, 5, 6, 8, 9, 11, 12, 15, 16, 20]
A.
Random.choice(list, 3)
B.
Random.sample(list, 3)
C.
Random.get(list, 3)
D.
Random.choice(list, 4)
Correct Answer B. Random.sample(list, 3)
Explanation The correct method to get 3 elements randomly from the list [3, 5, 6, 8, 9, 11, 12, 15, 16, 20] is random.sample(list, 3). This method uses the random module's sample function, which returns a specified number of unique elements chosen randomly from the given list. In this case, it will return 3 random elements from the list.
Rate this question:
2.
Which method is correct to get 3 elements randomly from the list = [3, 5, 6, 8, 9, 11, 12, 15, 16, 20]
A.
Random.choice(list, 3)
B.
Random.sample(list, 3)
C.
Random.get(list, 3)
D.
Random.choice(list, 4)
Correct Answer B. Random.sample(list, 3)
Explanation The correct method to get 3 elements randomly from the list is "random.sample(list, 3)". This method uses the random.sample() function from the random module in Python, which returns a specified number of unique elements chosen randomly from the given list. In this case, it will return 3 random elements from the list [3, 5, 6, 8, 9, 11, 12, 15, 16, 20].
Rate this question:
3.
Choose the correct function to get 3 elements from the list randomly in such a way that each element of the list has a different probability of being selected numberList = [1, 2, 3, 4, 5]
Correct Answer A. Random.choices(numberList, weights=(10, 5, 15, 20, 50), k=3)
Explanation The correct function to use in order to get 3 elements from the list randomly with different probabilities of being selected is random.choices(numberList, weights=(10, 5, 15, 20, 50), k=3). This function allows us to specify the weights for each element in the list, where the higher the weight, the higher the probability of that element being selected. The k parameter specifies the number of elements to be selected.
Rate this question:
4.
What gets printed (with python version 3.X) assuming the user enters the following at the prompt?
#: foo
a = input("#: ") print a
A.
F
B.
Foo
C.
Not a number
D.
An exception is thrown
Correct Answer B. Foo
Explanation The correct answer is "Foo" because the program prompts the user for input using the string "#: " and assigns the user's input to the variable "a". The program then prints the value of "a", which is "Foo" in this case.
Rate this question:
5.
What gets printed?
class NumFactory:
def __init__(self, n):
self.val = n
def timesTwo(self):
self.val *= 2
def plusTwo(self):
self.val += 2
f = NumFactory(2)
for m in dir(f):
mthd = getattr(f,m)
if callable(mthd):
mthd()
print f.val
A.
2
B.
4
C.
6
D.
8
E.
An exception is thrown
Correct Answer E. An exception is thrown
Explanation The code creates an instance of the NumFactory class and assigns it to the variable f. It then iterates over the attributes of the f object using the dir() function. For each attribute, it checks if it is callable (i.e., a method) using the callable() function. If it is callable, it calls the method. Finally, it prints the value of f.val. Since the timesTwo() and plusTwo() methods are called in the loop, the value of f.val is modified multiple times. Therefore, the correct answer is that an exception is thrown because the print statement is outside the loop and f.val is not defined.
Rate this question:
6.
What is the function used to open a text file in python?
A.
Op
B.
Open
C.
Opened
D.
With
Correct Answer B. Open
Explanation The correct answer is "open". In Python, the "open" function is used to open a text file. This function takes in the file name as a parameter and returns a file object that can be used to read or write data to the file.
Rate this question:
7.
In programming, there are different data types...(in Python, they are stored as objects)
Which one is an integer?
A.
.02
B.
"40"
C.
67
D.
12.2
E.
Str(4)
Correct Answer C. 67
Explanation The correct answer is 67 because it is a whole number without any decimal places, which is the definition of an integer data type. The other options are not integers because they either have decimal places (0.02 and 12.2), are strings of characters ("40"), or are a string representation of a number (Str(4)).
Rate this question:
8.
In programming, there are different data types...
(in Python, they are stored as objects)
Which one is a string?
A.
Int("642")
B.
3
C.
3.2
D.
"821"
E.
Apple
Correct Answer D. "821"
Explanation The correct answer is "821". In programming, a string is a data type that represents a sequence of characters. It is typically used to store and manipulate text. In this case, "821" is enclosed in double quotes, indicating that it is a string literal. The other options are not strings. "Int("642")" is a function call that converts the string "642" to an integer. 3 and 3.2 are numeric values, and Apple is not enclosed in quotes, making it a variable or identifier rather than a string.
Rate this question:
9.
Which function returns a random integer?
A.
Random.randint()
B.
Random.randint(a, b)
C.
Random.shuffle()
D.
Rand()
Correct Answer(s) A. Random.randint() B. Random.randint(a, b)
Explanation The functions Random.randint() and Random.randint(a, b) both return random integers. The first function returns a random integer from a given range, while the second function returns a random integer between two specified numbers. Therefore, both of these functions can be used to generate random integers in Python. On the other hand, Random.shuffle() is used to randomly shuffle the elements of a list, and rand() is not a valid function in Python.
Rate this question:
10.
Which function shuffles a sequence?
A.
Random.shuffle()
B.
Math.shuffle(x[, random])
C.
Random.shuffle(x,[, random])
D.
Math.shuffle()
E.
Math.shuffle(x)
Correct Answer(s) A. Random.shuffle() C. Random.shuffle(x,[, random])
Explanation The correct answers for this question are Random.shuffle() and Random.shuffle(x,[, random]). The Random.shuffle() function shuffles a sequence randomly, while the Random.shuffle(x,[, random]) function shuffles the elements of a sequence x using a specified random number generator. Both functions are used to change the order of elements in a sequence randomly.
Rate this question:
11.
How would you describe Python?
A.
Natural coding
B.
Machine language
C.
High-level language
D.
Low level language
Correct Answer C. High-level language
Explanation Python is described as a high-level language because it is designed to be easily readable and understandable by humans. It provides a simplified syntax and a wide range of built-in functions and libraries, making it accessible for beginners and experienced programmers alike. Unlike low-level languages, Python abstracts away many complex details of computer hardware and memory management, allowing developers to focus on solving problems rather than worrying about the nitty-gritty of the underlying system. Python's high-level nature also enables faster development and easier maintenance of code, making it a popular choice for various applications.
Rate this question:
12.
Which built-in function converts a number or string to an integer?
A.
Int()
B.
Int([number | string[, base]])
C.
Str()
D.
Str([object[, encoding[, errors]]])
Correct Answer(s) A. Int() B. Int([number | string[, base]])
Explanation The correct answer is Int() and Int([number | string[, base]]). The Int() function is a built-in function that converts a number or string to an integer. It can take an optional base parameter to specify the base of the number being converted.
Rate this question:
13.
Which built-in function returns a string version of an object?
A.
Str()
B.
Str([object[, encoding[, errors]]])
C.
Int()
D.
Int([number | string[, base]])
Correct Answer(s) A. Str() B. Str([object[, encoding[, errors]]])
Explanation The correct answer is Str() and Str([object[, encoding[, errors]]). The Str() function is a built-in function in Python that returns a string version of an object. It can be used to convert other data types, such as integers or floats, into strings. The optional parameters in the parentheses allow for specifying the encoding and handling of errors during the conversion process.
Rate this question:
14.
Which are the valid identifiers in the following?
A.
2um
B.
Pie
C.
My-File
Correct Answer B. Pie
Explanation The valid identifier in the given options is "Pie". In programming, an identifier is a name used to identify a variable, function, or any other user-defined item. It must follow certain rules, such as starting with a letter or underscore, and can contain letters, numbers, and underscores. "Pie" follows these rules and is a valid identifier. However, "2um" starts with a number, which is not allowed, and "My-File" contains a hyphen, which is also not allowed in identifiers.
Rate this question:
15.
Are Float and Double valid data types in python?
A.
True
B.
False
Correct Answer B. False
Explanation Float and Double are not valid data types in Python. Python has a built-in data type called "float" which represents decimal numbers. However, there is no specific data type called "Double" in Python. The "float" data type is used to represent both single and double-precision floating-point numbers in Python. Therefore, the correct answer is False.
Rate this question:
16.
Which of the following is not a valid data type in python?
A.
Double
B.
Int
C.
Float
D.
Str
Correct Answer A. Double
Explanation The data type "Double" is not a valid data type in Python. Python does not have a specific data type called "Double". Instead, Python uses the "float" data type to represent decimal numbers.
Rate this question:
17.
Complete the code to print the following numbers:
1
3
5
7
9
for i in range(1, 10, ________):
print i
Correct Answer 2
Explanation The code should be completed with a step value of 2 in the range function. This will make the loop iterate from 1 to 9, incrementing by 2 each time. As a result, the numbers 1, 3, 5, 7, and 9 will be printed.
Rate this question:
18.
If we want to write the output to console rather than printing in a file, Which option should we use?
A.
Echo
B.
Output
C.
Print
D.
Console.log
Correct Answer C. Print
Explanation If we want to write the output to the console rather than printing it in a file, we should use the "print" option. This option allows us to display the output directly on the console screen.
Rate this question:
19.
what does the following code do?
def a(b, c, d): pass
A.
Defines a list and initializes it
B.
Defines a function, which does nothing
C.
Defines a function, which passes its parameters through
D.
Defines an empty class
Correct Answer B. Defines a function, which does nothing
Explanation The given code defines a function named "a" which takes three parameters (b, c, and d) but does not contain any code or instructions. Therefore, the function does nothing when called.
Rate this question:
20.
All keywords in Python are in
A.
Lower case
B.
UPPER CASE
C.
Capitalized
D.
None of these
Correct Answer D. None of these
Explanation In Python, keywords are predefined reserved words that cannot be used as variable names. The correct answer is "None of these" because keywords in Python are case-sensitive. They are written in lowercase, such as "if," "else," "for," and "while." Using keywords in uppercase or capitalized forms would result in a syntax error.
Rate this question:
21.
Which of these is one of the characteristics of Python?
A.
Reachability
B.
Simplicity
C.
Initiative interface
D.
Permissions system
Correct Answer A. Reachability
Explanation One of the characteristics of Python is reachability. This means that Python allows easy access to a wide range of libraries and resources, making it a versatile and flexible programming language. With its extensive standard library and support for third-party packages, Python enables developers to easily connect and interact with various tools and resources, enhancing the reach and capabilities of their applications. Additionally, Python's reachability extends to its community, which is known for its active and supportive nature, providing ample resources and assistance to developers.
Rate this question:
22.
Python has how many standard data types?
A.
3
B.
4
C.
5
D.
6
Correct Answer C. 5
Explanation Python has five standard data types, which are integer, float, string, list, and tuple. These data types are built-in and can be used to store and manipulate different kinds of data in Python programming.
Rate this question:
23.
What is the output of print str[0] if str = ‘Hello World!’?
A.
Hello
B.
Hello World
C.
H
D.
W
Correct Answer C. H
Explanation The given code snippet is attempting to print the first character of the string variable "str". Since the string "str" is assigned the value "Hello World!", the first character of the string is "H". Therefore, the output of print str[0] will be "H".
Rate this question:
24.
Is Python case sensitive when dealing with identifiers?
A.
Yes
B.
No
C.
Machine dependent
D.
None of the mentioned
Correct Answer A. Yes
Explanation Python is case sensitive when dealing with identifiers. This means that Python treats uppercase and lowercase letters as distinct and separate characters. For example, the variable "myVar" is different from "myvar" in Python. Therefore, when working with identifiers in Python, it is important to use the correct casing to avoid any errors or confusion.
Rate this question:
25.
Which of the following is invalid?
A.
_a = 1
B.
__a = 1
C.
__str__ = 1
D.
None of them
E.
All of them
Correct Answer D. None of them
Explanation All of the given options are valid. "_a = 1" and "__a = 1" are both valid variable assignments. "__str__ = 1" is also valid, as it assigns a value of 1 to the "__str__" variable. Therefore, the correct answer is "None of them".
Rate this question:
26.
What will the following Python statements output?
print("Answer = ", 2*5)
A.
Answer = 10
B.
Answer = 10
C.
Answer=10
D.
10
Correct Answer B. Answer = 10
Explanation The given Python statements will output "Answer = 10". This is because the print statement is used to display the text "Answer = " followed by the result of the expression 2*5, which is 10.
Rate this question:
27.
What will the following Python statements output?
print("Age:", "20+5")
A.
Age: 20+5
B.
Age:20+5
C.
Age:25
D.
Age:,25
E.
Age: 25
F.
Age:,20+5
Correct Answer A. Age: 20+5
Explanation The given Python statements will output "Age: 20+5". This is because the print() function is used to display the string "Age:" followed by the string "20+5". The comma between the two strings acts as a separator and does not perform any mathematical operation. Therefore, the output will be the exact combination of the two strings without any evaluation.
Rate this question:
28.
Name the most popular open-source Python library used for doing data analysis.
A.
Numpy
B.
Pandas
C.
Matplotlib
Correct Answer B. Pandas
Explanation Pandas is the most popular open-source Python library used for data analysis. It provides easy-to-use data structures and data analysis tools, making it efficient for manipulating and analyzing data. Pandas offers powerful features such as data alignment, handling missing data, and merging, reshaping, and slicing datasets. It is widely used in various fields, including finance, economics, social sciences, and data science. Numpy and Matplotlib are also commonly used libraries in Python, but they are primarily focused on numerical computing and data visualization, respectively.
Rate this question:
29.
Which of the following statements are true?
A.
When you open a file for reading, if the file does not exist, an error occurs
B.
When you open a file for writing, if the file does not exist, a new file is created
C.
When you open a file for writing, if the file exists, the existing file is overwritten with the new file
Correct Answer(s) A. When you open a file for reading, if the file does not exist, an error occurs B. When you open a file for writing, if the file does not exist, a new file is created C. When you open a file for writing, if the file exists, the existing file is overwritten with the new file
Explanation When opening a file for reading, if the file does not exist, an error occurs because the program cannot read data from a file that does not exist. When opening a file for writing, if the file does not exist, a new file is created because the program needs a file to write data into. If the file already exists when opening it for writing, the existing file is overwritten with the new file, meaning that the previous content of the file is replaced with the new data being written.
Rate this question:
30.
To read two characters from a file object infile, we use ____________
A.
Infile.read(2)
B.
Infile.read(3)
C.
Infile.read()
D.
Infile.readline(2)
E.
Infile.readline(3)
F.
Infile.readlines()
Correct Answer A. Infile.read(2)
Explanation The correct answer is "Infile.read(2)". This is because the read() method is used to read a specified number of characters from a file. In this case, the number 2 indicates that we want to read two characters from the file object "infile".
Rate this question:
31.
Python writes data on screen using which function?
A.
Print()
B.
Display()
C.
Echo
D.
Cout<<
E.
Console.log()
F.
System.out.println()
Correct Answer A. Print()
Explanation The correct answer is "print()". In Python, the print() function is used to write data on the screen. It allows us to display the output of our program or any other information we want to show to the user.
Rate this question:
32.
Python can recognize this data:
"$hello"
A.
True
B.
False
Correct Answer A. True
Explanation Python can recognize the data "$hello" because it is a valid string in Python. In Python, strings can be enclosed in either single quotes ('') or double quotes (""). The presence of the dollar sign ($) and the combination of letters and numbers after it does not affect the recognition of the string by Python. Therefore, the statement is true.
Rate this question:
33.
What is the output of the following?
elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(elements, incr)))
A.
[1, 2, 3]
B.
[0, 1, 2]
C.
Error
D.
None of the above
Correct Answer C. Error
Explanation The code is trying to use the map function to apply the incr function to each element in the elements list. However, the map function expects the first argument to be a function and the second argument to be an iterable. In this case, the first argument is the elements list, which is not a function, causing an error.
Rate this question:
34.
Carefully observe the code and give the answer.
def example(a):
a = a + '2'
a = a*2
return a
>>>example("hello")
A.
Indentation Error
B.
Cannot perform mathematical operation on strings
C.
Hello2
D.
Hello2hello2
Correct Answer A. Indentation Error
Explanation The given code will result in an Indentation Error. This error occurs when the indentation of the code is not consistent. In this case, the lines of code inside the function are not indented properly. The correct indentation should be consistent, typically using four spaces or a tab for each level of indentation.
Rate this question:
35.
Which of the following is a bad Python variable name?
A.
SPAM23
B.
23spam
C.
Spam
D.
_spam
Correct Answer B. 23spam
Explanation The variable name "23spam" is a bad Python variable name because it starts with a number, which is not allowed in Python. Variable names in Python cannot start with a number, they must start with a letter or an underscore.
Rate this question:
36.
What will be the value of x after the following statement executes: x = 1 + 2 * 3 - 8 / 4
Correct Answer 5.0
Explanation The given statement uses the order of operations, also known as PEMDAS. According to this rule, multiplication and division should be performed before addition and subtraction. So, first, 2 * 3 is calculated, which equals 6. Then, 8 / 4 is calculated, which equals 2. Finally, 1 is added to 6 and the result is subtracted by 2. Therefore, the final value of x is 5.0.
Rate this question:
37.
What is missing at the end of the following line?
if x<=5
Correct Answer :
Explanation The missing item at the end of the line "if x
Rate this question:
38.
Which is correct if the input I want is specifically a number?
A.
Input("Please enter your age")
B.
Print "Your age is 25"
C.
Raw_input("Please enter your age")
D.
Year=input("Please enter your age")
Correct Answer D. Year=input("Please enter your age")
Explanation The correct answer is "Year=input("Please enter your age")". This is because the input() function in Python is used to take user input, and in this case, the user is expected to enter their age. The input() function returns a string, so if we want to specifically get a number as input, we need to use the int() function to convert the input to an integer. In the given answer, the input is stored in the variable "Year", indicating that it is expected to be a number.
Rate this question:
39.
>>> l = range(3) >>> l[3] = 3
A.
It creates a tuple of integers named "l"
B.
It creates an iterator
C.
It raises a TypeError exception
D.
It raises an IndexError exception
Correct Answer D. It raises an IndexError exception
Explanation The given code creates a list named "l" with three elements using the range() function. However, when trying to access the fourth element of the list using indexing (l[3]), it raises an IndexError exception because the list only has elements at indices 0, 1, and 2. The index 3 is out of range for the list, hence the exception is raised.
Rate this question:
40.
Explain the following code: >>> l = (1, 2, 3) >>> l[2] = 3
A.
It creates a list of integers named "l"
B.
It creates an iterator
C.
It raises an IndexError exception
D.
It raises a TypeError exception
Correct Answer D. It raises a TypeError exception
Explanation The given code tries to assign a new value to the element at index 2 of the tuple "l". However, tuples are immutable, meaning their elements cannot be changed once they are assigned. Therefore, attempting to modify a tuple element raises a TypeError exception.
Rate this question:
41.
Write the output for the following lines of code
var1=5 print(float(var1))
Correct Answer 5.0
Explanation The code assigns the value 5 to the variable var1. The print statement then converts the value of var1 to a float and prints it. Since the value of var1 is 5, when it is converted to a float it becomes 5.0. Therefore, the output of the code is 5.0.
Rate this question:
42.
What is the output of the following code?
x = 'Hello'
y = 'there'
print x+y
A.
'Hello there'
B.
Hello there
C.
Hellothere
D.
Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'str' and 'str'
Correct Answer C. Hellothere
Explanation The code will output "Hellothere" because the variables x and y are both strings, and the "+" operator is used to concatenate strings in Python.
Rate this question:
43.
Which of the following assignment statements are syntactically valid?
w - my_name = "Brendan"
x - 76trombones = "big parade"
y - more$ = 10000
z - my_salary = 10, 000
A.
W
B.
X and y
C.
W and z
D.
X, y and z
E.
All of w, x, y and z
Correct Answer C. W and z
Explanation The assignment statement "w - my_name = "Brendan"" is syntactically valid because it follows the correct syntax of assigning a value to a variable. On the other hand, the assignment statement "z - my_salary = 10,000" is not syntactically valid because the comma in the value "10,000" is not allowed in variable assignment.
Rate this question:
44.
Which two lines of code are valid strings in Python?
A.
This is a string
B.
‘This is a string’
C.
(This is a string)
D.
“This is a string”
Correct Answer(s) B. ‘This is a string’ D. “This is a string”
Explanation The two lines of code that are valid strings in Python are '‘This is a string’' and '“This is a string”'.
Rate this question:
45.
A string can be surrounded by three sets of single quotation marks or by three sets of double quotation marks.
A.
True
B.
False
Correct Answer A. True
Explanation The statement is true because a string can indeed be surrounded by three sets of single quotation marks or three sets of double quotation marks. This allows for flexibility in representing strings in programming languages or when writing text. The use of triple quotation marks is often seen in languages like Python, where they can be used to represent multi-line strings.
Rate this question:
46.
What is the output of the following?
elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(elements, incr)))
A.
[1, 2, 3]
B.
[0, 1, 2]
C.
Error
D.
None of the mentioned
Correct Answer C. Error
Explanation The code is trying to use the map function to apply the incr function to each element in the elements list. However, the map function expects the first argument to be a function and the second argument to be an iterable. In this case, the arguments are reversed, causing an error. Therefore, the output is an error.
Rate this question:
47.
What does os.name contain?
A.
The name of the operating system dependent module imported
B.
The address of the module os
C.
Error, it should’ve been os.name()
D.
Os kernel name
E.
None of the above
Correct Answer A. The name of the operating system dependent module imported
Explanation The os.name variable contains the name of the operating system dependent module that has been imported. This variable is used to determine the name of the operating system on which the Python script is running. It provides a way to write platform-independent code by checking the value of os.name and performing different operations based on the operating system.
Rate this question:
48.
What will be the output of the following Python code?
def f1():
x=15
print(x)
x=12
f1()
A.
Error
B.
12
C.
15
D.
1512
Correct Answer C. 15
Explanation The output of the code will be 15. This is because the function f1() is called after the variable x is assigned the value 12. However, within the function f1(), a new variable x is created and assigned the value 15. When the function is called and executed, it prints the value of this new variable x, which is 15. The initial assignment of x=12 outside the function does not affect the value of x within the function.
Rate this question:
49.
What will be the output?
def f1():
x=100
print(x)
x=+1
f1()
A.
Error
B.
100
C.
101
D.
99
E.
100.0
F.
101.0
G.
99.0
Correct Answer B. 100
Explanation The output will be 100. The function f1() is called after assigning the value of 1 to the variable x. Inside the function, a new variable x is created and assigned the value of 100. When the print statement is executed, it prints the value of the local variable x which is 100.
Rate this question:
50.
Read the following Python code carefully and point out the global variables?
y, z = 1, 2
def f():
global x
x = y+z
A.
X
B.
Y and z
C.
X, y and z
D.
None
Correct Answer C. X, y and z
Explanation The global variables in the given Python code are x, y, and z. The code defines y and z as global variables and then the function f() declares x as a global variable.