Test Practice Flashcards

1
Q

What is the output of the following code?

a , b = 10 , 20
print( (b , a)[a < b] )

A

10

a < b evaluates to True

True = 1

So, the 1 index of the tuple (b, a) is a, which is set to the value of 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

UTF-8

A

1 - can handle any Unicode point

2 - is a type of encoding

3 - the 8 in UTF-8 means 8-bit values are used in encoding

4 - ASCII characters are also valid UTF-8 text

5 - stands for Unicode transformation format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What I’d the output of the following code snippet?

i = 0

while i != 0:
i -= 1
else:
i += 1
print(i)

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does this print? And why?

g = “good lord”

print(g[::1])

A

good lord

A blank index at the first colon defaults to 0

A blank index at the second colon defaults to the end of the string

The number after the third colon indicated the step of the slice, so it would:

Start at 0
End at the end of the string
Step through by 1 index at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the following code snippet print and why?

s = “PYTHON”
print( s[ : : -5] )

A

NP

since the first two colons are blank, the
1 - string starts slicing at 0
2 - ends slicing at the end of the string
3 - steps at through from the END of the string at a negative 5 steps

So it will print the index at the end of the string first ( N )and then go -5 indexes back from that point and print the P

So NP is the print out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you pass arguments to a function?

A

A keyword argument
A positional argument
A mix of both positional and keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the output of the following code snippet?

try:
print(“START”)
n = 1 / 0
print(“END”)
except:
print(“FAIL”)
print(“BACK TO NORMAL”)

A

START
FAIL
BACK TO NORMAL

the code executes until the ZeroDivisionError

Then it moves to the except portion where it performs both Prints

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Do keyword arguments precede positional arguments?

A

No. Keyword arguments must follow positional arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can keyword arguments be passed to a function in any order?

A

Yes. So long as they follow any positional arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of the following code?

list_1 = [ ‘a’, ‘b’, ‘c’ ]
list_2 = list_1

del list_1[0]

print ( list_2 )

A

[‘b’, ‘c’]

list_2 refers to the same place in memory as list_1

So changing data in list_1 will change it in list_2 as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Look at this code-what is the output? Why?

list_a = [1 , 2 , 3]
list_b = list_a
list_c = list_b[ : ]

list_a.pop(1)

print(list_b)
print(list_c)

A

[1,3]
[1,2,3]

list b is a memory copy of list_a so any changes to list_a will reflect in list_b

list_c is just a regular copy of list_b though so no changes to list_a will affect it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which of the following will display a message on the screen?

print()
str()
len()
input()

A

print()
And
input()

Input allows you to print a message to the console so the user knows to provide some type of input to the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Insert the correct snippet so that the program produces the expected output.
Expected output:
True

Code:

list = [False, True, “2”, 3, 4, 5]
# insert code here
print (b)

Code options:

A. b = 0 not in list
B. b = list[0]
C. b = 0 in list
D. b = False

A

C

Is 0 in the list? Yes it is. False evaluates to 0 (and True evaluates to 1) so it is True that 0 is in the list named “list”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Assuming that the tuple is a correctly created tuple, the fact that tuples are immutable means that the following instruction:

my_tuple[1] = my_tuple[1] + my_tuple[0]

A. is illegal
B. may be illegal if the tuple contains strings
C. can be executed if and only if the tuple contains at least two elements
D. is fully correct

A

A

The code will result in an error

You CAN concatenation two tuples, but the resulting value must be assigned to a new variable.

my_tuple[1] = my_tuple[1] + my_tuple[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the expected output of the following code?

x = [0, 1, 2]
x. insert(0, 1)
del x[1]
print (sum(x))

A

4

insert a 1 at index 0 which moves the 0 already AT 0 to index 1

Deleting index 1 just deletes the 0 so now you’re left with x = [1, 1, 2]

Summing list x produces 4 (1 + 1 + 2)

17
Q

What is the expected output of the following code?

list1 = [1, 3]
list2 = list1
list1[0] = 4
print (list2)

A

4, 3

list 2 is a clone of list 1 — they point to the same spot in memory. So updating list 1 will also update list 2

18
Q

What is the expected output of the following code?

data = [‘Peter’, 404, 3.03, “tone”, 4, 17]
print (data[1:3])

A

404, 3.03

Python slicing by index is not inclusive. So the start point is 1 (and 404) and the end point is 3 (“tone”), but the slice is not inclusive so the values returned are data[1] and data[2]

19
Q

Take a look at the snippet, and choose the true statements: (Choose two.)

nums = [1, 2, 3]
vals = nums
del vals [1:2]

A. nums is longer than vals
B. nums and vals are of the same length
C. vals is longer than nums
D. nums and vals refer to the same list

A

B & D

Since both lists refer to the same spot in memory, editing one will edit the other.

So all attributes of both lists will be exactly the same.