string="The current world population is 7.9 billion as of December 2021"
y=[xforxin(xforxinstringifx.isdigit())]
print(y)
[7, 9, 2, 0, 2, 1]
[‘7’, ‘9’, ‘2’, ‘0’, ‘2’, ‘1’]
[7, 2, 0, 2, 1]
Runtime error!
Answer: 2 Explanation: This is an example of list comprehension in Python. List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. List Comprehensions
Q2. What are the name of the operators |, &, >> and << respectively?
AND, OR, RIGHT SHIFT and LEFT SHIFT
AND, OR, LEFT SHIFT and RIGHT SHIFT
OR, AND, LEFT SHIFT and RIGHT SHIFT
OR, AND, RIGHT SHIFT and LEFT SHIFT
Answer: 4 Explanation: OR, AND, RIGHT SHIFT and LEFT SHIFT are name of the |, &, >> and << operators respectively.
Q3. What will be the output of following code:/code/
1
2
3
4
a=[1,2]
b=a
b+=[3,4]
print(a,b)
[1, 2] [1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4]
[1, 2, 3, 4] [1, 2]
[1, 2] [3, 4]
Answer: 2 Explanation: In Python language, assigning one variable to another variable creates an alias of each variable. Here variable b is an alias of variable a. It means both variable a and b points to the same object in the memory. That’s why when you are updating the value of variable b, value of variable a is also changing.
Q4. Python is an object oriented programming language.
True
False
Answer: 1 Explanation: Python is an object oriented programming language. Almost everything in Python is an object. A class in Python is a blueprint for creating objects.
Q5. What will be the output of following code:
1
2
3
num=(1,2,3,4,5)
(a,b,*c)=num
print(a,b,c)
1 2 (3, 4, 5)
1 2 3
1 2 [3, 4, 5]
ValueError
Answer: 3 Explanation: *variable is basically extended iterable unpacking. It will assign a list of all items not assigned to a regular variable name. So from tuple “num”, 1 is assinged to a, 2 is assigned to b and rest all items will be assigend as list to c. Extended Iterable Unpacking
Q6. Which of the following statement is the correct way of assigning an integer value to a variable in Python?
x = ‘6’
int x = 6;
x = 6
Integer x = 6;
Answer: 3 Explanation: x = ‘6’ -> will create a string data type. int x = 6; and Integer x = 6; -> It is a way of assignment and declaration in Java language.
In Python there is no need to declare the data type using int or Integer. It will create a variable with respect to the type of data you assing to it.
Q7. What is the syntax for multiline comment in Python?
Use /* at the start of the first line and use */ at the end of the last line.
Use <-- at the start of the first line and use --> at the end of the last line.
Python does not have syntax for multiline comment. You can either use # at the start of every line or make it as multiline string (triple quote).
None of the above.
Answer: 3 Explanation: Since Python does not have syntax for multiline comments, there is a workaround. As Python ignores the string literal if it is not assigned to a variable, we can add multiline string in our code and place the multiline comment inside it.
Q8. What will be the output of following code:
1
2
3
4
5
6
7
8
i=1
whilei<5:
print(i,end=" ")
i+=1
ifi==4:
break
else:
print(i+1)
1 2 3
1 2 3 4
1 2 3 5
1 2 3 4 5
Answer: 1 Explanation: while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed. Reference: While Loop