Final Flashcards

1
Q

How would you create a blank list in Python?

A

x = [ ]

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

What would the following code display?
psycho = [22,23,24,98,100,101]
numbers[-4] = 99
print(numbers)

A

error numbers not defined
if psycho instead of numbers the output would be
[22,23,99,24,98,100,101]

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

What would the following code display?
numbers = [1,2,3,4,5]
my_list = numbers[1:3]
print(my_list)

A

[2, 3]

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

Write a program that opens an output file with the filename my_name.txt, writes your name to
the file and then closes the file.

A
def main():
     my_file = open('my_name.txt', 'w')
     my_file.write("Lillian")
     my_file.close()
main()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A file that data is written to, is known as a(n)

a. input file
b. output file
c. sequential access file
d. binary file

A

b. output file

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

This function returns the length of a list.

a. highest
b. length
c. length of
d. len

A

d. len

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

Which of the following is a count controlled loop?

a. for
b. foreach
c. while
d. erst

A

a. for

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

A group of statements that exist within a program for the purpose of performing a specific task is a(n) __________.

a. block
b. parameter
c. function
d. expression

A

c. function

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

Each repetition of a loop is known as a (n) __________.

a. cycle
b. revolution
c. orbit
d. iteration

A

d. iteration

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

he += operator is an example of a(n) __________ operator.

a. relational
b. augmented assignment
c. complex
d. reverse

A

b. augmented assignment

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

A variable that is used to mark the end of a sequence of values is known as a (n)

a. accumulator
b. counter
c. terminator
d. sentinel

A

d. sentinel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
The first line of a function definition is known as the 
\_\_\_\_\_\_\_\_\_\_.
a. body
b. introduction
c. initialization
d. header
A

d. header

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

A __________ is a variable that is created inside a function.

a. global variable
b. local variable
c. hidden variable
d. none of the above, you cannot create a variable in a function

A

b. local variable

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

A __________ is a piece of data that is sent into a function.

a. argument
b. parameter
c. header
d. packet

A

a. argument

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

Briefly describe an exception

A

An error that causes the program to stop

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

If an exception is raised and the program does not handle it with a try/except statement, what
happens?

A

An error message is displayed and the program crashes

17
Q

What is a record? What is a field?

A

A record is a set of data based on one topic. A field is a single piece of data within a record.

18
Q

What will happen with the following code:
try:
x = float(‘abcdef’)
print(‘The conversion is complete.’)
except:
print(‘This code caused an error.’)
else:
print(“This just happened.”)
finally:
print(‘An error may have happened’.)

A
try:
except:
#happens with an exception
else:
#this happens without an exception
finally:
#This always happens

answer:
This code caused an error.
An error may have happened.

19
Q

Write a while loop that will print the sum of the numbers from 1 to 100.

A

while x <= 100:
total += x
x += 1
print (total)

20
Q

Write a function that will take 2 different numbers as parameters. The function will return the largest of the numbers.

A
def largest (x,y):
      if x > y:
              return x
      else:         
               return y