Você está na página 1de 14

Unit-I

Programming and Computational Thinking (30 MARKS)

1. Name the Python Library modules which need to be imported to invoke the following
functions :
i. load ()
ii. pow ()
Answer:
i. pickle
ii. math

2. Name the modules to which the following func-tions belong:


i. Uniform ()
ii. fabs ()

Answer:
i. random ()
ii. math ()

3.Differentiate between the round() and floor() functions with the help of suitable
example.
Answer:
The function round() is used to convert a fractional number into whole as the nearest next
whereas the
function floor() is used convert to the nearest lower whole number, e.g.,
round (5.8) = 6, round (4.1) = 5 and foor (6.9) = 6, floor (5.01) = 5

4. The __________ data type allows only True/False values


bool b) boolean c) Boolean d) None
ans : (a)

6. If the value of a = 20 and b = 20, then a+=b will assign ________ to a


a) 40 b) 30 c) 20 d) 10
ans:- (A) 40

7 The ____________ operator is used to find out if division of two number


yields any remainder
/ b) + c) % d) //
Ans:- (c)

8. What is the difference between selection and repetition?


Ans:- selection is used to check the condition and execute true condition once where as
repetition is loop and repeat the statement number of times.
state True or False

9. The break statement allows us to come out of a loop


Ans:- true

10. The continue and break statement have same effect


Ans:- false

11. How can we import a module in Python ?


Ans. 1. By using import statement
e.g. import math
2. by using from keyword
e.g. from math import sqrt
12. What are default arguments ?
Ans. Default arguments are used in function definition, if the function is called without
the argument, the default argument gets its default value.
13. What is the difference between actual and formal parameters ?
Ans. Actual parameters are those parameters which are used in function call statement
and formal parameters are those parameters which are used in function header
(definition).
e.g. def sum(a,b): # a and b are formal parameters
return a+b
x,y=5,10
res=sum(x,y) # x and y are actual parameters
14. Which of the following mode is used to open a file in append mode ?
‘r’ b. ‘a’ c. ‘w’ d. ‘s’
Ans. b. ‘a’
15. Write a statement to open a file in reading mode.
Ans. file = open (“abc.txt”, ‘r’)
16. What is a recursive function ?
Ans. Recursion is a way of programming or coding a problem, in which a function calls
itself one or more times in its body. Usually, it is returning the return value of this
function call. If a function definition fulfils the condition of recursion, we call this
function a recursive function.
Example:
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)

17. Which keyword is used to define a function in python ?


Ans. def
What is the difference between read() and readlines() function ?
ans. The read() function reads from a file in read mode, and stores its contents in a string
type variable.
The readlines() function reads from a file in read mode and returns a list of all lines in the
file.

18. Find the error (if any) in the following code and write the corrected code and
underline it:
Def sum(a=1,b)
return a+b
print “The sum =” Sum(7,-1)
Ans: def sum(b,a=1) :
return a+b
print (“The sum =” ,Sum(7,-1))
19 . Read the code given below and answer the question:
fh= open(“main.txt”,”w”)
fh.write(“Bye”)
fh.close()
If the file contains “GOOD” before execution, what will be the contents of the file after
execution of this code?
Ans: Bye
20. What is the difference between built-in functions and modules?
Ans: Built in functions can be used directly in a program in python, but in order to use
modules, we have to use import statement to use them.
21. what is the difference between local variable and global variable?
Ans:
SR.NO. LOCAL VARIABLE GLOBAL VARIABLE
1 It is a variable which is declared within a It is a variable which is declared
function or within a block. outside all the functions.
2 It is accessible only within a function/ It is accessible throughtout the
block in which it is declared. program.
For example,
def change():
n=10 # n is a local variable
x=5 # x is a global variable
print( x)

22. What are the advantages of writing functions with keyword arguments ?
Ans: i) using the function is easier as we do not need to remember the order of the
arguments.
ii) we can specify values of only those parameters which we want to give, as other
parameters have default argument values.
23. What do you mean by scope of variables ?
Ans: Scope of variables refers to the part of the program where it is visible, i.e, the area
where you can use it.
24. what do you mean by PYTHONPATH ?
Ans: PYTHONPATH is the variable that tells the interpreter where to locate the module
files imported into a program.
25. What is _init_.py file ?
Ans: It is a file that is used to consider the directories on the disk as package of Python.
It is basically used to initialize the Python packages.
26. Which of the following is not an advantage of using modules ?
Ans: a) provide a means of reusing program code.
b) provide a means of dividing up tasks.
c) provide a means of reducing the size of program
d) provide a means of testing individual parts of the program.
27.Rewrite the following code in Python after removing all syntax errors(s). Underline
each correction done in the code. for Name in [Amar, Shveta, Parag]
if Name [0] = ‘s’:
Print (Name)
Answer:
for Name in [“_Amar”, ”_Shveta_” , "_Parag_”] :
if Name [0] E == ‘S’ :
Print (Name)

28. Rewrite the following code is Python after removing all syntax errors(s).
Underline each correction done in the code. [CBSE Outside Delhi-2016]
for Name in [Ramesh, Suraj, Priya]
if Name [0] = ‘S’:
Print (Name)
Answer:
for Name in [“_Ramesh_”, “_Suraj_” , “_Priya_”]
if Name [0] =_=‘S’ :
print (Name)

Q.29.What will be the output of the following python code considering the following set
of inputs?
AMAR
THREE
A123
1200
Also, explain the try and except used in the code.
Start = 0
while True :
Try:
Number = int (raw input (“Enter Number”))
break
except valueError : start=start+2
print (“Re-enter an integer”)
Print (start)

Answer:
Output:
Enter Number AMAR
Re-enter an integer
Enter Number THREE
Re-enter an integer
Enter Number A123
Re-enter an integer
Enter Number 12006
Explanation : The code inside try makes sure that the valid number is entered by the user.
When any input other an integer is entered, a value error is thrown and it prompts the user
to enter another
value.

30. Give the output of following with justilocation.


x=3
x+ = x-x
print x

Answer:
Output: 3
Working:
x=3
x = (x+ x-x ):x = 3 + 3 - 3 = 3
31.What will be printed, when following Python code is executed?
class person:
def init (self,id):
self.id = id arjun = person(150)
arjun. diet [‘age’] = 50
print arjun.age + len(arjun. diet )
Justify your answer.

Answer:
52
arjun.age = 50
arjun.dict has 2 attributes so length of it is 2. So total = 52.

32. What would be the output of the following code snippets?


print 4+9
print “4+9”

Answer:
13 (addition), 49 (concatenation).

33. What is the result of 4+4/2+2?


Answer:
4 + (4/2) + 2 = 8.

34.Write the output from the following code:


x= 10
y = 20
if (x>y):
print x+y
else:
print x-y
Answer:
– 10

35.Write the output from the following code:


s=0
for I in range(10,2,-2):
s+=I
print “sum= ",s

Answer:
sum= 28

36.Write the output from the following code:


n = 50
i=5
s=0
while i<n:
s+ = i
i+ = 10
print “i=”,i
print “sum=”,s
Answer:
i= 15
i= 25
i= 35
i= 45
i= 55
sum= 125

37. Observe the following program and answer the question that follows:
import random
x=3
N = random, randint (1, x)
for 1 in range (N):
print 1, ‘#’, 1 + 1
a. What is the minimum and maximum number of times the loop will execute?
b. Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
i. 0#1
ii. 1#2
iii. 2#3
iv. 3#4
Answer:
a. Minimum Number = 1
Maximum number = 3
b. Line iv is not expected to be a part of the output.

38.Observe the following Python code carefully and obtain the output, which will appear
on the screen after execution of it.
def Findoutput ():
L = "earn"
X=""
count = 1
for i in L:
if i in ['a', 'e',' i', 'o', 'u']:
x = x + 1. Swapcase ()
else:
if (count % 2 ! = 0):
x = x + str (len (L[:count]))
else:
x=x+1
count = count + 1
print x
Findoutput ()
Answer:
EA3n

39.. Find and write the output of the following Python code:
Number = [9,18,27,36] for N in Numbers:
print (N, "#", end = " ")
print ()
Answer:
Element Stack of operators Post􀂦x Expression
1# 0 0
1# (1#) (1#)
2# (1#) (1#2#)
1# (2#) (1#2#3#)
2# (1#) 1#
3# (2#) 1#2#
(3#) 1#2#3#

40.What are the possible outcome(s) executed from the following code? Also,
specify the maximum and import random
PICK=random.randint (0,3)
CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"];
for I in CITY :
for J in range (1, PICK)
print (I, end = " ")
Print ()
(i) (ii)
DELHIDELHI DELHI
MUMBAIMUMBAI DELHIMUMBAI
CHENNAICHENNAI DELHIMUMBAICEHNNAI
KOLKATAKOLKATA
(iii) (iv)
DELHI DELHI
MUMBAI MUMBAIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATA
Answer:
Option (i) and (iii) are possible option (i) only
PICKER maxval = 3 minval = 0

41.Find the output from the following code:


T=(10,30,2,50,5,6,100,65)
print max(T)
print min(T)
Answer:
100
2
Q.53.Write the output from the following code:
T1=(10,20,30,40,50)
T2 =(10,20,30,40,50)
T3 =(100,200,300)
cmp(T1, T2)
cmp(T2,T3)
cmp(T3,T1)
Answer:
0
-1
1

42.Rewrite the following code in Python after remo¬ving all syntax error(s).
Underline each correction done in the code.
for student in [Jaya, Priya, Gagan]
If Student [0] = ‘S’:
print (student)

Answer:
for studednt in values [“Jaya”, “Priya”, “Gagan”]:
if student [0] = = “S”
print (student)
Question 33.
Find and write the output of the following Python code:
Values = [11, 22, 33, 44] for V in Values:
for NV in range (1, V%10):
print (NV, V)
Answer:
1, 11
2,22
3,33
4, 44

43. What are the possible outcome(s) executed from the following code ? Also specify
the maximum and minimum values that can be assigned to variable PICKER.
import random
PICKER = random randint (0, 3)
COLOR = ["BLUE", "PINK", "GREEN", "RED"]:
for I in COLOR :
for J in range (1, PICKER):
Print (I, end = " ")
Print ()
(i) (ii) (iii) iv)
BLUE BLUE PINK SLUEBLUE
PINK BLUEPINK PINKGREEN PINKPINK
GREEN BLUEPINKGREEN GREENRED GREENGREEN
RED BLUEPINKGREEN RED REDRED
Answer:
Option (i) and (iv) are possible
OR
option (i) only
PICKER maxval = 3 minval = 0

44.What are the possible outcome(s) expected from the following python code? Also
specify maximum and minimum value, which we can have. [CBSE SQP 2015]
def main():
p = ‘MY PROGRAM’
i=0
while p[i] != ‘R’:
l = random.randint(0,3) + 5
print p[l],’-’,
i += 1
(i) R – P – O – R –
(ii) P – O – R – Y –
(iii) O -R – A – G –
(iv) A- G – R – M –
Answer:
Minimum value=5
Maximum value=8
So the only possible values are O, G, R, A
Only option (iii) is possible.

45.Rewrite the following Python code after removing all syntax error(s). Underline the
corrections done.
def main():
r = raw-input(‘enter any radius : ’)
a = pi * math.pow(r,2)
print “ Area = ” + a
Answer:
def main ():
r = raw_input(‘enter any radius : ’)
a = pi * math.pow(r,2)
print “ Area = ”, a

46.Rectify the error (if any) in the given statements.


>> str=“Hello Python”
>>> str[6]=‘S’
Answer:
str[6] = ‘S’ is incorrect ‘str’ object does not support item assignment.
str.replace(str[6],‘S’).

47.Find the errors from the following code:


i=10 ;
while [i<=n]:
print i
i+=10

Answer:
i=10
n=100
while (i<=n):
print i
i+=10
48. Find the errors from the following code:
if (a>b)
print a:
else if (a<b)
print b:
else
print “both are equal”
Answer:
if (a>b) // missing colon
print a:
else if (a<b) // missing colon // should be elif
print b:
else // missing colon
print “both are equal"

49. Find errors from the following codes:


c=dict()
n=input(Enter total number)
i=1
while i<=n:
a=raw_input(“enter place”)
b=raw_input(“enter number”)
c[a]=b
i=i+1
print “place”,“\t”,“number”
for i in c:
print i,“\t”,c[a[i]]

Answer:
c=dict()
n=input(‘‘Enter total number”)
i=1
while i<=n :
a=raw_input(“enter place”)
b=raw_inputf enter number”)
c[a]=b
i=i+1
print “place”,“\t”,“number”
for i in c:
print i,“\t”,c[i]

50. The algorithm’s growth rate determine the algorithm performance when its input size
:
(i) grows/increases (ii) decreases (iii) remains same (iv) Input size does not
affect performance
51. Reorder the following efficiencies from the smallest to the largest:
(a) 2n (b) n! (c) n5 (d) 10,000 (e) nlog2(n)
Ans. 10,000< nlog2(n) < n5 < 2n <n!
52. Given that the efficiency of an algorithm is 5n2, if a step in this algorithm takes 1
nanosecond(10-9), how long does it take the algorithm to process an input of size 1000.
Ans. Given size is n=1000, hence the time taken is : 5x10002x10-9
53 What is time complexity of following algorithms?
(i) Binary Search (ii) Linear Search
Ans. (i) O(log2(n)) (ii) O(n)
54. For data visualization, which library is used?
Ans. Matplotlib
55. What is PyPlot?
Ans. Collections of methods within matplolib which allows user to construct 2D plots
easily.
56. Which of the following are not plotting functions of PyPlot ?
(i) plot() (ii) bar() (iii) line() (iv)pie()
57. Which of the following plotting functions does not plot multiple data services:
(a) plot (b) bar (c) pie (d) barh
58. Which argument will you provide to change the following in a line chart-
(i) Color of line (ii) Width of line
Ans. (i) <colorcode> (ii) linewidth
59. What is technical name of insertion in stack:
(a) Push (b) Pop (c) Insert (d) Enstack
60. What is the situation called, when deletion is attempted in empty stack:
(a) Overflow (b) Underflow (c) Push (d) Pop
61. What is Peek in stack?
(i) Viewing topmost element (ii) Inserting element at top
(iii) Removing element from top (iv) None of above
62. Stack is also called as-
(a) Last in first out (b) First in last out
(c) Last in last out (d) First in first out
63. __________________ is very useful in situation when data have to be stored and then
retrieved in reverse order:
(a) Stack (b) Queue (c) List (d) Linked List
64. Which data structure allows deleting data elements from front and inserting at rear?
(a) Stack (b) Queues (c) Dequeue (d) Tree
65. What will be value of top, if there is a size of stack if stack_size is 5.
(a) 5 (b) 6 (c) 4 (d)None
66. A data structure where elements can be added or removed at either end but not in the
middle is called-
(i) Linked list (b) Stack (c) Queue (d) Dequeue
67. Which of the following data structure is non-linear type?
(a)Queue (b)List (c) Stack (d) Tree
68. What is infix expression-
(i) Where operands are placed before operands
(ii) Where operands are placed after operands
(iii) Where operands are placed in between operands
(iv) None of the above
69. Which of the following is not application of stack:
(i) Reversing a line (ii) Polish string (iii) Factorial of a function (d)
Fibonacci Series

Você também pode gostar