Comp Sci General Questions Flashcards
Python programs are…
Interpreted
Python is Case Sensitive…
True or False
True
PyCharm would be described as a…
A. Intergrated development
IDE(Integrated Development Environment)
How would you convert the string a = “HELLO” to lower case?
a) a.lower()
b) lower(a)
c) recase(a, TO_LOWER)
d) a.recase(TO_LOWER)
a.lower()
What would be the best way to find the length of the string a?
a) len(a)
b) a.length()
c) a[*]
d) Count each character
len(a)
What is the correct way to create a comment in Python?
a) // My comment
b) # My comment
c) /* My comment */
My comment
This is called
(2+3) == 2**(3)
Logical Expression
Which of the following is not a control statement in loops?
a) break
b) end
c) continue
d) none of the above
e) all of the above
end
In Python, what is the primary purpose of the “continue” keyword within a loop?
To skip the current iteration of the loop and move to the next one
What does the keys() method do?
Returns the keys in a dictionary as a list
What is the correct way to open the file grades.tsv for reading?
file = open(“grades.tsv”, “r”)
What is the correct way to read one full line from the already open file?
line = file.readline()
When reading a file, what will the readlines() method return when you reach the end of the file?
It will return an empty string(“”)
What should you do when you are done reading or writing a file?
Close the file wirh the .close() method
What does sep= method do?
Seperates the strings or lists with a new line or tab
How would you remove the extra spaces in the strings and convet the integers and floats correctly?
Use the .strip() to remove spaces and int() or float() to covert the strings
What does this method do?
lst = sorted(my_set, reverse=True)
Create a list from the following set sorted in decending order
What does lambda mean?
Used as an anonymous function inside another function. Can take any number of arguments, but only have one the expression
What does the union() method do?
Will return what both sets have
What does the difference() method do?
Will return what each set does not have
What does the intersection() method do?
Will get the common values that both sets has
What is the issuperset() method?
Returns turn if all items in the specified set exists in the original set
What is the issubset() method?
Returns True if all items in a set exists in the specified set, otherwise returns false
What is the order of the ouput of the following program?
nums = {5, 9, 2, 0, 1, 3, 4}
print(nums)
Sets are unordered, the order of the ouput is implementation dependent
What is the name of the constructor method in Python class?
__init__()
What is an instance variable in Python?
A variable defined inside a class to specific to each instance
In python, a function within a class definion is called a
method
Why is self used as the first parameter in class methods?
It is automatically assigned to the instance calling the method
True or False.
To divide 2 integers to get an integer result, you can use %%
False
What is a way to write counter = counter + 1?
counter += 1
What is the main purpose of the “break” function?
To terminate the loop and exit it prematurely
What does the term variable scope mean?
The portion of code where a variable can be accessed and modified.
def is_mutiple(n):
if n % 7 == 0:
return n
elif n % 3 == 0:
return -1
elif n % 2 == 1:
return True
else:
return False
a) -1
b) False
c) 21
d) True
c) 21