Beginners
- What is the output of the following code?
str = "pynative"
print(str[1:3])
- py
- yn
- pyn
- yna
- What is the output of the following code?
var1 = 1
var2 = 2
var3 = "3"
print(var1 + var2 + var3)
- 6
- 33
- 123
- Error. Mixing operators between numbers and strings are not supported
- 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,
- What is the output of the following code? ```python salary = 8000 def printSalary(): salary = 12000 print(“Salary:”, salary)
printSalary() print(“Salary:”, salary)
- [x] Salary: 12000
Salary: 8000
- [ ] Salary: 8000
Salary: 12000
- [ ] The program failed with errors
5. Which operator has higher precedence in the following list?
- [ ] % Modulus
- [ ] & BitWise AND
- [ ] **, Exponent
- [ ] > Comparison
6. What is the output of the following code?
```python
var = "James Bond"
print(var[2::-1])
- Jam
- dno
- maJ
- dnoB semaJ
- 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
- What is the output of the following code?
sampleList = ["Jon", "Kelly", "Jessa"]
sampleList.append(2, "Scott")
print(sampleList)
- The program executed with errors
- [‘Jon’, ‘Kelly’, ‘Scott’, ‘Jessa’]
- [‘Jon’, ‘Kelly’, ‘Jessa’, ‘Scott’]
- [‘Jon’, ‘Scott’, ‘Kelly’, ‘Jessa’]
- A string is immutable in Python? Every time when we modify the string, Python Always create a new String and assign a new string to that variable.
- True
- False
- Can we use the “else” clause for loops? for example:
for i in range(1, 5):
print(i)
else:
print("this is else block statement" )
- No
- Yes
- 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
- 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
- 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
- What is the output of the following
x = 36 / 4 * (3 + 2) * 4 + 2
print(x)
- 182.0
- 37
- 117
- The Program executed with errors
- What is the output of the following code?
valueOne = 5 ** 2
valueTwo = 5 ** 3
print(valueOne)
print(valueTwo)
- 10
15 - 25
125 - Error: invalid syntax
- 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
- What is the output of the following code?
sampleSet = {"Jodi", "Eric", "Garry"}
sampleSet.add(1, "Vicki")
print(sampleSet)
- {‘Vicki’, ‘Jodi’, ‘Garry’, ‘Eric’}
- {‘Jodi’, ‘Vicki’, ‘Garry’, ‘Eric’}
- The program executed with error
- What is the output of the following code?
var = "James" * 2 * 3
print(var)
- JamesJamesJamesJamesJamesJames
- JamesJamesJamesJamesJames
- Error: invalid syntax
Variables and Data Types
- What is the output of the following code
print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
- False True False True
- True True False True
- True True False True
- False True True True
- What is the output of the following code
x = 50
def fun1():
x = 25
print(x)
fun1()
print(x)
- NameError
- 25
25 - 25
50
- What is the result of
print(type([]) is list)
- False
- True
- Select all the valid String creation in Python
- str1 = “str1”
- str1 = ‘str1’
- str1 = “‘str1”‘
- str1 = str(“str1”)
- What is the output of the following variable assignment
x = 75
def myfunc():
x = x + 1
print(x)
myfunc()
print(x)
- Error
- 76
- 1
- None
- What is the data type of the following
aTuple = (1, 'Jhon', 1+3j)
print(type(aTuple[2:3]))
- list
- complex
- tuple
- What is the data type of
print(type(10))
- float
- integer
- int
- What is the result of
print(type({}) is set)
- True
- False
- Select the right way to create a string literal
Ault'Kelly
- str1 = ‘Ault\‘Kelly’
- str1 = ‘Ault\’Kelly’
- str1 = “””Ault’Kelly”””
- In Python 3, what is the type of
type(range(5))
- int
- list
- range
- What is the output of the following code
def func1():
x = 50
return x
func1()
print(x)
- 50
- NameError
- None
- Please select the correct expression to reassign a global variable
**x**
to 20 inside a functionfun1()
x = 50
def fun1():
# your code to assign global x = 20
fun1()
print(x) # it should print 20
- global x =20
- global var x
x = 20 - global.x = 20
- global x
x = 20
- What is the data type of
print(type(0xFF))
- number
- hexint
- hex
- int
Operators and Expresion
- What is the output of the following Python code
x = 10
y = 50
if (x ** 2 > 100 and y < 100):
print(x, y)
- 100 500
- 10 50
- None
- What is the output of
print(2 * 3 ** 3 * 4)
- 216
- 864
- What is the output of
print(10 - 4 * 2)
- 2
- 12
- Which of the following operators has the highest precedence?
- not
- &
- *
- +
- What is the output of the following code
x = 100
y = 50
print(x and y)
- True
- 100
- False
- 50
- What is the output of
print(2 ** 3 ** 2)
- 64
- 512
- What is the value of the following Python Expression
print(36 / 4)
- 9.0
- 9
- What is the output of
print(2%6)
- ValueError
- 0.33
- 2
- What is the output of the following code
x = 6
y = 2
print(x ** y)
print(x // y)
- 66
0 - 36
0 - 66
3 - 36
3
- What is the output of the following addition (
+
) operatora = [10, 20]
b = a
b += [30, 40]
print(a)
print(b)
- [10, 20, 30, 40]
[10, 20, 30, 40] - [10, 20]
[10, 20, 30, 40]
- What is the output of the expression
print(-18 // 4)
- -4
- 4
- -5
- 5
- What is the output of the following code
print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
- True True False True
- False True True True
- True True False True
- False True False True
- 4 is
100
in binary and 11 is1011
. What is the output of the following bitwise operators?a = 4
b = 11
print(a | b)
print(a >> 2)
- 15
1 - 14
1
- What is the output of the following assignment operator
y = 10
x = y += 2
print(x)
- 12
- 10
- SynatxError
- Bitwise shift operators (
<<
,>>
) has higher precedence than Bitwise And(&
) operator
- False
- True
Input and Output
- Which of the following is incorrect file handling mode in Python
- wb+
- ab
- xr
- ab+
- What is the output of the following
print()
functionprint(sep='--', 'Ben', 25, 'California')
- Syntax Error
- Ben–25–California
- Ben 25 California
- What is the output of the following code
print('PYnative ', end='//')
print(' is for ', end='//')
print(' Python Lovers', end='//')
[ ] PYnative /
is for /<br /> Python Lovers /<br />
[ ] PYnative //
is for //<br /> Python Lovers //
[ ] PYnative // is for // Python Lovers//
- PYnative / is for / Python Lovers/
- What is the output of
print('%x, %X' % (15, 15))
- 15 15
- F F
- f f
- f F
- Use the following file to predict the output of the code
test.txt Content:
Code:aaa
bbb
ccc
ddd
eee
fff
ggg
f = open("test.txt", "r")
print(f.readline(3))
f.close()
- bbb
- Syntax Error
- aaa
- aa
- In Python3, Whatever you enter as input, the
input()
function converts it into a string
- False
- True
- What is true for file mode
x
- create a file if the specified file does not exist
- Create a file, returns an error if the file exists
- Create a file if it doesn’t exists else Truncate the existed file
- Which of the following is incorrect file handling mode in Python
- r
- x
- t+
- b
- What is the output of
print('[%c]' % 65)
- 65
- A
- [A]
- Syntax Error
- What is the output of the following
print()
functionprint('%d %d %.2f' % (11, '22', 11.22))
- 11 22 11.22
- TypeError
- 11 ’22’ 11.22
- What will be displayed as an output on the screen
x = float('NaN')
print('%f, %e, %F, %E' % (x, x, x, x))
- nan, nan, NAN, NAN
- nan, NaN, nan, NaN
- NaN, NaN, NaN, NaN,
- In Python3, which functions are used to accept input from the user
- input()
- raw_input()
- rawinput()
- string()
Functions
- Choose the correct function declaration of
fun1()
so that we can execute the following function call successfullyfun1(25, 75, 55)
fun1(10, 20)
- def fun1(**kwargs)
- No, it is not possible in Python
- def fun1(args*)
- def fun1(*data)
- Python function always returns a value
- False
- True
- What is the output of the following code
def outerFun(a, b):
def innerFun(c, d):
return c + d
return innerFun(a, b)
res = outerFun(5, 10)
print(res)
- 15
- Syntax Error
- (5, 10)
- What is the output of the following
displayPerson()
function calldef displayPerson(*args):
for i in args:
print(i)
displayPerson(name="Emma", age="25")
- TypeError
- Emma
25 - name
age
- Select which is true for Python function
- A Python function can return only a single value
- A function can take an unlimited number of arguments.
- A Python function can return multiple values
- Python function doesn’t return anything unless and until you add a return statement
- What is the output of the following function call
def outerFun(a, b):
def innerFun(c, d):
return c + d
return innerFun(a, b)
return a
result = outerFun(5, 10)
print(result)
- 5
- 15
- (15, 5)
- Syntax Error
- Given the following function
fun1()
Please select the correct function callsdef fun1(name, age):
print(name, age)
- fun1(name=’Emma’, age=23)
- fun1(name=’Emma’, 23)
- fun1(‘Emma’, 23)
- Select which true for Python function
- A function is a code block that only executes when it is called.
- Python function always returns a value.
- A function only executes when it is called and we can reuse it in a program
- Python doesn’t support nested function
- What is the output of the
add()
function calldef add(a, b):
return a+5, b+5
result = add(3, 2)
print(result)
- 15
- 8
- (8, 7)
- Syntax Error
- What is the output of the following function call
def fun1(num):
return num + 25
fun1(5)
print(num)
- 25
- 5
- NameError
- What is the output of the following
display()
function calldef display(**kwargs):
for i in kwargs:
print(i)
display(emp="Kelly", salary=9000)
- TypeError
- Kelly
9000 - (’emp’, ‘Kelly’)
(‘salary’, 9000) - emp
salary
- What is the output of the following function call
def fun1(name, age=20):
print(name, age)
fun1('Emma', 25)
- Emma 25
- Emma 20
Conditions and Loops
- Given the nested
if-else
structure below, what will be the value ofx
after code execution completesx = 0
a = 0
b = -5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
- 2
- 0
- 3
- 4
- What is the output of the following nested loop
for num in range(10, 14):
for i in range(2, num):
if num%i == 1:
print(num)
break
- 10
11
12
13 - 11
13
- Select which is true for
for
loop
- Python’s for loop used to iterates over the items of list, tuple, dictionary, set, or string
- else clause of for loop is executed when the loop terminates naturally
- else clause of for loop is executed when the loop terminates abruptly
- We use for loop when we want to perform a task indefinitely until a particular condition is met
- Given the nested
if-else
below, what will be the valuex
when the code executed successfullyx = 0
a = 5
b = 5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
- 0
- 4
- 2
- 3
- What is the output of the following loop
for l in 'Jhon':
if l == 'o':
pass
print(l, end=", ")
- J, h, n,
- J, h, o, n,
- What is the value of x
x = 0
while (x < 100):
x+=2
print(x)
- 101
- 99
- None of the above, this is an infinite loop
- 100
- What is the value of the
var
after the for loop completes its executionvar = 10 for i in range(10): for j in range(2, 10, 1): if var % 2 == 0: continue var += 1 var+=1 else: var+=1 print(var)
- 20
- 21
- 10
- 30
- What is the output of the following
range()
functionfor num in range(2,-5,-1): print(num, end=", ")
- 2, 1, 0
- 2, 1, 0, -1, -2, -3, -4, -5
- 2, 1, 0, -1, -2, -3, -4
- What is the output of the following nested loop
numbers = [10, 20] items = ["Chair", "Table"] for x in numbers: for y in items: print(x, y)
- 10 Chair
10 Table
20 Chair
20 Table - 10 Chair
10 Table
- What is the output of the following
if
statementa, b = 12, 5 if a + b: print('True') else: print('False')
- False
- True
if -3
will evaluate to true
- True
- False
- What is the value of
x
after the following nested for loop completes its executionx = 0 for i in range(10): for j in range(-1, -10, -1): x += 1 print(x)
- 99
- 90
- 100
- What is the output of the following for loop and
range()
functionfor num in range(-2,-5,-1): print(num, end=", ")
- -2, -1, -3, -4
- -2, -1, 0, 1, 2, 3,
- -2, -1, 0
- -2, -3, -4,
Numbers
- What is the output of the following math function
import math print(math.ceil(252.4)) print(math.floor(252.4))
- 252
252 - 252
253 - 253
252
- Select all correct float numbers
- a = 10.1256
- b = -10.5
- c = 42e3
- d = -68.7e100
- What is the output of the following
round()
function callprint(round(100.2563, 3)) print(round(100.000056, 3))
- 100.256
100 - 100.256
100.000 - 100.256
100.0
- Select which is right for Python integers
- An integer is a whole number that can be positive or negative
- In Python 3, Integers have unlimited precision
- What is the output of the following code
print(0b101) print(0o10) print(0x1F)
- 101
10
1F - 5
8
31 - Syntax Error: Invalid Token
- What is the output of the following number conversion
z = complex(1.25)
- (1.25+0j)
- Value Error: Missing an imaginary part of a complex number
- In Python 3, the integer ranges from -2,147,483,648 to +2,147,483,647
- False
- True
- What is the output of the following code
print(int(2.999))
- ValueError: invalid literal for int()
- 3
- 2
- What is the output of the following
isinstance()
functionfrom numbers import Number from decimal import Decimal from fractions import Fraction print(isinstance(2.0, Number)) print(isinstance(Decimal('2.0'), Number)) print(isinstance(Fraction(2, 1), Number)) print(isinstance("2", Number))
- True
False
True
True - True
True
True
True - True
False
True
False - True
True
True
False
- What is the type of the following variable
x = -5j
- int
- complex
- real
- imaginary
- What is the output of
print(abs(-45.300))
- 45.3
- -45.3
- -45.300
- 45.300
- What is the output of the following number comparison function call
print( (1.1 + 2.2) == 3.3 )
- True
- False
Strings
- Choose the correct function to get the character from ASCII number
- ascii(‘number)
- char(number)
- chr(number)
- Select the correct output of the following String operations
myString = "pynative" stringList = ["abc", "pynative", "xyz"] print(stringList[1] == myString) print(stringList[1] is myString)
- true false
- true true
- What is the output of the following code
str1 = "My salary is 7000"; str2 = "7000" print(str1.isdigit()) print(str2.isdigit())
- False
True - False
False - True
False
- Select the correct output of the following String operations
strOne = str("pynative") strTwo = "pynative" print(strOne == strTwo) print(strOne is strTwo)
- false false
- true true
- true false
- false true
- Strings are immutable in Python, which means a string cannot be modified.
- True
- False
- Which method should I use to convert String
"welcome to the beautiful world of python"
to"Welcome To The Beautiful World Of Python"
- capitalize()
- title()
- Choose the correct function to get the ASCII code of a character
- char(‘char’)
- ord(‘char’)
- ascii(‘char’)
- Select the correct output of the following String operations
str = "my name is James bond"; print (str.capitalize())
- My Name Is James Bond
- TypeError: unsupported operand type(s) for * or pow(): ‘str’ and ‘int’
- My name is james bond
- Guess the correct output of the following String operations
str1 = 'Welcome' print(str1*2)
- WelcomeWelcome
- TypeError: unsupported operand type(s) for * or pow(): ‘str’ and ‘int’
- What is the output of the following string operations
str = "My salary is 7000"; print(str.isalnum())
- True
- False
- Guess the correct output of the following code?
str1 = "PYnative" print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
- PYn PYnat ive PYnativ vitanYP
- Yna PYnat tive PYnativ vitanYP
- Yna PYnat tive PYnativ PYnativ
- Select the correct output of the following String operations
str1 = 'Welcome' print (str1[:6] + ' PYnative')
- Welcome PYnative
- WelcomPYnative
- Welcom PYnative
- WelcomePYnative
- Python does not support a character type; a single character is treated as strings of length one.
- False
- True
- What is the output of the following string comparison
print("John" > "Jhon") print("Emma" < "Emm")
- True
False - False
False
- Select the correct output of the following String operations
str1 = "my isname isisis jameis isis bond"; sub = "is"; print(str1.count(sub, 4))
- 5
- 6
- 7
Lists
- Select all the correct options to join two lists in Python
listOne = ['a', 'b', 'c', 'd'] listTwo = ['e', 'f', 'g']
- newList = listOne + listTwo
- newList = extend(listOne, listTwo)
- newList = listOne.extend(listTwo)
- newList.extend(listOne, listTwo)
- What is the output of the following list comprehension
resList = [x+y for x in ['Hello ', 'Good '] for y in ['Dear', 'Bye']] print(resList)
- [‘Hello Dear’, ‘Hello Bye’, ‘Good Dear’, ‘Good Bye’]
- [‘Hello Dear’, ‘Good Dear’, ‘Hello Bye’, ‘Good Bye’]
- What is the output of the following list function? ```python sampleList = [10, 20, 30, 40, 50] sampleList.append(60) print(sampleList)
sampleList.append(60) print(sampleList)
- [ ] [10, 20, 30, 40, 50, 60]<br /> [10, 20, 30, 40, 50, 60]
- [ ] [10, 20, 30, 40, 50, 60]<br /> [10, 20, 30, 40, 50, 60, 60]
4. What is the output of the following list function?
```python
sampleList = [10, 20, 30, 40, 50]
sampleList.pop()
print(sampleList)
sampleList.pop(2)
print(sampleList)
- [20, 30, 40, 50]
[10, 20, 40] - [10, 20, 30, 40]
[10, 20, 30, 50] - [10, 20, 30, 40]
[10, 20, 40]
- What is the output of the following
aList = [5, 10, 15, 25] print(aList[::-2])
- [15, 10, 5]
- [10, 5]
- [25, 10]
- What is the output of the following code
list1 = ['xyz', 'zara', 'PYnative'] print (max(list1))
- PYnative
- zara
- What is the output of the following list assignment
aList = [4, 8, 12, 16] aList[1:4] = [20, 24, 28] print(aList)
- [4, 20, 24, 28, 8, 12, 16]
- [4, 20, 24, 28]
- What is the output of the following code?
sampleList = [10, 20, 30, 40] del sampleList[0:6] print(sampleList)
- []
- list index out of range.
- [10, 20]
- What is the output of the following list operation
aList = [10, 20, 30, 40, 50, 60, 70, 80] print(aList[2:5]) print(aList[:4]) print(aList[3:])
- [20, 30, 40, 50]
[10, 20, 30, 40]
[30, 40, 50, 60, 70, 80] - [30, 40, 50]
[10, 20, 30, 40]
[40, 50, 60, 70, 80]
- What is the output of the following
l = [None] * 10 print(len(l))
- 10
- 0
- Syntax Error
- What is the output of the following list operation
sampleList = [10, 20, 30, 40, 50] print(sampleList[-2]) print(sampleList[-4:-1])
- 40
[20, 30, 40] - IndexError: list index out of range
- Select all the correct options to copy a list
aList = ['a', 'b', 'c', 'd']
- newList = copy(aList)
- newList = aList.copy()
- newList.copy(aList)
- newList = list(aList)
- What is the output of the following code
my_list = ["Hello", "Python"] print("-".join(my_list))
- HelloPython-
- Hello-Python
- -HelloPython
- What is the output of the following
aList = [1, 2, 3, 4, 5, 6, 7] pow2 = [2 * x for x in aList] print(pow2)
- [2, 4, 6, 8, 10, 12, 14]
- [2, 4, 8, 16, 32, 64, 128]
- What is the output of the following code
aList = ["PYnative", [4, 8, 12, 16]] print(aList[0][1]) print(aList[1][3])
- P 8
Y 16 - P
12 - Y
16
- In Python,
list
is mutable
- False
- True
Set
- What is the output of the following code
sampleSet = {"Yellow", "Orange", "Black"} print(sampleSet[1])
- Yellow
- Syntax Error
- Orange
- What is the output of the following set operation.
set1 = {"Yellow", "Orange", "Black"} set2 = {"Orange", "Blue", "Pink"} set1.difference_update(set2) print(set1)
- {‘Black’, ‘Yellow’}
- {‘Yellow’, ‘Orange’, ‘Black’, ‘Blue’, ‘Pink’}
- What is the output of the following
sampleSet = {"Yellow", "Orange", "Black"} sampleSet.add("Blue") sampleSet.add("Orange") print(sampleSet)
- {‘Blue’, ‘Orange’, ‘Yellow’, ‘Orange’, ‘Black’}
- {‘Blue’, ‘Orange’, ‘Yellow’, ‘Black’}
- The
isdisjoint()
method returns True if none of the items are present in both sets, otherwise, it returns False.
- True
- False
- Select which is true for Python
set
- Sets are unordered.
- Set doesn’t allow duplicate
- sets are written with curly brackets {}
- set Allows duplicate
- set is immutable
- a set object does support indexing
- What is the output of the following union operation
set1 = {10, 20, 30, 40} set2 = {50, 20, "10", 60} set3 = set1.union(set2) print(set3)
- {40, 10, 50, 20, 60, 30}
- {40, ’10’, 50, 20, 60, 30}
- {40, 10, ’10’, 50, 20, 60, 30}
- SynatxError: Different types cannot be used with sets
- What is the output of the following set operation
set1 = {"Yellow", "Orange", "Black"} set2 = {"Orange", "Blue", "Pink"} set3 = set2.difference(set1) print(set3)
- {‘Yellow’, ”Black’, ‘Pink’, ‘Blue’}
- {‘Pink’, ‘Blue’}
- {‘Yellow’, ”Black’}
- What is the output of the following code
aSet = {1, 'PYnative', ('abc', 'xyz'), True} print(aSet)
- TypeError
- {‘PYnative’, 1, (‘abc’, ‘xyz’), True}
- {‘PYnative’, 1, (‘abc’, ‘xyz’)}
- What is the output of the following
set1 = {10, 20, 30, 40, 50} set2 = {60, 70, 10, 30, 40, 80, 20, 50} print(set1.issubset(set2)) print(set2.issuperset(set1))
- False
False - True
True
- Select all the correct options to remove “Orange” from the set.
sampleSet = {"Yellow", "Orange", "Black"}
- sampleSet.pop(“Orange”)
- sampleSet.discard(“Orange”)
- sampleSet.discard(“Orange”)
- del sampleSet [“Orange”]
- The
symmetric_difference()
method returns a set that contains all items from both sets, but not the items that are present in both sets.
- False
- True
- What is the output of the following code
aSet = {1, 'PYnative', ['abc', 'xyz'], True} print(aSet)
- {1, ‘PYnative’, [‘abc’, ‘xyz’]}
- {1, ‘PYnative’, [‘abc’, ‘xyz’], True}
- TypeError
- The union() method returns a new set with all items from both sets by removing duplicates
- True
- False
- What is the output of the following set operation
sampleSet = {"Yellow", "Orange", "Black"} sampleSet.update(["Blue", "Green", "Red"]) print(sampleSet)
- {‘Yellow’, ‘Orange’, ‘Red’, ‘Black’, ‘Green’, ‘Blue’}
- {‘Yellow’, ‘Orange’, ‘Black’, [“Blue”, “Green”, “Red”]}
- TypeError: update() doesn’t allow list as a argument.
- What is the output of the following
sampleSet = {"Yellow", "Orange", "Black"} sampleSet.discard("Blue") print(sampleSet)
- {‘Yellow’, ‘Orange’, ‘Black’}
- KeyError: ‘Blue’
- Select all the correct ways to copy two sets
- set2 = set1.copy()
- set2 = set1
- set2 = set(set1)
- set2.update(set1)
Dictionary
- Please select all correct ways to empty the following dictionary
student = { "name": "Emma", "class": 9, "marks": 75 }
- del student
- del student[0:2]
- student.clear()
- In Python, Dictionaries are immutable
- False
- True
- Select the correct way to print Emma’s age.
student = {1: {'name': 'Emma', 'age': '27', 'sex': 'Female'}, 2: {'name': 'Mike', 'age': '22', 'sex': 'Male'}}
- student[0][1]
- student[1][“age”]
- student[0][“age”]
- Select the correct ways to get the value of marks key.
student = { "name": "Emma", "class": 9, "marks": 75 }
- m = student.get(2)
- m = student.get(‘marks’)
- m = student[2])
- m = student[‘marks’])
- Select the all correct way to remove the key ‘marks‘ from a dictionary
student = { "name": "Emma", "class": 9, "marks": 75 }
- student.pop(“marks”)
- del student[“marks”]
- student.popitem()
- dict1.remove(“key2”)
- Select correct ways to create an empty dictionary
- sampleDict = {}
- sampleDict = dict()
- sampleDict = dict{}
- What is the output of the following
sampleDict = dict([ ('first', 1), ('second', 2), ('third', 3) ]) print(sampleDict)
- [ (‘first’, 100), (‘second’, 200), (‘third’, 300) ]
- Options: SyntaxError: invalid syntax
- {‘first’: 1, ‘second’: 2, ‘third’: 3}
- Select the correct way to access the value of a history subject
sampleDict = { "class":{ "student":{ "name":"Mike", "marks":{ "physics":70, "history":80 } } } }
- sampleDict[‘class’][‘student’][‘marks’][‘history’]
- sampleDict[‘class’][‘student’][‘marks’][1]
- sampleDict[‘class’][0][‘marks’][‘history’]
- What is the output of the following dictionary operation
dict1 = {"name": "Mike", "salary": 8000} temp = dict1.get("age") print(temp)
- KeyError: ‘age’
- None
- Items are accessed by their position in a dictionary and All the keys in a dictionary must be of the same type.
- True
- False
- Select all correct ways to copy a dictionary in Python
- dict2 = dict1.copy()
- dict2 = dict(dict1.items())
- dict2 = dict(dict1)
- dict2 = dict1
- What is the output of the following code
dict1 = {"key1":1, "key2":2} dict2 = {"key2":2, "key1":1} print(dict1 == dict2)
- True
- False
- Dictionary keys must be immutable
- True
- False
- What is the output of the following dictionary operation
dict1 = {"name": "Mike", "salary": 8000} temp = dict1.pop("age") print(temp)
- KeyError: ‘age’
- None
Tuple
- Select which is true for Python tuple
- A tuple maintains the order of items
- A tuple is unordered
- We cannot change the tuple once created
- We can change the tuple once created
- What is the type of the following variable
aTuple = ("Orange") print(type(aTuple))
- list
- tuple
- array
- str
- What is the output of the following
aTuple = "Yellow", 20, "Red" a, b, c = aTuple print(a)
- (‘Yellow’, 20, ‘Red’)
- TyepeError
- Yellow
- What is the output of the following code
aTuple = (100, 200, 300, 400, 500) print(aTuple[-2]) print(aTuple[-4:-1])
- IndexError: tuple index out of range
- 400
(200, 300, 400)
- What is the output of the following
tuple1 = (1120, 'a') print(max(tuple1))
- TypeError
- 1120
- ‘a’
- What is the output of the following code
aTuple = (100, 200, 300, 400, 500) aTuple[1] = 800 print(aTuple)
- TypeError
- (100, 800, 200, 300, 400, 500)
- (800, 100, 200, 300, 400, 500)
- Select true statements regarding the Python tuple
- We can remove the item from tuple but we cannot update items of the tuple
- We cannot delete the tuple
- We cannot remove the items from the tuple
- We cannot update items of the tuple.
- What is the output of the following tuple operation
aTuple = (100,) print(aTuple * 2)
- TypeError
- (100, 100)
- (200)
- What is the output of the following tuple operation
aTuple = (100, 200, 300, 400, 500) aTuple.pop(2) print(aTuple)
- (100, 200, 400, 500)
- (100, 300, 400, 500)
- AttributeError
- A Python tuple can also be created without using parentheses
- False
- True
- Choose the correct way to access value 20 from the following tuple
aTuple = ("Orange", [10, 20, 30], (5, 15, 25))
- aTuple[1:2][1]
- aTuple1:2
- aTuple[1:2][1]
- aTuple[1][1]
- What is the output of the following
aTuple = (10, 20, 30, 40, 50, 60, 70, 80) print(aTuple[2:5], aTuple[:4], aTuple[3:])
- (30, 40, 50) (10, 20, 30, 40) (40, 50, 60, 70, 80)
- (20, 30, 40, 50) (10, 20, 30, 40) (30, 40, 50, 60, 70, 80)
Random Data Generation
- To generate a random secure Universally unique ID which method should I use
- uuid.uuid4()
- uuid.uuid1()
- uuid.uuid3()
- random.uuid()
str = “PYnative”
.
Choose the correct function to pick a single character from a given string randomly.
- random.sample(str)
- random.choice(str)
- random.get(str, 1)
- To Generate a random secure integer number, select all the correct options.
- random.SystemRandom().randint()
- random.System.randint()
- secrets.randbelow()
- Choose the correct function from the following list to get the random integer between 99 to 200, which is divisible by 3.
- random.randrange(99, 200, 3)
- random.randint(99, 200, 3)
- random.random(99, 200, 3)
numberList = [100, 200, 300, 400, 500]
Choose the correct function to get 3 elements from the list randomly in such a way that each element of the list has a different probability of being selected.
-
random.choices(numberList, weights=(10, 5, 15, 20, 50), k=3)
-
random.choice(numberList, weights=(10, 5, 15, 20, 50), k=3)
-
random.sample(numberList, weights=(10, 5, 15, 20, 50), k=3)
- Which method should i use to capture and change the current state of the random generator
[ ] random.getstate()
random.setstate(state)
[ ] random.currentstate()
random.setcurrentstate(state)
- I want to generate a random secure hex token of 32 bytes to reset the password, which method should I use
- secrets.hexToken(32)
- secrets.hex_token(32)
- secrets.tokenHex(32)
- secrets.token_hex(32)
random.seed()
method is used to initialize the pseudorandom number generator. The random module uses the seed value as a base to generate a random number. If seed value is not present, it takes the system’s current time.
- True
- False
- To generate a random float number between 20.5 to 50.5, which function of a random module I need to use
- random.random(20.5, 50.5)
- random.uniform(20.5, 50.5)
- random.randrange(20.5, 50.5)
- random.randfloat(20.5, 50.5)
- Which method should I use to get 4 elements from the following list randomly
samplelist = [20, 40, 60, 80, 100, 120, 140, 160, 180, 200]
- random.choice(samplelist, 4)
- random.sample(samplelist, 4)