WorksheetsPython Quiz 1
Total questions: 100
Worksheet time: 56mins
Choose the INVALID identifiers from the following
NUmbEr1
1NUMBer
A_B
A&B
Choose the keyword from the following
IF
Else
iF
else
Examples of String Literals
"ABC1273"
nfjdlsj@@$
12638
'7e920938'
Escape sequence is represented by a ____ followed by one or more characters
Front slash
Double Quote
Single Quote
Back Slash
Size of the string "Ant Man"
(a)
str="Red\
block\
a"
Size of str is 11. True/False ?
True
False
An escape sequence represents how many characters ?
"\t"
3
2
1
4
What is the difference between a variable and a data type in Python?
A variable is a type of data, while a data type is a specific value in Python.
A variable is a name that refers to a value, while a data type specifies the type of data that a variable can hold.
A variable and a data type are the same thing in Python.
A variable is used for storing data temporarily, while a data type is used for performing operations in Python.
Explain the purpose of a function in Python and give an example.
The purpose of a function in Python is to display a message on the screen. For example, print('Hello, World!')
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
The purpose of a function in Python is to define a variable. For example, x = 5
The purpose of a function in Python is to create a loop. For example, for i in range(5):
How would you add element to an existing list?
Using a function called append()
by writing the name of the list and element together
add_element() function
A list cannot be further modified.
How do you concatenate two strings in Python?
string1 . string2
string1 * string2
string1 + string2
string1 - string2
Write a for loop that prints numbers from 1 to 10.
for num in range(1, 11): print(num + 1)
for num in range(1, 10): print(num)
for num in range(10): print(num)
for num in range(1, 11): print(num)
Write a while loop that counts down from 10 to 1.
while count > 0: print(count) count -= 1
count = 10 while count > 0: print(count) count = count - 1
for i in range(10, 0, -1): print(i)
while count < 10: print(count) count += 1
What are the different types of operators in Python?
Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity
Addition, Subtraction, Multiplication, Division
Equals, Not Equals, Greater Than, Less Than
If, Else, While, For
What is the purpose of using the 'break' statement in a loop in Python?
To skip the current iteration and continue with the next iteration
To exit the loop immediately
To execute a specific block of code when a condition is met
To repeat the loop for a specific number of times
Which of the following is invalid?
_a = 1
__a = 1
__str__ = 1
None of the mentioned
Which of the following is an invalid variable?
my_string_1
foo
1st_string
_
Python is case senisitive-
Yes
No
What is the output of
0.1 + 0.2 == 0.3
True
False
Machine Dependent
Error
Select the logical operators-
or
not
and
in
What is the result of
4+4/2+2
4
6
8
5
Write the output of the following code
30
-10
10
-30
Output of the code-
mywork
my work
work my
workmy
Output ?
MyWork
MYwork
workMY
workMy
L = [ 1, 23, 'hi',6 ]
Identify L
dictionary
tuple
list
set
Negative index -1 belongs to the ______ of string
first character
last character
second last character
second character
Which of the following functions will return the total number of characters in a string
count()
len()
index()
all of these
Which of the following functions will return a list containing all words of a string-
find()
index()
partition()
split()
Which of the following will create an empty list-
L=[ ]
L=list(0)
L=list()
L=List(empty)
L = [ 1, 23, 'hi',6 ]
Identify L
dictionary
tuple
list
set
Which of the following is mutable datatype?
int
str
list
bool
Which of the following is immutable datatype?
int
set
list
dict
Q20. Find the output :
3 3 3
3 1 5
5 5 5
3 5 1
Q19. What will be the output :
>>> int('3'+'4')
A. ‘7’
B. 7
C. 34
D. ‘34’
Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
Error
None
25
2
Which one of these is NOT a property of a variable?
Has a name.
Holds a value.
Has a data type.
Has a length.
What will be the output of the following statement?
type('5')
str
int
float
Boolean
Which one is NOT a legal variable name?
my_var
myvar
myvar2023
my var
Which operator can be used to compare two values?
=
<>
><
==
What is printed when we run this code if the user enters the values 5 and 6?
11
56
-1
Error
What is the value of total_cost that gets printed?
15
10
5
25
Which of the following will produce a Syntax Error?
answer = "Error"
answer = An Error
answer = 200
All of them
Which of the following correctly stores 'price' as a decimal number?
price = int(input("Price: "))
price = str(input("Price: "))
price = float(input("Price: "))
price = input("Price: ")
What are the basic data types available in Python?
string, number, boolean, collection
int, float, str, bool, list, tuple, set, dict
char, decimal, array, object
integer, double, character, map
How do you declare a variable in Python?
declare variable_name as value
You declare a variable in Python by using the syntax: variable_name = value.
variable_name : value
value = variable_name
What is the difference between a list and a tuple in Python?
Lists are faster than tuples in all operations.
Lists can contain only numbers while tuples can contain any data type.
The main difference is that lists are mutable and tuples are immutable.
Tuples are created using square brackets while lists use parentheses.
What is a for loop and how is it used in Python?
A for loop is a method for creating classes in Python.
A for loop is a type of variable in Python.
A for loop is a control flow statement that allows code to be executed repeatedly for each item in a sequence.
A for loop is used to define functions in Python.
How can you iterate over a dictionary in Python?
Convert the dictionary to a list and iterate over it.
Use a while loop to iterate over the dictionary.
Use recursion to access each key-value pair in the dictionary.
Use a for loop with dict.keys(), dict.values(), or dict.items() to iterate over a dictionary.
What is the significance of indentation in Python control flow?
Indentation is optional in Python and has no effect on control flow.
Indentation is used to separate different programming languages in Python.
Indentation is only used for comments in Python code.
Indentation is crucial in Python as it defines the structure and flow of control statements.
Which command will display the text 'Hello world!' on the screen?
print(Hello world!)
print "Hello world!"
print("Hello world!")
print = "Hello world!"
Which command will correctly ask the user for their age?
age = input("How old are you?")
age = input(How old are you?)
input("How old are you?") = age
How old are you = input()
Which decision statement will be triggered if the variable x is less than 20?
if x > 20:
if x <> 20:
if x == 20:
if x < 20:
Which of these tests whether the variable x is the same as 'Hello'?
x = "Hello"
x != "Hello"
x == "Hello"
x <> "Hello"
What is the output of the following program?
Yes
No
Error
No output
What is the output of the following program?
Yes
No
Error
No output
What would the outcome of this program be?
22
50
Nothing, it won't run due to errors in the code
38
What would the outcome of this program be?
prints 5 to 50 numbers
prints 5 to 49 numbers
prints 5 to 55 numbers
prints multiples of 5 upto 50
What is the output of the following program?
for i in range(0, 5):
print(i)
i=i+1
1 to 5 numbers
1 to 4 numbers
0 to 4 numbers
0 to 5 numbers
1. What is the output of the following code?
var1 = 1
var2 = 2
var3 = "3"
print(var + var2 + var3)
6
33
123
ERROR
2. What is the output of the following code?
p, q, r = 10, 20 ,30
print(p, q, r)
10 20
10 20 30
Error: invalid syntax
3. What is the Output of the following code?
for x in range(0.5, 5.5, 0.5):
print(x)
[0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5]
[0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
The Program executed with errors
4. What is the output of the following code?
salary = 8000
def printSalary():
salary = 12000
print("Salary:", salary)
printSalary()
print("Salary:", salary)
Salary: 12000 Salary: 8000
Salary: 8000 Salary: 12000
The program failed with errors
5. What is the output of the following code?
def calculate (num1, num2=4):
res = num1 * num2
print(res)
calculate(5, 6)
20
The program executed with errors
30
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.
TRUE
FALSE
8. What is the output of the following code?
sampleList = ["Jon", "Kelly", "Jessa"]
sampleList.append(2, "Scott")
print(sampleList)
The program executed with errors
[‘Jon’, ‘Scott’, ‘Kelly’, ‘Jessa’]
[‘Jon’, ‘Kelly’, ‘Scott’, ‘Jessa’]
9. What is the output of the following?
x = 36 / 4 * (3 + 2) * 4 + 2
print(x)
182.0
37
117
ERROR
11. What is the output of the following code?
for i in range(10, 15, 1):
print( i, end=', ')
10, 11, 12, 13, 14,
10, 11, 12, 13, 14, 15,
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)
TRUE
TRUE
TRUE
FALSE
FALSE
TRUE
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.
TRUE
FALSE
14. What is the output of the following code?
str = "pynative"
print (str[1:3])
py
yn
pyn
yna
15. What is the output of the following code?
valueOne = 5 ** 2
valueTwo = 5 ** 3
print(valueOne)
print(valueTwo)
10
15
25
125
ERROR
How do you declare a variable in Python?
variable_name = value
variable_name : value
variable_name = value =
value = variable_name
What is the purpose of loops in Python?
Loops in Python are used for sorting data
The purpose of loops in Python is to iterate over sequences or execute a block of code repeatedly.
Loops in Python are designed to skip over code blocks
The purpose of loops in Python is to print statements only once
How do you define a function in Python?
define function_name(parameters):
function_name(parameters):
function_name(parameters)
def function_name(parameters):
How do you add comments in Python code?
Use the '#' symbol to add comments in Python code.
Use the '/* */' symbols to add comments in Python code.
Use the '//' symbol to add comments in Java code.
Use the '//' symbol to add comments in Python code.
Why are comments important in programming?
Comments are used to hide malicious code within the program.
Comments are only for experienced programmers and not beginners.
Comments are unnecessary and slow down the code execution.
Comments provide clarity and context to the code.
What is the output of the following code snippet: 'x = 5 if x > 3: print('x is greater than 3')'?
x is less than 3
x is equal to 3
x is greater than 3
x is not defined
How many times will a 'for' loop iterate if the range is specified as range(5)?
3
7
10
5
How do you pass arguments to a Python function?
Arguments are passed to a Python function by placing them inside the parentheses of the function call.
Arguments are passed to a Python function by telepathy
Arguments are passed to a Python function by writing them on a piece of paper and mailing it
Arguments are passed to a Python function by sending them via email
Predict the output of the following code:
x=3
If x>2 or x<5 and x==6:
Print(“ok”)
else:
print(“no output”)
ok
okok
no output
none of above
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’ )
1,4
2
2,4
4
Find the output of the following program segments:
for i in range(20,30,2):
print(i)
20 22 24 26 28
20
22
24
26
28
20 22 24 26 28 30
20
22
24
26
28
30
What is the output of the given below program?
print("Know Program".split())
‘Know’, ‘Program’
(‘Know’, ‘Program’)
[‘Know’, ‘Program’]
{‘Know’, ‘Program’}
Find the output of the given Python program?
print(list("abcd".split('')))
[‘a’, ‘’, ‘b’, ‘’, ‘c’, ‘*’, ‘d’]
[‘a’, ‘b’, ‘c’, ‘d’]
[‘abcd’]
[‘abc*d’]
How do you create a list in Python?
You create a list in Python using curly braces, e.g., my_list = {1, 2, 3}.
You create a list in Python using square brackets, e.g., my_list = [1, 2, 3].
You create a list in Python using parentheses, e.g., my_list = (1, 2, 3).
You create a list in Python by using the list() function without brackets, e.g., my_list = list 1, 2, 3.
How do you convert a string to an integer in Python?
Use eval('string') to get an integer from a string.
Apply float('string') for conversion to integer.
Use int('string') to convert a string to an integer.
Use str('string') to convert a string to an integer.
What is the output of print('Hello' + ' World')?
Error
Hello World
HelloWorld
WorldHello
What does the 'append()' method do in a list?
It sorts the list in ascending order.
It creates a copy of the list.
It removes the last element from the list.
It adds an element to the end of the list.
Which symbol is used for single-line comments in Python?
//
/* */
#
<!-- -->
What is the output of print(type("Hello")) in Python?
<class 'int'>
<class 'str'>
<class 'float'>
<class 'list'>
In Python, 'Hello', is the same as "Hello"
True
False
Which of the following is the correct output of the call to below line of code?
print(list("hello"))
['h', 'e', 'l', 'l', 'o']
None of these
[h,e,l,l,o]
['h' 'e' 'l' 'l' 'o']
Which of the following is the output of the instructions given below?
mylist=[1, 5, 9, int('0')]
print(sum(mylist))
16
5
1
15
Which of the following is the output of the Python code fragment given below?
var1 = 4.5
var2 = 2
print(var1//var2)
22.5
2.5
2.0
2.25
20.0
Which method can be used to return a string in upper case letters?
upperCase()
uppercase()
toUpperCase()
upper()
Which collection is ordered, changeable, and allows duplicate members?
LIST
DICTIONARY
SET
TUPLE
How do you start writing a for loop in Python?
for x in y:
for each x in y:
for x > y:
What is the syntax error with this code:
prin(Hello World)
No brackets around "Hello World"
Speech marks are not needed
Colon is not needed
print is spelt incorrectly
Print has a capital P
