Assignment-Questions-Python 2 Feb24
Assignment-Questions-Python 2 Feb24
5) Write a program to calculate profit and loss. Input is selling price and cost price.
MCQ Question:
1. All keywords in Python are in _________
a) lower case
b) UPPER CASE
c) Capitalized
d) None of the mentioned
2. Which of the following is true for variable names in Python?
a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special characters allowed
d) none of the mentioned
3. Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
4. Which of the following cannot be a variable?
a) __init__
b) in
c) it
d) on
5. Which of these in not a core data type?
a) Lists
b) Dictionary
c) Tuples
d) Class
6. What will be the output of the following Python code?
>>>str="hello"
>>>str[:2]
a) he c) olleh
b) lo d) hello
7. Which of the following will run without errors?
a) round(45.8) c) round(6352.898,2,5)
b) round() d) round(7463.123,2,1)
8. What is the return type of function id?
a) int b) float
c) bool d) dict
9. What error occurs when you execute the following Python code snippet? apple
= mango
a)SyntaxError b) NameError
c) ValueError d) TypeError
10. What will be the output of the following Python code snippet?
def example(a):
a = a + '2'
a = a*2
return a
>>>example("hello")
a) indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2
11. What data type is the object below?
L = [1, 23, 'hello', 1]
a) list
b) dictionary
c) array
d) tuple
12. In order to store values in terms of key and value we use what core data
type.
a) list b) tuple
c) class d) dictionary
13. Which of the following is not a complex number?
a) k = 2 + 3j b) k = complex(2, 3)
c) k = 2 + 3l d) k = 2 + 3J
14. What is the type of inf?
a) Boolean b) Integer
c) Float d) Complex
15. What does ~4 evaluate to?
a) -5 b) -4
c) -3 d) +3
16. What does ~~~~~~5 evaluate to?
a) +5 b) -11
c) +11 d) -5
17. Which of the following is incorrect?
a) x = 0b101 b) x = 0x4f5
c) x = 19023 d) x = 03964
18. What is the result of cmp(3, 1)?
a) 1 b) 0
c) True d) False
19. Which of the following is incorrect?
a) float(‘inf’) b) float(‘nan’)
c) float(’56’+’78’) d) float(’12+34′)
20. What is the result of round(0.5) – round(-0.5)?
a) 1.0 b) 2.0
c) Value depends on Python version d) 0.0
21. What does 3 ^ 4 evaluate to?
a) 81 b) 12
c) 0.75 d) 7
22. Which is the correct operator for power(xy)?
a) X^y b) X**y
c) X^^y d) None of the mentioned
23. Which one of these is floor division?
a) / b) //
c) % d) None of the mentioned
24. What is the order of precedence in python?
i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi d) i,ii,iii,iv,vi,v
25. What is the answer to this expression, 22 % 3 is?
a) 7 b) 1
c) 0 d) 5
26. Mathematical operations can be performed on a string.
a) True b) False
27. Operators with the same precedence are evaluated in which manner?
a) Left to Right b) Right to Left
c) Can’t say d) None of the mentioned
28. What is the output of this expression, 3*1**3?
a) 27 b) 9
c) 3 d) 1
29. Which one of the following has the same precedence level?
a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication
30. The expression Int(x) implies that the variable x is converted to integer.
a) True b) False
31. Which one of the following has the highest precedence in the expression?
a) Exponential b) Addition
c) Multiplication d) Parentheses
32. Is Python case sensitive when dealing with identifiers?
a) yes
b) no
c) machine dependent
d) none of the mentioned
33. What is the maximum possible length of an identifier?
a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned
34. Which of the following is invalid?
a) _a = 1
b) __a = 1
c) __str__ = 1
d) none of the mentioned
35. Which of the following is an invalid variable?
a) my_string_1
b) 1st_string
c) foo
d) _
36. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution
37. Which of the following is not a keyword?
a) evalb) assertc) nonlocald) pass
2. Python Operators and Control Flow Statements
Descriptive Question:
1) Explain arithmetic operators in python with an example.
2) Explain bitwise operators in python with an example.
3) Explain membership operator and identity operators in python with
examples.
4) Explain if-else statement with example.
5) Show the use of keyword elif in python with example.
6) How to use short-hand if-else statement in python. Explain with
example.
7) Explain for-loop in python with example.
8) Can we use keyword else with any loop? Justify your answer.
9) State the use of keyword pass.
10) Explain the use of keywords break and continue in python.
MCQ Question:
1. Which of the following is not used as loop in Python?
a) for loop b) while loop
c) do-while loop d)None of the above
2. Which of the following is False regarding loops in Python?
a) Loops are used to perform certain tasks repeatedly.
b) While loop is used when multiple statements are to executed repeatedly
until
the given condition becomes False
c) While loop is used when multiple statements are to executed repeatedly
until the
given condition becomes True
d) for loop can be used to iterate through the elements of lists.
3. Which of the following is True regarding loops in Python?
a) Loops should be ended with keyword "end".
b) No loop can be used to iterate through the elements of strings.
c) Keyword "break" can be used to bring control out of the current loop.
d) Keyword "continue" is used to continue with the remaining statements
inside the
loop.
4. How many times will the loop run?
i=2
while(i>0):
i=i-1
a) 2 b) 3
c) 1 d) 0
5. What will be the output of the following Python code?
list1 = [3 , 2 , 5 , 6 , 0 , 7, 9]
sum = 0
sum1 = 0
forelem in list1:
if (elem % 2 == 0):
sum = sum + elem
continue
if (elem % 3 == 0):
sum1 = sum1 + elem
print(sum , end=" ")
print(sum1)
a) 8 9 b) 8 3
c) 2 3 d) 8 12
6. Which one of the following is a valid Python if statement :
a) if a>=2: b) if (a >= 2)
c) if (a => 22) d) if a >= 22
7. What keyword would you use to add an alternative condition to an if
statement?
a) else if b) elseif
c) elif d) None of the above
8. Can we write if/else into one line in python?
a) Yes b) No
c) if/else not used in python d) None of the above
9. In a Python program, a control structure:
a) Defines program-specific data structures
b) Directs the order of execution of the statements in the program
c) Dictates what happens before the program starts and after it terminates
d) None of the above
10. What will be output of this expression:
'p' + 'q' if '12'.isdigit() else 'r' + 's'
a) pq b) rs
c) pqrs d) pq12
11. Which statement will check if a is equal to b?
a) if a = b: b) if a == b:
c) if a === c: d) if a == b
12. Does python have switch case statement?
a) True
b) False
c) Python has switch statement but we can not use it.
d) None of the above
13. What will be the output of given Python code?
n=7; c=0
while(n):
if(n>5):
c=c+n-1
n=n-1
else:
break
print(n)
print(c)
a) 2 b) 6 5 2
c) 3 d) 5 2
14. What will be the output of given Python code?
str1="hello"
c=0
for x in str1:
if(x!="l"):
c=c+1
else:
pass
print(c)
a) 2 b) 0
c) 4 d) 3
15. Which of the following Python code will give different output from the
others?
a) for i in range(0,5):
print(i)
b) for j in [0,1,2,3,4]:
print(j)
c) for k in [0,1,2,3,4,5]:
print(k)
d) for l in range(0,5,1):
print( l)
16. What will be the output of the following Python code?
str1="learn python"
str2=""
str3=""
for x in str1:
if(x=="r" or x=="n" or x=="p"):
str2+=x
pass
if(x=="r" or x=="e" or x=="a"):
str3+=x
print(str2,end=" ")
print(str3)
a) rnpnea b) rnpn ear
c) rnpea d) rnp ear
17. What will be the output of the following Python code?
for i in range(0,2,-1):
print("Hello")
a) Hello b) Hello Hello
c) No Output d) Error
18. Which of the following is a valid for loop in Python?
a) for(i=0; i < n; i++) b) for i in range(0,5):
c) for i in range(0,5) d) for i in range(5)
19. Which of the following sequences would be generated bt the given line
of code?
range (5, 0, -2)
a) 5 4 3 2 1 0 -1 b) 5 4 3 2 1 0
c) 5 3 1 d) None of the above
20. A while loop in Python is used for what type of iteration?
a) indefinite b) discriminant
c) definite d) indeterminate
21. When does the else statement written after loop executes?
a) When break statement is executed in the loop
b) When loop condition becomes false
c) Else statement is always executed
d) None of the above
22. What will be the output of the following code?
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
a) a b c d e f b) abcdef
c) iiii i..... d) No Output
23. What will be the output of the following code?
x = "abcd"
for i in range(len(x)):
print(i)
a) abcd b) 0 1 2 3
c) 1 2 3 4 d) a b c d
24. What will be the output of the following code?
x = 12
for i in x:
print(i)
a) 12 b) 1 2
c) Error d) None of the above
25. What will be the output of the following Python code?
print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))
a) Hello foo and bin b) Hello {name1} and {name2}
c) Error d) Hello and
26. What will be the output of the following Python code?
print("Hello {0!r} and {0!s}".format('foo', 'bin'))
a) Hello foo and foo b) Hello ‘foo’ and foo
c) Hello foo and ‘bin’ d) Error
27. What will be the output of the following Python code?
print("Hello {0} and {1}".format(('foo', 'bin')))
a) Hello foo and bin b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error d) None of the mentioned
28. What will be the output of the following Python code?
print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))
a) Hello foo and bin b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error d) None of the mentioned
29. What will be the output of the following Python code snippet?
print('The sum of {0} and {1} is {2}'.format(2, 10, 12))
a) The sum of 2 and 10 is 12 b) Error
c) The sum of 0 and 1 is 2 d) None of the mentioned
30. What will be the output of the following Python code snippet?
print('The sum of {0:b} and {1:x} is {2:o}'.format(2, 10, 12))
a) The sum of 2 and 10 is 12 b) The sum of 10 and a is 14
c) The sum of 10 and a is c d) Error
31. What will be the output of the following Python code snippet
if x=1?
x<<2
a) 8 b) 1
c) 2 d) 4
32. What will be the output of the following Python expression?
bin(29)
a) ‘0b10111’ b) ‘0b11101’
c) ‘0b11111’ d) ‘0b11011’
33. What will be the output of the following Python expression?
int(1011)?
a) 1011 b) 11
c) 13 d) 1101
34. To find the decimal value of 1111, that is 15, we can use the function:
a) int(1111,10) b) int(‘1111’,10)
c) int(1111,2) d) int(‘1111’,2)
35. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.
a) True b) False
36. What will be the value of the following Python expression?
4+3%5
a) 4 b) 7
c) 2 d) 0
37. Which of the following operators has its associativity from right to left?
a) + b) //
c) % d) **
38. What are the values of the following Python expressions?
2**(3**2)
(2**3)**2
2**3**2
a) 64, 512, 64 b) 64, 64, 64
c) 512, 512, 512 d) 512, 64, 512
39. What is the value of the following expression?
8/4/2, 8/(4/2)
a) (1.0, 4.0) b) (1.0, 1.0)
c) (4.0. 1.0) d) (4.0, 4.0)
40. What is the value of the following expression?
float(22//3+3/3)
a) 8 b) 8.0
c) 8.3 d) 8.33