ITSCM 180 Principles Exam 2
What will be assigned to the string variable even after the execution of the following code? special = '0123456789' even = special[0:10:2]
'02468'
What is the value of the variable new_string after the execution of the following code? string = 'abcd' new_string = string.capitalize()
'Abcd'
What will be assigned to s_string after the execution of the following code? special = '1357 Country Ln.' s_string = special[-3: ]
'Ln.'
Which mode specifier will open a file but will not let you change the file or write to it?
'r'
Which mode specifier will erase the contents of a file if it already exists and create it if it does not exist?
'w'
What will be assigned to the string variable pattern after the execution of the following code? i = 3 pattern = 'z' * (5*i)
'zzzzzzzzzzzzzzz'
What are the valid indexes for the string 'New York'?
0 through 7
Which method could be used to strip newline characters from the end of a line in a text file handle?
rstrip
What statement can be used to handle bad file name errors (entered by user) in a program?
try/except statement
If the + operator is used on strings, it produces a string that is the concatenation of the two strings used as its operands.
True
In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.
True
Indexing works with both strings and lists.
True
The following expression, if executed in python, returns -1 as output: animal = 'Lion' animal.find('z')
True
What would be the output of the following code in python? some = [1, 9, 21, 10, 16] 18 not in some
True
What would be the value of the variable list after the execution of the following code? list = [1, 2, 3, 4] list[3] = 10
[1, 2, 3, 10]
What would be the output of running the following codes in python? friends = ['Joe', 'Jennifer', 'Ben'] print(friends[2])
Ben
The built-in function _______________ returns the number of elements of a list/tuple.
len
Python sees a given text file as a sequence of _________________, much like it sees a string as a sequence of ___________________.
lines - characters
If a file with the specified name already exists when the file is opened, and the file is opened in 'w' mode, then an alert will appear on the screen.
False
The sort method rearranges the elements of a list so they appear in descending order.
False
Unlike strings and lists, tuples are mutable data structures.
False
When we use write method to add some string to a file through its handle, the string will be saved to the text file immediately after running the code.
False
When a file has been opened using the 'r' mode specifier, which method/function will return the file's contents as a string?
Read
What would be the output of running the following codes in python? friends = ['Joe', 'Jennifer', 'Ben'] print(friends[2: ])
['Ben']
What would be the output of the following code in python? abc = 'stay home stay safe' stuff = abc.split() print(stuff)
['stay', 'home', 'stay', 'safe']
What would be the output of the following code in python? abc = 'we#will#beat#coronavirus' stuff = abc.split() print(stuff)
['we#will#beat#coronavirus']
Which list will be referenced by the variable number after the execution of the following code? number = range(9)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Assume that the customer file handle has been created for a text file, and the file was opened using the 'w' mode specifier. How would you write the string 'Mary Smith' to the file?
customer.write('Mary Smith')
Which method would you use to determine whether a substring is present in a string?
find(substring)
A(n) ________________ keyword can be used to check if a particular substring is contained in a line of a text file.
in
