CS115: Chapter 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

1. Suppose x is 1, what is x after x += 1? 2. Suppose x is 1, what is x after x -= 1? 3. Use an augmented assignment operator to write a statement to add 5 into variable x.

1. 2 2. 0 3. x+=5

What will be displayed by the following code? x, y = 1, 2 x, y = y, x print(x, y)

2 1

What will be displayed by the following code? x = 1 x = 2 * x + 1 print(x)

3

42 / 5 42 // 5 42 % 5 40 % 5 1 % 2 2 % 1 45 + 4 * 4 - 2 45 + 43 % 5 * (23 * 3 % 2) 5 ** 2 5.1 ** 2

42 / 5 = 8.4 42 // 5 = 8 42 % 5 = 2 40 % 5 = 0 1 % 2 = 1 2 % 1 = 0 45 + 4 * 4 - 2 = 59 45 + 43 % 5 * (23 * 3 % 2) = 48 5 ** 2 = 25 5.1 ** 2 = 26.0099

An identifier cannot be a keyword?

True

Which of the following identifiers are valid? Which are Python keywords (see Appendix A)? miles, Test, a+b, b-a, 4#R, $4, #44, apps if, elif, x, y, radius

Valid identifiers: miles, Test, apps, x, y, radius Invalid identifiers: a+b, b-a, 4#R, $4, #44, if, elif Keywords: if, elif

You can place the line continuation symbol __ at the end of a line to tell the interpreter that the statement is continued on the next line.

\

How do you break a long statement into multiple lines?

\ To break a long line, place the line continuation symbol () at the end of a line to tell the interpreter that the statement is continued on the next line.

* *

^ (exponent)

Assume that a = 1, and that each expression is independent. What are the results of the following expressions? a += 4 a -= 4 a *= 4 a /= 4 a //= 4 a %= 4 a = 56 * a + 6

a += 4 (a is 5) a -= 4 (a is -3) a *= 4 (a is 4) a /= 4 (a is 0.25) a //= 4 (a is 0) a %= 4 (a is 1) a = 56 * a + 6 (a is 62)

What is wrong in the following statement? 2 = a

a = 2

Assume that a = 1 and b = 2, what is a and b after the following statement? a, b = b, a

a becomes 2 and b becomes 1

Analyze the following code? interestRate = 0.05 interest = interestrate * 45

interestrate is not defined.

Suppose m and r are integers. Write a Python expression for mr 2 . What is 2 * 2 ** 3?

m * r * r or m * r ** 2 2 * 2 ** 3 is 16.

Write a statement that assigns integer value 45 to a variable max.

max = 45

If today is Tuesday, what day of the week will it be in 100 days?

(2 + 100) % 7 = 4. So it is Thursday

25 % 1 is _____

0

what is the result of 45 / 4?

11.25

What is the result of evaluating 2 + 2 ** 3 / 2?

6.0

What is the value of i printed? j = i = 1 i += j + j * 5 print("What is i?", i)

7

2 ** 3 evaluates to __________.

8

Show the result of the following code: print(2 * (5 // 2 + 5 // 2)) print(2 * 5 // 2 + 2 * 5 // 2) print(2 * (5 // 2)) print(2 * 5 // 2)

8 10 4 5

_______ is the code in natural language mixed with some program code.

Pseudocode

What is the UNIX epoch?

The UNIX epoch is the time 00:00:00 on January 1, 1970 GMT.

How do you obtain the seconds from the returned value for time.time()?

To obtain seconds from the return value of time.time(), use int(time.time()).

An identifier can contain digits, but cannot start with a digit?

True

What is the naming convention for variables?

Use lowercase for variables. If a name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each subsequent word-for example, the variables radius, area, and numberOfStudents.

Variable: is named with lowercase for the first word and the first letterin each subsequentword in capital. Constant: is named with all uppercase letters by convention. The words are concatenatedusing underscores.

Variable: is named with lowercase for the first word and the first letterin each subsequentword in capital. Constant: is named with all uppercase letters by convention. The words are concatenatedusing underscores.

If a number is too large to be stored in memory, it _____________.

causes overflow

/

decimal

What function do you use to read a string?

input("Enter a string")

//

integer

The time.time() returns ________________ .

the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

What is x, y, and z after the following statement? x = y = z = 0

x, y, and z become 0

What is the result of 45 // 4?

11

What is the result of 25 / 4? How would you rewrite the expression if you wished the result to be an integer number?

25/4= 6.25 If you want the quotient to be an integer, use 25 // 4.

24 % 5 is _____

4

Which of the following functions returns 4.

round(3.9)

Write a statement that assigns float value 4.5 to a variable x.

x = 4.5

hat is x after the following statements? x = 1 x *= x + 1

x is 2

What is x after the following statements? x = 2 y = 1 x *= y + 1

x is 4

What will be displayed by the following code? x = 1 x = x + 2.5 print(x)

3.5

Which of the following is equivalent to 0.025?

A.0.25E-1 B. 2.5e-2 C. 0.0025E1 D. 0.00025E2 E. 0.0025E+1

Algorithm: describes how a problem is solved by listing the actions that need to be taken andthe order of their executions. Pseudocode: describes algorithms using natural language mixed with code. Variable: represents a value stored in the computer's memory. Data Type: is the type of the value stored in a variable.

Algorithm: describes how a problem is solved by listing the actions that need to be taken andthe order of their executions. Pseudocode: describes algorithms using natural language mixed with code. Variable: represents a value stored in the computer's memory. Data Type: is the type of the value stored in a variable.

What happens if the user enters 5a when executing the following code? radius = float(input("Enter a radius: "))

It will cause an error. You have to enter a numeric value.

Assignment statement: is a statement for assigning a value to a variable. Assignment operator: is the equal sign (=). Expression: represents a computation involving values, variables, and operators that, taken together, evaluate to a value. Scope of variable: is the part of the program where the variable can be referenced.

Assignment statement: is a statement for assigning a value to a variable. Assignment operator: is the equal sign (=). Expression: represents a computation involving values, variables, and operators that, taken together, evaluate to a value. Scope of variable: is the part of the program where the variable can be referenced.

Which of the following is a valid identifier? A. $343 B. mile C. 9X D. 8+9 E. max_radius

B and E

Which of the following expression results in a value 1? A. 2 % 1 B. 15 % 4 C. 25 % 5 D. 37 % 6

D 2 % 1 is 0, 15 % 4 is 3, 25 % 5 is 0, and 37 % 6 is 1

Line continuation symbol: tells the interpreter that the statement is continued on the next line. IPO: stands for Input, Process, and Output, which are the typical three steps for most simple programs in the text.

Line continuation symbol: tells the interpreter that the statement is continued on the next line. IPO: stands for Input, Process, and Output, which are the typical three steps for most simple programs in the text.

Overflow: is caused when the result of an expression is too large to be stored. Underflow: is caused by a floating-point number that is too small to be stored.The number is approximated to zero. The / operator: performs a float division that results in a floating-point number. The // operator: performs an integer division; the result is the quotient, and any fractional part is truncated. The % operator: yields the remainder after division. The ** operator: is to compute an expression with an exponent.

Overflow: is caused when the result of an expression is too large to be stored. Underflow: is caused by a floating-point number that is too small to be stored.The number is approximated to zero. The / operator: performs a float division that results in a floating-point number. The // operator: performs an integer division; the result is the quotient, and any fractional part is truncated. The % operator: yields the remainder after division. The ** operator: is to compute an expression with an exponent.

Requirement Specification: is a formal process that seeks to understand the problem that the software will address and to document in detail what the software system needs to do. Analysis: seeks to analyze the data flow and to identify the system's input and output. Design: is to design a process for obtaining the output from the input. Implementation: involves translating the system design into programs. Testing: ensures that the code meets the requirements specification and weeds out bugs. Deployment: makes the software available for use. Maintenance: is concerned with updating and improving the product.

Requirement Specification: is a formal process that seeks to understand the problem that the software will address and to document in detail what the software system needs to do. Analysis: seeks to analyze the data flow and to identify the system's input and output. Design: is to design a process for obtaining the output from the input. Implementation: involves translating the system design into programs. Testing: ensures that the code meets the requirements specification and weeds out bugs. Deployment: makes the software available for use. Maintenance: is concerned with updating and improving the product.

What does a conversion from a float to an integer do with the fractional part of the float value? Does the int(value) function change the variable value?

The fractional part is truncated. Calling int(value) does not change variable value.

Translate the following algorithm into Python code: Step 1: Use a variable named miles with initial value 100. Step 2: Multiply miles by 1.609 and assign it to a variable named kilometers. Step 3: Display the value of kilometers. What is kilometers after Step 3?

miles = 100 kilometers = 1.609 * miles print(kilometers) The value of kilometers is 160.9.

number1 = float(input("Enter number1: ")) number2 = float(input("Enter number2: ")) number3 = float(input("Enter number3: ")) average = (number1 + number2 + number3) / 3 print("The average of", number1, number2, number3, "is", average)

number1 = float(input("Enter number1: ")) number2 = float(input("Enter number2: ")) number3 = float(input("Enter number3: ")) average = (number1 + number2 + number3) / 3 print("The average of", number1, number2, number3, "is", average)

Are the following statements correct? If so, show their printout. value = 4.6 print(int(value)) print(round(value))

print(int(value)) 4 print(round(value)) 5

What does time.time() return?

time.time() returns the seconds with millisecond precision since the UNIX epoch.

How do you write a statement to prompt the user to enter a numeric value?

value = float(input("Enter a numeric value: "))


Ensembles d'études connexes

Ap History "Industrial development"

View Set