Computer Science
Order of Software development process tasks
-Analyze the problem -Design the code for the program -Write the code in computer -Test/debug the program language
Legal identifiers cannot
-have spaces -punctuation (except underscores) -begin with a number
Which is not a legal python statement a) print("x") b) 3 + 4 = x c) x = 3 + 4 d) x = eval(input("Enter x: "))
3 + 4 = x
What data type does the string's split method return?
A list of strings.
With top-down design, which function in your design must you implement first? a) The topmost function. b) a function in the middle. c) One of the functions at the bottom. d) It doesn't matter which one you implement first.
Answer: d it doesn't matter
What is „def" used for in python
Defining functions def function_name():
What is „for" used for in python
For loops to iterate through lines of code for i in range(): for variable in list:
Purpose of comments
Help a human reading the comment understand the code #comment """multiple line comment"""
Why don't we program computers using a common human language such as English?
Human languages are complex and often ambiguous or imprecise
What is input used for
It allows the user to input information into the code which can be saved in a variable variable = input("question: ")
What is short-circuit evaluation in Python?
It is when Boolean expressions are not fully evaluated because the result of the expression can be determined without checking the full expression.
What is print used for
It print text to the screen print("")
What is IPO and what does it stand for
Most simple computer programs follow the basic pattern defined by the acronym IPO, Input, Process, Output.
What would the following Python program output? for s in "the quick fox": print(s)
Output each of the characters (including the spaces) in the string with one character per line.
What would the following Python program output? items = "the,quick,fox".split(",") for s in items: print(s)
Output the on the first line, quick on the second line, and fox on the third line.
Algorithm
Step by step process that will eventually solve a problem
Syntax
The rules for forming legal statements
7. If you need to write a loop that will execute a fixed number of times, which statement should you use? a) for b) if c) while d) all of the above e) none of the above
a
What is the result of the Python expression "3" + "4"? a) "34" b) "7" c) 7 d) "7"
a) "34"
What is the purpose of exception handling? a) To handle errors gracefully so the program does not crash. b) To make a program crash. c) To make algorithms more efficient. d) none of the above
a) To handle errors gracefully so the program does not crash.
Which of the following is a reason to use top-down design? a) It will likely make your program easier to update and maintain. b) It will likely make your program easier to understand. c) It will likely make it easier to test your program. d) all of the above
answer: d all of the above
Which of the following statements could be true about the following Python code? from random import randrange for i in range(10): print(randrange(10)) a) It might output the numbers 0 through 9 in that order. b) It might output 0 through 9 in some order. c) It might output 0 ten times. d) all of the above e) none of the above
answer: d all of the above
If you want to use the random() function in the random module to get a value that is likely to be true 75% of the time, which of the following Python statements could you use? a) if random() < 0.25: b) if random() <= 0.25: c) if random() > 0.25 d) if random() >= 0.25 e) none of the above
answer: d if random() >= 0.25:
What is short-circuit evaluation in Python? a) It is when your program has an error and causes a short circuit in the CPU. b) It is when Boolean expressions are not fully evaluated because the result of the expression can be determined without checking the full expression. c) It is a way of writing Boolean expressions so they are shorter. d) none of the above
b - It is when Boolean expressions are not fully evaluated because the result of the expression can be determined without checking the full expression.
Which of the following statements is true about computer simulations? a) They model the real world exactly. b) Once the simulation is written, the input values can be tweaked to see how it a ects the results. c) All computer simulations are easy to write. d) all of the above e) none of the above
b Once the simulation is written, the input values can be tweaked to see how it a effects the results.
How do you determine if a name is a variable holding a value or if it is a function call? a) If it is a function call, it will start with an uppercase letter. b) If it is a function call, it will have parentheses after the name to call the function. c) If it is a variable, it will be on the right-hand side of an assignment statement. d) If it is a variable it will be part of an expression.
b) If it is a function call, it will have parentheses after the name to call the function.
How do you determine if a variable in a method is an instance variable for the class? a) Instance variables have leading underscores in their names. b) Instance variables are prefixed with self.. c) Instance variables start with a capital letter. d) all of the above e) none of the above
b) Instance variables are prefixed with self..
Which of the following statements about string comparisons is true? a) Shorter strings are less than longer strings. b) Strings that begin with the letter 'A' are less than strings that begin with the letter 'a' c) Strings that begin with the letter 'a' are less than strings that begin with the letter 'A' d) Strings cannot be compared; only numbers may be compared.
b) Strings that begin with the letter 'A' are less than strings that begin with the letter 'a'
When the execution of a function ends, what Python code is executed next? a) The line of code immediately after the end of the function. b) The code immediately after where the function was called from. c) The program ends without executing any more code. d) The function is automatically executed again.
b) The code immediately after where the function was called from
How do you determine which actual parameter to assign to each formal parameter? a) The names of the formal parameters must match. b) The order of the parameters indicates how they are assigned. c) You can only pass one parameter to a function. d) You manually assign each parameter at the beginning of the function.
b) The order of the parameters indicates how they are assigned.
Which of the following statements is true? a) There is usually only one algorithm to solve a problem. b) There is often more than one way to solve a problem and some ways may be more efficient than others. c) There is often more than one way to solve a problem but they are all equally efficient. d) all of the above e) none of the above
b) There is often more than one way to solve a problem and some ways may be more efficient than others.
What is the purpose of the statement if __name__ == '__main__':? a) To check if the name of a Python program is main. b) To check if a Python file was executed or imported. c) It may be an error unless the program included a line that set __name__ to a string value. d) all of the above e) none of the above
b) To check if a Python file was executed or imported.
What is the purpose of an if statement? a) To execute code repeatedly. b) To choose whether or not to execute certain code. c) To assign a bool value. d) all of the above
b) To choose whether or not to execute certain code.
Which of the following Python statements is invalid? a) if x != 4: b) if x = 4: c) if x == 4: d) if 2 < x < 4:
b) if x = 4:
If you need to write a loop where a sentinel value indicates you want to stop executing the loop, which statement should you use? a) for b) if c) while d) all of the above e) none of the above
c
A while loop written as while True a) will always be an infinite loop no matter what the loop body contains b) will never be an infinite loop no matter what the loop body contains c) might be an infinite loop depending on what the loop body contains d) will never execute the loop body
c might be an infinite loop depending on what the loop body contains
Which of the following calculations would require the use of an if statement? a) A program that asks the user to enter a value and adds up the odd numbers between 1 and the entered number (not including the entered number). b) A program that asks the user to enter a value and adds up the even numbers between 2 and the entered number (not including the entered number). c) A program that asks a user to enter two values and adds up the even numbers between the first entered value and the second entered value including the first entered value but not including the second entered value. d) all of the above e) none of the above
c) A program that asks a user to enter two values and adds up the even numbers between the first entered value and the second entered value including the first entered value but not including the second entered value.
When does the value of an actual parameter of a function change? a) Whenever the parameter is a mutable type b) Only when the parameter is a mutable type and you assign a new value to the formal parameter (i.e., the parameter is a list and you assign it to a new list such as x = [2, 3, 4]). c) Only when the parameter is a mutable type and you mutate the formal parameter (i.e., append an item to a list or change the value of an item in the list such as x[0] = 4). d) Any change to a formal parameter a ects the corresponding actual parameter.
c) Only when the parameter is a mutable type and you mutate the formal parameter (i.e., append an item to a list or change the value of an item in the list such as x[0] = 4).
If you are going to use the accumulator pattern and append items to a list, what should you initialize the list to? a) [0] b) [1] c) [] d) ["accumulate"]
c) []
What is the purpose of the return statement in a function definition? a) To cause execution of the function to end and return to the code that called it. b) To send a value or values back to the code that called it. c) either a or b d) none of the above
c) either a or b
Which of the following would remove the first character and last character from the string s and return the string between the first and last characters? a) s[2:0] b) s[1:0] c) s[1:-1] d) s[2:-2]
c) s[1:-1]
How do you access the letter h in the string s = "there"? a) s[2] b) s[3] c) s[1] d) s[0]
c) s[1]
print function
causes text to be printed on the screen print()
What is an assignment statement?
causes the computer to store the result of the statement's expression in memory so you can use the result in a later Python statement.
6. Which of the following expressions might use short-circuit evaluation? a) if x = 3 and y > 0: Reason: if x doesn't ==3 you or your computer don't have to look at the rest of the condition b) if x == 3 or y > 0: c) if n > 0 and total / float(n) > 90.0: d) all of the above e) none of the above
d
What does the Python expression 3 * "A" evaluate to? a) It produces an error. b) "3A" c) "3A3A3A" d) "AAA"
d) "AAA"
Which of the following statements are true? a) An if statement must have an else statement. b) An if statement must have an elif statement. c) An if statement must have both an elif and an else statement. d) An else statement must have a matching if statement.
d) An else statement must have a matching if statement.
What variable names does a function have access to? a) All variables used in a program. b) Only variables created in the function. c) Only the parameters passed to the function. d) Parameters that are passed to the function and any variables created in the function.
d) Parameters that are passed to the function and any variables created in the function.
How do you determine which is the last line of a function? a) The first return statement is the last line of a function. b) The last return statement is the last line of the function. c) The next non-blank line contains a def statement. d) The next non-blank line of code is indented at the same indentation level as the def statement for the function.
d) The next non-blank line of code is indented at the same indentation level as the def statement for the function.
When a function returns a value, the options for the caller are to a) ignore the return value b) use the result as part of an expression (e.g., x = y + math.sqrt(5)) c) assign it to a variable (e.g., x = math.sqrt(5)) d) all of the above
d) all of the above
When should you use a Python list to store your data? a) You find yourself using variables named item1, item2, item3, item4, etc. b) The data is related and you want to be able to process it using a loop instead of multiple statements processing each item individually. c) The data is related and you don't know how many items you will need to store. d) all of the above e) none of the above
d) all of the above
Which of the following statements is true? a) All try statements must have a corresponding except statement. b) All except statements must have a corresponding try statement. c) A try statement may have multiple statements indented underneath it. d) all of the above e) none of the above
d) all of the above
Which of the following statements is true? a) An if statement may have another if statement indented beneath it. b) An if statement may have a for loop nested inside it. c) A for loop may have an if statement nested inside it. d) all of the above e) none of the above
d) all of the above
When a function returns a value, usually you want to a) ignore the returned value b) store the result in a variable c) use the result as part of an expression d) either b or c
d) either b or c
How do you determine the number of characters in the string s? a) s.length() b) s.len() c) length(s) d) len(s)
d) len(s)
what is "def" used for
defining functions
input function
displays text on the screen and allows someone using your program to enter a value from the keyboard variable = input(question? )
CPU
executes instructions that are written in the CPU (only processes 0s and 1s)
what is "for" used for
for loops to iterate through code
Conversion from gallons to liters
liters = gallons * 4.0 / 1.05669
what is "print" used for
printing information to the screen
Semantics
the meaning of statements
what is "input" used for
to ask for user input and subsequently assign the information to a variable