Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 03 – An Introduction to Programming using Python Schneider


 

Download  file with the answers

If you are not a member register here to download this file 


 

Chapter 3

1. Which function returns the single-character string of the character with ASCII value n for nonnegative numbers?
a. chr(n) 
b. ascii(n)
c. ord(n)
d. string(n)

2. Given x = 5, y = 7 and z = “kitten” what does the following condition evaluate to?
(x + y) / 2 == len(z)
a. True 
b. False
c. it cannot be determined because integers are being mixes with strings
d. it cases a Traceback error

3. When comparing two lists of different lengths, if all items pairs match up to the end of the shorter sequence, which sequence is said to be the lesser of the two?
a. the shorter sequence 
b. the longer sequence
c. they cannot be compared if they are different lengths
d. relational operators cannot be used with lists

4. Conditions that use the logical operators are called ____________ conditions.
a. compound 
b. logical
c. relational
d. all of the above

5. When Python stops evaluating a compound condition with the logical and operator because a condition evaluates to False, it is called ____________evaluation.
a. short-circuit 
b. pre-
c. compound
d. first

6. When an if-else statement contains other if-else statements, it is said to be ____________.
a. nested 
b. contained
c. entwined
d. entangled

7. When an if-else statement needs to allow for more than two possible alternatives, you use a(n) ____________ clause.
a. elif 
b. nested
c. fi
d. else

8. An if-elif-else statement can contain ____________ clauses.
a. any number of 
b. 1
c. 2
d. 10

9. When an if-elif-else statement is evaluated, which block of statements is evaluated?
a. the first condition satisfied 
b. the last condition satisfied if more than one is possible
c. every condition that is satisfied
d. the first condition satisfied and each condition after that one to the end

10. When an empty string, list, or tuple is used as a condition, what does it evaluate to?
a. False 
b. True
c. nothing
d. it creates a Traceback error

11. A part of a program that executes a block of code repeatedly is called a(n) ____________.
a. loop 
b. repeater
c. elif
d. circle

12. What Python loop repeatedly executes a block of statements as long as a certain condition is met?
a. while 
b. for
c. do
d. repeater

13. In a while loop, the line beginning with while is called a(n) ____________.
a. header 
b. intro
c. precursor
d. starter

14. Each execution of the body of a loop is called a(n) ____________.
a. pass 
b. repetition
c. circle
d. job

15. Using a while loop to ensure a proper response is received from a request is called ____________.
a. input validation 
b. verification
c. input identification
d. user validation

16. The ____________ statement causes an exit from anywhere in the body of a loop.
a. break 
b. elif
c. pass
d. continue

17. The ____________ statement causes the current iteration of the body of a loop to terminate and execution returns to the loop’s header.
a. continue 
b. break
c. pass
d. elif

18. A ____________ is a Boolean-valued variable used to report whether a certain circumstance has occurred.
a. flag 
b. pass
c. break
d. check

19. A(n) ____________ loop is one that never ends.
a. infinite 
b. unterminated
c. boundless
d. executing

20. The ____________ loop is used to iterate through a sequence of values.
a. for 
b. while
c. iterative
d. range

21. When one loop is contained in the body of another loop, it is said to be ____________.
a. nested 
b. entangled
c. surrounded
d. blocked

22. The ____________ statement is a do-nothing statement.
a. pass 
b. void
c. break
d. continue

23. The parentheses of the range function can contain ____________ values.
a. one
b. two
c. three
d. all of the above 

True/False (20)
1. “happy” < “sad” Answer: true 2. “happiness” > “happy”
Answer: false
3. The relational operators cannot be applied to lists or tuples.
Answer: false
4. In order for two tuples to be equal, they must have the same length and corresponding items must have the same value.
Answer: true
5. isinstance(55.3, int)
Answer: false
6. isinstance(“5”, int)
Answer: false
7. De Morgan’s law can be applied from left to right or from right to left.
Answer: true
8. not((x < 0) and word.isalpha())
is the same as
not(x < 0) and not(word.isalpha())
Answer: false
9. not((x < 0) or word.isalpha())
is the same as
not(x < 0) and not(word.isalpha()) Answer: true 10. When indenting a block of statements for an if-else statement, as long as the statements are indented to the right enough to visually see, the number of spaces for each line does not have to be exactly the same. Answer: false 11. The words “if” and “else” are reserved words. Answer: true 12. The else part of an if statement cannot be omitted. Answer: false 13. Every object in Python has a truth value associated with it. Answer: true 14. When numbers are used as conditions, 0 evaluates to False. Answer: true 15. You should never use elif when test conditions are mutually exclusive. Answer: false 16. The body of a while loop will be continually executed until the continuation condition evaluates to True. Answer: false 17. When the break statement is executed, the loop terminates after the body has finished executing. Answer: false 18. Break statements usually appear in the bodies of if-statements Answer: true 19. Continue statements cannot be used with if-statements. Answer: false 20. The function range(0, n) can be abbreviated to range(n). Answer: true Short Answer (15) 1. How can short-circuit evaluation improve performance? Answer: Once it has been determined that a compound condition is true or false regardless of the remaining conditions, evaluating those conditions uses unnecessary time, and more so if the condition is complex and time-consuming. 2. What does the following code output? x = 10 y = 31 word = “python” print ((x > y) and (((y % 2) == 1) or (word < “boa”)))
Answer: False
3. What does the following code output?
x = 10
y = 31
word = “python”
print (((x < y) and ((y % 2) == 1)) or (word < “boa”)) Answer: True 4. Simplify the following condition using a list. (color == “red”) or (color == “blue”) or (color == “yellow”) Answer: color in [“red”, “blue”, “yellow”] 5. Simplify the following condition. (n >= 0) and (n < 100)
Answer: 0 <= n < 100 6. Why can’t you use the sort method in an assignment statement? Ex., inventory = items.sort() Answer: You cannot use it in an assignment statement because the method does not return a value to be assigned. It simply reorders the items in the list in place. 7. What is the difference between = and ==? Answer: The first, =, is an assignment operator that places a value in memory. The second, ==, is a relational operator that checks for equality. 8. Condense the following statement. if num > 0 == True:
x = 100
Answer: if num > 0
x = 100
9. Write code for the following flowchart.

Answer:
number = eval(input(“Enter an odd number: “))
while (number % 2 == 1):
print(“odd”)
number = eval(input(“Enter an odd number: “))

10. What is the output of the following program?
count = 5
while (count > 0):
print(“Woot!”)
count -= 1
Answer:
Woot!
Woot!
Woot!
Woot!
Woot!
11. What sequence is generated by range(6) ?
Answer: 0, 1, 2, 3, 4, 5
12. What sequence is generated by range(21, 26)?
Answer: 21, 22, 23, 24, 25
13. What sequence is generated by range(31, 44, 3)?
Answer: 31, 34, 37, 40, 43
14. What sequence is generated by range(15, 5, -3)?
Answer: 15, 12, 9, 6
15. Write a for loop that prints “Hello World!” 100 times.
Answer:
for i in range(100):
print(“Hello World!”)

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!