wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz 1

Total questions: 100

Worksheet time: 56mins

Name
Class
Date
1.

Choose the INVALID identifiers from the following

a)

NUmbEr1

b)

1NUMBer

c)

A_B

d)

A&B

2.

Choose the keyword from the following

a)

IF

b)

Else

c)

iF

d)

else

3.

Examples of String Literals

a)

"ABC1273"

b)

nfjdlsj@@$

c)

12638

d)

'7e920938'

4.

Escape sequence is represented by a ____ followed by one or more characters

a)

Front slash

b)

Double Quote

c)

Single Quote

d)

Back Slash

5.

Size of the string "Ant Man"

(a)  

6.

str="Red\

block\

a"

Size of str is 11. True/False ?

a)

True

b)

False

7.

An escape sequence represents how many characters ?

"\t"

a)

3

b)

2

c)

1

d)

4

8.

What is the difference between a variable and a data type in Python?

a)

A variable is a type of data, while a data type is a specific value in Python.

b)

A variable is a name that refers to a value, while a data type specifies the type of data that a variable can hold.

c)

A variable and a data type are the same thing in Python.

d)

A variable is used for storing data temporarily, while a data type is used for performing operations in Python.

9.

Explain the purpose of a function in Python and give an example.

a)

The purpose of a function in Python is to display a message on the screen. For example, print('Hello, World!')

b)

The purpose of a function in Python is to encapsulate a set of instructions that can be called multiple times to perform a specific task. For example, a function to calculate the area of a circle can be defined as follows: def calculate_area(radius): return 3.14 * radius * radius

c)

The purpose of a function in Python is to define a variable. For example, x = 5

d)

The purpose of a function in Python is to create a loop. For example, for i in range(5):

10.

How would you add element to an existing list?

a)

Using a function called append()

b)

by writing the name of the list and element together

c)

add_element() function

d)

A list cannot be further modified.

11.

How do you concatenate two strings in Python?

a)

string1 . string2

b)

string1 * string2

c)

string1 + string2

d)

string1 - string2

12.

Write a for loop that prints numbers from 1 to 10.

a)

for num in range(1, 11): print(num + 1)

b)

for num in range(1, 10): print(num)

c)

for num in range(10): print(num)

d)

for num in range(1, 11): print(num)

13.

Write a while loop that counts down from 10 to 1.

a)

while count > 0: print(count) count -= 1

b)

count = 10 while count > 0: print(count) count = count - 1

c)

for i in range(10, 0, -1): print(i)

d)

while count < 10: print(count) count += 1

14.

What are the different types of operators in Python?

a)

Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity

b)

Addition, Subtraction, Multiplication, Division

c)

Equals, Not Equals, Greater Than, Less Than

d)

If, Else, While, For

15.

What is the purpose of using the 'break' statement in a loop in Python?

a)

To skip the current iteration and continue with the next iteration

b)

To exit the loop immediately

c)

To execute a specific block of code when a condition is met

d)

To repeat the loop for a specific number of times

16.

Which of the following is invalid?

a)

_a = 1

b)

__a = 1

c)

__str__ = 1

d)

None of the mentioned

17.

Which of the following is an invalid variable?

a)

my_string_1

b)

foo

c)

1st_string

d)

_

18.

Python is case senisitive-

a)

Yes

b)

No

19.

What is the output of

0.1 + 0.2 == 0.3

a)

True

b)

False

c)

Machine Dependent

d)

Error

20.

Select the logical operators-

a)

or

b)

not

c)

and

d)

in

21.

What is the result of

4+4/2+2

a)

4

b)

6

c)

8

d)

5

22.

Write the output of the following code

a)

30

b)

-10

c)

10

d)

-30

23.

Output of the code-

a)

mywork

b)

my work

c)

work my

d)

workmy

24.

Output ?

a)

MyWork

b)

MYwork

c)

workMY

d)

workMy

25.

L = [ 1, 23, 'hi',6 ]


Identify L

a)

dictionary

b)

tuple

c)

list

d)

set

26.

Negative index -1 belongs to the ______ of string

a)

first character

b)

last character

c)

second last character

d)

second character

27.

Which of the following functions will return the total number of characters in a string

a)

count()

b)

len()

c)

index()

d)

all of these

28.

Which of the following functions will return a list containing all words of a string-

a)

find()

b)

index()

c)

partition()

d)

split()

29.

Which of the following will create an empty list-

a)

L=[ ]

b)

L=list(0)

c)

L=list()

d)

L=List(empty)

30.

L = [ 1, 23, 'hi',6 ]


Identify L

a)

dictionary

b)

tuple

c)

list

d)

set

31.

Which of the following is mutable datatype?

a)

int

b)

str

c)

list

d)

bool

32.

Which of the following is immutable datatype?

a)

int

b)

set

c)

list

d)

dict

33.

Q20. Find the output :

a)

3 3 3

b)

3 1 5

c)

5 5 5

d)

3 5 1

34.

Q19. What will be the output :

>>> int('3'+'4')

a)

A. ‘7’

b)

B. 7

c)

C. 34

d)

D. ‘34’

35.

Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?

a)

Error

b)

None

c)

25

d)

2

36.

Which one of these is NOT a property of a variable?

a)

Has a name.

b)

Holds a value.

c)

Has a data type.

d)

Has a length.

37.

What will be the output of the following statement?

type('5')

a)

str

b)

int

c)

float

d)

Boolean

38.

Which one is NOT a legal variable name?

a)

my_var

b)

myvar

c)

myvar2023

d)

my var

39.

Which operator can be used to compare two values?

a)

=

b)

<>

c)

><

d)

==

40.

What is printed when we run this code if the user enters the values 5 and 6?

a)

11

b)

56

c)

-1

d)

Error

41.

What is the value of total_cost that gets printed?

a)

15

b)

10

c)

5

d)

25

42.
In programming, what is iteration?
a)
The repetition of steps within a program
b)
The order in which instructions are carried out
c)
A decision point in a program
d)
Testing a program to make sure it works
43.
Which two statements are used to implement iteration?
a)
IF and WHILE
b)
ELSE and WHILE
c)
FOR and WHILE
d)
IF and ELSE
44.

Which of the following will produce a Syntax Error?

a)

answer = "Error"

b)

answer = An Error

c)

answer = 200

d)

All of them

45.

Which of the following correctly stores 'price' as a decimal number?

a)

price = int(input("Price: "))

b)

price = str(input("Price: "))

c)

price = float(input("Price: "))

d)

price = input("Price: ")

46.

What are the basic data types available in Python?

a)

string, number, boolean, collection

b)

int, float, str, bool, list, tuple, set, dict

c)

char, decimal, array, object

d)

integer, double, character, map

47.

How do you declare a variable in Python?

a)

declare variable_name as value

b)

You declare a variable in Python by using the syntax: variable_name = value.

c)

variable_name : value

d)

value = variable_name

48.

What is the difference between a list and a tuple in Python?

a)

Lists are faster than tuples in all operations.

b)

Lists can contain only numbers while tuples can contain any data type.

c)

The main difference is that lists are mutable and tuples are immutable.

d)

Tuples are created using square brackets while lists use parentheses.

49.

What is a for loop and how is it used in Python?

a)

A for loop is a method for creating classes in Python.

b)

A for loop is a type of variable in Python.

c)

A for loop is a control flow statement that allows code to be executed repeatedly for each item in a sequence.

d)

A for loop is used to define functions in Python.

50.

How can you iterate over a dictionary in Python?

a)

Convert the dictionary to a list and iterate over it.

b)

Use a while loop to iterate over the dictionary.

c)

Use recursion to access each key-value pair in the dictionary.

d)

Use a for loop with dict.keys(), dict.values(), or dict.items() to iterate over a dictionary.

51.

What is the significance of indentation in Python control flow?

a)

Indentation is optional in Python and has no effect on control flow.

b)

Indentation is used to separate different programming languages in Python.

c)

Indentation is only used for comments in Python code.

d)

Indentation is crucial in Python as it defines the structure and flow of control statements.

52.

Which command will display the text 'Hello world!' on the screen?

a)

print(Hello world!)

b)

print "Hello world!"

c)

print("Hello world!")

d)

print = "Hello world!"

53.

Which command will correctly ask the user for their age?

a)

age = input("How old are you?")

b)

age = input(How old are you?)

c)

input("How old are you?") = age

d)

How old are you = input()

54.

Which decision statement will be triggered if the variable x is less than 20?

a)

if x > 20:

b)

if x <> 20:

c)

if x == 20:

d)

if x < 20:

55.

Which of these tests whether the variable x is the same as 'Hello'?

a)

x = "Hello"

b)

x != "Hello"

c)

x == "Hello"

d)

x <> "Hello"

56.

What is the output of the following program?

a)

Yes

b)

No

c)

Error

d)

No output

57.

What is the output of the following program?

a)

Yes

b)

No

c)

Error

d)

No output

58.

What would the outcome of this program be?

a)

22

b)

50

c)

Nothing, it won't run due to errors in the code

d)

38

59.

What would the outcome of this program be?

a)

prints 5 to 50 numbers

b)

prints 5 to 49 numbers

c)

prints 5 to 55 numbers

d)

prints multiples of 5 upto 50

60.

What is the output of the following program?

for i in range(0, 5):

print(i)

i=i+1

a)

1 to 5 numbers

b)

1 to 4 numbers

c)

0 to 4 numbers

d)

0 to 5 numbers

61.

1. What is the output of the following code?


var1 = 1

var2 = 2

var3 = "3"

print(var + var2 + var3)

a)

6

b)

33

c)

123

d)

ERROR

62.

2. What is the output of the following code?


p, q, r = 10, 20 ,30

print(p, q, r)

a)

10 20

b)

10 20 30

c)

Error: invalid syntax

63.

3. What is the Output of the following code?


for x in range(0.5, 5.5, 0.5):

print(x)

a)

[0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5]

b)

[0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]

c)

The Program executed with errors

64.

4. What is the output of the following code?


salary = 8000


def printSalary():

salary = 12000

print("Salary:", salary)


printSalary()

print("Salary:", salary)

a)

Salary: 12000 Salary: 8000

b)

Salary: 8000 Salary: 12000

c)

The program failed with errors

65.

5. What is the output of the following code?


def calculate (num1, num2=4):

res = num1 * num2

print(res)


calculate(5, 6)

a)

20

b)

The program executed with errors

c)

30

66.

7. A string is immutable in Python?

Every time when we modify the string, Python Always creates a new String and assigns a new string to that variable.

a)

TRUE

b)

FALSE

67.

8. What is the output of the following code?

sampleList = ["Jon", "Kelly", "Jessa"]

sampleList.append(2, "Scott")

print(sampleList)

a)

The program executed with errors

b)

[‘Jon’, ‘Scott’, ‘Kelly’, ‘Jessa’]

c)

[‘Jon’, ‘Kelly’, ‘Scott’, ‘Jessa’]

68.

9. What is the output of the following?

x = 36 / 4 * (3 + 2) * 4 + 2

print(x)

a)

182.0

b)

37

c)

117

d)

ERROR

69.

11. What is the output of the following code?

for i in range(10, 15, 1):

print( i, end=', ')

a)

10, 11, 12, 13, 14,

b)

10, 11, 12, 13, 14, 15,

70.

12. What is the output of the following code?


listOne = [20, 40, 60, 80]

listTwo = [20, 40, 60, 80]


print(listOne == listTwo)

print(listOne is listTwo)

a)

TRUE

TRUE

b)

TRUE

FALSE

c)

FALSE

TRUE

71.

13. The ‘in’ operator is used to check if a value exists within an iterable object container such as a list. Evaluates to true if it finds a variable in the specified sequence and false otherwise.

a)

TRUE

b)

FALSE

72.

14. What is the output of the following code?

str = "pynative"

print (str[1:3])

a)

py

b)

yn

c)

pyn

d)

yna

73.

15. What is the output of the following code?


valueOne = 5 ** 2

valueTwo = 5 ** 3


print(valueOne)

print(valueTwo)

a)

10

15

b)

25

125

c)

ERROR

74.

How do you declare a variable in Python?

a)

variable_name = value

b)

variable_name : value

c)

variable_name = value =

d)

value = variable_name

75.

What is the purpose of loops in Python?

a)

Loops in Python are used for sorting data

b)

The purpose of loops in Python is to iterate over sequences or execute a block of code repeatedly.

c)

Loops in Python are designed to skip over code blocks

d)

The purpose of loops in Python is to print statements only once

76.

How do you define a function in Python?

a)

define function_name(parameters):

b)

function_name(parameters):

c)

function_name(parameters)

d)

def function_name(parameters):

77.

How do you add comments in Python code?

a)

Use the '#' symbol to add comments in Python code.

b)

Use the '/* */' symbols to add comments in Python code.

c)

Use the '//' symbol to add comments in Java code.

d)

Use the '//' symbol to add comments in Python code.

78.

Why are comments important in programming?

a)

Comments are used to hide malicious code within the program.

b)

Comments are only for experienced programmers and not beginners.

c)

Comments are unnecessary and slow down the code execution.

d)

Comments provide clarity and context to the code.

79.

What is the output of the following code snippet: 'x = 5 if x > 3: print('x is greater than 3')'?

a)

x is less than 3

b)

x is equal to 3

c)

x is greater than 3

d)

x is not defined

80.

How many times will a 'for' loop iterate if the range is specified as range(5)?

a)

3

b)

7

c)

10

d)

5

81.

How do you pass arguments to a Python function?

a)

Arguments are passed to a Python function by placing them inside the parentheses of the function call.

b)

Arguments are passed to a Python function by telepathy

c)

Arguments are passed to a Python function by writing them on a piece of paper and mailing it

d)

Arguments are passed to a Python function by sending them via email

82.

Predict the output of the following code:

x=3

If x>2 or x<5 and x==6:

Print(“ok”)

else:

print(“no output”)

a)

ok

b)

okok

c)

no output

d)

none of above

83.

Which one of the following if statements will not execute successfully?

1) if (1, 2);

print(‘foo’)

2) if (1, 2):

print(‘foo’)

3) if (1):

print( ‘foo’ )

4) if (1);

print( ‘foo’ )

a)

1,4

b)

2

c)

2,4

d)

4

84.

Find the output of the following program segments:

for i in range(20,30,2):

print(i)

a)

20 22 24 26 28

b)

20

22

24

26

28

c)

20 22 24 26 28 30

d)

20

22

24

26

28

30

85.

What is the output of the given below program?

print("Know Program".split())

a)

‘Know’, ‘Program’

b)

(‘Know’, ‘Program’)

c)

[‘Know’, ‘Program’]

d)

{‘Know’, ‘Program’}

86.

Find the output of the given Python program?

print(list("abcd".split('')))

a)

[‘a’, ‘’, ‘b’, ‘’, ‘c’, ‘*’, ‘d’]

b)

[‘a’, ‘b’, ‘c’, ‘d’]

c)

[‘abcd’]

d)

[‘abc*d’]

87.

How do you create a list in Python?

a)

You create a list in Python using curly braces, e.g., my_list = {1, 2, 3}.

b)

You create a list in Python using square brackets, e.g., my_list = [1, 2, 3].

c)

You create a list in Python using parentheses, e.g., my_list = (1, 2, 3).

d)

You create a list in Python by using the list() function without brackets, e.g., my_list = list 1, 2, 3.

88.

How do you convert a string to an integer in Python?

a)

Use eval('string') to get an integer from a string.

b)

Apply float('string') for conversion to integer.

c)

Use int('string') to convert a string to an integer.

d)

Use str('string') to convert a string to an integer.

89.

What is the output of print('Hello' + ' World')?

a)

Error

b)

Hello World

c)

HelloWorld

d)

WorldHello

90.

What does the 'append()' method do in a list?

a)

It sorts the list in ascending order.

b)

It creates a copy of the list.

c)

It removes the last element from the list.

d)

It adds an element to the end of the list.

91.

Which symbol is used for single-line comments in Python?

a)

//

b)

/* */

c)

#

d)

<!-- -->

92.

What is the output of print(type("Hello")) in Python? 

a)

<class 'int'>

b)

<class 'str'>

c)

<class 'float'>

d)

<class 'list'>

93.

In Python, 'Hello', is the same as "Hello"

a)

True

b)

False

94.

Which of the following is the correct output of the call to below line of code?


print(list("hello"))

a)

['h', 'e', 'l', 'l', 'o']

b)

None of these

c)

[h,e,l,l,o]

d)

['h' 'e' 'l' 'l' 'o']

95.

Which of the following is the output of the instructions given below?


mylist=[1, 5, 9, int('0')]

print(sum(mylist))

a)

16

b)

5

c)

1

d)

15

96.

Which of the following is the output of the Python code fragment given below?


var1 = 4.5

var2 = 2

print(var1//var2)

a)

22.5

b)

2.5

c)

2.0

d)

2.25

e)

20.0

97.

Which method can be used to return a string in upper case letters?

a)

upperCase()

b)

uppercase()

c)

toUpperCase()

d)

upper()

98.

Which collection is ordered, changeable, and allows duplicate members?

a)

LIST

b)

DICTIONARY

c)

SET

d)

TUPLE

99.

How do you start writing a for loop in Python?

a)

for x in y:

b)

for each x in y:

c)

for x > y:

100.

What is the syntax error with this code:

prin(Hello World)

a)

No brackets around "Hello World"

b)

Speech marks are not needed

c)

Colon is not needed

d)

print is spelt incorrectly

e)

Print has a capital P