CSE Quiz 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

How many times is the following while loop going to check it's condition? i = 0 while i <= 5: print (i+1) i = i + 1

6

what is a library in python?

A library contains modules, a standard one contains the math and the random modules. You can import these into a program.

what is the elif syntax rules?

An if statement must come before the first elif statement elif is followed by a boolean expression and a : Indent four spaces and write the commands for when the elif statement is true You can write as many elif statements as you want An else statement must come after the last elif statement

What are the 3 elements a while loop will have?

An initial condition, usually involving initialization of a loop control variable. a Boolean condition that is tested each time through the loop to determine whether the loop body (the statements indented under the while statement) will be run or not. a statement at the bottom of the loop that will potentially change the loop control variable.

What data type is used by a conditional to know which part of the code should be executed? Boolean Integer Float String

Boolean

What does an if statement not do?

Evaluates code when the boolean expression is false.

What is a function of python?

Group sections of codes into its own segment/unit, this helps with reusability of code, readability of code, and great for testing. You can basically call a section of code by its function name. Modules can contain functions.

Select all of the statements below that are true with regards to if statements. Hint, there is more than one correct answer. If statements will perform a set of actions if the boolean expression is false. If statements provide the most numeric precision of all the conditional statements. If statements only ask if a boolean expression is true. If statements are the simplest of all the conditionals.

If statements only ask if a boolean expression is true. If statements are the simplest of all the conditionals.

What is one way to exit a infinite loop?

If you use the break statement, but in this class you should never use it.

Why is bad to break the loop from inside the body of a loop?

It hides the stopping condition inside the body of the loop. Its important to consider the understandability for a reader rather then shortcuts for the software writer.

How does branching effect the flow of code?

It still executes top to bottom but then reach a decision and then it chooses a code.

Which of the following will cause an infinite loop?

Not updating the loop control variable in the body of the loop.

What are conditionals?

Pieces of code that make a decision about what the program is going to do next. The common conditional is the if statement.

What does the "if else" statement do ?

The if statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed.

What are the requirements of using an else statement?

There is no indentation Use a : at the end Do not use another boolean expression Code after the else must be indented

What is wrong with the code below? if 3 > 4 print("This is true") else print("This is false")

There needs to be an : after the if and else statement. and the print statement needs to be indented.

What do if statements do?

They test to see if a certain condition is true, If it is then specific commands are run. A simple if statement does not do anything if false.

What is the purpose of the random.seed() function

To set up a predictable sequence of pseudo-random numbers.

How do you bring modules like random and math into code?

Typing import math. If you want to use things from the module like sqrt you need to do math.sqrt. If you are only going to use one function you can also do from math import sort and you don't need to do math.sqrt.

What is a loop?

When you repeat a section of code according to certain conditions.

Which of the following type of questions is the most accurate to describe what happens with branching in a computer program? Multiple Choice question Fill in the blank question Yes/No question

Yes/No question

What is an module in python?

a grouping of related pieces of code that are put in one file.

What is the main difference between a loop and a conditional?

a loop repeats the block of code as long as its condition is true while a conditional just executes it once.

What is a definite loop?

a loop that is coded or predetermined to iterate a certain number of times

What is an indefinite loop?

a loop whose number of iterations is unknown; depends on user response or some other event.

Why is an elif statement more efficient then a series of if statements?

a series of elif statements can be more efficient that a series of if statements because the elif statements will stop as soon as there is a true boolean expression. The if statement, however, will keep going even if there is a true boolean expression.

Fill in the blanks below so that the loop prints python ten times. ______ = 0 while count __ 10 print("Python") count = count ___ 1

count, < , +

Which of the following keywords allows you to have more than 2 branches with a single conditional statement?

elif

Which of the following elifstatements is correct?

elif a < 10:

What does the statement need to be a complete if statement? ____num > 100 ____ print("num is greater than 100")

if and :

Construct code that will print out messages based on the weather: "Bring an umbrella!" if it is rainy but not windy "Wear a rain jacket." if it is rainy and windy "You might need a coat." if it is cold but not rainy "Enjoy your day!" if it is not cold and not rainy

if rainy: if windy: print("Wear a rain jacket.") else: print("Bring an umbrella!") else: if cold: print("You might need a coat.") else: print("Enjoy your day!")

Which of the following pieces of code will print a message that it's comfortable if the temperature is between 65 and 75 degrees Fahrenheit, and a message that it is too hot or cold if it's outside that range. 65 andf 75 are included in the "comfortable" range. Assume there is a variable named temp that holds the current temperature.

if temp <= 64 or temp >= 76: print("It is too hot or too cold!") else: print("The temperature is comfortable")

Similarities between loops and conditionals

indented, a colon, a boolean test condition, and a block of code that runs if true.

What type of question doesn't an elif statement ask?

is this true or is this false? it gives you more precision when making decisions.

n the following code, identify the loop control variable, the looping condition, and the update to the loop control. n = int(input("Enter a number: ")) sum = 0 while n > 0: sum = sum + n n = n - 1 print("The sum of the first n numbers is", sum)

n n>0 n = n-1

Which statements will fill in the blanks of the following code so it will print out all the even numbers from 0 to n? n = int(input("Enter a number\n")) i = 0 while i < ________________: print(i) ______________

n + 1 i = i + 2

What do if statements in python must contains?

the keyword if a boolean expression a colon 4 spaces (1 tab) of indentation for all lines of code that will run if the boolean expression is true.

How do programmers use Logic

the way in we resolve a question, these questions to have true or false questions. If something is true then the program will do one thing and if its false it will do another.

What is the keyword for a loop?

while

What is branching?

you want to have set of instructions that execute under one set of conditions and another set of instructions that execute in another set of conditions


Conjuntos de estudio relacionados

RAC Chapter 1 - FDA and related regulatory agencies

View Set

Nonverbal Communication Final Study Guide

View Set

anatomy: chapter 13, peripheral nervous system

View Set

CISSP - Domain 10: Physical (Environmental) Security

View Set