0% found this document useful (0 votes)
63 views19 pages

Pynative Full Python

Uploaded by

urxrur28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views19 pages

Pynative Full Python

Uploaded by

urxrur28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Basic Python

1)​ 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
2)​ What is a “tuple” in Python?
a)​ A mutable, ordered sequence of items.
b)​ An immutable, ordered sequence of items.
c)​ A mutable, unordered collection of unique items.
d)​ An immutable, unordered collection of key-value pairs.
3)​ 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: invalid syntax
4)​ What is the correct operator for exponentiation in Python?
a)​ ^
b)​ **
c)​ exp()
d)​ //
5)​ How do you add an element to the end of a list in Python?
a)​ list.add(element)
b)​ list.insert(element)
c)​ list.append(element)
d)​ list.extend(element)

6)​ 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’, ‘Kelly’, ‘Scott’, ‘Jessa’]
c)​ [‘Jon’, ‘Kelly’, ‘Jessa’, ‘Scott’]
d)​ [‘Jon’, ‘Scott’, ‘Kelly’, ‘Jessa’]
7)​ 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
8)​ What is the output of the following code?
a = [1, 2, 3]
b=a
b.append(4)
print(a)
a)​ [1, 2, 3]
b)​ [1, 2, 3, 4]
c)​ [4, 1, 2, 3]
d)​ Error
9)​ 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)​ Error: The Program executed with errors
10)​What is the output of the following code?
x = 10
y=3
print(x % y)
a)​ 3
b)​ 1
c)​ 0
d)​ 3.33
11)​Are the statements below true or false?
Statements 1: Strings are immutable in Python.
Statements 2: Every time we modify a string, Python always creates a new
string and assigns it to that variable.
a)​ True
b)​ False
12)​Which of the following data types is mutable in Python?
a)​ int
b)​ tuple
c)​ str
d)​ List
13)​What is the output of print(type([]))?
a)​ class ‘tuple’
b)​ class ‘list’
c)​ class ‘dict’
d)​ class ‘set’
14)​What is the output of the following code?
var= "James Bond"
print(var[2::-1])
a)​ Jam
b)​ dno
c)​ maJ
d)​ dnoB semaJ
15)​What is the output of the following code?
var1 = 1
var2 = 2
var3 = "3"
print(var1 + var2 + var3)
a)​ 6
b)​ 33
c)​ 123
d)​ Error: Program executed with error
16)​What is the output of print(10 // 3)
a)​ 3.333
b)​ 3
c)​ 4
d)​ 1
17)​What is the output of the following code?
my_list = [1, 2, 3, 4]
print(my_list[2])
a)​ 1
b)​ 2
c)​ 3
d)​ 4
18)​What is the output of the following code?
result = True and False
print(result)
a)​ True
b)​ False
c)​ Error
d)​ None
19)​Can we use the “else” block for for loop?
for example:
for i in range(1, 5):
print(i)
else:
print("this is else block statement" )
a)​ No
b)​ Yes
20)​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
21)​How do you remove an item from a dictionary with a specific key key_name?
a)​ del my_dict[key_name]
b)​ my_dict.remove(key_name)
c)​ my_dict.delete(key_name)
d)​ my_dict.pop_key(key_name)

Variables and Data Types


22)​Which built-in function is used to check the data type of a variable?
a)​ datatype()
b)​ type()
c)​ typeof()
d)​ info()
23)​What will be the output of the following Python code?
a)​ my_string = "Python"
b)​ print(my_string[2:5])
c)​ yth
d)​ tho
24)​What will be the output of the following Python code?
my_dict = {"a": 1, "b": 2}
my_dict["c"] = 3
my_dict["a"] = 10
print(my_dict["a"])
a)​ 1
b)​ 10
c)​ Error
d)​ {'a': 10, 'b': 2, 'c': 3}
25)​What will be the output of the following Python code?
data = (10, 20, 30)
a, b, c = data
print(b)
a)​ 10
b)​ 20
c)​ 30
d)​ Error
26)​What will be the output of the following Python code?
text = "hello world"
parts = text.split(" ")
print(parts[1])
a)​ hello
b)​ world
c)​ [‘hello’, ‘world’]
d)​ Error
27)​Which function is used to convert a string to an integer?
a)​ str_to_int()
b)​ int()
c)​ to_int()
d)​ parse_int()
28)​Select all valid String creation in Python
a)​ str1 = 'str1'
str1 = "str1"
str1 = '''str'''
b)​ str1 = 'str1'
str1 = "str1""
str1 = '''str1''
c)​ str1 = str(Jessa)
29)​What will be the output of the following code?
x = 50
def fun1():
x = 25
print(x)
fun1()
print(x)
a)​ NameError
b)​ 25
25
c)​ 25
50
30)​ What is the result of print(type([]) is list)
a)​ False
b)​ True
31)​What will be the output of the following code?
x = 75
def myfunc():
x=x+1
print(x)
myfunc()
print(x)
a)​ Error
b)​ 76
c)​ 1
d)​ None
32)​When would you typically use a tuple instead of a list?
a)​ When you need a collection that can be modified frequently.
b)​ When you need a collection of items that should not change.
c)​ When you need to perform mathematical operations on numbers.
d)​ When you need to quickly look up items by a key.
33)​What is the data type of the following
aTuple = (1, 'Jhon', 1+3j)
print(type(aTuple[2:3]))
a)​ list
b)​ complex
c)​ Tuple
34)​Please select the correct expression to reassign a global variable “x” to 20 inside
a function fun1()
x = 50
def fun1():
# your code to assign global x = 20
fun1()
print(x) # it should print 20
a)​ global x =20
b)​ global var x
x = 20
c)​ global.x = 20
d)​ global x
x = 20
35)​In Python 3, what is the output of type(range(5)). (What data type it will return).
a)​ int
b)​ list
c)​ range
d)​ None
36)​Which of the following Python data types is mutable?
a)​ tuple
b)​ str
c)​ int
d)​ List
37)​Which of the following data types can legally be used as a key in a Python
dictionary?
a)​ list
b)​ set
c)​ tuple (containing only immutable elements)
d)​ Dict
38)​What will be the output of the following Python code?
my_set = {1, 2, 3, 2, 1}
print(len(my_set))
a)​ 5
b)​ 3
c)​ Error
39)​Select the right way to create a string literal Ault'Kelly
a)​ str1 = 'Ault\\'Kelly'
b)​ str1 = 'Ault\'Kelly'
40)​What is the data type of print(type(0xFF))
a)​ number
b)​ hexint
c)​ hex
d)​ Int
41)​If my_string = "hello", what happens when you try to execute my_string[0] = 'H'?
a)​ The string becomes “Hello”.
b)​ A new string “Hello” is created and my_string refers to it.
c)​ A TypeError is raised.
d)​ The character at index 0 remains ‘h’.
42)​Which of the following characteristics best describes a Python set?
a)​ An ordered, mutable collection that allows duplicate elements.
b)​ An unordered, immutable collection of unique elements.
c)​ An unordered, mutable collection of unique elements.
d)​ An ordered, mutable collection of key-value pairs.
43)​What will be the output of the following code?
x = 50
def func1():
x = 75
return x
num = func1()
print(num)
a)​ 50
b)​ NameError
c)​ None
d)​ 75
44)​What will be the output of the following Python code?
my_tuple = (1, 2, 3)
my_tuple[0] = 5
print(len(my_tuple))
a)​ 1
b)​ 3
c)​ Error
d)​ (1, 2, 3)
45)​What will be the output of the following Python code?
a = [1, 2, 3]
b=a
b.append(4)
print(a)
print(b)
a)​ [1, 2, 3]
[1, 2, 3, 4]
b)​ [1, 2, 3, 4]
[1, 2, 3, 4]
c)​ [1, 2, 3, 4]
[1, 2, 3]
d)​ Error
46)​What is the output of print(type({}) is set)
a)​ True
b)​ False

Operators and Expression


47)​What is the output of print(2 ** 3 ** 2)
a)​ 64
b)​ 512
48)​Which of the following operators checks if two variables refer to the exact same
object in memory?
a)​ ==
b)​ in
c)​ =
d)​ Is
49)​What will be the output of the following addition (+) operator
a = [10, 20]
b=a
b += [30, 40]
print(a)
print(b)
a)​ [10, 20, 30, 40]
[10, 20, 30, 40]
b)​ [10, 20]
[10, 20, 30, 40]
50)​What will be the output of the following code?
x=6
y=2
print(x ** y)
print(x // y)
a)​ 66
0
b)​ 36
0
c)​ 66
3
d)​ 36
3
51)​4 is 100 in binary and 11 is 1011. What is the output of the following bitwise
operators?
a=4
b = 11
print(a | b)
print(a >> 2)
a)​ 15
1
b)​ 14
1
52)​What will be the output of the following Python code?
x = 10
y=3
print(x // y)
a)​ 3.33
b)​ 3
c)​ 4
d)​ 1
53)​What will be the output of the following Python code?
result = 10 / 2 * 3
print(result)
a)​ 5.0
b)​ 15.0
c)​ 1.666…
d)​ Error
54)​8. What will be the output of the following Python code?
x = 10
y = "2"
print(x + int(y))
a)​ 102
b)​ 12
c)​ Error
d)​ 10 + 2
55)​Bitwise shift operators (<<, >>) has higher precedence than Bitwise And(&)
operator
a)​ False
b)​ True
56)​Which operator is used to check if a sequence (like a string or list) contains a
specific element?
a)​ has
b)​ contains
c)​ in
d)​ Includes
57)​What will be the output of the following Python code?
print(bool(0))
print(bool(""))
print(bool([1]))
a)​ True
True
False
b)​ False
False
True
c)​ False
True
False
d)​ True
False
True
58)​12. What is the value of the following Python Expression
print(36 / 4)
a)​ 0.0
b)​ 9.0
c)​ 1.0
d)​ Error
59)​What will be the output of the following code?
x = 100
y = 50
print(x and y)
a)​ True
b)​ 100
c)​ False
d)​ 50
60)​What is the output of print(2%6)
a)​ ValueError
b)​ 0.33
c)​ 2
61)​What will be the output of the following Python code?
message = "Hello"
print('e' in message)
print('x' in message)
a)​ True
False
b)​ False
True
c)​ True
True
d)​ False
False
62)​In the expression result = a or b, what value is assigned to result if a is False and
b is True?
a)​ False
b)​ True
c)​ a
d)​ B
63)​What is the output of print(10 - 4 * 2)
a)​ 2
b)​ 12
64)​Which operator was introduced in Python 3.
a)​ ::
b)​ :=
c)​ ->
d)​ ??
65)​What will be the output of the following Python code?
print(5 % 2)
a)​ 0
b)​ 1
c)​ 2.5
d)​ Error
66)​What will be the output of the following Python code?
x=5
print(x > 3 and x < 10)
a)​ True
b)​ False
c)​ 0
d)​ Error
67)​What is the output of print(2 * 2 ** 2 * 2)
a)​ 16
b)​ 32
c)​ 64
d)​ 8
68)​What does the bitwise & operator do?
a)​ Performs logical AND on boolean values.
b)​ Performs bit-by-bit AND operation on integer binary representations.
c)​ Checks if both operands are true.
d)​ Concatenates two strings.
69)​Which of the following operators has the highest precedence?
a)​ not
b)​ &
c)​ *
d)​ +
70)​What will be the output of the following Python code?
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)
print(x is y)
a)​ True
True
b)​ False
False
c)​ True
False
d)​ False
True
71)​Which operation has higher precedence: multiplication (*) or addition (+)?
a)​ Multiplication
b)​ Addition
c)​ They have the same precedence
d)​ It depends on the order they appear

Input and Output


72)​ What will be the output of the following code?
print('PYnative ', end='//')
print(' is for ', end='//')
print(' Python Lovers', end='//')

a)​ PYnative /
is for /
Python Lovers /

b)​ PYnative //
is for //
Python Lovers //

c)​ PYnative // is for // Python Lovers//

d)​ PYnative / is for / Python Lovers/


73)​ What exception is typically raised when the input() function encounters an
End-of-File (EOF) condition (e.g., when reading from a redirected file and there’s
no more data)?
a)​ IOError
b)​ EOFError
c)​ StopIteration
d)​ FileNotFoundError
74)​ What is the output of the following print() function
print('Mike', 'Sydney', sep='**')

a)​ Error
b)​ MikeSydneyMikeSydney
c)​ MikeMikeSydneySydney
d)​ Mike**Sydney
75)​Which of the following is the correct way to get integer input from the user and
store it in a variable age
a)​ age = input("Enter your age: ")
b)​ age = int(input("Enter your age: "))
c)​ age = raw_input("Enter your age: ")
d)​ age = get_int("Enter your age: ")
76)​What will be the output of print('[%c]' % 65)
a)​ [65]
b)​ 65
c)​ A
d)​ Error
77)​What will be the output of the following print() function
print('%d %d %.2f' % (11, 22, 11.345))

a)​ 11 22 11.34
b)​ 11 22 11.35
c)​ 11 22 11.345
d)​ 11 22 11
78)​In Python 3, which functions is used to accept input from the user
a)​ input()
b)​ raw_input()
c)​ rawinput()
d)​ string()
79)​What will be the output of print('%x, %X' % (15, 15))
a)​ 15 15
b)​ F F
c)​ f f
d)​ f F
80)​What will be the output if the user types abc when prompted?
try:
num = int(input("Enter a number: "))
print(f"Number: {num}")
except ValueError:
print("Invalid input!")
except TypeError:
print("Type error!")
a)​ Number: abc
b)​ Invalid input!
c)​ Type error!
d)​ Program crashes
81)​What is the output of the following print() function
print(sep='--', 'Ben', 25, 'California')
a)​ Syntax Error
b)​ Ben–25–California
c)​ Ben 25 California
d)​ Ben–25 California
82)​In Python3, Whatever you enter as input, the input() function converts it into a
string
a)​ False
b)​ True

Functions
83)​1. What is the output of the following function call
84)​
85)​def add(a, b):
86)​ return a + b
87)​ a = 20
88)​ b = 30
89)​ return a + b
90)​
91)​result = add(5, 10)
92)​print(result)
93)​ (15, 50)
94)​ 15
95)​ 50
96)​ Error
97)​2. What is the output of the following function call
98)​
99)​def fun1(name, age=20):
100)​ print(name, age)
101)​
102)​ fun1('Emma', 25)
103)​ Emma 25
104)​ Emma 20
105)​ Error
106)​ 3. What is the output of the following display() function call
107)​
108)​ def display(**kwargs):
109)​ for i in kwargs:
110)​ print(i)
111)​
112)​ display(emp="Kelly", salary=9000)
113)​ Error
114)​ Kelly
115)​ 9000
116)​ (’emp’, ‘Kelly’)
117)​ (‘salary’, 9000)
118)​ emp
119)​ salary
120)​ 4. Which of the following best describes a Python lambda function?
121)​
122)​ A named function defined using the def keyword.
123)​ An anonymous, small, single-expression function.
124)​ A function that can only accept one argument.
125)​ A function used exclusively for mathematical operations.
126)​ 5. Select Correct Function
127)​
128)​ Which random module function would you use to get a random integer within a
specific range (inclusive of both ends)?
129)​
130)​ random.random()
131)​ random.uniform()
132)​ random.randint(a, b)
133)​ random.choice(sequence)
134)​ 6. Which of the following modules is NOT part of Python’s standard library
(meaning you usually need to install it separately)?
135)​
136)​ json
137)​ requests
138)​ datetime
139)​ re
140)​ 7. What will be the output of the following Python code?
141)​
142)​ list1 = [1, 2, 3]
143)​
144)​ def append_item(a_list, item):
145)​ a_list.append(item)
146)​
147)​ append_item(list1, 4)
148)​ print(list1)
149)​ [1, 2, 3]
150)​ [1, 2, 3, 4]
151)​ [4]
152)​ Error
153)​ 8. Select which is True for Python function
154)​
155)​ A function only executes when it is called and we can reuse it in a program
156)​ A function can take an unlimited number of arguments.
157)​ A Python function can return multiple values.
158)​ All of the above
159)​ 9. Choose the correct declaration for fun1().
160)​
161)​ To enable the successful execution of the below two fun1() function call,
choose the correct declaration for fun1().
162)​
163)​ fun1(25, 75, 55)
164)​ fun1(10, 20)
165)​ def fun1(**kwargs)
166)​ No, it is not possible in Python
167)​ def fun1(args*)
168)​ def fun1(*args)
169)​ 10. What is the output of the following display_person() function call
170)​
171)​ def display_person(*args):
172)​ for i in args:
173)​ print(i)
174)​
175)​ display_person(name="Emma", age="25")
176)​ Error
177)​ Emma
178)​ 25
179)​ name
180)​ age
181)​ 11. What will be the output of the following Python code?
182)​
183)​ x = 5
184)​
185)​ def update_x():
186)​ global x
187)​ x = 10
188)​
189)​ update_x()
190)​ print(x)
191)​ 5
192)​ 10
193)​ Error (Cannot assign to global variable)
194)​ None
195)​ 12. Select all correct function calls
196)​
197)​ Given the following function fun1() Please select all the correct function calls.
198)​
199)​ def fun1(name, age):
200)​ print(name, age)
201)​
202)​ fun1("Emma", age=23)
203)​ fun1(age =23, name="Emma")
204)​ fun1(name="Emma", 23)
205)​ fun1(age =23, "Emma")
206)​ 13. What is the output of the following display() function call
207)​
208)​ def display(**kwargs):
209)​ for i in kwargs.values():
210)​ print(i)
211)​
212)​ display(emp="Kelly", salary=9000)
213)​ Error
214)​ Kelly
215)​ 9000
216)​ emp: Kelly
217)​ salary: 9000
218)​ emp
219)​ salary
220)​ 14. What will be the output of the following Python code?
221)​
222)​ def do_nothing():
223)​ pass
224)​
225)​ result = do_nothing()
226)​ print(result)
227)​ do_nothing()
228)​ pass
229)​ None
230)​ Error
231)​ 15. What is a key characteristic of a Python generator function?
232)​
233)​ It uses the return keyword multiple times.
234)​ It stores all its results in memory before returning them.
235)​ It uses the yield keyword to produce a sequence of values lazily.
236)​ It can only be called once.
237)​ 16. What will be the output of the following Python code?
238)​
239)​ count = 0
240)​
241)​ def increment():
242)​ global count
243)​ count += 1
244)​
245)​ increment()
246)​ print(count)
247)​ 0
248)​ 1
249)​ Error
250)​ None
251)​ 17. What is the primary purpose of the if __name__ == "__main__": block in a
Python script?
252)​
253)​ To define global variables
254)​ To prevent the code from running when imported as a module
255)​ To mark the start of the script’s execution when run directly
256)​ To indicate that the script is a library
257)​ 18. How would you get the current date and time using the datetime module?
258)​
259)​ datetime.current_time()
260)​ datetime.datetime.now()
261)​ datetime.get_time()
262)​ 19. What will be the output of the following Python code?
263)​
264)​ def multiply(a, b):
265)​ return a * b
266)​
267)​ def apply_operation(func, x, y):
268)​ return func(x, y)
269)​
270)​ result = apply_operation(multiply, 5, 3)
271)​ print(result)
272)​ 8
273)​ 15
274)​ Error
275)​ multiply
276)​ 20. One of the main benefits of using modules in Python is:
277)​
278)​ They make your code run faster.
279)​ They allow you to reuse code across different programs.
280)​ They automatically fix errors in your code.
281)​ They reduce the amount of memory your program uses.
282)​ 21. Which of the following is the correct way to import only the pi constant
from the math module?
283)​
284)​ import math.pi
285)​ from math import pi
286)​ import pi from math
287)​ All of the above
288)​ 22. What is a “default argument” in a Python function?
289)​
290)​ An argument that must always be provided
291)​ An argument that has a predefined value if not supplied by the caller.
292)​ An argument that can only be passed by keyword.
293)​ An argument that is always a string.
294)​ 23. The os.path module in Python is primarily used for:
295)​
296)​ Interacting with the operating system’s environment variables.
297)​ Performing mathematical operations on file paths.
298)​ Pathname manipulation (e.g., joining paths, checking existence, getting file
extensions).
299)​ Managing processes and threads.
300)​ 24. Which sys module function/attribute is commonly used to exit a Python
script programmatically?
301)​
302)​ sys.exit()
303)​ sys.exit()
304)​ sys.terminate()
305)​ sys.shutdown()
306)​ 25. Which function from the random module would you use to get a random
integer between 1 and 10 (including 1 and 10)?
307)​
308)​ random.number(1, 10)
309)​ random.choice(1, 10)
310)​ random.randint(1, 10)
311)​ random.integer(1, 10)

You might also like