Quiz 2

Ace your homework & exams now with Quizwiz!

In the code of Part (a), even if you do not indent the else suite's second print statement, it's still in the else's suite. So, the code runs correctly.

Which of the following statements a), b) or c) is false? A. The following code shows two statements in the else suite of an if...else statement: grade = 49 if grade >= 60: print('Passed') else: print('Failed') print('You must take this course again') B. In the code of Part (a), even if you do not indent the else suite's second print statement, it's still in the else's suite. So, the code runs correctly. C. In the code in Part (a), grade is less than 60, so both statements in the else's suite execute. D. All of the above statements are true.

0 1 2 3 4 5 6 7 8 9

What does the following for statement print? for counter in range(10): ​ print(counter, end=' ') A. It doesn't run because it's syntactically incorrect. B. 1 2 3 4 5 6 7 8 9 C. 0 1 2 3 4 5 6 7 8 9 D. 0 1 2 3 4 5 6 7 8 9 10

10, 20, 30

What does the following line of code display? print(10, 20, 30, sep=', ') A. 102030 B. 10, 20, 30 C. 10,20,30 D. 10 20 30

All of the above statements are true.

Which of the following statements about the IPython session below is true? In [1]: gender = 'Female' In [2]: age = 70 In [3]: if gender == 'Female' and age >= 65: ​ print('Senior female') ​ Senior female A. The combined condition can be made clearer by adding redundant (unnecessary) parentheses (gender == 'Female') and (age >= 65) B. The right side of the and operator evaluates only if the left side is True. C. The session defines two variables, then tests a condition that's True if and only if both simple conditions are True-if either (or both) of the simple conditions is False, the entire and expression is False. D. All of the above statements are true.

The following session creates a list called grades, then uses the built-in sum and len functions to calculate the median "by hand"-sum calculates the total of the grades (397) and len returns the number of grades (5): In [1]: grades = [85, 93, 45, 89, 85] In [2]: sum(grades) / len(grades) Out[2]: 79.4

Which of the following statements is false? A. Like functions min and max, sum and len are both examples of functional-style programming reductions-they reduce a collection of values to a single value. B. The descriptive statistics mean, median and mode are measures of central tendency-each is a way of producing a single value that is in some sense typical of the others. C. The following session creates a list called grades, then uses the built-in sum and len functions to calculate the median "by hand"-sum calculates the total of the grades (397) and len returns the number of grades (5): In [1]: grades = [85, 93, 45, 89, 85] In [2]: sum(grades) / len(grades) Out[2]: 79.4 D. The Python Standard Library's statistics module provides functions for calculating the mean, median and mode-these, too, are reductions.

The following code snippet produces the sequence 0 1 2 3 4 5 5 6 7 8 9: for number in range(10): ​ if number == 5: ​ continue ​ print(number, end=' ')

Which of the following statements is false? A. The following code snippet produces the sequence 0 1 2 3 4 5 5 6 7 8 9: for number in range(10): ​ if number == 5: ​ continue ​ print(number, end=' ') B. The while and for statements each have an optional else clause that executes only if the loop terminates normally. C. The following snippet produces the integer sequence 0 1 2 3 4 5 6 7 8 9: for number in range(100): if number == 10: break ​ print(number, end=' ') D. Executing a break statement in a while or for immediately exits that statement.

The following snippet produces the sequence 10 8 6 4 2 0. for number in range(10, 0, -2): ​print(number, end=' ')

Which of the following statements is false? A. The following snippet produces the sequence 10 8 6 4 2 0. for number in range(10, 0, -2): ​print(number, end=' ') B. Function range's one-argument version produces a sequence of consecutive integers from 0 up to, but not including, the argument's value. C. The following snippet produces the sequence 0 2 4 6 8. for number in range(0, 10, 2): ​print(number, end=' ') D. The following snippet produces the sequence 5 6 7 8 9. for number in range(5, 10): ​print(number, end=' ')

The if...elif...else statement is called a double-selection statement because it selects one of two different actions (or groups of actions).

Which of the following statements is false? A. The if...else statement performs an action if a condition is True or performs a different action if the condition is False. B. Python provides three types of selection statements that execute code based on a condition-any expression that evaluates to either True or False. C. The if...elif...else statement is called a double-selection statement because it selects one of two different actions (or groups of actions). D. Anywhere a single action can be placed, a group of actions can be placed.

The if statement is nested in the for statement.

With regard to the following code segment:# process 10 studentsfor student in range(10): ​ # get one exam result ​​ result = int(input('Enter result (1=pass, 2=fail): ')) ​​ ​ if result == 1: ​ ​ ​ ​passes = passes + 1 ​​ ​ else: ​​ ​ ​ failures = failures + 1 which of the following statements is true? A. The for statement is nested in the if statement. B. The if statement is nested in the for statement. C. The if statement follows the for statement in sequence. D. None of the above.

The following code sets product to the first power of 2 larger than 64: product = 2 while product < 64: product = product * 2

Which of the following statements is false? A. Something in a while statement's suite must ensure that the condition eventually becomes False. Otherwise, a logic error called an infinite loop occurs. B. The following code sets product to the first power of 2 larger than 64: product = 2 while product < 64: product = product * 2 C. In applications executed from a Terminal, Command Prompt or shell, type Ctrl+c or control + c (depending on your keyboard) to terminate an infinite loop. D. The while statement allows you to repeat one or more actions while a condition remains True.


Related study sets

Chapter 8 study set: Planning -Nursing Process

View Set

ECO 202 Exam One Multiple-Choice NVCC

View Set

B & G Chptr 4. Equipment for General use.

View Set

Epidemiology Measures of Morbidity and Mortality

View Set

Chapter 7: Thinking and Intelligence

View Set

Combo AP Biology Final Exam Review Study Guide

View Set

health aseessment random questions review

View Set