programming midterm

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/36

flashcard set

Earn XP

Description and Tags

carleton

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

modulus (remainder)

% a%b= a-a/ / b*b

2
New cards

int division

// (rounds to the lowest int value)

3
New cards

variable

stores value and name

4
New cards

x = 2            # 1
y = x            # 2
y = x == 2       # 3
y = (x = 2)      #4
y = (x == 2)     #5

which of the statements contains a SyntaxError?

4

5
New cards

Write a code fragment that sets a variable test to True when the float variable x is equal to 0.0 +/- 0.001

Check all that apply.

1

test=x<=0.001 and x>=-0.001

2

test=x>=-0.001 or x<=0.001

3

test=(x>=-0.001) and (x<=0.001)

4

test=abs(x) <= 0.001

5

test = x == 0

1

3

4

6
New cards

x = (1/3) == 0.3333

what is x

False (It is inadvisable to compare floating point numbers for equality using ==.

Instead floating point numbers are generally compared to be equal within a window of precision)

7
New cards

What decimal value does the 8-bit word 010000112 represent, if the word is interpreted as a signed magnitude number?

67

8
New cards

when converting decimal to binary, when given 1 / 2 it equals 1 NOT 0. but then will give you 0 / 2 which is also 1

wipefhipWGHwehfpwewf

9
New cards

how to convert decimal to binary fraction (ex. 0.625)

times by 2, and do it to a diff decimal place each time (first number will be 1 or 0)

10
New cards

What is the most appropriate precondition that should be included in the docstring for this function:

def slope(x1, x2, y1, y2):
    return (y2 - y1) / (x2 - x1)

Precondition: x2 - x1 > 0 and y2 - y1 > 0

Precondition: x2 > x1

Precondition: x2 != x1

No precondition is needed.

Precondition: x2 != x1

11
New cards

By annotating parameter x, as float. what type of values can x be in a function?

int or float

12
New cards

def add_one(x):
    x = x + 1
    return x

x = 0
y = add_one(3)

print('x = ', x, 'y = ', y)

How many function calls are in the code?

2

13
New cards

def plus_two(x):
    return x + 2

which of the following code fragments contain valid calls to the function? Select all correct choices.

1

a = 5
y = plus_two(a)

2

x = 7
y = plus_two()

3

y = plus_two(plus_two(6))

4

plus_two(6, y)

5

plus_two(5)

6

x = 3
x = plus_two(x)

1, 3, 5, 6

14
New cards

After this code is executed, what value does x refer to?

def func1(x):
    x = 4 * x

x = 0
x = func1(10)

0

10

40

None

none, (Functions without a return statement in their body return None)

15
New cards

def add_one(x):
    x = x + 1
    return x

x = 0
y = add_one(3)

How many variables named x and y appear in the memory model just before the return statement is executed?

two x's and one y

 two x's

one x and one y

 one x

2 x’s

16
New cards

How do we indicate the end of a block of statements in Python?

Ensure that the statement that follows the block is "undented"; that is, its indentation is less than the indentation of the last statement in the block.

17
New cards

What is printed by the following code snippet?

x = 4
y = 3
if x == 3:
    print(1)
    print(2)
    if y == 3:
        print(3)
    print(4)

NOTHING (if isnt right spacing)

18
New cards


An expression that causes the function body to be executed.


Function call

19
New cards


A variable that refers to a value that is passed to the function.

Parameter

20
New cards

The header and body of a function.

Function definition

21
New cards

An expression whose value is passed to a function.

Argument

22
New cards

in a function call, arguments appear between

parentheses

23
New cards

The first line of a function definition is called the

header

24
New cards

parameter is the

variable

25
New cards

the value of the parameter is the

argument

26
New cards

What do while-loops do

Repeat a chunk of code until a condition is false but will excute the code the first time the condition is false (live and learn)

27
New cards

number = 5
while number >= 0:
    number -= 1
    print (number)

4

3

2

1

0

-1

(forces it to print since the code has already in excuted???)

28
New cards

number = 5
while number >= 0:
    print (number)
    number -= 1


5

4

3

2

1

0

(number it printed before thing so it already realizes that its false???)

29
New cards

what is range when there is 3 variable

(start, stop, step) (whatever the last number is doesnt count, ex , (1, 2,5) would just have 1) 

30
New cards

does range work for floats

NO only ints

31
New cards

what for loop do

variable is assigned for elements in a sequence(repeats sqeunce fixed amount of times, need to know how many times code need to be iterated)

32
New cards

for num in range(6):
      print (num, end=" ")
print("")

0 1 2 3 4 5 (end=" “ tells python not to go to next line, and instead but space)

33
New cards

how to call function from file import function

from (file) import (function)

just use function name (ex. from f_mod.py import f(). call = f() )

34
New cards

how to call function by import file

import (file)

just put file name (without .py) and function name

(ex. import f_mod. call = f_mod.f() )

35
New cards

What is the difference between executing a module vs. importing a module?

The variable "__name__" changes value inside the module

36
New cards

When designing test cases, it is important to ensure full "coverage". What does this mean?

Your test cases should include extreme values (min, max of expected range) for arguments

Include at least one test case for each type of behaviour for the function

Make sure every logical path through your code is executed by the test cases

37
New cards

what are examples of iterable data

lists and strings