Python Exam 2
Which method would you use to return the value associated with a specified key and remove that key-value pair from the dictionary? a. list b. items c. popitem d. pop
pop
The ____ method returns the value associated with a specified key and removes that key-value pair from the dictionary . a. pop( ) b. random( ) c. popitem( ) d. rand_pop( )
pop( )
The ____ method returns a randomly selected key-value pair from a dictionary. a. pop( ) b. random( ) c. popitem( ) d. rand pop( )
popitem( )
The _________________ programming practice is centered on creating functions that are separate from the data that they work on. a. object-centric b. objective c. procedural d. object-oriented
procedural
This string method returns true if a string contains only alphabetic characters and is at least one character in length. a. the isalpha method b. the alpha method c. the alphabetic method d. the isletters method
the isalpha method
This string method returns true if a string contains only numeric digits and is at least one character in length. a. the digit method b. the isdigit method c. the numeric method d. the isnumber method
the isdigit method
What is the difference between calling a list's remove method and using the del statement to remove an element?
the remove method is used to remove a specific element in a list. the del method is used to remove any element that is present at a specific index, regardless of what that element is.
Built-in function to convert a tuple to a list
list ( )
Returns a copy of the string with all alphabetic letters converted to lowercase. Any character that is already lowercase, or is not an alphabetic letter, is unchanged.
lower( )
This string method returns a copy of the string with all leading whitespace characters removed. a. lstrip b. rstrip c. remove d. strip_leading
lstrip
Returns a copy of the string with all leading whitespace characters removed. Leading whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the beginning of the string.
lstrip( )
The char argument is a string containing a character. Returns a copy of the string with all instances of char that appear at the beginning of the string removed.
lstrip(char)
This string method returns the lowest index in the string where a specified substring is found. a. first index of b. locate c. find d. index of
find
The substring argument is a string. The method returns the lowest index in the string where substring is found. If substring is not found, the method returns -1.
find(substring)
The ____ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value. a. pop( ) b. key( ) c. value( ) d. get( )
get( )
An object is a(n) _______. a. blueprint b. cookie cutter c. variable d. instance
instance
What happens if you try to use an invalid index to access a character in a string?
An IndexError Exception occurs
This will happen if you try to use an index that is out of range for a list. a. A ValueError exception will occur. b. An IndexError exception will occur. c. The list will be erased and the program will continue to run. d. Nothing-the invalid index will be ignored.
An IndexError exception will occur.
This term refers to an individual item in a list. a. element b. bin c. cubbyhole d. slot
element
This will happen if you try to use an index that is out of range for a string. a. A ValueError exception will occur. b. An IndexError exception will occur. c. The string will be erased and the program will continue to run. d. Nothing-the invalid index will be ignored.
An IndexError exception will occur.
List
An object that contains multiple data items. Lists are mutable, which means that their contents can be changed during a program's execution.
You use ____ to delete an element from a dictionary. a. the remove method b. the erase method c. the delete method d. the del statement
the del statement
How do you find the number of elements in a list?
By using the len Function. The len function takes the list as an argument and it returns the number of the elements present in the list .
What is the number of the first index in a list dictionary? a. 0 b. 1 c. Dictionary is not indexed by number. d. Size of the dictionary minus one.
Dictionary is not indexed by number.
A class method does not have to have a self parameter. T or F
False
A file object's writelines method automatically writes a newline ( '\n' ) after writing each list item to the file. T or F
False
A list can be a dictionary key. T or F
False
Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory: list2 = list1 T or F
False
It is a common practice in object-oriented programming to make all of a class's data attributes accessible to statements outside the class. T or F
False
Lists in Python are immutable. T or F
False
One way to find the classes needed for an object-oriented program is to identify all of the verbs in a description of the problem domain. T or F
False
Sets are created using curly braces {}.
False
Sets are immutable
False
The dictionary method popitem does not raise an exception if it is called on an empty dictionary. T or F
False
The following statement creates an empty set: myset = ( ) T or F
False
The index of the first element in a set is 0.
False
The index of the first key-value pair in a dictionary is 0.
False
The isupper method converts a string to all uppercase characters. T or F
False
The keys in a dictionary must be mutable objects. T or F
False
The union of two sets is a set that contains only the elements that appear in both sets.
False
When you call a string's split method, the method divides the string into two substrings. T or F
False
You can remove an element from a tuple by calling the tuple's remove method. T or F
False
You can store duplicate elements in a set. T or F
False
This operator can be used to find the union of two sets. a. I b. & c. - d. ^
I
insert(index, item) Method
Inserts an item into the list at the specified index.
What would be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna'] a. false b. -1 c. 0 d. KeyError
KeyError
What is the process used to convert an object to a stream of bytes that can be saved in a file? a. Pickling b. Streaming c. Writing d. Dumping
Pickling
Give two reasons why tuples exist.
Processing a tuple is faster than processing a list. Tuples are safe. Because you are not allowed to change the contents of a tuple, you can store data in one and that data can not be modified (accidentally or otherwise) by any code in your program.
remove(item) Method
Removes the first occurrence of the item from the list. A ValueError exception is raised if the item is not found in the list.
What does the get method do if the specified key is not found in the dictionary? a. Throw an exception b. Nothing c. Return a default value d. You do not specify a key for the get method
Return a default value
index( item) Method
Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list.
reverse() Method
Reverses the order of the items in the list.
sort() Method
Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value).
You created the following dictionary relationships = {'Jimmy':'brother'}. You then executed the following code, and received a KeyError exception. What is the reason for the exception? relationships['jimmy'] a.) String comparisons are case sensitive so 'jimmy' does not equal 'Jimmy'. b.) You used the wrong syntax for creating the dictionary. c.) You should have used the code relationships['brother']. d.) There is a bug in Python.
String comparisons are case sensitive so 'jimmy' does not equal 'Jimmy'.
Which of the following does not apply to sets? a. The stored elements can be of different data types. b. All the elements must be unique - no two elements can have the same value. c. The elements are unordered. d. The elements are pairs.
The elements are pairs.
max Function
The max function accepts a sequence, such as a list, as an argument and returns the item that has the highest value in the sequence.
min Function
The min function accepts a sequence, such as a list, as an argument and returns the item that has the lowest value in the sequence.
When the * operator's left operand is a list and its right operand is an integer, the operator becomes this. a. The multiplication operator b. The repetition operator c. The initialization operator d. Nothing-the operator does not support those types of operands.
The repetition operator
Repetition Operator
The repetition operator makes multiple copies of a list and joins them all together. The star symbol is used, when the operand on the left side of the * symbol is a sequence (such as a list) and the operand on the right side is an integer, it becomes a repetition operator.
This is the last index in a list. a. 1 b. 99 c. 0 d. The size of the list minus one
The size of the list minus one
This is the last index in a string. a. 1 b. 99 c. 0 d. The size of the string minus one
The size of the string minus one
A dictionary can include the same value several times, but cannot include the same key several times.
True
A list can be an element in another list. T or F
True
A tuple can be a dictionary key. T or F
True
Dictionaries are not sequences. T or F
True
Object reusability has been a factor in the increased use of object-oriented programming. T or F
True
Once a string is created, it cannot be changed. T or F
True
Sets store their elements in an unordered fashion. T or F
True
Starting an attribute name with two underscores will hide the attribute from code outside the class. T or F
True
The del statement deletes an item at a specified index in a list. T or F
True
The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set2.
True
The following statement creates an empty dictionary: mydct = { } T or F
True
The issubset method can be used to determine whether set1 is a subset of set2.
True
The practice of procedural programming is centered on the creation of objects. T or F
True
The remove method raises an exception if the specified element is not found in the set. T or F
True
The repetition operator ( *) works with strings as well as with lists. T or F
True
The set remove and discard methods behave differently only when a specified item is not found in the set.
True
Tuples in Python are immutable. T or F
True
You can use the + operator to concatenate two lists. T or F
True
You can use the for loop to iterate over the individual characters in a string. T or F
True
You cannot directly call the _ _str_ _ method. T or F
True
You would typically use a for loop to iterate over the elements in a set.
True
A Tuple does not support what methods?
Tuples do not support methods such as append, remove, insert, reverse, and sort.
How do you find the length of a string?
Use the len( ) function
This operator can be used to find the symmetric difference of two sets. a. I b. & c. - d. ^
^
The substring argument is a string. The method returns true if the string ends with substring.
endswith(substring)
If a string has 10 characters, what is the index of the last character?
9
If you call the index method to locate an item in a list and the item is not found, this happens. a. A ValueError exception is raised. b. An Invalidindex exception is raised. c. The method returns - 1. d. Nothing happens. The program continues running at the next statement.
A ValueError exception is raised.
This operator can be used to find the intersection of two sets. a. I b. & c. - d. ^
&
This operator can be used to find the difference of two sets. a. I b. & c. - d. ^
-
This is the first index in a list. a. - 1 b. 1 c. 0 d. The size of the list minus one
0
This is the first index in a string. a. - 1 b. 1 c. 0 d. The size of the string minus one
0
What is the index of the first character in a string?
0
len Function
a built-in function that returns the length of a sequence, such as a list.
What is the primary difference between a list and a tuple?
a list is mutable, which means that a program can change its contents. a tuple is immutable, which means that once it is created, its contents cannot be changed.
two-dimensional list (nested lists)
a list that has other lists as its elements.
A(n) method gets the value of a data attribute but does not change it. a. retriever b. constructor c. mutator d. accessor
accessor
You can add one element to a set with this method. a. append b. add c. update d. merge
add
Sequence
an object that holds multiple items of data, stored one after the other. You can perform operations on a sequence to examine and manipulate the items stored in it.
This list method adds an item to the end of an existing list. a. add b. add to c. increase d. append
append
By doing this you can hide a class's attribute from code outside the class. a. avoid using the self parameter to create the attribute b. begin the attribute's name with two underscores c. begin the name of the attribute with private _ _ d. begin the name of the attribute with the @ symbol
begin the attribute's name with two underscores
If a class has a method named _ _str_ _, which of these is a way to call the method? a. you call it like any other method: object.._ _str_ _( ) b. by passing an instance of the class to the built in str function c. the method is automatically called when the object is created d. by passing an instance of the class to the built-in state function
by passing an instance of the class to the built in str function
How do you find the lowest and highest values in a list?
by using the min and/or the max functions
in Operator
can be used to determine whether an item is contained in a list.
append(item) Method
commonly used to add items to a list. The item that is passed as an argument is appended to the end of the list's existing elements.
appending one string to the end of another string
concatenation
A(n) _______ is a component of a class that references data. a. method b. instance c. data attribute d. module
data attribute
Which statement would you use to delete an existing key-value pair from a dictionary? a. del b. remove c. delete d. unpair
del
This set method removes an element but does not raise an exception if the element is not found. a. remove b. discard c. delete d. erase
discard
means that once they are created, they cannot be changed
immutable
In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the _____ operator. a. included b. of c. in d. not in
in
This operator determines whether one string is contained inside another string. a. contains b. is in c. == d. in
in
You can use the ____ operator to determine whether a key exists in a dictionary. a. & b. in c. ^ d. ?
in
This is a number that identifies an item in a list. a. element b. index c. bookmark d. identifier
index
The _______ method is automatically called when an object is created. a. init b. _ _init_ _ c. _ _str_ _ d. _ _object_ _
init
Tuple
is a sequence, very much like a list, but is an immutable sequence, which means that that once a tuple is created, its contents cannot be changed.
slice
is a span of items that are taken from a sequence. A slicing expression selects a range of elements from a sequence.
Returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise.
isalnum( )
Returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise.
isalpha( )
Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise.
isdigit( )
Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise.
islower( )
Returns true if the string contains only whitespace characters and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs (\t).
isspace( )
Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise.
isupper( )
Which method would you use to returns all the elements in the dictionary as a list of tuples? a. list b. items c. pop d. keys
items
The ____ method returns all of a dictionary's keys and their associated values as a sequence of tuples. a. keys_values( ) b. values( ) c. items ( ) d. get( )
items ( )
In a dictionary, you use a(n) _____ to locate a specific value. a. datum b. element c. item d. key
key
This function returns the length of a list. a. length b. size c. len d. lengthof
len
This function returns the length of a string. a. length b. size c. len d. lengthof
len
Which function would you use to get the number of elements in a dictionary? a. size b. length c. len d. invalid code
len
The ____ function returns the number of elements in a dictionary: a. size( ) b. len( ) c. elements( ) d. count( )
len( )
The following function returns the number of elements in a set: a. size( ) b. len( ) c. elements( ) d. count( )
len( )
This built-in function returns the highest value in a list. a. highest b. max c. greatest d. best of
max
Mutable
means their elements can be changed
A(n) _______ method stores a value in a data attribute or changes its value in some other way. a. modifier b. constructor c. mutator d. accessor
mutator
Assume the following statement appears in a program: mylist = [ ] Which of the following statements would you use to add the string 'Labrador' to the list at index 0? a. mylist[0] = 'Labrador' b. mylist.insert(0, 'Labrador') c. mylist. append ( 'Labrador' ) d. mylist.insert('Labrador', 0)
mylist.insert(0, 'Labrador')
In one approach to identifying the classes in a problem, the programmer identifies the _______ in a description of the problem domain. a. verbs b. adjectives c. adverbs d. nouns
nouns
The ____________ programming practice is centered on creating objects. a. object-centric b. objective c. procedural d. object-oriented
object-oriented
This file object method returns a list containing the file's contents. a. to_list b. getlist c. readline d. readlines
readlines
This set method removes an element and raises an exception if the element is not found. a. remove b. discard c. delete d. erase
remove
del Statement
removes an element from a specific index, regardless of the item that is stored at that index.
The old and new arguments are both strings. The method returns a copy of the string with all instances of old replaced by new.
replace(old, new)
In one approach to identifying a class's data attributes and methods, the programmer identifies the class's ______. a. responsibilities b. name c. synonyms d. nouns
responsibilities
Returns a copy of the string with all trailing whitespace characters removed. Trailing whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of the string.
rstrip( )
The char argument is a string containing a character. The method returns a copy of the string with all instances of char that appear at the end of the string removed.
rstrip(char)
The substring argument is a string. The method returns true if the string starts with substring.
startswith(substring)
This string method returns a copy of the string with all leading and trailing whitespace characters removed. a. clean b. strip c. remove_whitespace d. rstrip
strip
Returns a copy of the string with all leading and trailing whitespace characters removed.
strip( )
Returns a copy of the string with all instances of char that appear at the beginning and the end of the string removed.
strip(char)
a slice from a string, you get a span of characters from within the string. String slices are also called
substrings
A set of standard diagrams for graphically depicting object-oriented systems is provided by _____. a. the Unified Modeling Language b. flowcharts c. pseudocode d. the Object Hierarchy System
the Unified Modeling Language
This removes an item at a specific index in a list. a. the remove method b. the delete method c. the del statement d. the kill method
the del statement
Built-in function to convert a list to a tuple
tuple ( )
What method can be used to add a group of elements to a set? a. add b. addgroup c. update d. Elements must be added one at a time.
update
You can add a group of elements to a set with this method. a. append b. add c. update d. merge
update
Returns a copy of the string with all alphabetic letters converted to uppercase. Any character that is already uppercase, or is not an alphabetic letter, is unchanged.
upper( )
Which of the following statements creates a tuple? a. values [1, 2, 3, 4] b. values {1, 2, 3, 4} c. values ( 1) d. values ( 1,)
values ( 1,)
What is the correct structure for creating a dictionary of month names to be accessed by month numbers? a. { 1 ; 'January', 2 ; 'February', 3 ; 'March' } b. { 1 : 'January', 2 : 'February', 3 : 'March' } c. [ 1 : 'January', 2 : 'February', 3 : 'March' ] d. { 1, 2, 3 : 'January', 'February', 'March' }
{ 1 : 'January', 2 : 'February', 3 : 'March' }
You can use ____ to create an empty dictionary. a. { } b. ( ) c. [ ] d. empty( )
{ }
What is the value of the variable phones after the execution of the following code? phones = {'John': '5555555', 'Julie' : '7777777'} phones['John'] = '1234567' a. {'John': '5555555', 'Julie' : '7777777'} b. {'John': '1234567', 'Julie' : '7777777'} c. {'John': '1234567' } d. invalid code
{'John': '1234567', 'Julie' : '7777777'}