Chapter 3 test bank questions



Download 170.85 Kb.
Date18.07.2017
Size170.85 Kb.
#23631

Introduction to Computer Science Using Python: A Computational Problem-Solving Focus

Charles Dierbach, John Wiley and Sons (1st Edition)







CHAPTER 3 TEST BANK QUESTIONS



Section 3.1 What is a Control Structure?
True/False Questions

  1. True or False? A control statement is a statement that determines the control flow of a set of instructions.




  1. True or False? “Control structure” is another name for a control statement.




  1. True or False? Sequential, selection and iterative control are all based on a given condition.




  1. True or False? Most programs are straight-line programs.



Multiple Choice Questions

  1. Control flow refers to

  1. the order that instructions are executed in a program

  2. the speed at which a set of instructions are executed in a program

  3. the changing values of variables in a program



  1. Which of the following provide explicit control flow in a program?

  1. sequential, selection and iterative control statements

  2. selection and iterative control

  3. sequential and selection control



  1. Which of the following is not a fundamental form of control that programming languages provide?

(a) sequential control

(b) iterative control



(c) secondary control

  1. Most computer programs use

(a) sequential and selection control

(b) sequential, selection and iterative control

(c) sequential and iterative control


Matching


  1. Match the following control flow types to their description

(a) sequential control __b__ based on a condition that is checked once

(b) selection control __a__ implicit form of control

(c) iterative control __c__ based on a condition that may be checked many times


Fill-in-the-Blank Questions

  1. The order that instructions are executed in a program is called _____________________.

ANSWER: control flow


  1. Statements that determine the flow of control of a set of instructions are called
    _____________________.


ANSWER: control statement


  1. The three forms of control provided by programming languages are ________________,
    __________, and __________________ control.


ANSWER: sequential, selection, iterative


  1. Program that do not have any form of control flow other than sequential control are called

__________________________.
ANSWER: straight-line programs
Open Response Questions

  1. What is the term for the order that instructions are executed in a program?

ANSWER: Control flow


  1. What is the term for a statement that determines the control flow of a set of instructions?

ANSWER: Control statement


  1. What are the three forms of control flow provided by programming languages?

ANSWER: Sequential, selection and iterative control


  1. Which of the three forms of control flow is implicit in a program?

ANSWER: Sequential control


  1. What is the term for a program that does not have any control flow, other than sequential control?

ANSWER: Straight-line program


  1. Which forms of control flow require the use of a condition?

ANSWER: Selection and iterative control


  1. Which form of control flow repeatedly executes a set of instructions?

ANSWER: Iterative control



Section 3.2 Boolean Expressions
True/False Questions

  1. True or False? The Boolean data type contains two Boolean values?




  1. True or False? A Boolean expression is an expression that evaluates to True or False.




  1. True or False? All Boolean expressions contain at least one Boolean literal (value).




  1. True or False? Boolean literals True and False are considered Boolean expressions.




  1. True or False? All relational operators produce a Boolean result (value).




  1. True or False? The == operator is used for the assignment of Boolean values.




  1. True or False? The != operators is used to determine if two values are not equal to each other.




  1. True or False? The relational operators can be applied to string values.




  1. True or False? The Boolean expression 'Abe' < 'Apple' evaluates to true.




  1. True or False? The Boolean expression 'apple' < 'Banana' evaluates to true.




  1. True or False? The membership operator in of Python can be used to determine if a given

value occurs in a particular tuple, or if a given substring occurs in a given string value.


  1. True or False? In order to determine if a certain value is not in a particular list (or string),

the membership operator not in can be used.


  1. True or False? The operators and, or, not, ==, !=, <, <=, >, >= are called Boolean operators.




  1. True or False? 1 <= num <= 10 is not a valid Boolean expression in most programming
    languages, but is allowed in Python.




  1. True or False? Operator precedence applies to the arithmetic operators and relational operators,

but not to the Boolean operators.


  1. True or False? All Boolean operators are applied before any relational operators.




  1. True or False? Short-circuit evaluation is when a Boolean expression contains the invalid use of Boolean operators.




  1. True or False? not (num == 0) and num != 0 are logically equivalent Boolean expressions, for integer value num.




  1. True or False? not (num != 0) is logically equivalent to num < 0 and num > 0, for integer value num.



  1. True or False? num1 <= num2 is logically equivalent to not (num1 > num2), where num1 and num2
    each contain an integer value.

  2. True or False? not (flag1 and flag2) is logically equivalent to (not flag1) and (not flag2), where flag1 and flag2 are Boolean values.




  1. True or False? not (flag1 or flag2) is logically equivalent to (not flag1) and (not flag2), where flag1 and flag2 are Boolean values.



Multiple Choice Questions

  1. Which of the following is not a Boolean operator?

(a) and


(b) or

(c) not


(d) with


  1. Examine the following statements.

x = 10

x == 10
What will the second line evaluate to?


(a) False

(b) 10


(c) True

(d) None of the Above




  1. Examine the following expression:

10 < 20 and 30 > 50
What does this expression evaluate to?
(a) True

(b) False

(c) True and False

(d) That line of code is not valid in Python and will result in an error


  1. Examine the following expression.

10 in (10, 20, 30)
What will this expression evaluate to?

(a) False

(b) 10

(c) True

(d) None of the Above



Matching

  1. Match the following Boolean expressions to the expressions that they are logically equivalent to.

(a) x < y __c__ not (x != y)

(b) x <= y __e__ (not x) or (not y)

(c) x == y __f__ (not x) and (not y)

(d) x != y __a__ not (x >= y)

(e) not (x and y) __d__ not (x == y)

(f) not (x or y) __b__ not (x > y)

Fill-in-the-Blank Questions


  1. True and False are the two _________ values in Python.


ANSWER: Boolean


  1. A _____________ expression is an expression that evaluates to True or False.


ANSWER: Boolean


  1. The Boolean operators in Python are _____, _____, and _____.


ANSWER: and, or, not


  1. Both the Boolean and _____________ operators evaluate to a Boolean result.


ANSWER: relational



  1. Fill in the table, evaluating the Boolean expressions for x and y with the given values. Some have already been filled in.



x

y




x and y

x or y

True

True




True

True

True

False




False

True

False

True




False

True

False

False




False

False


Open Response Questions

  1. Evaluate the following Python expressions.

(a) 14 <= 14 (b) 5 > 4 or 12 < 8 (c) 6 != 5 (d) 10 not in (10, 20, 30)



(a) True (b) True (c) True (d) False
(e) 5 < 10 and 8 > 20 or 4 < 2 (f) not (12 == 12 and 4 < 6)

(e) False (f) False

  1. Evaluate the following Python expressions for the variable assignments given.

flag1 = True flag2 = False num1 = 10 num2 = 25


(a) num1 > num2 and flag1 (b) num1 == 12 or num1 == num2 or flag1

(a) False (b) True
(c) num1 + 10 < num2 and flag2 (d) not flag1 or not flag2 and num2 < num1

(c) False (d) False

  1. For each of the following Boolean expressions, indicate the value of x that would make each expression false.

(a) x and True (b) x or False (c) not (x or False) (d) not (x and True)



(a) x = False (b) x = False (c) x = True (d) x = True


  1. Give an appropriate expression for each of the following.

(a) To determine if the number 25 appears in a list of numbers called nums.

(b) To determine if the numbers 10 and 20 do not appear in a list of numbers named nums.

(c) To determine if the name ‘Charles’ appears in a list called names.
(a) 25 in nums

(b) 10 not in nums and 20 not in nums

(c) 'Charles' in nums


  1. Give a logically equivalent expression for each of the following.

(a) num1 > 0 and num2 > 0

(b) num1 <= 0 or num2 <= 0

(c) not (num1 == 0 and num2 == 0)
(a) not (num1 <= 0 or num2 <= 0)

(b) not (num1 > 0 and num2 > 0)

(c) num1 != 0 or num2 != 0



Section 3.3 Selection Control
True/False Questions

  1. True or False? The if statement in Python provides selection control.




  1. True or False? If statements always make use of a Boolean expression.




  1. True or False? All if statements must include an else header.




  1. True or False? if Statements may contain as many else headers as necessary.




  1. True or False? In Python, indentation is not significant. It is simply a strict convention used for readability and considered good programming etiquette.




  1. True or False? The elif header provides a convenient means of using a single if statement for multiway selection that can always be implemented instead by use of nested if statements.



Multiple Choice Questions

  1. Which form of control does the if statement in Python provide?

(a) sequential

(b) selection

(c) iteration




  1. Which of the following can the condition of an if statement be based on?

(a) Boolean variable

(b) relational expression

(c) Boolean expression

(d) All of the above


  1. A header in Python is

(a) a specific keyword followed by a colon

(b) any line containing a condition (Boolean expression)

(c) the first line of any control structure


  1. A suite in Python is

(a) a collection of control structures

(b) a set of statements following a header

(c) an if statement providing multiway selection




  1. Multiway selection in Python can be implemented as

(a) a single if statement with else header

(b) nested if statements



(c) both of the above



  1. In the following lines of code, which line is the header?

if which == 'F': # line 1

converted_temp = (temp – 32) * 5 / 9 # line 2

print(temp, 'degress Fahrenheit equals', # line 3

converted_temp, 'degrees Celcius') # line 4



  1. Line 1 (c) Line 3

  2. Line 2 (d) Line 4




  1. The following code is an example of

if grade >= 90:

print('Grade of A')

else:

if grade >= 80:



print('Grade of B')

else:


if grade >= 70:

print('Grade of C')

else:

if grade >= 60:



print('Grade of D')

else:


print('Grade of F')


  1. Cascading if Statements

  2. Nested if Statements

  3. Relational if Statements

  4. Invalid Syntax


Fill-in-the-Blank Questions

  1. The if statement in Python provides a means of __________ control.

ANSWER: selection


  1. All if statements must include the use of a _____________ expression.

ANSWER: Boolean


  1. An if statement may include the use of both the ________ header and the _________ header.

ANSWER: else / elif


  1. In Python, __________________ is used to denote the nesting of statements.

ANSWER: indentation


  1. In order to construct an if statement that performs multiway selection between three or more sets
    of instructions (suites), the _______ header must be used.

ANSWER: elif


  1. Multiway selection in Python can be constructed either by the use of a single if statement (with
    the use of multiple headers), or by the construction of a set of __________ if statements.

ANSWER: nested

Open Response Questions

  1. For a variable named num with an integer value, give an if statement that displays “valid value”
    only if the value of num is between 1 and 100, inclusive.

ANSWER:

if num >= 1 and num <= 100:

print(’valid value’)


  1. For a variable named with integer value, give an if statement that displays “valid value” if the value of num is between 1 and 100, inclusive, and displays “invalid value” otherwise.

ANSWER:

if num >= 1 and num <= 100:

print('valid value')

else:

print('invalid value')


  1. Rewrite the following program code with proper indentation.

if num > 100:

print('invalid value')

else:

if num > 50:



print('high value')

else:


print('low value')

ANSWER:

if num > 100:

print('invalid value')

else:

if num > 50:

print('high value')

else:

print('low value')


  1. For variable current_month (equal to ‘January’ or ‘February’ ot ‘March’, etc.) write an if statement that assigns variable fall_season to True if the value of current_month is either ‘September‘, ‘October‘ or ‘November‘; otherwise, fall_season should have the value False.

ANSWER:

if current_month == 'September' or current_month == 'October' or \

current_month == 'November':

fall_season = True

else:

fall_season = False


  1. Construct multiway selection by use of nested if statements, based on variable n with an integer value in the range 1-100, so that if n is less than or equal to 30, “low value” is displayed; if n is greater than 30 and less than or equal to 60, “median value” is displayed; otherwise, “high value” is displayed.


ANSWER:

if num <= 30:

print('low value')

else:

if num <= 60:

print('median value')

else:

print('high value')


  1. Write a simple Python program by use of a single if statement with elif headers that prompts the user to enter the name of a month (‘January’, ‘February’, etc.) and sets each of the variables fall_season, winter_season, spring_season, summer_season to the appropriate Boolean value. For example, if the user enters ‘January’, variable winter_season should be set to True, and the rest of the variables set to False. (Consider the months January, February and March as winter; April, May and June as spring; etc.


ANSWER:

month = input('Enter month: ')

fall_season = False

winter_season = False

spring_season = False

summer_season = False
if month == 'January' or month == 'February' or month == 'March':

winter_season = True

elif month == 'April' or month == 'May' or month == 'June':

spring_season = True

elif month == 'July' or month == 'August' or month == 'September':

summer_season = True

else:

fall_season = True


  1. Construct multiway based on the value of variable n with an integer value in the range 1-100, so that if n is less than or equal to 25, “very low value” is displayed; if n is greater than 25 and less than or equal to 50, “low value” is displayed; if n is greater than 50 and less than or equal to 75, “median value” is displayed; otherwise, “high value” is displayed.


ANSWER:

if num <= 25:

print('very low value')

else:

if num <= 50:

print('low value')

else:

if num <= 75:

print('median value')

else:

print('high value')


  1. For variable sunny assigned a Boolean value, and variable temperature assigned an integer value, write Python code that displays “Swimming Day” if it is a sunny day and the temperature is at least 80 degrees, or if it is a cloudy day and the temperature is at least 84 degrees; otherwise, “Not a Swimming Day” should be displayed.


ANSWER:

if sunny and temperature >= 80 or temperature >= 84:

print('Swimming Day')

else:

print('Not a Swimming Day')


  1. Write a simple Python program that reads in three integer values from the user, and displays “Numbers Unique” if each of the values are different, otherwise displays “Duplicate Values Found.”


ANSWER:

num1 = int(input('Enter first number: '))

num2 = int(input('Enter second number: '))

num3 = int(input('Enter thrid number: '))
if (num1 != num2 and num2 != num3 and num1 != num3):

print('Numbers Unique')

else:

print('Duplicate Values Found')


  1. Write a simple Python program that prompts the user for a given number of pennies, and displays the
    monetary value of the coins such that “xx cents” is displayed if the total number of pennies is less than 100, “x dollars and xx cents” is displayed if the number of pennies add up to dollars and cents, and “x dollars” is displayed if the pennies add up to whole dollars, with no leftover cents.


ANSWER:

num_pennies = int(input('Enter the number of pennies: '))
if num_pennies < 100:

print(num_pennies, 'cents')

elif num_pennies % 100 == 0:

print(num_pennies // 100, 'dollars')

else:

print(num_pennies // 100, 'dollars and', num_pennies % 100, 'cents')


  1. Write simple Python program that determines a person’s age (in years) by prompting for the current month, day and year and the month, day and year that they were born. Design the program so that
    all values are entered as integer values (i.e., integer values for months). The program must compare the month and day information (in addition to the years) to determine if a person’s birthday has yet passed for the current year. (If the current day is their birthday, then consider them a year older than they
    would have been the day before.)


ANSWER:

current_month = int(input('Enter the current month (1 - January, etc.): '))

current_day = int(input('Enter the current day of the month: '))

current_year = int(input('Enter the current year: '))
birth_month = int(input('Enter the month you were born: '))

birth_day = int(input('Enter the day of the month you were born '))

birth_year = int(input('Enter the year you were born: '))
if (month_born < current_month) or (month_born == current_month and \

day_born <= current_day):

print('You are', current_year - birth_year, 'years old')

else:

print('You are', current_year - birth_year - 1, 'years old')



Section 3.4 Iterative Control
True/False Questions

  1. True or False? The while statement in Python provides iterative control.




  1. True or False? While statements always make use of a Boolean expression.




  1. True or False? All iterative control needed in a program can be achieved by use of the while statement.




  1. True or False? As long as the condition of a while statements is true, the statements within the loop (its suite) are repeatedly executed.




  1. True or False? The statements in a while loop are always executed at least once.




  1. True or False? While loops are well suited for input error checking.




  1. True or False? Infinite loops are the result of user input errors.




  1. True of False? A definite loop is a loop that always executes at least once, whereas an indefinite loop
    is a loop that may or not execute at all.




  1. True of False? A boolean flag is a variable with either a True or False value indicating whether a
    given event has occurred or not.


Multiple Choice Questions

  1. Which form of control does the while statement in Python provide?

(a) sequential

(b) selection



(c) iteration


  1. Which of the following can the condition of a while statement be based on?

(a) Boolean variable

(b) relational expression

(c) Boolean expression

(d) All of the above


  1. Examine the following lines of code:

current = 1

sum = 0


n = 5

while current <= n:

sum = sum + current

current = current + 1


What is the value of sum after the loop completes all of its iterations?

  1. 5

  2. 1

  3. 15

  4. The value of sum will never be available, as this is an infinite loop

  1. Examine the following lines of code:

current = 1

sum = 0


n = 5

while current <= n:

sum = sum + current
What is the value of sum after the loop completes all of its iterations?


  1. 5

  2. 1

  3. 15

  4. The value of sum will never be available, as this is an infinite loop




  1. Examine the following lines of code:

while True:

print('Hello World!')


This is an example of


  1. Syntax error

  2. Semantics error

  3. An infinite loop

  4. Input error checking




  1. Examine the following lines of code:

current = 1

sum = 0


n = 5

while current <= n:

sum = sum + current

current = current + 1


This is an example of:


  1. A definite loop

  2. An indefinite loop

  3. An infinite loop

  4. Input error checking




  1. Examine the following lines of code:

which = input('Please enter 'F' or 'C')

while which != 'F' and which != ‘C’:



which = input('Please enter 'F' or 'C')
This is an example of:


  1. A definite loop

  2. An indefinite loop

  3. Correct syntax

  4. Input error checking

Fill-in-the-Blank Questions

  1. Python built-in functions _____ and _____ can be used to convert a string type to a numeric type.

ANSWERS: int() and float()
Open Response Questions

  1. Give a while loop that adds up all the even numbers between 2 and 200, inclusive, and displays the result.

ANSWER:

num = 2

total = 0

while num <= 200:

total = total + num

num = num + 2

print(total)


  1. Give Python code that prompts the user to enter a value between 1 and 5, displaying an error
    message if an invalid value is enterted and prompting the user to reenter.

ANSWER:

value = int(input('Enter a value between 1 and 5: '))

while value < 1 or value > 5:

print('INVALID - Please Reenter')

value = int(input('Enter a value between 1 and 5: '))


  1. Give Python code that prompts the user to enter positive integer values, and continues to prompt
    the user until a value of -1 is entered, adding up all the value entered and displaying the result.

    ANSWER:

total = 0
value = int(input('Enter a positive integer value (-1 to quit): '))

while value != -1:

total = total + value

value = int(input('Enter a positive integer value (-1 to quit): '))

print('The total of the entered values is', total)


  1. Give Python code that prompts the user to enter positive integer values, and continues to prompt
    the user until a value of -1 is entered, displaying whether there are more even numbers entered,
    more odd numbers entered, or the same number of even and odd numbers.


ANSWER:

num_even = 0

num_odd = 0
num = int(input('Enter an integer value (-1 to quit): '))
while num != -1:

if num % 2 == 0:

num_even = num_even + 1

else:

num_odd = num_odd + 1
num = int(input('Enter next integer value (-1 to quit): '))
if num_even > num_odd:

print('More even than odd numbers entered')

elif num_odd > num_even:

print('More odd than even numbers entered')

else:

print('Same number of odd and even numbers entered')


  1. Give Python code that prompts the user to enter positive integer values, and continues to prompt
    the user until the Enter key is hit, adding up all the values entered and displaying the result.

    ANSWER:

empty_str = ''

total = 0
value = input('Enter a positive integer value (Hit Enter to quit): ')

while value != empty_str:

total = total + int(value)

value = input('Enter a positive integer value (Hit Enter to quit): ')

print('The total of the entered values is', total)


  1. Write a simple Python program that prompts the user for a letter (in lower case) to search for, and then prompts the user to enter a series of words (using all lower case letters), one by one, until the Enter key is hit. The program should display how many of the words entered contained the search letter.


ANSWER:

empty_str = ''

total = 0
search_letter = input('Enter a letter (in lower case) to search for: ')
print('Enter the words, one per line (Hit Enter when done) ')

word = input(': ')
while word != empty_str:

if search_letter in word:

total = total + 1

word = input(': ')

print(total, 'of the entered words contain the letter', search_letter)



Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons




Download 170.85 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page