0% found this document useful (0 votes)
15 views4 pages

Dsa Reviewer 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Dsa Reviewer 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Consider a Python program where an empty dictionary is being initialized.

Which of
the following codes correctly initializes an empty dictionary?
b.my_dict = dict()
For a list variable initialized as 'list = [6, 1, 15, 6, 13, 15, 4, 8, 12, 5]', how many
elements does it contain?
b.10
Given a list variable in Python initialized as 'list = [6, 1, 15, 6, 13, 15, 4, 8, 12, 5]',
what is its initial content?
b.[6, 1, 15, 6, 13, 15, 4, 8, 12, 5]
Given a Python dictionary where keys are integer numbers and values are their
corresponding names in English (e.g., 1: "One", 2: "Two", etc.), what would be the
value associated with the key '3'?
c."Three"
Given a Python dictionary where keys are numbers from 1 to 4, and values are the
corresponding names in English. What is the type of the keys in this dictionary?
c.Integers

Given the Python code snippet 'list = [0] * size', what is its function?
c.To create a list of 'size' number of zeros

Given the Python code snippet 'print("Element:", list[index])', what would be


printed?
d.The element of the list at the position entered by the user
Given the Python code snippet 'print("Sum:", sum)' where sum is calculated as 'sum
= list[1] + list[4] + list[8]', what would be the output?
a."Sum: 26"

Given the Python code snippet 'sum = list[1] + list[4] + list[8]', what is it
computing?
a.The sum of the second, fifth, and ninth elements of the list

How are dictionary elements accessed in Python?


b.By using their key

How are list elements accessed in Python?


a.Using their indices

How can a function enhance the efficiency of a Python program?


a.By promoting code reusability.

How can we add a new key-value pair to a dictionary in Python?


c.my_dict[key] = value

How can we get the number of items (key-value pairs) in a dictionary in Python?
c.Using the len() function

How can we remove a key-value pair from a dictionary in Python?


d.Using the pop() method

How can you create a list in Python?


b.Using square brackets []

How can you iterate over the keys in a Python dictionary?


d.Both A and B
How do you access the value associated with the key "name" in the dictionary
person?
b.person["name"]

How do you add a new key-value pair to the dictionary?


a.person("city") = "New York"

How do you add key-value pairs to a dictionary in Python?


c.dict[key] = value

How do you change the value of 'U' in my_dict to 'Ulysses'?


c.my_dict["U"] = "Ulysses"

How do you check if a specific key exists in a dictionary?


d."name" in person
How do you create a copy of a dictionary?
a.copied_dict = dict(person)

How do you create a dictionary from a list of tuples?


c.person = dict([("name", "John"), ("age", 30), ("occupation", "Engineer")])

How do you create a dictionary of squares from 1 to 5 using dictionary


comprehension?
d.squares = {x: x*x for x in range(1, 6)}

How do you create an empty dictionary in Python?


empty_dict = {}
How do you get the number of key-value pairs in a dictionary?
len(person)
How do you initialize a list with values in Python?
listName = [value1, value2, ...]
How do you loop through keys and values in a dictionary?
for key, value in person.items(): print(key, value)
How do you modify the value of the "age" key in the person dictionary?
person["age"] = 31
How do you remove all elements from a dictionary?
person.clear()
How do you remove the "age" key and its value from the dictionary?
del person["age"]
How does function abstraction benefit code in Python?
It allows users to interact with a function without understanding its internal workings.
How does function reusability enhance software development efficiency?
By reducing the need for writing new functions.
How is a list of n elements, all initialized to 0, created in Python?
[0] * n
How is the value of a dictionary key changed in Python?
By reassigning the key a new value
How many elements does the list contain in the Python code 'list = [2.3, 7.72, 5.94,
7.85, 5.13, 5.29, 8.84, 4.1, 10.31, 2.96]'?
10
How would you access the element at the middle index of a list in Python?
list[middle_index]
How would you add a new key-value pair, 'N' and 'Navarro', to my_dict?
my_dict["N"] = "Navarro"
How would you check if 'J' is a key in my_dict?
"J" in my_dict
How would you create a nested dictionary in Python?
By adding a dictionary as a value to another dictionary.
How would you print a specific element of a list in Python?
print("Element:", list[i])
If a Python list has an even number of elements, what does the following code
return: [list[middle_index-1], list[middle_index]]?
The two elements in the middle of the list.
In a Python dictionary, what data types can be used for keys?
Any immutable type
In a Python for loop, what is the purpose of the range function?
a. It allows the code to execute a specific number of times.
In a Python program, consider the line of code: my_dict[key] = value. What is the
result of executing this line of code?
It adds a new key-value pair to the dictionary.
In a Python program, the function input() is used inside the int() function. What does
this achieve?
a.It converts user input into an integer.
In Python, how can you print all key-value pairs in a dictionary?
Using a for loop to iterate over the dictionary keys and print each key and its
corresponding value.
In Python, how do you access the last element of a list?
list[-1]
In Python, how do you access the second to last element of a list?
list[-2]
In Python, how do you create a list of n elements all initialized to 0?
[0] * n
In Python, how would you replace the i-th element of a list with a given value?
list[i] = value
In Python, if a dictionary has keys ranging from 1 to 4 with their corresponding
English names as values, how many key-value pairs does the dictionary contain?
4
In Python, what does the keys() method return when used on a dictionary?
It returns all the keys from the dictionary.
In Python, what does the len() function do when applied to a dictionary?
Returns the number of keys in the dictionary.
In Python, what does the syntax 'list[1]' refer to when applied to a list?
The second element of the list
In Python, what does the syntax 'list[i] = value' inside a loop do?
Replaces the ith element of the list with the value
In the 'add' function example, what does 'a' and 'b' represent?
The parameters of the function.
In the given Python code, what does the syntax 'list2[-1]' refer to?
The last element of list2
In the given Python code, what does the syntax 'list2[-2]' refer to?
The second element from the end of list2
In the Python code snippet 'for x in range(size):', what does 'range(size)' do?
Creates a list with elements from 0 to 'size' (not including 'size')
In the Python code snippet 'index = int(input("Enter index: "))', what is expected
from the user?
To enter an integer
In the Python code snippet 'print("Product: %.2f" % (list[0] * list[4]))', what does the
"%.2f" format specifier do?
It ensures the output is printed as a float with two digits after the decimal point
Suppose you have a Python dictionary with keys ranging from 1 to 4, with their
corresponding English names as values. If we execute the command 'dictionary[5]
= "Five"', how many key-value pairs will the dictionary contain after the execution?
5
What are Python lists?
A data structure that allows storing of elements of different types
What data types can a Python list contain?
Any data type
What does function composition refer to in Python?
Building more complex functionality by combining smaller functions.
What does it mean when we say a function in Python is 'invoked'?
The function is called to perform its task.
What does range(size) return in Python?
A sequence of numbers from 0 to size-1.
What does size % 2 == 0 check in Python?
Whether size is divisible by 2.
What does the expression 'list[0] * list[4]' in the Python code snippet 'print("Product: %.2f"
% (list[0] * list[4]))' represent?
The product of the first element and the fifth element in the list
What does the following line do: my_dict["key_1"] = new_value?
it modifies the value of the existing 'key_1' key
What does the items() method return?
The keys and values of the dictionary as a tuple in a list.
What does the modulo operator % do in Python?
It gives the remainder of a division.
What does the negative index '-2' in 'list[-2]' refer to in Python?
The second element from the end of the list
What does the print statement do in the 'add' function example?
It outputs data to the console.
What does the print() function do in Python?
It outputs data to the console
What does the Python code 'list[-1]' return?
The last element of the list
What does the Python code 'list1[0] + list2[-1]' compute?
The sum of the first element of list1 and the last element of list2
What does the Python code 'list1[1] * list2[-2]' compute?
The product of the second element of list1 and the second to the last element of list2
What does the Python code snippet 'list[x] = int(input())' inside the loop do?
It replaces the xth element of the list with the user input
What does the Python code snippet 'list1 = [0] * size1' do?
Creates a list1 of 'size1' number of zeros
What does the Python code snippet 'list1[1] * list2[-2]' represent?
The product of the second element of list1 and the second to the last element of list2
What does the Python expression '[0] * size1' generate?
A list of 'size1' number of zeros
What does the Python function int(input()) do?
It prompts the user for input and converts the input to an integer.
What is the advantage of function abstraction?
It simplifies the complexity of the code.
What is the benefit of Python lists?
They provide dynamic and flexible storage, allow data organization, aid in iteration
and processing, and can store diverse data types

What is the correct way to access the value 'Alejandro' from my_dict?
my_dict["A"]
What is the initial content of the list in the Python code 'list = [2.3, 7.72, 5.94, 7.85, 5.13,
5.29, 8.84, 4.1, 10.31, 2.96]'?
[2.3, 7.72, 5.94, 7.85, 5.13, 5.29, 8.84, 4.1, 10.31, 2.96]
What is the output of the 'greet()' function in the provided code?
"Hello, there!"
What is the output of the print statement in the given Python code snippet 'print("Product:
%.2f" % (list[0] * list[4]))'?
"Product: 11.79"
What is the output of the print statement in the given Python code snippet 'print("Sum of
first and last:", list1[0] + list2[-1])'?
It prints the sum of the first element of list1 and the last element of list2
What is the primary purpose of a function in Python?
To perform a specific task efficiently.
What is the purpose of a dictionary in Python?
To store data in key-value pairs.
What is the purpose of Python lists?
To group related data, provide dynamic storage and retrieval of elements, and for
iteration and processing
What is the purpose of the 'dict()' function in Python?
To create an empty dictionary
What is the purpose of the return statement in a Python function?
To determine the result that the function should produce.
What is the purpose of using for i in range(size): in Python?
It runs a loop size times.
What is the starting index of a Python list?
0
What role do functions play in Python programming?
They provide modularity, code reusability, and abstraction.
What value does the 'add' function return when called with arguments 10 and 20?
30
What will 'int(input())' in a Python script do when executed?
It will convert the input from the user to an integer
What will be printed out if the user enters 100 when asked to "Enter a new value for
key_1: "?
{ "key_0": 4, "key_1": 100, "key_2": 7, "key_3": 1, "key_4": 5, "key_5": 10 }
What will be the output of print(person.get("address", "Unknown")) if "address" key does
not exist in the person dictionary?
"Unknown"
What will be the output of the following command: print(my_dict.get('B'))?
Belantar
Which built-in Python function is used to convert a user's input to an integer?
int()
Which function is used to create a dictionary in Python?
dict()
Which of the following best describes function modularity?
Breaking down a program into smaller, reusable parts.
Which of the following is the correct way to access the third element of a Python list
named 'numbers'?
numbers[2]
Why are lists important in Python programming?
Lists help in organizing and processing data effectively, are dynamic and flexible, and
are used widely in various Python applications

You might also like