wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python PCAP 31-03

Total questions: 50

Worksheet time: 25mins

Name
Class
Date
1.

What is the output of the following code if spam.py is run?

a)

Syntax Error

b)

spam eggs ham

c)

spam ham

d)

eggs ham spam

e)

spam ham eggs

2.

How do you call the function ham() saved as spam.py below?

a)

import spam; ham()

b)

import spam; spam.ham()

c)

Import spam; spam.ham()

d)

Import spam; ham()

e)

import spam.ham; ham()

3.

How will you shorten the function call to spam() defined inside

packageA.subpackageB.subpackageC.moduleD?

a)

import packageA.subpackageB.subpackageC.moduleD

b)

import packageA.subpackageB.subpackageC.moduleD alias p

c)

from packageA.subpackageB.subpackageC.moduleD import spam alias s

d)

from packageA.subpackageB.subpackageC.moduleD import spam

e)

from packageA,subpackageB.subpackageC,moduleD import *

4.

What is the output of the following code?

a)

-1.1

b)

-2

c)

-1

d)

-1.0

e)

-2.0

5.

What is the output of the following code?

a)

0

b)

0.5

c)

1

d)

1.0

e)

TypeError: type int doesn't define __sqrt__ method

6.

Select all option(s) which returns a random number between 0 and 100?

a)

random.random(100)

b)

random.random(0, 100)

c)

random.random()*100

d)

random.random(100.0)

e)

random.random(0. 100)

7.

What is the datatype of the return value of the function platform.version()?

a)

float

b)

int

c)

char

d)

str

e)

array

8.

Which of the statements below is valid?

a)

Python is interpreted however it compiles the py file into pyc file

b)

Python is interpreted therefore it never compiles the py files

c)

Compiled Python files is not stored inside the __pycache__ folder

d)

Compiled Python files is stored inside the __cache__ folder

e)

Compiled Python files is stored inside the __pyc__ folder

9.

The extension of a compiled bytecode of the Python source file is

a)

.pyc

b)

.py

c)

.pc

d)

.__pycache__

e)

__pycache__

10.

Select valid option(s) about __init__.py

a)

__init__.py is not automatically executed when the regular package is

imported

b)

__init__.py is contained in namespace packages

c)

__init__.py is contained in regular packages

d)

__init__.py is automatically executed when the namespace package is

imported

e)

__init__.py is not contained in regular packages

11.

What directories are searched by the interpreter for spam.py given the code

below, EXCEPT?

a)

Directory where spam.py was run

b)

List of directories contained in PATH environment variable

c)

Current directory if the interpreter is run interactively

d)

List of directories in sys.path

e)

List of directories contained in PYTHONPATH environment variable

12.

What is the output of the following code?

a)

No output

b)

abcdefgh

c)

SyntaxError: invalid syntax

d)

NameError: name 'UndefinedException' is not defined

e)

Add () on Line 2 and 3 to fix the syntax error

13.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

ZeroDivisionError: division by zero

c)

No Output

d)

NameError: name 'ZeroDivisionError' is not defined

e)

0

14.

Which option will print ELSE given the following code?

a)

raise Exception

b)

raise TypeError

c)

raise ZeroDivisionError

d)

replace <<< INSERT CODE HERE >>> with blank

e)

pass

15.

What is the output of the following code?

a)

1345

b)

1235

c)

1245

d)

145

e)

135

16.

What is the output of the following code?

a)

Unhandled Exception

b)

Surprise

c)

Stop

d)

Goodbye

e)

None

17.

What is the output of the following code?

a)

Spam

b)

Spam Eggs

c)

Spam Ham

d)

Spam Ham Eggs

e)

Syntax Error

18.

What is the output of the following code?

a)

Spam

b)

Spam Ham

c)

Spam Spam

d)

Ham Ham

e)

Spam Ham Spam Ham

19.

Which option are valid replacements for the marker in the given code?

a)

except MathError:

b)

except ArithmeticException:

c)

except ZeroDivisionError:

d)

except DivisionZeroError:

e)

except TypeError:

20.

What is the output of the following code?

print("\\\\")

a)

Syntax Error

b)

\

c)

\\

d)

\\\

e)

\\\\

21.

What is the output of the following code?

a)

Syntax Error

b)

TypeError: ord() takes exactly two arguments (1 given)

c)

97 a

d)

TypeError: chr() takes exactly two arguments (1 given)

e)

TypeError: an integer is required (got type str)

22.

Which option will return True given the following code, EXCEPT?

a)

spam.startswith(ham)

b)

spam.equals(ham)

c)

spam.endswith(ham)

d)

spam == ham

e)

id(spam) == id(ham)

23.

What is the output of the following code if the user enters 1 on the first

prompt and 2 on the second prompt?

a)

TypeError: input() takes 0 positional arguments but 1 was given

b)

a+b

c)

3

d)

12

e)

TypeError: unsupported operand type(s) for +: 'str' and 'str'

24.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

(3, hamhamham)

c)

(3, 0)

d)

TypeError: unsupported operand type(s) for *=: 'str' and 'int'

e)

(9,hamhamhamhamhamhamhamhamham)

25.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

TypeError: unsupported operand type(s) for *: 'int' and 'str'

c)

'Yes!!!Yes!!!'

d)

0

e)

8

26.

What is the output of the following code?

a)

Hello World

b)

spam

c)

eggs

d)

No output

e)

SyntaxError: invalid syntax

27.

What is the output of the following code?

a)

eggs spam ham

b)

spam ham eggs

c)

eggs eggs eggs

d)

eggs ham eggs

e)

spam eggs ham

28.

What is the output of the following code?

a)

TypeError: __init__() missing 1 required positional argument: 'v'

b)

3 3

c)

3 4

d)

3 5

e)

3

29.

What is the output of the following code?

a)

0 2

b)

1 2

c)

2 2

d)

Invalid Syntax

e)

AttributeError: 'Spam' object has no attribute 'v1'

30.

What is the output of the following code?

a)

Hello

b)

World

c)

Hello World

d)

SyntaxError: invalid syntax

e)

No output

31.

What is the output of the following code?

a)

100 101

b)

Error in Line 3

c)

Error in Line 4

d)

Error in Line 5

e)

Error in Line 6

32.

What is the output of the following code?

a)

TypeError: Cannot create a consistent method resolution

b)

B.spam B.spam

c)

B.spam A.spam

d)

A.spam A.spam

e)

A.spam B.spam

33.

What is the output of the following code?

a)

No output

b)

Super Spam

c)

Super Ham

d)

Super Spam Super Ham

e)

Super Ham Super Spam

34.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

[[0], [1]]

c)

[[1], [2]]

d)

[[0], [0, 1]]

e)

[[1], [1, 2]]

35.

What is the output of the following code?

a)

[0, 2, 4, 6, 8]

b)

[2, 4, 6, 8, 10]

c)

[1, 3, 5, 7, 9]

d)

[0, 1, 3, 4, 5, 6, 7, 8, 9]

e)

SyntaxError: invalid syntax

36.

What is the output of the following code?

a)

[]

b)

[False]

c)

[0,1,2]

d)

[False, False, False]

e)

SyntaxError: invalid syntax

37.

What is the output of the following code?

a)

None

b)

0

c)

[0]

d)

[]

e)

SyntaxError: invalid syntax

38.

Which option will produce a non-empty list?

a)

lst = [i for i in range(1, 5)]

b)

lst = [i for i in range(5, 1)]

c)

lst = [l for i in range(-5, -1)]

d)

lst = [i for i in range(0, -5)]

e)

lst = [i for i in range(-1, -5)]

39.

What is the output of the following code?

a)

[8, 9]

b)

[9, 8]

c)

[0, 1, 2, 3, 4, 5, 6, 7]

d)

[7, 6, 5, 4, 3, 2, 1, 0]

e)

[2, 3, 4, 5, 6, 7, 8, 9]

40.

What is the output of the following code?

a)

[0, 1, 1]

b)

[1, 1]

c)

[0, 1]

d)

[0, 1, 2]

e)

ZeroDivisionError: integer division or modulo by zero

41.

What is the output of the following code?

a)

[0, 2, 4, 6, 8]

b)

[1, 2, 3, 4, 5]

c)

[1, 2, 4, 8, 16]

d)

[2, 4, 6, 8, 10]

e)

SyntaxError: invalid syntax

42.

How many stars will the following code print?

a)

0

b)

1

c)

2

d)

3

e)

4

43.

What is the output of the following code?

a)

0

b)

2

c)

10

d)

20

e)

SyntaxError: invalid syntax

44.

What is the output of the following code?

a)

10

b)

20

c)

True

d)

TypeError: tuple indices must be integers or slices

e)

Invalid Syntax

45.

What is the output of the following code?

a)

[0, 1, 2]

b)

[1, 2, 3]

c)

[0, 2, 4]

d)

[[0,0], [1,2], [2,4]]

e)

SyntaxError: invalid syntax

46.

Which option(s) are valid results of the following code if spam.txt does not

exist?

a)

No output

b)

2

c)

ENOENT

d)

No such file or directory

e)

Error Line 6

47.

What is the output of the following code?

a)

10

b)

20

c)

True

d)

False

e)

Invalid Syntax

48.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

TypeError: unsupported operand type(s) for *: 'list' and 'int'

c)

['spamspam', 'hamham']

d)

['spam', 'ham', 'spam', 'ham']

e)

None

49.

What is the output of the following code?

a)

4.0

b)

5.0

c)

6.0

d)

7.0

e)

8.0

50.

What is the output of the following code?

a)

SyntaxError: invalid syntax

b)

True

c)

False

d)

1

e)

0