computer science midterm chap 1-4
exponent
Double stars ** are used to calculate an
The while loop
Examples of loop applications Calculation compound interest Simulations, event driven programs Drawing tiles The loop executes instructions repeatedly while a condition is true
>
Greater than
>=
Greater than or equal
A library
a collection of code, written and compiled by someone else, that is ready for you to use in your program
Variables
a named storage location in a computer
Software
a sequence of instructions and decisions implemented in some language and translated to a form that can be executed or run on the computer.
Algorithm
a step by step description of how to solve a problem.
Constants
a variable whose value should not be changed after it's assigned initial value
Which of the following code snippets displays the output exactly 10 times? a. i = 0 while i < 10 : print("This is example 2.") i = i + 1 b. i = 0 while i < 10 : print("This is example 3.") c. i = 1 while i < 10 : print("This is example 4.") i = i + 1 d. i = 0 while i <= 10 : print("This is example 1.") i = i + 1
a. i = 0 while i < 10 : print("This is example 2.") i = i + 1
What is the output of the following code snippet? i = 1 while i <= 10 : print("Inside the while loop") i = i + 10 answers: a. "Inside the while loop" will be displayed only once. b. No output after successful compilation. c. "Inside the while loop" will be displayed 10 times. d. No output because of compilation error.
a. "Inside the while loop" will be displayed only once.
What will be printed by the statements below? val = 1 sum = 0 while val < 5 : sum = sum + val val = val + 1 print(sum) answers: a. 10 b. 4 c. 5 d. 15
a. 10
What is the value of the price variable after the following code snippet is executed? price = 42 if price < 40 : price = price + 10 if price > 30 : price = price * 2 if price < 100 : price = price - 20 answers: a. 64 b. 52 c. 84 d. 42
a. 64
What is the last output line of the code snippet given below? i = 0 j = 0 while i < 10 : num = 1 j = i while j > 1 : print(j, end = " ") num = num * 2 j = j - 1 print("***") i = i + 1 answers: a. 9 8 7 6 5 4 3 2 *** b. 2 *** c. 8 7 6 5 4 3 2 *** d. 3 2 ***
a. 9 8 7 6 5 4 3 2 ***
What value is displayed by the following code segment? name = "John Smith" print(name.startswith("john")) answers: a. False b. True c. -1 d. 0
a. False
Consider the following code snippet: age = int(input("Enter your age: ")) if age < 10 : print("Child") if age < 30 : print("Young Adult") if age < 70 : print("Old") if age < 100 : print("Impressively old") Assuming that the user inputs 80 as the age, what is the output? answers: a. Impressively old b. Child Young adult Old c. Child Young adult Old Impressive old d. Young adult Old
a. Impressively old
What does the following code snippet display? for n in range(1, 11) : for x in range(1, 11) : print(n*x, end = " ") print() answers: a. It displays a multiplication table for numbers 1-10 times 1-10 b. Nothing because it has compilation error. c. It displays a multiplication table for numbers 1-11 times 1-11 d.It displays a table of all numbers squared from 1-10
a. It displays a multiplication table for numbers 1-10 times 1-10
What will be the output of the following code snippet? token = False while token : print("Hello") answers: a. No output after successful compilation. b. "Hello" will be displayed only once. c. "Hello" will continue to be displayed until the user stops the program. d. No output because of compilation error.
a. No output after successful compilation.
What is printed from the following code snippet: message = "ho.." print(message * 3) answers: a. ho..ho..ho.. b. ho.. c. nothing is printed, this code snippet causes an error d. ho..ho..ho
a. ho..ho..ho..
Consider the following code snippet. What should be placed in the blank to cause a message to be displayed when the user enters the same letter twice in a row? letter = input("Enter the next letter in the alphabet: ") while letter != "": previous = letter letter = input("Enter the next letter") if ________________________ : print("Duplicate input") answers: a. letter == previous b. alphabet[0] == letter c. alphabet[0] == previous d. letter == letter
a. letter == previous
Which of the following statements correctly multiplies num1 times num2? answers: a. num1 * num2 b. num1 · num2 c. num1 ** num2 d. num1 x num2
a. num1 * num2
Consider the following code snippet. num1 = 0 num2 = 0 num3 = 0 num4 = 0 num5 = 0 num1 = int(input("Enter a number: ")) num2 = int(input("Enter a number: ")) if num1 < num2 : num3 = num1 else : num3 = num2 if num1 < num2 + 10 : num4 = num1 elif num1 < num2 + 20 : num5 = num1 print("num1 =", num1, "num2 =", num2, "num3 =", num3, "num4 =", num4, "num5 =", num5) Assuming that the user enters the numbers 20 and 12 as the two input values, what is the output of the code snippet? answers: a. num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0 b. num1 = 20 num2 = 12 num3 = 12 num4 = 0 num5 = 20 c. num1 = 20 num2 = 12 num3 = 20 num4 = 0 num5 = 20 d. num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 = 0
a. num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0
What is the output of the following code fragment? i = 1 sum = 0 while i <= 15 : sum = sum + i i = i + 1 print("The value of sum is", sum) answer: a. The value of sum is 120 b. The value of sum is 0 c. The value of sum is 136 d. The value of sum is 105
a. the value of sum is 120
the absolute value of x
abs(x)
The if statement
allows a program to carry out different actions depending on the nature of the data to be processed
two boolean operators
and, or
A storyboard consists of
annotated sketches for each step in an action sequence
Built in functions
are a small set of functions that are defined as part of the python language (can be used without importing any modules)
What is the sentinel value in the following code segment? value = 15 x = int(input("Enter an integer: ")) while x != 0 : value = value * 2 print(value + 3) x = int(input("Enter an integer: ")) answers: a. 2 b. 0 c. 3 d. 15
b. 0
What are the two parts of an if statement? answers: a. A check and an increment b. A condition and a body c. An increment and a return value d. An increment and a body
b. A condition and a bod
A loop inside another loop is called: answers: a. A while loop b. A nested loop c. A parallel loop d. A sentinel loop
b. A nested loop
Which of the following variables is used to store a condition that can be either True or False? answers: a. Conditional b. Boolean c. Algebraic d. Logical
b. Boolean
Which statement corrects the off-by-one error in the following code: # This code prints the first 10 numbers starting with zero i = 0 while i <= 10 : print(i) i = i + 1 answers: a. Replace i = i + 1 with i = i + 2 b. Replace while i <= 10 with while i < 10 c. Replace i = 0 with i = 1 d. Replace while i <= 10 with while i + 1< 10
b. Replace while i <= 10 with while i < 1
What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1? sum = 0 count = 0 salary = 0 average = 0 while salary != -1 : salary = float(input("Enter salaries (-1 to stop): ")) if salary != -1 : sum = sum + salary count = count + 1 if count > 0 : average = sum / count print("The average salary: ", average) else : print("No data!") answers: a. There will be no output as the code snippet will not compile. b. The average salary: 30.0 c. The average salary: 0.0 d. The average salary: 24.83333
b. The average salary: 30.0
When hand-tracing the loop in the code snippet below, which variables are important to evaluate? i = 10 j = 5 k = -10 sum = 0 while i > 0 : sum = sum + i + j i = i - 1 print("Iteration: ", i) answers: a. The variables j and k b. The variables i and sum c. The variables i, j, and k d. The variables i and j
b. The variables i and sum
What is printed by the following code snippet if itemCount contains a value of 10 and cost contains 80: if itemCount > 5 : discount = 0.8 totalCost = cost * discount print("Total discounted price is:", totalCost) answers: a. Total discounted price is: 16.0 b. Total discounted price is: 64.0 c. Nothing, the program will run but not print any results d. Total discounted price is: 0.0
b. Total discounted price is: 64.0
Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed? answers: a. The physical gears can lead to ideas for the correct algorithm to use. b. A storyboard can be used. c. Pseudocode can guide algorithm design through divide-and-conquer strategy. d. Hand-tracing can confirm code that implements gear selection.
b. a storyboard can be used
The following program is supposed to print a message any time the user enters two consecutive values that are the same. What line of code must be inserted in the blank so that the program will achieve this goal? value = int(input("Enter a value: ") inputStr = input("Enter a value: ") while inputStr != "" : previous = value value = int(inputStr) ____________________ print("Found consecutive values that are the same") inputStr = input("Enter a value: ") answers: a. if previous == inputStr : b. if previous == value : c. if value == input : d. if value == inputStr :
b. if previous == value :
What will be the values of the variables num1 and num2 after the given set of assignments? num1 = 20 num2 = 10 num1 = num1 + num2 / 2 num2 = num1 answers: a. num1 = 15.0, num2 = 15.0 b. num1 = 25.0, num2 = 25.0 c. num1 = 15.0, num2 = 10.0 d. num1 = 20.0, num2 = 10.0
b. num1 = 25.0, num2 = 25.0
Which code snippet is the correct Python equivalent to the following Algebraic expression ? c = √(a2 + b2) answers: a. sqrt(a ^ 2 + b ^ 2) b. sqrt(a ** 2 + b ** 2) c. squareroot(a ** 2 + b ** 2) d. sqrt(a * 2 + b * 2)
b. sqrt(a ** 2 + b ** 2)
What value is displayed by the following code segment? s = "Computer Science" x = s.find("TER") print(x) answers: a. 0 b. 5 c. -1 d. 6
c. -1
What is the output of the code snippet given below? s = "12345" i = 0 while i < 5 : print(s[i]) i = i + 1 answers: a. 2345 b. No output c. 12345 d. 1234
c. 12345
Assuming a user enters 30, 20, and 10 as the input values, what is the output of the following code snippet? num1 = int(input("Enter a number: ")) num2 = int(input("Enter a number: ")) num3 = int(input("Enter a number: ")) if num1 > num2 : if num1 > num3 : print(num1) else : print(num3) else : if num2 > num3 : print(num2) else : print(num3) answers: a. 0 b. 20 c. 30 d. 10
c. 30
What is the value of i at the end of the following code segment? i = 1 while i < 32 : i = i * 2 answers: a. 16 b. 31 c. 32 d. 64
c. 32
What are the final values of the variables i, j, and n at the end of this loop? i = 0 j = 12 n = 0 while i != j : i = i + 2 j = j - 2 n = n + 1 answers: a. 0, 12, 0 b. 4, 8, 2 c. 6, 6, 3 d. 2, 10, 1
c. 6 6 3
What is the wrong with the following code snippet? grade = int(input("Enter student grade: ")) if grade >= 90 : letterGrade = "A" if grade >= 80 : letterGrade = "B" if grade >= 70 : letterGrade = "C" if grade >= 60 : letterGrade = "D" else : letterGrade = "E" print(letterGrade) answer: a. Everyone will get an "E" b. The code block will not compile c. Anyone with a grade higher than 60 will receive a "D" d. Nothing is wrong, students will get the correct grade
c. Anyone with a grade higher than 60 will receive a "D"
When will the loop in the following code snippet stop? sum = 0 count = 1 str = input("Enter values, Q to quit: ") while count < 100 and str != "Q" : value = float(str) sum = sum + value count = count + 1 str = input("Enter values, Q to quit: ") I. When the user enters an integer II. When the user enters the character Q III. After the user enters 100 numbers answers: a. I or II b. III only c. II or III d. II only
c. II or III
What is the output of the code snippet given below? s = "abcde" length = len(s) i = 1 while i < length : print(s[i]) i = i + 1 a. abcd b. abcde c. bcde d. No output
c. bcde
Given that the following code snippet: isFelon = False answer = input("have you ever committed a felony? ") if answer == "Yes" or answer == "yes" : isFelon = True age = int(input("what is your age? ")) which statement assigns the variable mayVote a value of True if a person may vote if they are 18 or older and not a felon? answers: a. mayVote = age > 18 or not isFelon b. mayVote = not ( age >= 18 and isFelon) c. mayVote = age >= 18 and not isFelon d. mayVote = not ( age >= 18 or isFelon)
c. mayVote = age >= 18 and not isFelon
Equality testing
checks if something is true
Hardware
consists of the physical elements in a computer system
the cosine of x in radians
cos(x)
Of the following options, what should the user enter to cause the following while loop to terminate? done = False while not done : x = float(input("Enter a number: ")) if x > 5.0 : print(x) elif x > 0.0 : done = False elif x < -5.0 : print(-x) else : done = True answers: a. 7.5 b. -7.5 c. 2.5 d. -2.5
d. -2.5
What will be printed by the statements below? for ctr in range(10, 5, -1) : print(ctr, end = " ") answer choices: a. 5, 6, 7, 8, 9, 10 b. 10, 9, 8, 7, 6, 5 c. 6, 7, 8, 9, 10 d. 10, 9, 8, 7, 6
d. 10, 9, 8, 7, 6
What is the output of this loop? counter = 1 for i in range(1, 100) : counter = counter + 1 print(counter) answers: a. 10 b. 49 c. 60 d. 100
d. 100
How many times does the following code fragment display "Hi"? i = 10 while i >= 0 : print("Hi") i = i - 1 a. 12 times b. 10 times c. 9 times d. 11 times
d. 11 times
Given the following code snippet: MIN_SPEED = 45 MAX_SPEED = 65 speed = 55 if not (speed < MAX_SPEED) : speed = speed - 10 if not (speed > MIN_SPEED) : speed = speed + 10 print(speed) what output is produced? answers: a. 50 b. 45 c. 65 d. 55
d. 55
Which of the following correctly identifies what is wrong with the code snippet below: if y > 300 : x = y else : x = 0 print("x:", x) answers: a. No colon is needed after the else statement b. Nothing, the program runs as intended c. The statement after the if statement must be indented d. The statement after the if statement and the statement after the else statement must be indented
d. The statement after the if statement and the statement after the else statement must be indented
What is the output of the code snippet given below? s = "abcde" j = len(s) - 1 while j >= 0 : print(s[j]) j = j - 1 answers: a. abcd b. bcde c. bcbcd d. edcba
d. edcba
What function is used to read a value from the keyboard? answers: a. print b. keyboard c. next d. input
d. input
What is printed by the following code snippet: street = " Main Street" address = 123 + street print(address) answers: a. 123Main Street b. 123 "Main Street" c. 123 Main Street d. nothing is printed, this code snippet causes an error
d. nothing is printed, this code snippet causes an error
What string method can be used to determine if all characters within a string are lowercase? answers: a. text.isdigit() b. text.isalnum() c. text.isalpha() d. text.islower()
d. text.islower()
Which statement about this code snippet is accurate? years = 50 balance = 10000 targetBalance = 20000 rate = 3 for i in range(1 , years + 1) : if balance >= targetBalance : i = years + 1 else : interest = balance * rate / 100 balance = balance + interest answers: a. The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance. b. There is a compilation error. c. The loop will never stop. d. The loop will run 50 times.
d. the loop will run 50 times.
converts x from radians to degrees
degrees(x)
==
equal
e^x
exp(x)
pseudocode
half way between natural language and a programming language. The steps that the pseudocode describes must be: unambiguous, executable, terminating.
The two keywords of the if statement are
if, else
the natural logarithm of x (to base e) or the logarithm of x given the base
log(x) and log(x, base)
Assignment
makes something true
!=
not equal
Use the str() function to convert between
numbers and strings
boolean variable
often called a flag because it can either be up (true) or down (false)
Statement blocks signal that
one or more statements are part of a given compound statement
Central processing unit
performs program control and data processing
Steps to writing a loop
planning : 1. Decide what work to do inside the loop 2. Specify the loop condition 3. Determine loop type 4. Setup variables before the first loop 5. Process results when the loop is finished 6. Trace the loop with typical examples Coding: 7. Implement the loop in python
two types of storage
primary and secondary
converts x degrees into radians
radians (x)
Nested decisions
required for problems that have two levels of decision making
the floating-point value x rounded to a whole number or to n places
round(x) or round(x, n)
the sine of x in radians
sin(x)
the tangent of x in radians
tan(x)
Computer program
tells a computer the sequence of steps needed to complete a specific task
Total += cans is a shortcut for
total = total + cans
truncates floating-point value x to an integer
trunc(x)
The for loop
used to iterate over the contents of any container
Nested branches
when a decision statement is contained inside the branch of another decision statement
s.isspace()
Returns true if string s consists of only white space characters (blank, new line, tab) and it contains at least one character. Otherwise it returns false.
s.islower()
Returns true if string s contains at least one letter and all letters in the string are lower case. Otherwise it returns false.
s.isupper()
Returns true if string s contains at least one letter and all letters in the string are uppercase. Otherwise it returns false
s.isdigit()
Returns true is string s consists only of digits and contains at least one character. Otherwise it returns false
Elif statement
Short for else, if.. As soon as one on the test conditions succeeds, the statement block is executed No other tests are attempted If none of the test conditions succeed the final else clause is executed
Compile-time errors
Syntax errors Spelling, capitalization, punctuation Ordering of statements, matching parenthesis, quotes, etc....
Programming
The act of designing, implementing, and testing computer programs
Logic errors
The program runs, but produces unintended results The program may ' crash'
How many times is the text "Let's have fun with Python." printed when this code snippet is run? i = 0 while i <= 10 : print("Let's have fun with Python.") i = i + 1 if i % 2 == 0 : i = 10 answers: A. 3 B. 2 C. 1 D. 10
A. 3
<
Less than
<=
Less than or equal
s.isalpha()
Returns true if string s consists of only letters and contains at least one character. Otherwise it returns false
s.isalnum()
Returns true if string s consists of only letters or digits and it contains at least one character, otherwise it returns false.
