NEW
Font size
WorksheetsPython Basics Worksheet — Multiple-Choice Questions
Total questions: 43
Worksheet time: 22mins
What will be the output of the following Python code? x = 'abcd' for i in range(len(x)): print(i)
0 1 2 3
1234
a b c d
error
Which of the following is true for variable names in Python?
none of the mentioned
all private members must have leading and trailing underscores
underscore and ampersand are the only two special characters allowed
unlimited length
To pipeline the use of functions which are present in the random library, we must use the option:
import random
random.h
import.random
random.random
Which of the following is used to define a block of code in Python language?
All of the mentioned
Indentation
Key
Brackets
Python supports the creation of anonymous functions at runtime, using a construct called ________.
none of the mentioned
lambda
anonymous
pi
del method is used to destroy instances of a class.
True
False
Which of the following is the use of id() function in python?
None of the mentioned
The id() function compares two objects.
The Python id function returns the unique identity of the object
Not every object in Python has a unique id
What will be the output of the following Python code? f = None for i in range(5): with open("data.txt", "w") as f: if i > 2: break print(f.closed)
None
False
Error
True
Which one of the following is the use of function in Python?
Functions don’t provide better modularity for your application
you can’t also create your own functions
All of the mentioned
Functions are reusable pieces of programs
To read the entire remaining contents of the file as a string from a file object infile, we use ________.
infile.readlines()
infile.read()
infile.read(2)
infile.readline()
Which one of the following is not a keyword in Python language?
assert
global
pass
eval
What will be the value of the following Python expression? 9 + 3 % 5
2
1
12
4
Which of the following is a feature of Python DocString?
In Python all functions should have a docstring
Docstrings can be accessed by the doc attribute on objects
It provides a convenient way of associating documentation with Python modules, functions, classes, and methods
All of the mentioned
Which keyword is used to define a function in Python language?
Function
Define
Fun
def
What will be the output of the following Python code? class A: def init(self): self.multiply(15) print(self.i) def multiply(self, i): self.i = 4 * i class B(A): def init(self): super().init() def multiply(self, i): self.i = 2 * i obj = B()
60
15
30
An exception is thrown
Which of these statements about a dictionary is false?
Dictionaries aren’t ordered
The values of a dictionary can be accessed using keys
Dictionaries are mutable
The keys of a dictionary can be accessed using values
What will be the output of the following Python code snippet? a = [ppl-ai-file-upload.s3.amazonaws] i = 2 for i not in a: print(i) i += 1
None of the mentioned
Error
0
-2 -1
Which of the following is the correct extension of the Python file?
.py
.pl
.python
.p
Which of the following is the truncation division operator in Python?
%
//
|
/
What will be the output of the following Python code? a, b = 6, 7 a, b = b, a a, b
Invalid syntax
Nothing is printed
(7,6)
(6,7)
What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)
3
1
2
error, there is more than one return statement in a single try-finally block
What will be the output of the following Python code? names1 = ['Amir', 'Bala', 'Charlie'] names2 = [name.lower() for name in names1] print(names2)
None
b
a
c
Which function overloads the == operator?
equ()
eq()
none of the mentioned
isequal()
What will be the output of the following Python code? class A(): def disp(self): print("A disp()") class B(A): pass obj = B() obj.disp()
Nothing is printed
Invalid syntax for inheritance
A disp()
Error because when object is created, argument must be passed
Which of the following statements are true?
When you open a file for writing, if the file exists, the existing file is overwritten with the new file
When you open a file for writing, if the file does not exist, a new file is created
When you open a file for reading, if the file does not exist, an error occurs
All of the mentioned
Which of the following functions is a built-in function in Python?
sqrt()
print()
factorial()
What type does the variable have when the following code line is executed? x = 2 * 3.0
str
bool
float
int
What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id = "224" temp = tester(12) print(temp.id)
None
error
12
224
What will be the output of the following Python code? class A: def __init__(self, x = 1): self.x = x class B(A): def __init__(self, y = 2): super().__init__() self.y = y def main(): obj = B() print(obj.x, obj.y) main()
2 1
1 2
The program runs fine but nothing is printed
Error, the syntax of the invoking method is wrong
All keywords in Python are in ________.
lower case
Capitalized
UPPER CASE
None of the mentioned
What will be the output of the following Python program? i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0)
0 1 2
0 1 2 0
none of the mentioned
error
Is the following Python code valid? a = 2, 3, 4, 5 a
No, too many values to unpack
Yes, (2,3,4,5) is printed
Yes, 2 is printed
Yes, is printed
What type of inheritance is illustrated in the following Python code? class A(): pass class B(): pass class C(A, B): pass
Single-level inheritance
Multi-level inheritance
Hierarchical inheritance
Multiple inheritance
Is Python case sensitive when dealing with identifiers?
yes
none of the mentioned
no
machine dependent
Which of the following statements about properties is true?
The @property decorator allows you to define getter, setter, and deleter methods for attributes.
Properties are useful for validating data, computing attributes, and creating read-only or read-write attributes.
You use properties when you need controlled access or want to encapsulate logic without changing the API.
All of the mentioned.
In ____________________ copy, the modification done on one list affects the other list. In ____________________ copy, the modification done on one list does not affect the other list.
memberwise shallow
deep shallow
deep memberwise
shallow deep
How many except statements can a try-except block have?
one
more than zero
zero
more than one
Which type of Programming does Python support?
object-oriented programming
all of the mentioned
structured programming
functional programming
What is the order of precedence in Python?
Exponential, Parentheses, Division, Multiplication, Addition, Subtraction
Parentheses, Exponential, Multiplication, Addition, Division, Subtraction
Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
What will be the output of the following Python code? def add(c, k): c.test = c.test + 1 k = k + 1 class A: def __init__(self): self.test = 0 def main(): Count = A() k = 0 for i in range(25): add(Count, k) print("Count.test=", Count.test) print("k =", k) main()
Exception is thrown
Count.test=25 k=25
Count.test=25 k=0
Count.test=0 k=0
Which of the following is not a core data type in Python programming?
Class
List
Dictionary
Tuple
_____ is used to create an object.
constructor (the __init__ method)
class
User-defined functions
In-built functions
Which of the following character is used to give single-line comments in Python?
#
//
/*
!
