Python 1 QP

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

St.

Xavier’s College(Autonomous), Kolkata


Python Batch 1
Final Exam
Date: 01/04/2023, Time: 11:00AM-12:30PM
Full Marks:100

Roll. Number: 62
Name: Anik Chakraborty
Email:[email protected]
Mobile:9432063408

Instructions:

The question paper contains 25 multiple choice questions and 5


broad questions.
Write your answers in this word document and save the document with your
name.

On completion, mail the document to the E-mail ID from your personal email:
[email protected]

Write your Roll Number and Name in the subject line.

Please confirm with the receipt of the answer document with the teacher
before you leave MS-Teams.

Page 1 of 10
Gr-A:Marks=(2x25=50)

Multiple Choice Question:

1) What is a correct syntax to output "Hello World" in Python?


a) p("Hello World")    
b) print("Hello World")       
c) echo("Hello World");
d) echo "Hello World"

Answer:b)

2) How do you insert COMMENTS in Python code?


a) /*This is a comment*/    
b) //This is a comment
c) #This is a comment  

Answer:c)

3) Which one is NOT a legal variable name?


a) Myvar    
b) _myvar
c) my-var       
d) my_var

Answer:c)

4) How do you create a variable with the numeric value 5?


a) x = 5    
b) x = int(5)
c) Both the other answers are correct  

Answer:c)

5) What is the correct file extension for Python files?


a) .pt    
b) .pyth
c) .py    
d) .pyt

Answer:c)

Page 2 of 10
6) How do you create a variable with the floating number 2.8?
a) Both the other answers are correct    
b) x = 2.8
c) x = float(2.8)

Answer:a)

7) What is the correct syntax to output the type of a variable or object in Python?
a) print(type(x))    
b) print(typeof(x))
c) print(typeOf(x))
d) print(typeof x)

Answer:b)

8) What is the correct way to create a function in Python?


a) create myFunction():       
b) function myfunction():
c) def myFunction():  

Answer:c)

9) In Python, 'Hello', is the same as "Hello"


a) True       
b) False

Answer:b)

10) What is a correct syntax to return the first character in a string?


a) x = sub("Hello", 0, 1)       
b) x = "Hello"[0]    
c) x = "Hello".sub(0, 1)

Answer:b)

Page 3 of 10
11) Which method can be used to remove any whitespace from both the beginning and the end of
a string?
a) ptrim()    
b) len()
c) strip()    
d) trim()

Answer:c)

12) Which method can be used to return a string in upper case letters?
a) uppercase()    
b) upperCase()
c) upper()       
d) toUpperCase()

Answer:c)

13) Which method can be used to replace parts of a string?


a) replace()    
b) replaceString()
c) repl()
d) switch()

Answer:a)

14) Which operator is used to multiply numbers?

a) *       
b) #
c) %
d) X

Answer:a)

Page 4 of 10
15) Which operator can be used to compare two values?

a) <>    
b) ==    
c) ><
d) =

Answer:b)

16) Which of these collections defines a LIST?

a) {"apple", "banana", "cherry"}       


b) {"name": "apple", "color": "green"}
c) ["apple", "banana", "cherry"]    
d) ("apple", "banana", "cherry")

Answer:c)

17) Which of these collections defines a TUPLE?

a) {"apple", "banana", "cherry"}       


b) {"name": "apple", "color": "green"}
c) ("apple", "banana", "cherry")       
d) ["apple", "banana", "cherry"]

Answer:c)

18) Which of these collections defines a SET?

a) {"name": "apple", "color": "green"}    


b) {"apple", "banana", "cherry"}    
c) ["apple", "banana", "cherry"]
d) ("apple", "banana", "cherry")

Answer:b)

Page 5 of 10
19) Which of these collections defines a DICTIONARY?

a) {"apple", "banana", "cherry"}       


b) ("apple", "banana", "cherry")
c) {"name": "apple", "color": "green"}       
d) ["apple", "banana", "cherry"]

Answer:c)

20) Which collection is ordered, changeable, and allows duplicate members?

a) DICTIONARY       
b) TUPLE
c) LIST       
d) SET

Answer:c)

21) Which collection does not allow duplicate members?

a) TUPLE    
b) LIST
c) SET  

Answer:c)

22) How do you start writing an if statement in Python?

a) if (x > y)    
b) if x > y:    
c) if x > y then:

Answer:b)

Page 6 of 10
23) How do you start writing a while loop in Python?

a) while x > y:       


b) while (x > y)
c) x > y while {
d) while x > y {

Answer:a)

24) How do you start writing a for loop in Python?

a) for x in y:       
b) for x > y:
c) for each x in y:

Answer:a)

25) Which statement is used to stop a loop?

a) return       
b) exit
c) stop
d) break       

Answer:d)

Page 7 of 10
Broad Questions:

Gr-B: Marks=(10x5=50)

Q.No.26.
Write algorithm to input 3 arbitrary numbers (a,b,c). Calculate and print (i) largest value, (ii)smallest
value and (iii) average value.

Q.No.27.
Write a program in Python to input any sentence from keyboard. Calculate and print(i)Number
vowels, (ii) number of consonants within that sentence.

Q.No.28.
Input 2 positive integers( say a,b). Write a program in Python to calculate and print (i) HCF of a,b
and (ii) LCM of a,b.

Q.No.29.
Write a program in Python to input (i) Input File Name(=file1), (ii) output file-1 name(file2), (iii)
output file-2 name(file3). Split the content of input file into 2(two) components and then transfer
two(2) components to 2 output files. The program should also display size of input file and 2 output
files.

Q.No.30.
Write a program in Python to input name of some RGB image file(e,g, image1.jpg). Do the following
operations on input image file : Convert the RGB pixels to Gray scale. Print both RGB image and also
Gray scale images on screen. Use numpy and matplotlib to do all the above.

Answers to Broad Questions:


26.Step1:Start
Step2:Input a,b,c
Step3:Set max=a
Step4:Set min=a
Step5:Set avg=(a+b+c)/3
Step6:if b>a and b>c goto step7 else goto step8
Step7:set max=b goto step10
Step8:if c>a and c>b goto step9 else goto step10
Step9:set max=c goto step10
Step10:print(“value of maximum element is “,max)
Step11:if b<a and b<c goto step12 else goto step13
Step12:set min=b goto step15
Step13:if c<a and c<b goto step14 else goto step15
Step14:set min=c goto step15
Step15:print(“value of minimum element is “,min)
Step16:print(“value of the average is”,avg)
Step17:End

27.a=input(‘Enter a string:’)
a=a.strip()
a=a.lower()
n=len(a)
v=0

Page 8 of 10
alp=0
for i in range n:
ch=a[i]
if(ord(ch>=97 and ord(ch)<=122):
alp+=1
if(ch==’a’ or ch==’e’ or ch==’i’ or ch==’o’ or ch==’u’):
v+=1
print(‘Number of vowels in the entered string is ‘,v)
print(‘Number of consonants in the entered string is ‘,(alp-v))

28. def hcf(a,b):


r=a%b
while(r !=0):
a=b
b=r
r=a%b
return b

def lcm(a,b):
lc=a*b//hcf(a,b)
return lc

# main program starts


a=int(input('Enter 1st number='))
b=int(input('Enter 2nd number='))
h=hcf(a,b)
lc=lcm(a,b)
print('hcf(',a,',',b,')=',h)
print('lcm',a,',',b,')=',lc)

29.file1=input(‘Enter Input File Name=’)


file2=input(‘Enter Output File-1 Name=’)
file3=input(‘Enter Output File-2 Name=’)
fp1=open(file1,’rb’)
fp2=open(file2,’wb’)
fp3=open(file3,’wb’)
data=fp1.read()
n=len(data)
n1=n//2
n2=n-n1
data1=data[0:n1]
data2=data[n1:n]
fp2.write(data1)
fp3.write(data2)
print(‘Size of ‘,file1,’=’,n,’Bytes’)
print(‘Size of ’,file2,’=’,n1,’Bytes’)
print(‘Size of ‘,file3,’=’,n2,’Bytes’)
fp1.close()
fp2.close()
fp3.close()

30.import numpy as np
import matplotlib.pyplot as plt
img1=input(‘Enter your Image Filename =’)
data1=plt.imread(img1)

Page 9 of 10
shape1=data1.shape
print(‘Shape of data1=’,shape1)
r,c,d=shape1[0],shape1[1],shape1[2]
print(‘r=’,r,’c=’,c,’d=’,d)
R,G,B=data1[:,:,0],data1[:,:,1],data1[:,:,2]
print(‘R=’,R)
print(‘G=’,G)
print(‘B=’,B)
gray=.2989*R+.587*G+.114*B
shape2=gray.shape
print(‘Shape of gray scale image=’,shape2)
print(‘gray=’,gray)
rows=3
column=4
#The RGB Image gets displayed
fig=plt.figure(figsize=(20,20))
fig.add_subplot(rows,columns,1)
plt.axis(‘off’)
plt.title(‘RGB Image’)
plt.imshow(data1)
#The GrayScale Image gets displayed
fig.add_subplot(rows,columns,2)
plt.axis(‘off’)
plt.title(‘GrayScale Image’)
plt.imshow(gray,cmap=’Greys’)

Page 10 of 10

You might also like