Foundations Of Computer Science Mid Term Exam

Ace your homework & exams now with Quizwiz!

Which statement finds the last letter of the string variable name? last = name[len(name)] last = len(name) - 1 last = len(name) last = name[len(name) - 1]

last = name[len(name) - 1]

Which statement correctly creates a new variable by combining the two string variables: firstName and lastName?

name = firstName + lastName

Suppose that b is False and x is 0. Which of the following expressions evaluates to True? not b and x == 1 b or x == -1 not b or b x == 1 or x == -1

not b or b

What output is generated by the following code snippet? firstName = "Pamela" middleName = "Rose" lastName = "Smith" print(firstName[0], middleName[0], lastName[5])

nothing, this causes an index of bounds error

What type of operator is <= operator?

relational

Which of the following values make the expression not x == y and z > x true? x = 10, y = 10, z = 15 x = 10, y = 20, z = 15 x = 10, y = 2, z = 5 x = 10, y = 20, z = 10

x = 10, y = 20, z = 15

The line of code which reads a value from the user and stores it in a variable named x as a floating-point value is:

x = float(input("Enter the value of x: "))

Assuming that the user provides 95 as input, what is the output of the following code snippet? y = int(input("Please enter a number: ")) if y > 300 : x = y else : x = 0 print("x:", x)

x: 0

Which output format correctly prints an item description left justified with up to 10 letters?

"%-10s"

Which output format string correctly allows for 5 positions before and two digits after the decimal point?

"%8.2f"

Consider the following code segment: title = "Python for Everyone" newTitle = title.replace("e", "*") After this code runs, the value stored in newTitle is

"Python for Ev*ryon*"

What symbol is used to begin a comment in a Python program?

#

Which of the following statements correctly calculates the average of three numbers: num1, num2, and num3?

( num1 + num2 + num3 ) / 3

What extension is used for Python files?

.py

How many copies of the letter B are printed by the following loop? i = 0 while i == 5 : print("B") i = i + 1

0

What is the output of the code snippet given below? i = 0 while i != 11 : print(" ", i) i = i + 2

0 2 4 6 8 ... (infinite loop)

What is the output of the code snippet given below? i = 0 while i != 9 : print(i, end = " ") i = i + 2

0 2 4 6 8 10 12 14 ... (infinite loop)

Consider the following code segment: numPizzas = 1 numPeople = 4 if numPeople == 5 : numPizzas = 2 After this code segment executes, what value is in the variable numPizzas?

1

What is the output of the following code snippet? i = 1 while i < 10 : print(i, end = " ") i = i + 2 if i == 5 : i = 9

1 3 9

How many times does the following loop run? i = 0 j = 1 while j >= 1 : print("" , i , ";" , j) i = j + 1 if i % 2 == 0 : j = j - 1

1 time

Consider the follow code segment. It is supposed to convert numeric marks to letter grades. However, it may contain a bug. Examine the program, and identify what bug, if any, is present. grade = "F" if mark >= 80 : grade = "A" if mark >= 70 : grade = "B" if mark >= 60 : grade = "C" if mark >= 50 : grade = "D"

All instances of if, except the first, need to be replaced with elif

Which of the following variables is used to store a condition that can be either True or False? Logical Boolean Algebraic Conditional

Boolean

What part of the computer carries out arithmetic operations, such as addition, subtraction, multiplication and division?

CPU

What is the output of the following code snippet when the user enters 75 as the grade? 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)

D

What is the output for the following code snippet: area = 25 print("The area is %05d" % area)

The area is 00025

Which of the following statements is true about the if statement? The if statement can have only one condition that evaluates to an integer value. The if block is optional. The else block is optional. The if and else blocks can be aligned to any indentation level

The else block is optional.

The following logical expression will 'short-circuit'... quantity > 0 and price/quantity < 10 When quantity is equal to 0 When quantity is equal to 5 When price/quantity is less than 10 When price/quantity is greater than 10

When quantity is equal to 0

Consider the following code segment: if a == b : print("W") else : print("X") if b == c : print("Y") else : print("Z") If a is 0, b is 1 and c is 0 then the output generated by this code segment is:

X followed by Z on the next line

What is wrong with the following code snippet? print("Hello") print("World!")

The second line should not be indented

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)

The value of sum is 120

Consider the following code segment. It is designed to convert letter grades to grade points. Examine the program, and identify what bug, if any, is present. if letter == "A" : gradePoints = 4.0 elif letter == "B" : gradePoints = 3.0 elif letter == "C" : gradePoints = 2.0 elif letter == "D" : gradePoints = 1.0 else : gradePoints = 0.0

There is nothing wrong with the code segment (it works as intended)

How is a value stored in a variable?

an assignment statement

Which operator has the lowest precedence? != * ** and

and

The following code snippet has an error, how can this be corrected so it prints: 123 Main Street? 1. street = " Main Street" 2. address = 123 + street 3. print(address)

change the value '123' in line 2 to a string using the str function

Which statement correctly saves the price in the variable cost? userInput = input("Please enter the price:")

cost = float(userInput)

A store provides a 10% discount on items with a price of at least $100. Otherwise, no discount is applicable. Which of the following DOES NOT correctly compute the discount amount when the item's price is stored in the price variable? discount = 0 if price >= 100 : discount = 0.10 * price discount = 0.10 * price if price <= 100 : discount = 0 discount = 0 if price >= 100 : discount = price / 10 discount = 10 if price >= 100 : discount = 0.1 * price else : discount = 0

discount = 0.10 * price if price <= 100 : discount = 0

Which expression is equivalent to the expression shown below? 13 < floor - 1 13 >= floor - 1 floor < 12 floor - 1 <= 12

floor - 1 <= 12

The operator >= stands for greater than greater than or equal to not equal to this is not a valid Python operato

greater than or equal to

What letter is displayed by the following code segment? title = "Python for Everyone" print(title[3])

h

What is printed from the following code snippet: message = "ho.." print(message * 3)

ho..ho..ho..

What are the values of i and j after the following code fragment runs? i = 60 j = 50 count = 0 while count < 5 : i = i + i i = i + 1 j = j - 1 j = j - j count = count + 1 print("i =", i, ", j =", j)

i = 1951, j = 0

What are the values of i and j after the following code snippet executes? i = 20 j = 70 count = 0 while count < 5 : i = i + i i = i + 1 j = j - 1 j = j - j count = count + 1 print(i) print(j)

i = 671, j = 0

Which of the following conditions is True only when the variables a, b, and c contain three different values? if a != b and a != c and b != c : if a != b or a != c or b != c : if not (a == b and b == c and a == c) : if a != b != c :

if a != b and a != c and b != c :

Which of the following options checks that city is neither Atlanta or Philadelphia? if not city == "Atlanta" or not city == "Philadelphia" if not (city == "Atlanta" or city == "Philadelphia") if not (city == "Atlanta" and city == "Philadelphia") if not city == "Atlanta" or city == "Philadelphia"

if not (city == "Atlanta" or city == "Philadelphia")

Which of the following expressions represents a legal way of checking whether a value assigned to the num variable is either less than 100 or more than 200? if num < 100 and num > 200 : if num < 100 and num > 200 : if num < 100 or num > 200 : if num <= 100 or num >= 200 :

if num < 100 or num > 200 :

Consider the following code snippet: number = int(input("Enter a number: ")) if number > 30 : ... elif number > 20 : ... elif number > 10 : ... else : ... Assuming that the user input is 40, which block of statements is executed?

if number > 30 : ...

Which of the following if statements has a condition that evaluates to True? if s1 == s2 : if s1 = s2 : if s1 < s2 : if s1 >= s2 :

if s1 < s2 :

Which of the following if statements is problematic because of the limited precision of floating-point numbers? if 4 // 3 == 1 : if sqrt(2) * sqrt(2) == 2.0 : if "10" == 5 : if 4 <= 4

if sqrt(2) * sqrt(2) == 2.0 :

Which of the following is the correct syntax for an if statement? if (x < 10) size = "small"; if (x < 10) size = "small" else (x < 20) size = "medium" if x < 10 : size = "small" else : size = "medium" if x < 10 : size = "small" else size = "medium"

if x < 10 : size = "small" else : size = "medium"

Given two variables x and y, how do you test whether exactly one of them is zero? if x == 0 or y == 0 : if x = 0 or y = 0 : if x == 0 and y != 0 or y == 0 and x != 0 : if x == 0 and y != 0 and y == 0 and x != 0 :

if x == 0 and y != 0 or y == 0 and x != 0 :

Given two variables x and y, how do you test whether at least one of them is zero? if x == 0 or y == 0 : if x = 0 or y = 0 : if x == 0 and y != 0 or y == 0 and x != 0 : if x == 0 and y != 0 and y == 0 and x != 0

if x == 0 or y == 0 :

Suppose that b is False and x is 0. Which of the following expressions evaluates to True? b or x == 1 b and x == 0 not b and x == 1 not b or x == 1

not b or x == 1

Which of the following statements correctly multiplies num1 times num2? num1 * num2 num1 x num2 num1 · num2 num1 ** num2

num1 * num2

The Central Processing Unit is primarily responsible for:

performing program control and data processing.

Which statement causes A and B to be printed on different lines?

print("A\nB")

Given the code snippet below, what code is needed to print the person's initials? firstName = "Pamela" middleName = "Rose" lastName = "Smith"

print(firstName[0], middleName[0], lastName[0])

The message used to tell the user what input is expected is known as a(n):

prompt

What will be the result of running the following code fragment? year = 0 rate = 5 principal = 10000 interest = 0 while year < 10 : interest = (principal * year * rate) / 100 print("Interest ", interest)

The code fragment will continue to display the calculated interest forever because the loop will never end.

The code snippet below is supposed to check whether an integer greater than 1 is a prime number. What will be the result of executing it? j = 2 result = 0 number = int(input("Please enter an integer (2 or greater):")) while j < number : if number % j == 0 : result = 1 j = j + 1 if result == 1 : print("Number:", number, "is Not Prime.") else : print("Number:", number, "is Prime.")

The code snippet displays the desired result

Given the following code snippet: score = 0 price = 100.0 if score > 0 and price < 200 and price / score > 10 : print("buy!") which of the following statements is true? The output is buy! The code snippet runs, but there is no output The code snippet has syntax errors The code snippet causes a divide-by-zero exception

The code snippet runs, but there is no output

What is printed by the following code snippet?print("The answer is", 25 + 84)

The answer is 109

Which of the following items is an example of a floating-point literal? 100000 100,000 100000.0 100,000.0

100000.0

What is printed by the following code snippet? print(25 + 84)

109

What is the value of x after the following code segment? x = len("Hello World!")

12

Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output displayed is:

15

How many times does the code snippet below display "Hello"? i = 0 while i != 15 : print("Hello") i = i + 1

15 times

What is a variable called that should remain unchanged throughout your program?

A constant variable

What is the definition of a nested statement?

A decision statement that is contained inside the statement block of another decision statement

What is the output of the following loop? s = 1 n = 1 while s < 10 * n : s = s + n n = n + 1 print(s)

211

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

3

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)

30

What is the value of i at the end of the following code segment? i = 1 while i < 32 : i = i * 2

32

Assuming a user enters 30, 55, and 10 as the input, 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 and num1 > num3 : print(num1) elif num2 > num1 and num2 > num3 : print(num2) elif num3 > num1 and num3 > num2 : print(num3)

55

What is the value of 4 ** 3?

64

What is returned by the function: sqrt(64)?

8.0

What is the value of the variable num after the following code snippet? num = 5 num2 = 6 num = num2 + 3

9

Which of the following is not an example of a relational operator? = < <= !=

=

What term is used to describe a loop where the number of times that the loop will execute is known before the body of the loop executes for the first time?

Definite

Consider the following code snippet: number = int(input("Enter a number: ")) if number < 10 : print("Too small") elif number < 50 : print("Intermediate") elif number < 100 : print("High") else : print("Too high") Assuming that the user input is 60, what is the output of the this code snippet?

High

In Python, which of the following orderings is used to compare strings? Lexicographic Semantic Alphabetic Syntatic

Lexicographic

Which line in the following program is a comment line? 1: print("Your lucky number is...") 2: lucky = 7 3: # Display the lucky number 4: print(lucky)

Line number 3

Which of the following hardware devices is NOT considered an output device? Microphone Speaker Monitor Printer

Microphone

What is the last line of output produced by the code snippet below? i = 0 total = 0 while total < 0 : i = i + 1 total = total - i print(i, total)

No output

Consider the following code segment: if a > b : print("X") if a == b : print("Y") What is displayed if a is 0 and b is 0?

Nothing

Consider the following code segment: if a > b : print("X") if a == b : print("Y") What is displayed if a is 1 and b is 2?

Nothing

What is the output of the following code snippet if count contains 56? if count % 2 == 0 : print("Count is an even number") else : print("Count is an odd number")

Nothing, there is a syntax error

What error will Python display when it attempts to execute the following if/else statement? if a = b : print("Equal") else : print("Not Equal") if a > b : print("a is larger") else : print("b is larger")

Python will display an error indicating that = is invalid sytax

What is printed by the following code snippet? name = "Robert" formalName = name.upper() print(formalName)

ROBERT

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

Replace while i <= 10 with while i < 10

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 Replace i = 0 with i = 1 Replace while i <= 10 with while i < 10 Replace i = i + 1 with i = i + 2 Replace while i <= 10 with while i + 1< 10

Replace while i <= 10 with while i < 10

Which type of error could be reported by Python when the program contains an "off-by-one" error?

Run-time error

Computers store both data and programs not currently running in:

Secondary storage


Related study sets

CH 17 Pregnancy at Risk: Pregnancy-Related Complications

View Set

InQuizitive SYG2000 Chapter 7. Stratification

View Set

Introducing Lifespan Development

View Set

D13 VoIP Call Processors and Endpoints

View Set

Chapter 2: Theory, Research, and Evidence Based Practice

View Set

GENETICS EXAM 1-4 (COMBINED) UH LIN/DAANE

View Set