0% found this document useful (0 votes)
223 views9 pages

Python Variable Types and Expressions

The document consists of a series of questions and tasks related to Python programming, covering topics such as keywords, data types, expressions, control structures, loops, and functions. It includes multiple-choice questions, code evaluation, and requests for code corrections and program writing. The content is designed to test knowledge and understanding of Python programming concepts.

Uploaded by

nikhilsujith44
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)
223 views9 pages

Python Variable Types and Expressions

The document consists of a series of questions and tasks related to Python programming, covering topics such as keywords, data types, expressions, control structures, loops, and functions. It includes multiple-choice questions, code evaluation, and requests for code corrections and program writing. The content is designed to test knowledge and understanding of Python programming concepts.

Uploaded by

nikhilsujith44
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

1. Which of the following is not a keyword?

a) eval b) assert c) nonlocal d) pass


2. Which of these is not a core data type?
a) Lists b) Dictionary c) Tuples d) Class
3. How would you write x power y in Python as an expression?
4. Evaluate the expression given below if A=16 and B=15. A%B//A
5. The expression 8/4/2 will evaluate equivalent to which of the
following expressions:
a) 8/(4/2) b) (8/4)/2
6. Which two operators can be used on numeric values in Python?
a) @ b) % c) + d) #
7. Which of the following is valid arithmetic operator in Python:
a) // b) ? c) < d) and
8. The datatypes whose values cannot be changed in place are called
_______________.
9. The __________ statement is an empty statement in Python.
10. The ____________ statement skips the rest of the loop statements and
causes the next iteration of the loop to take place.
11. Python’s _____________ cannot be used as variable name.
12. The value of expressions 4/(3*(2-1)) and 4/3*(2-1) is the same. (T/F)
13. The expression 2**2**3 is evaluated as: (2**2)**3. (T/F)
14. In Python, a variable is a placeholder for data. (T/F)
15. In a nested loop, a break statement terminates all the nested loops
in one go. (T/F).
16. Differentiate between round() and floor() functions with suitable
example.
17. Point out the error in the following Python code:
a=bc+d
Here the product of b and c is to be added to d and then
assigns the final value to a.
18. What will be the output of the following Python code and why?
age=raw_input(“Enter your age”)
ageafter10=age+10
print(“Age after 10 years”, ageafter10)
19 What will be the output of the following expressions when,
a=10, b=56, c=89, name=” Hello”, name1=” hello”
i) print(a<b) ii) print(name < name1)
iii) print(a<b<c) iv) print(a<b>c)
20 Give the output of the following with justification.
x=3
x+=x-x
print(x)
21 Why does the expression 2+3*4 result in the value 14 and not the
value 24?
22 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.
23 What will be the output of the following code?
x=”abcdef”
i=”i"
while i in x:
print(i,end=” “)
24 Assertion (A): Datatypes are used to identify the type of data.
Reason (R): Datatypes are two types as numbers and strings.
a) Both A and R are true and R is the correct explanation for A.
b) Both A and R are true and R is not the correct explanation for A.
c) A is true but R is false d) A is false but R is true
25 Assertion (A): Siddh and siddh are same.
Reason(R): Python treats upper case and lower case characters
differently.
a) Both A and R are true and R is the correct explanation for A.
b) Both A and R are true and R is not the correct explanation for A.
c) A is true but R is false d) A is false but R is true
26. Write a program in Python to read a number. If the number is even
print half the number otherwise print the next number. End your
program by printing (“Thank You”.

27. Evaluate the following expressions:


a) 47 / 5 + 10 % 2 – 25%12
b) 34 < 56 or 90 < 60 and not 21 < 10
c) 10 < 15 and not 17 > 11 or 10 > 2
d) 3 > 6 or 9 < 60 and not 1 > 10
28. i) Rewrite the following code in Python after removing all syntax
error(s). Underline each correction done in the code:
a=5
work=true
b=hello
c=a+b
FOR i in range(10)
if i%7=0:
continue

ii) Rewrite the following code in python after removing all syntax
error(s). Underline each correction done in the code
x=integer(input('Enter 1 or 10'))
if x==1:
for x in range(1,11)
Print(x)
Else:
for x in range(10,0,-1):
print(x)
29. The radius of sphere is 7.5 metres. Write a menu driven Python code
to calculate its area and volume. Area of a sphere is πr2 and its Volume
of a sphere is 4πr3
30. (i) What is the size of each string?
a) “Python‟ b) “Learning@\nCS‟

(ii) Raj was working on application where he wanted to divide the


two number (A and B) , he has written the expression as C =
A/B, on execution he entered 30 and 7 and expected answer was
4 i.e. only integer part not in decimal, but the answer was 4.285
approx, help Raj to correct his expression and achieving the
desired output.
31. Fill in the blanks to execute loop from 10 to 100 and 10 to 1
(i)
for i in range (____________):
print i

(ii)
for i in range (_________):
print i

32. Find the errors in the following codes and write the correct codes.
Underline correction in each case.
(i) a=10
while (a<100):
print(a)
print(“Over!”)

(ii) while (x<10):


print(x)
x=x+2

(iii) for p in range(3)


for q in range(3)
print(p*q)
else:
print(“Outer loop ends”)

33. How many times will the given loops iterate:


(i) j=0
while (j<50):
print(“Hello World”)
j=j+1

(ii) j=25
while (25<=j<=30):
print(“Temp variable=”, j)
j=j+1

(iii) #Assume Boolean variable b is not known.


b=True
sum=0
while b:
sum=sum+5
print(sum)
if (b or not b):
break

34. Write a program in Python to find the sum of all digits of the given
number.
Find the output for the following:
35. a) a = 3 - 4 + 10
b=5*6
c = 7.0/8.0
print "These are the values:", a, b, c

b) x=4
y =x+ 1
x=2
print x, y

c) What is the output of this expression, 3*1**3?

36. a) What is the difference between an expression and a statement


in Python? Give an example.

b) How are keywords different from variable names? Give an


example.

37. The iteration statements or repletion statements allow a set of


instructions to be performed repeatedly until a certain condition is
fulfilled. The iteration statements are also called as loops or looping
statements. Python provides two kinds of loops: For loop and While
loop.

Q1: Python provides two categories of loop:


a) Counting b) Conditional c) Entry Control d) a & b
Q2: For loop is:
a) Entry Control loop b) Exit Control loop c) Counting
d) Conditional.
Q3: While loop is:
a) Entry Control loop b) Exit Control loop c) Counting
d) Conditional.
Q4: Consider the following code:
x=0
while (x<5):
print(x*x)
What will be the output of the above code?
a) It will print squares of all the numbers
b) It will print squares of 0, 1, 2, 3 & 4
c) It will print square of 1
d) It will print() infinitely.

38. Q1: Assertion(A): It is not necessary that a statement results in a


value.
Reason(R): A statement is any legal combination of symbols that
represents a value.
a) Both A and R are true and R is the correct explanation for A.
b) Both A and R are true and R is not the correct explanation for A.
c) A is true but R is false d) A is false but R is true

Q2: Assertion(A): Named labels whose value can be manipulated


during program are called variables.
Reason(R): Python variables are created by assigning value of desired
type to them.
a) Both A and R are true and R is the correct explanation for A.
b) Both A and R are true and R is not the correct explanation for A.
c) A is true but R is false d) A is false but R is true

39. Python considered the character enclosed in triple quotes as String


(True / False)
40. Write the output for the following code:
A=”We\nto\nmy\nb\tok”
print(A)
41. Write the output for the following code: Justify
Str= “Welcome”
Str[2]= ‘a’
print(Str)
42. Index value in String should be of type _________________.
43. String traversal can be done using ‘for’ loop only. (True / False)
44. Write the output for the following code: Justify
A= “Learn”
while(A):
print(A)
45. Write the output for the following code:
s = “blog”
for i in range(-1, -len(s), -1):
print(s[i], end= “$”)
46. Write the output for the following code:
>>>list( “Welcome”)
47. Write the output for the following code:
L=[0.5* x for x in range(4)]
print(L)
48. Write the output for the following code:
L=[[‘Physics’,101],[‘Chemistry’,202],[‘Maths’,303],45,6,’j’]]
print(len(L)
49. remove() function removes the __________ occurrences of an element
from the list.
50. Write the output for the following:
L=[[5,7,9,1][712,23,4,9]]
for r in L:
[Link]()
for e in r:
print(e, end= “”)
51. Both the print statement will produce the same result (True / False).
L=[“Amit”, “Ananya”, “Parth”]
print (L[-1])
print(L[-1][-1])
52. Write the output for the following code:
L=[‘Amit’, ‘anita’, ‘Sumant’, ‘Zaid’]
print(max(L))
53. We can repeat the values of key in Dictionary. (True / False).
54. Write the output for the following code: Justify
D1 = {“a”:50, “b”:50}
D2 = {“a”:500, “b”:50}
print(D1>D2)
55. There is no index value in dictionary like we have in list. (True /
False)
56. Assertion (A): Items in dictionaries are
unordered.
Reason(R): We may not get back the data in the same order in
which we have entered the data initially in the dictionary.
d) Both A and R are true and R is the correct explanation for A.
e) Both A and R are true and R is not the correct explanation for A.
f) A is true but R is false d) A is false but R is true
57. __________function returns the value corresponding to the
key passed as the arguments.
58. Anshu created two dictionaries D1 and D2 in python. Later on she
wants to add all the elements of D2 in D1. As a friend of Anshu, help
her to write python code to add D1 & D2.
59. Following statement return values in the form of ___________.
>>>[Link]() # D1 is a dictionary.
60. Write the output for the following code:
T1=(23, 45, 67, 43, 21, 41)
print(T1[1:2])
61. >>> min(t1) will return an error if the tuple t1 contains value of
mixed data type (True / False).
62. What is the length of the given tuple: ((((1,2,3,4), ‘a’, ‘b’), ‘g’, 1, 3),
“Sonu”).
63. a=(“Hello”, “how”, “are”, “you”)
for i in a:
print([Link](i),end=” “)

64. Write the output for the following code:


s="python string fun"
for i in range(0,len(s)):
if s[i]>='c' and s[i]<='l':
print(s[i].lower(),end="")
else:
print(s[i].upper(),end="")
65. str1=”Hello, world!””
str2=”I love Python.”
str3=”Hello, world!”
print(str1==str2)
print(str1==str3)
66. Predict the output for the following code:
Numbers = [9, 18, 27, 36]
for Num in Numbers:
for N in range(1, Num %8):
print(N, “#”, end=” “)
print()
67. Write a program to count occurrences of an element in a list.
68. Write a Python statement to declare a Dictionary named Class
Roll with Keys as 1, 2, 3 and corresponding values as ‘Reena’,
‘Rakesh’, ‘Zareen’ respectively.
69. Write short notes on the following functions in dictionary with an
example for each.
i) get() ii) items() iii) keys() iv) update()
70. Predict the output for the following code:
Str1=”Exam2021”
Str2= “”
I=0
while (I<len(Str1):
if (Str1[I)>=”A” and Str1[I]<=”M”):
Str2=Str2+Str1[I+1]
elif (Str1[I] >=”0” and Str1[I]<=”9”):
Str2=Str2+Str1[I-1]
else:
Str2=Str2+”*”
I=I+1
print(Str2)
71. Write a python program to remove all the occurrences of a given
item/element present in a list.
72. What does each of the following expressions evaluate to?
L=[“These”,[“are”, ”a”], [“few”, “words”], “that”, “we”, “will”, “use”]
a) L[3:4] + L[1:2] b) “few” in L[2:3] c) “few” in L[2]
d) L[2][1] e) L[1] + L[2]
Write the output for the following:
73. str= “Fun@Python3.0”)
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
74. Predict the output for the following code:
dictionary = {'GFG' : '[Link]', 'google' : '[Link]',
'facebook' : '[Link]' }
del dictionary['google'];
for key, values in [Link]():
print(key)
[Link]();
for key, values in [Link]():
print(key)
del dictionary;
for key, values in [Link]():
print(key)
75. Python strings are characters enclosed in quotes of any type: single,
double or triple. An empty string is a string that has 0 characters.
Python strings are immutable. Strings are sequence of characters,
which each character has a unique position: id / index. The indexes of
a string begin from 0 to (length – 1) in forward direction and -1, -2, -3,
…. length in backward direction.
Q1: Iteration through the elements of a string one character at a
time is called
a) Looping b) Iteration c) Traversing d) Searching

Q2: + & * are


a) Arithmetic b) Relational c) String d) Both A & C

Q3: Select the invalid statements:


a) C= 2 + 3 b) C = ‘2’ + 3 c) C = ‘2’ + ‘3’ d) C= ‘2’ * 3
e) C = ‘2’ * ‘3’ f) C = 2 * 3 g) C = 2 * ‘3’ h) C=(‘2’+’3’) *
3

a) c, f & h b) b, e c) e, c, f d) all are valid

Q4: ‘a’ < ‘A’ will


a) give error b) give true c) give false d) not possible

Q5: Consider the following code:


name = “ANUBHAV”
print (name[:3]+name[3:])
What will be the output:
a) ANUBHAV b) ANU c) BHAVANU d) ANUUBHAV
76. b= “Practice Questions of List in Python”
a=list(b) Write the output for the following print statements:
i) print (a[-2:-5:-2]) ii) print (a[17:-1]) iii) print(a[:])
iv) print(a[4:17]) v) print(a[Link]-1]) vi) print(a[::2]
vii) print (a[-5:] viiii) print(a[:4])

You might also like