Coding Exam Questions Flashcards
Printing the contents of a file containing two lines read with “readlines()” results in what?
[‘Line01\n’, ‘Line02’]
Printing the contents of a file containing two lines read in with “read()” results in what?
Line01\nLine02
Opening an existing text file with the option ‘w’ and then “write”ing some text to the file does what
File text_file.txt will be created if it does not exist
If a file is opened with the option ‘r’ but the file doesn’t exist, what is returned?
Error message
“ATGCATGC” return the results when the “GC” is replaced with “U”
ATUATU
What is the “len” of a sequence when you split “ATGCATGC” at “A”
3, first A results in a space.
stringvariable.find(‘string’) method returns what?
The index of the first occurrence of the substring if found or -1 if not found
string = [“Python”, “is”, “fun”] print(““.join(string)) returns what?
Python is fun - string join() method returns a string by joining all the elements of an iterable (list, string, tuple)
string.split() does what to this object, string = “Good Morning to you”
[“Good”, “Morning”, “to”, “you”] text.split() - splits string into a list of substrings at each space character, text.split(‘,’) splits at commas,
Class instantiation uses function notation that returns a new instance of the class. For example
tobject = Grades(1234, 5007) does what?
creates a new instance of the class and assigns this object to the local variable tobject.
How does class inheritance work?
Declare your class followed by parentheses and then list any classes that your class inherits from. In a function definition, parentheses after the function name represent arguments that the function accepts. In a class definition the parentheses after the class name instead represent the classes being inherited from. The second class has access to all the methods in the inherited class.
f1= Fraction(1,2)
f2= Fraction(2,3)
f3 = f1 + f2
print(f3.num)
returns 7, why?
The Fraction module is a built in class with a __add__ method which is = to the arithmetic function +
1/2 + 2/3 = 3/6+4/6=7/6
7 = num, 6 = den
f3.num calls the add function and the numerator is 7
If head = 0 in your Class and you instantiate the object with other variables, what does print(IList.head) return?
0 because the variable head = 0 in iList. Despite IList class being called in the previous line with a list of numbers which would set head to 4, that doesn’t persist because it isn’t saved when iList is called again. Rather than printing out a variable, the print command here calls the class with no input attributes so head is equal to the default value set in the class.
How do you call a method inside a class. ex. print(new_emp.getDept()) vs. call an attribute in a class by print(new_emp.dept)
We invoke a method by first typing in the name of the object we are referencing, followed by a dot (.), followed by the method with parenthesis.
What position does this command return in an XML file?
find(“.//country[1]”).attrib[“name”]
Returns the “name” attribute of the first country listed because parsing in XML starts at position 1 unlike JSON and CSV which starts at position 0.
- find(“.//country[2]”).attrib[“name”]
- finds the second country in an XML file.