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 of the following commands will create a list?
A.
List1 = list()
B.
List1 = []
C.
List1 = list([1, 2, 3])
D.
All of the mentioned
Correct Answer
D. All of the mentioned
Explanation All of the mentioned commands will create a list. The first command "list1 = list()" creates an empty list using the list() constructor. The second command "list1 = []" creates an empty list using square brackets. The third command "list1 = list([1, 2, 3])" creates a list with elements 1, 2, and 3 using the list() constructor. Therefore, all of these commands will create a list.
Rate this question:
2.
What is the output when we execute list(“hello”)?
A.
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
B.
[‘hello’]
C.
[‘llo’]
D.
[‘olleh’]
Correct Answer
A. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
Explanation Executing the command list("hello") will convert the string "hello" into a list of individual characters: ['h', 'e', 'l', 'l', 'o'].
Rate this question:
3.
Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
A.
5
B.
4
C.
None
D.
Error
Correct Answer
A. 5
Explanation The len() function in Python returns the length of a list, which is the number of elements in the list. In this case, the listExample contains 5 elements: 'h', 'e', 'l', 'l', and 'o'. Therefore, the length of listExample is 5.
Rate this question:
4.
Suppose list1 is [2445,133,12454,123], what is max(list1)?
A.
2445
B.
133
C.
12454
D.
123
Correct Answer
C. 12454
Explanation The correct answer is 12454 because it is the highest number in the given list, which consists of four numbers.
Rate this question:
5.
Suppose list1 is [1, 5, 9], what is sum(list1)?
A.
1
B.
9
C.
15
D.
Error
Correct Answer
C. 15
Explanation The correct answer is 15 because the sum of the numbers in list1 [1, 5, 9] is 1 + 5 + 9 = 15.
Rate this question:
6.
Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
A.
Error
B.
None
C.
25
D.
2
Correct Answer
C. 25
Explanation The correct answer is 25 because list1[-1] refers to the last element in the list, and in this case, the last element is 25.
Rate this question:
7.
Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
A.
[2, 33, 222, 14]
B.
Error
C.
25
D.
[25, 14, 222, 33, 2]
Correct Answer
A. [2, 33, 222, 14]
Explanation list1[:-1] is a slicing operation on list1. In Python, slicing allows us to extract a portion of a list. The syntax for slicing is list[start:end], where start is the index of the first element to include and end is the index of the first element to exclude. In this case, list1[:-1] means to slice the list from the beginning to the second last element, excluding the last element. So, the correct answer is [2, 33, 222, 14].
Rate this question:
8.
Suppose list1 is [1, 3, 2], What is list1 * 2?
A.
[2, 6, 4]
B.
[1, 3, 2, 1, 3]
C.
[1, 3, 2, 1, 3, 2]
D.
[1, 3, 2, 3, 2, 1]
Correct Answer
C. [1, 3, 2, 1, 3, 2]
Explanation The correct answer is [1, 3, 2, 1, 3, 2] because when you multiply a list by a number, it repeats the elements of the list that number of times. In this case, multiplying list1 by 2 repeats the elements of list1 twice, resulting in [1, 3, 2, 1, 3, 2].
Rate this question:
9.
To add a new element to a list we use which command?
A.
List1.add(5)
B.
List1.append(5)
C.
List1.addLast(5)
D.
List1.addEnd(5)
Correct Answer
B. List1.append(5)
Explanation The correct answer is list1.append(5). This is because the append() function is specifically designed to add elements to the end of a list. The add() function is not a valid command for adding elements to a list. Similarly, addLast() and addEnd() are not standard commands in Python for adding elements to a list. Therefore, list1.append(5) is the correct command to use in this case.
Rate this question:
10.
Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
A.
[3, 4, 5, 20, 5, 25, 1, 3]
B.
[1, 3, 3, 4, 5, 5, 20, 25]
C.
[25, 20, 5, 5, 4, 3, 3, 1]
D.
[3, 1, 25, 5, 20, 5, 4, 3]
Correct Answer
D. [3, 1, 25, 5, 20, 5, 4, 3]
Explanation The reverse() method in Python reverses the order of elements in a list. So, after applying list1.reverse(), the list1 will be reversed and the elements will be in the order [3, 1, 25, 5, 20, 5, 4, 3].
Rate this question:
11.
Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?
Explanation The correct answer is [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]. After the listExample.extend([34, 5]) operation, the elements [34, 5] are added to the end of the listExample. Therefore, the resulting list1 will be [3, 4, 5, 20, 5, 25, 1, 3, 34, 5].
Rate this question:
12.
Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
A.
[3, 4, 5, 20, 5, 25, 1, 3]
B.
[1, 3, 3, 4, 5, 5, 20, 25]
C.
[3, 5, 20, 5, 25, 1, 3]
D.
[1, 3, 4, 5, 20, 5, 25]
Correct Answer
C. [3, 5, 20, 5, 25, 1, 3]
Explanation After executing the code listExample.pop(1), the element at index 1 (which is 4) is removed from the list. The resulting list will be [3, 5, 20, 5, 25, 1, 3].
Rate this question:
13.
Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
A.
[3, 4, 5, 20, 5, 25, 1]
B.
[1, 3, 3, 4, 5, 5, 20, 25]
C.
[3, 5, 20, 5, 25, 1, 3]
D.
[1, 3, 4, 5, 20, 5, 25]
Correct Answer
A. [3, 4, 5, 20, 5, 25, 1]
14.
What will be the output of the following Python code?
>>>"Welcome to Python".split()
A.
[“Welcome”, “to”, “Python”]
B.
(“Welcome”, “to”, “Python”)
C.
{“Welcome”, “to”, “Python”}
D.
“Welcome”, “to”, “Python”
Correct Answer
A. [“Welcome”, “to”, “Python”]
Explanation The given Python code uses the split() function on the string "Welcome to Python". The split() function splits the string into a list of substrings based on the specified delimiter, which is a space in this case. Therefore, the output of the code will be a list containing the substrings "Welcome", "to", and "Python".
Rate this question:
15.
What will be the output of the following Python code?
>>>list("a#b#c#d".split('#'))
A.
[‘a’, ‘b’, ‘c’, ‘d’]
B.
[‘a b c d’]
C.
[‘a#b#c#d’]
D.
[‘abcd’]
Correct Answer
A. [‘a’, ‘b’, ‘c’, ‘d’]
Explanation The code is using the split() method to split the string "a#b#c#d" at each occurrence of the "#" character. This will result in a list of substrings ['a', 'b', 'c', 'd']. Therefore, the correct answer is [‘a’, ‘b’, ‘c’, ‘d’].
Rate this question:
16.
To which of the following the “in” operator can be used to check if an item is in it?
A.
Lists
B.
Dictionary
C.
Set
D.
All of the mentioned
Correct Answer
D. All of the mentioned
Explanation The "in" operator can be used to check if an item is in lists, dictionaries, and sets. In lists, it checks if the item is present in the list. In dictionaries, it checks if the item is a key in the dictionary. In sets, it checks if the item is an element in the set. Therefore, the correct answer is that the "in" operator can be used to check if an item is in all of the mentioned data structures.
Rate this question:
17.
What will be the output of the following Python code?
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print(len(list1 + list2))
A.
2
B.
4
C.
5
D.
8
Correct Answer
D. 8
Explanation The code first concatenates list1 and list2 using the + operator, resulting in a new list [1, 2, 3, 4, 5, 6, 7, 8]. The len() function is then used to determine the length of this new list, which is 8. Therefore, the output of the code will be 8.
Rate this question:
18.
Which of the following statements create a dictionary?
A.
D = {}
B.
D = {“john”:40, “peter”:45}
C.
D = {40:”john”, 45:”peter”}
D.
All of the mentioned
Correct Answer
D. All of the mentioned
Explanation All three statements create a dictionary. In the first statement, an empty dictionary is created with no key-value pairs. In the second statement, a dictionary is created with two key-value pairs, "john" with a value of 40 and "peter" with a value of 45. In the third statement, a dictionary is created with two key-value pairs, 40 with a value of "john" and 45 with a value of "peter". Therefore, all of the mentioned statements create a dictionary.
Rate this question:
19.
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
A.
“john”, 40, 45, and “peter”
B.
“john” and “peter”
C.
40 and 45
D.
D = (40:”john”, 45:”peter”)
Correct Answer
B. “john” and “peter”
Explanation The correct answer is "john" and "peter". This is because the code snippet defines a dictionary named 'd' with two key-value pairs: "john":40 and "peter":45. When we access the keys of the dictionary using the 'd.keys()' method, it returns an iterable containing the keys "john" and "peter".
Rate this question:
20.
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
"john" in d
A.
True
B.
False
C.
None
D.
Error
Correct Answer
A. True
Explanation The output of the given code snippet will be "true". This is because the code checks if the key "john" exists in the dictionary "d". Since "john" is one of the keys in the dictionary, the condition is satisfied and the output is "true".
Rate this question:
21.
Which of these about a dictionary is false?
A.
The values of a dictionary can be accessed using keys
B.
The keys of a dictionary can be accessed using values
C.
Dictionaries aren’t ordered
D.
Dictionaries are mutable
Correct Answer
B. The keys of a dictionary can be accessed using values
Explanation A dictionary in Python is a data structure that stores key-value pairs. The statement "The keys of a dictionary can be accessed using values" is false because in a dictionary, the values can be accessed using keys, but not the other way around. The keys are unique and act as identifiers for the corresponding values. Therefore, it is not possible to directly access keys using values in a dictionary.
Rate this question:
22.
Which of the following is not a declaration of the dictionary?
A.
{1: ‘A’, 2: ‘B’}
B.
Dict([[1,”A”],[2,”B”]])
C.
{1,”A”,2”B”}
D.
{ }
Correct Answer
C. {1,”A”,2”B”}
Explanation The correct answer is {1,”A”,2”B”} because it is missing the colon (:) between the keys and values in the dictionary. In Python, a dictionary declaration should have the format {key: value}.
Rate this question:
23.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
A.
1 A 2 B 3 C
B.
1 2 3
C.
A B C
D.
1:”A” 2:”B” 3:”C”
Correct Answer
A. 1 A 2 B 3 C
Explanation The code snippet defines a dictionary 'a' with key-value pairs. The for loop iterates over the items of the dictionary, where 'i' represents the key and 'j' represents the value. Inside the loop, it prints 'i' and 'j' with a space in between, using the 'end' parameter to prevent a new line after each print statement. Therefore, the output will be "1 A 2 B 3 C", as it prints the keys and values of the dictionary separated by a space.
Rate this question:
24.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
A.
1
B.
A
C.
4
D.
Invalid syntax for get method
Correct Answer
B. A
Explanation The code snippet creates a dictionary named "a" with key-value pairs 1:"A", 2:"B", and 3:"C". The "get" method is used to retrieve the value associated with the key 1 from the dictionary. Since the key 1 exists in the dictionary, the method returns the value "A". Therefore, the output of the code will be "A".
Rate this question:
25.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(5,4))
A.
Error, invalid syntax
B.
A
C.
5
D.
4
Correct Answer
D. 4
Explanation The code snippet creates a dictionary 'a' with keys 1, 2, and 3 mapped to values "A", "B", and "C" respectively. The print statement uses the get() method to retrieve the value corresponding to key 5 from the dictionary. Since the key 5 does not exist in the dictionary, the get() method returns the default value provided as the second argument, which is 4. Therefore, the output of the code will be 4.
Rate this question:
26.
Which of the statements about dictionary values if false?
A.
More than one key can have the same value
B.
The values of the dictionary can be accessed as dict[key]
C.
Values of a dictionary must be unique
D.
Values of a dictionary can be a mixture of letters and numbers
Correct Answer
C. Values of a dictionary must be unique
Explanation A dictionary in Python allows multiple keys to have the same value. This means that more than one key can have the same value, making the statement "Values of a dictionary must be unique" false.
Rate this question:
27.
What will be the output of the following Python code snippet?
>>> a={1:"A",2:"B",3:"C"}
>>> del a
A.
Method del doesn’t exist for the dictionary
B.
Del deletes the values in the dictionary
C.
Del deletes the entire dictionary
D.
Del deletes the keys in the dictionary
Correct Answer
C. Del deletes the entire dictionary
Explanation The code snippet is creating a dictionary named 'a' with three key-value pairs. The 'del' keyword is then used to delete the dictionary 'a'. Therefore, the output of the code will be that the entire dictionary 'a' will be deleted.
Rate this question:
28.
If a is a dictionary with some key-value pairs, what does a.popitem() do?
A.
Removes an arbitrary element
B.
Removes all the key-value pairs
C.
Removes the key-value pair for the key given as an argument
D.
Invalid method for dictionary
Correct Answer
A. Removes an arbitrary element
Explanation The popitem() method in a dictionary removes an arbitrary key-value pair from the dictionary. It does not remove all the key-value pairs or a specific key-value pair for a given key. This method is a valid method for dictionaries.
Rate this question:
Quiz Review Timeline +
Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.