wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Broken Python(Practice Test)

Total questions: 63

Worksheet time: 32mins

Name
Class
Date
1.

Which code should you use to import a function named abc from a Python module named xyz?

a)

import abc

b)

from xyz import abc

c)

import abc from xyz

d)

import mod xyz fn abc

2.

When using Python, which mathematic operation returns a value of 2?

a)

42 / 4

b)

42 / / 4

c)

42 % 4

d)

42 ** . 4

3.

When using Python, what is the result of the mathematic operation 1 ** (2 * 3) / 4 + 5?

a)

0.11

b)

0.67

c)

5.25

d)

6.50

4.

Which function from the math module can you use to round up the number 12.34 to 13?

a)

math.cell()

b)

math.float()

c)

math.floor()

d)

math.trunc()

5.

Which function returns only whole, even numbers from within the range from 1 to 100?

a)

random.choice(1, 100, even)

b)

random.randint (1, 100, 2)

c)

random.randrange(2, 101, 2)

d)

random.uniform(1, 100)

6.

True or False: A datetime.timedelta object stores only days, seconds, and microseconds.

a)

True

b)

False

7.

Which two statements about the pydoc module are true?

a)

It generates documentation from the docstrings in a module.

b)

It generates documentation for a module without importing it.

c)

It imports a module and then generates documentation for the module.

d)

It automatically suppresses module functions from running while generating documentation for the module.

8.

Which of the following are valid expressions for the datetime.timedelta objects t1, t2, and t3? (Choose all that apply.)

a)

t1 = t2 – t3

b)

t1 = 6 % t2

c)

month = datetime.timedelta(1 month)

d)

year = datetime.timedelta(365 days)

9.

Which command should you use to generate HTML documentation for a Python module named py?

a)

pydoc program

b)

pydoc -h program.py

c)

pydoc -t program.py

d)

pydoc -w program

10.

Which command should you use to generate text documentation for a Python module named py?

a)

pydoc program

b)

pydoc -h program.py

c)

pydoc -t program.py

d)

pydoc -w program

11.

Which expression defines an object named hiredate with a value of July 1, 2018?

a)

hiredate = datetime.date(year = 2018, month = 7, day = 1)

b)

hiredate = datetime.datetime(year = 2018, month = 7, day = 1)

c)

hiredate = datetime.timedate(year = 2018, month = 7, day = 1)

d)

hiredate = datetime.timedelta(days = 43282)

12.

Which element should come first in a multi-line docstring for a function?

a)

A blank line

b)

The function arguments

c)

The function parameters

d)

A summary line that describes the function as a command

13.

Which is an example of a valid one-line docstring?

a)

# Return the path of the current working directory

b)

// Return the path of the current working directory //

c)

docstring("Return the path of the current working directory.")

d)

"""Return the path of the current working directory."""

14.

Which information should you include in a multi-line docstring for a function?

a)

arguments, return values, and exceptions

b)

function name

c)

function parameters

d)

public methods and instance variables

15.

True or False: A global variable is available from any Python module.

a)

True

b)

False

16.

You are comparing two numerical values in the following operation:

In [1]: 9 ___ 9

Out[1]: True

Which operator correctly completes the operation?

a)

=>

b)

=!

c)

>=

17.

Which statement about local variables is true?

a)

A local variable can be read from the global scope.

b)

You can use the same variable name in multiple functions.

c)

A local variable in one function can be read from another function.

d)

A function can update a global variable in a local scope by using the same name for a local variable and assigning a new value.

18.

You perform the following Boolean comparison operation:

(x >= 10) not (x < 20) and (x % 2 == 0)

For which two numbers is the comparison operation True? (Choose two.)

a)

10

b)

20

c)

25

d)

50

19.

Complete the sentence to be true for code running in a Python environment.

A local scope is created...

a)

when a function is created

b)

when a function is called

c)

when a function returns to its caller

d)

when a function is destroyed

20.

When a function calls a subfunction, which variables are maintained in memory? (Choose all that apply.)

a)

The global variables

b)

The function variables

c)

The subfunction variables

21.

You are performing the following operation:

In [1]: 10 == 20

Out[1]: _____

What is the result of the operation?

#Hit from god there is two answers

a)

0

b)

10

c)

False

d)

True

22.

You run a Python script named my_script.py directly from a terminal window.

In the script environment, what is the value of the `__name__` variable?

a)

`__main__`

b)

`__my_script__`

c)

`my_script`

d)

`my_script.py`

23.

True or False: The containment operators in and not in return a True/False result.

a)

True

b)

False

24.

You run a Python script named my_script.py by importing it as a module into a script named second_script.py.

In the script environment, what is the value of the `__name__` variable?

a)

__main__

b)

__my_script__

c)

my_script

d)

my_script.py

25.

Which two statements are true for two objects that contain the same information and are saved in different memory locations?

Example:

```python

x = [1,2,3,a,b]

y = [1,2,3,a,b]

```

(Choose two.)

a)

They are equal

b)

They are not equal

c)

They are identical

d)

They are not identical

26.

Which element stores Python script command-line arguments and options?

a)

arg

b)

argc

c)

argparse

d)

argv

27.

Which Boolean operator has the highest precedence? (Choose one.)

a)

and

b)

not

c)

or

28.

Which Boolean operator has the highest precedence? (Choose one.)

a)

or

b)

is not

29.

Which two statements about a positional argument are true? (Choose two.)

a)

No answer text provided.

b)

It is a required argument

c)

It is an optional argument

d)

It is always the first argument

e)

Its position depends on the command

30.

Which operator has the highest precedence? (Choose one.)

a)

+

b)

==

c)

and

31.

In Python, what is the base class for all built-in exceptions?

a)

BaseException

b)

Error

c)

Exception

d)

IOError

32.

Which three data types can you use for dictionary keys? (Choose three.)

a)

float

b)

int

c)

lists

d)

other dictionaries

e)

string

33.

Which clause should you use for code that you want to execute regardless of whether an exception is raised?

a)

else

b)

elseif

c)

except

d)

finally

34.

Which process does Python use to optimize dictionary search capabilities?

a)

containment

b)

hashing

c)

iteration

d)

looping

35.

True or False: When handling exceptions, Python code can include an else clause or a finally clause, but not both.

a)

True

b)

False

36.

In what format does a dictionary store information?

a)

(key, value)

b)

[key:value]

c)

{key, value}

d)

{key:value}

37.

True or False: In Python, using the `raise` statement allows the programmer to define and raise their own exception.

a)

True

b)

False

38.

Which two statements about key:value pairs are correct? (Choose two.)

a)

Keys are unique

b)

Values are unique

c)

A value can be associated with multiple keys

d)

Keys within a dictionary must be of the same type

39.

Which of the following statements about tuples is true?

a)

A tuple can contain list elements.

b)

A tuple cannot contain another tuple.

c)

After you define a tuple, you can add elements to it.

d)

You define the elements of a tuple within square brackets.

40.

In which two ways is a tuple like a list? (Choose two.)

a)

Lists and tuples are data structures that can store a sequence of elements.

b)

Lists and tuples are immutable data structures.

c)

Lists and tuples can contain int, float, list, and string elements.

d)

You can reorder the elements of lists and tuples.

41.

Which string defines a heterogeneous tuple?

a)

T = ("switch",)

b)

T = tuple(L)

c)

T = ("Tobias", 23, 25.3, [])

d)

T = (10, -4, 59, 58, 23, 50)

42.

Which process can you use to assign the values of a tuple to multiple variables?

a)

Convert the tuple into a list

b)

Create a homogeneous tuple

c)

Slice the tuple

d)

Unpack the tuple

43.

When using printf-style string formatting, which conversion flag outputs a signed integer decimal?

a)

%c

b)

%d

c)

%s

d)

%x

44.

Which attributes can you define for a time object? (Choose all that apply.)

a)

Month

b)

Day

c)

Hour

d)

Second

e)

Millisecond

45.

When using printf-style string formatting, which conversion flag outputs a single character?

a)

%c

b)

%d

c)

%s

d)

%x

46.

What is the result of not setting the value of the year attribute of a date object?

a)

The year defaults to 0.

b)

The year defaults to the current year.

c)

The year defaults to 9999.

d)

The object is invalid.

47.

When using the format() method, which placeholder outputs the variable x as a three-digit integer with two digits before and one digit after the decimal point (for example, 12.3)?

a)

{x:2.1f}

b)

{x:3.1f}

c)

{x:3.2f}

d)

{x:4.2f}

48.

Which statement about the datetime.datetime data type is true?

a)

All variables are optional.

b)

All variables are required.

c)

All date variables are required.

d)

All time variables are required.

49.

When using the str.format() method, which flag places a - (negative sign) in front of negative numbers?

a)

"

b)

^

c)

-

d)

+

50.

True or False: Python code can include a script that returns the operating system platform your code is running on with the .sys

a)

True

b)

False

51.

Which function displays a date object in the form Monday, January 01, 2018?

a)

.strftime("%A, %B %d, %Y")

b)

.strftime("%A, %B %D, %Y")

c)

.strftime("%A, %M %d, %Y")

d)

.strftime("%A, %M %D, %Y")

52.

Which function can you use to list the contents of the current working directory?

a)

os.chdir()

b)

os.getcwd()

c)

os.listdir()

d)

os.rmdir()

53.

Which type of statement should you use as a placeholder for future function code?

a)

break

b)

continue

c)

pass

d)

place

54.

Which function can you use to delete an empty folder?

a)

os.chdir()

b)

os.getcwd()

c)

os.listdir()

d)

os.rmdir()

55.

In an os function, which syntax can you use to move up one folder in the current directory structure?

a)

/

b)

\

c)

\\

d)

..

56.

What type of loop should you use to repeat a code process as long as a condition resolves as `True`?

a)

compound

b)

for

c)

nested

d)

while

57.

Which function can you use to test whether an absolute or relative path to a folder is valid?

a)

os.path.commonpath(paths)

b)

os.path.exists(path)

c)

os.path.normpath(path)

d)

os.path.realpath(path)

58.

What type of loop should you use to iterate over the elements of a simple table (two-dimensional list)?

a)

compound

b)

for

c)

nested

d)

while

59.

True or False: An infinite loop is a loop that runs until the value returned by the loop condition is False.

a)

True

b)

False

60.

Which two os module functions can you use to delete a specific file? (Choose two.)

a)

os.delete

b)

os.path.re

c)

os.remove

d)

os.unlink

61.

True or False: You can use the remove function to delete a specific file directory.

a)

True

b)

False

62.

Which two functions can you use to determine whether a path leads to either a file or a directory? (Choose two.)

a)

os.path.isdir(path)

b)

os.path.isfile(path)

c)

os.path.exists(file_path)

d)

os.remove(file_path)

63.

Which statement ensures that a file will be closed even when an exception is raised?

a)

else: file.close()

b)

file = open(file_path, mode)

c)

file.close()

d)

with open(file_path, mode) as file: