Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
440 views

Python Questionaire

The document contains questions about Python classes, inheritance, dictionaries, sets and exceptions. Some key points: - Question 4 tests inheritance and method overriding between two classes. The answer is C - the __init__ method of the parent class A gets invoked and displays "i from A is 60". - Question 5 also tests inheritance and method overriding, but between a parent and child class. The answer is again C - the __init__ method of the parent class A gets invoked and displays "i from B is 60". - Question 11 creates a tuple with dictionary-like functionality. The output is option A - it prints the sum of the tuple values (34) and the length of the tuple (12

Uploaded by

Raghuvaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
440 views

Python Questionaire

The document contains questions about Python classes, inheritance, dictionaries, sets and exceptions. Some key points: - Question 4 tests inheritance and method overriding between two classes. The answer is C - the __init__ method of the parent class A gets invoked and displays "i from A is 60". - Question 5 also tests inheritance and method overriding, but between a parent and child class. The answer is again C - the __init__ method of the parent class A gets invoked and displays "i from B is 60". - Question 11 creates a tuple with dictionary-like functionality. The output is option A - it prints the sum of the tuple values (34) and the length of the tuple (12

Uploaded by

Raghuvaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSESSMENT : PYTHON & DBMS

1. What will be the output of the following 4. What will be the output of the following
code snippet? code snippet?
class Sales: class A:
def __init__(self, id): def __init__(self):
self.id = id self.calcI(30)
id = 100 print("i from A is", self.i)
val = Sales(123)
print (val.id) def calcI(self, i):
self.i = 2 * i;
A. SyntaxError, this program will not run
B. 100 class B(A):
C. 123 def __init__(self):
D. None of the above super().__init__()

2. What will be the output of the following def calcI(self, i):


code snippet? self.i = 3 * i;
class Person:
def __init__(self, id): b = B()
self.id = id A. The __init__ method of only class B gets
invoked.
sam = Person(100) B. The __init__ method of class A gets invoked
sam.__dict__['age'] = 49 and it displays “i from A is 0”.
print (sam.age + len(sam.__dict__)) C. The __init__ method of class A gets invoked
and it displays “i from A is 60”.
A. 1 B. 2 C. 49 D. 50 E. 51 D. The __init__ method of class A gets
invoked and it displays “i from A is 90”.
3. Which of the following statements are
correct about the given code snippet? 5. What will be the output of the following
class A: code snippet?
def __init__(self, i = 0): class A:
self.i = i def __init__(self):
self.calcI(30)
class B(A):
def __init__(self, j = 0): def calcI(self, i):
self.j = j self.i = 2 * i;

def main(): class B(A):


b = B() def __init__(self):
print(b.i) super().__init__()
print(b.j) print("i from B is", self.i)

main() def calcI(self, i):


self.i = 3 * i;
A. Class B inherits A, but the data field “i” in A
is not inherited. b = B()
B. Class B inherits A, thus automatically
inherits all data fields in A. A. The __init__ method of only class B gets
C. When you create an object of B, you have invoked.
to pass an argument such as B(5). B. The __init__ method of class A gets invoked
D. The data field “j” cannot be accessed by and it displays “i from B is 0”.
object b. C. The __init__ method of class A gets invoked
and it displays “i from B is 60”.

1
ASSESSMENT : PYTHON & DBMS
D. The __init__ method of class A gets GFG
invoked and it displays “i from B is 90”.
9. What is the output of the following
6. Predict the output of the following piece of program?
code. dictionary1 = {'Google' : 1,
dictionary = {} 'Facebook' : 2,
dictionary[1] = 1 'Microsoft' : 3
dictionary['1'] = 2 }
dictionary[1] += 1 dictionary2 = {'GFG' : 1,
sum = 0 'Microsoft' : 2,
for k in dictionary: 'Youtube' : 3
sum += dictionary[k] }
print sum dictionary1.update(dictionary2);
for key, values in dictionary1.items():
7. Predict the output of the following code. print(key, values)
check1 = ['Learn', 'Quiz', 'Practice',
'Contribute'] a) Compilation error b) Runtime error
check2 = check1 c) (‘Google’, 1)
check3 = check1[:] (‘Facebook’, 2)
(‘Youtube’, 3)
check2[0] = 'Code' (‘Microsoft’, 2)
check3[1] = 'Mcq' (‘GFG’, 1)
d) None of these
count = 0
for c in (check1, check2, check3): 10. What is the output of the following
if c[0] == 'Code': program?
count += 1
if c[1] == 'Mcq': from math import sqrt
count += 10 L1 = [x**2 for x in range(10)].pop()
L1 + = 19
print count print(sqrt(L1), end = " ")
L1 = [x**2 for x in reversed(range(10))].pop()
8. What is the output of the following L1 + = 16
program? print(int(sqrt(L1)))

dictionary = {'GFG' : 'geeksforgeeks.org', a) 10.0 4.0 b) 4.3588 4 c) 10 .0 4


'google' : 'google.com', d) 10.0 0
'facebook' : 'facebook.com'
} 11. What will be the output of the following
del dictionary['google']; program ?
for key, values in dictionary.items():
print(key) tuple = {}
dictionary.clear(); tuple[(1,2,4)] = 8
for key, values in dictionary.items(): tuple[(4,2,1)] = 10
print(key) tuple[(1,2)] = 12
del dictionary; _sum = 0
for key, values in dictionary.items(): for k in tuple:
print(key) _sum += tuple[k]
print(len(tuple) + _sum)
a) Both b and d b) Runtime error
c) GFG Options:
facebook 34 12 31 33
d) facebook

2
ASSESSMENT : PYTHON & DBMS
12. What is the output of the following a) No error b) Assertion error c)
program? Input output error
data = 50 d) Name error
try:
data = data/0 15. What will be the output of the following
except ZeroDivisionError: Python code?
print('Cannot divide by 0 ', end = '') class A:
else: def __init__(self,x):
print('Division successful ', end = '') self.x = x
def count(self,x):
try: self.x = self.x+1
data = data/5
except: class B(A):
print('Inside except block ', end = '') def __init__(self, y=0):
else: A.__init__(self, 3)
print('GFG', end = '') self.y = y
def count(self):
a) Cannot divide by 0 GFG self.y += 1
b) Cannot divide by 0
c) Cannot divide by 0 Inside except block GFG def main():
d) Cannot divide by 0 Inside except block obj = B()
obj.count()
13. What will be the output of the following print(obj.x, obj.y)
Python code?
main()
class student:
def __init__(self): a) 3 0 b) 3 1 c) 0 1 d) An exception in
self.marks = 97 thrown
self.__cgpa = 8.7
def display(self): 16. What will be the output of the following
print(self.marks) Python code?
obj=student() s1={3, 4}
print(obj._student__cgpa) s2={1, 2}
s3=set()
a) The program runs fine and 8.7 is printed i=0
b) Error because private class members can’t j=0
be accessed for i in s1:
c) Error because the proper syntax for name for j in s2:
mangling hasn’t been implemented s3.add((i,j))
d) The program runs fine but nothing is i+=1
printed j+=1
print(s3)
14. What happens if the file is not found in
the following Python code? a) {(3, 4), (1, 2)} b) Error
c) {(4, 2), (3, 1), (4, 1), (5, 2)} d) {(3, 1), (4,
a=False 2)}
while not a:
try: Sample Table – Worker
f_n = input("Enter file name") WORK FIRST_N LAST_N SALARY JOINING_DA DEPART
ER_ID AME AME TE MENT
i_f = open(f_n, 'r')
except: 001 Monika Arora 100000 2014-02-20 HR
09:00:00
print("Input file not found")
002 Niharika Verma 80000 2014-06-11 Admin
09:00:00

3
ASSESSMENT : PYTHON & DBMS
003 Vishal Singhal 300000 2014-02-20 HR 19. Write an SQL query to print details of the
09:00:00 Workers whose FIRST_NAME ends with ‘h’
004 Amitabh Singh 500000 2014-02-20 Admin and contains six alphabets.
09:00:00
005 Vivek Bhati 500000 2014-06-11 Admin 20. Write an SQL query to print details of the
09:00:00
Workers who have joined in Feb’2014.
006 Vipul Diwan 200000 2014-06-11 Account
09:00:00
21. Write an SQL query to print details of the
007 Satish Kumar 75000 2014-01-20 Account Workers who are also Managers.
09:00:00
008 Geetika Chauhan 90000 2014-04-11 Admin
09:00:00

Sample Table – Bonus


WORKER_ BONUS_DATE BONUS_
REF_ID AMOUN
T

1 2016-02-20 00:00:00 5000


2 2016-06-11 00:00:00 3000
3 2016-02-20 00:00:00 4000
1 2016-02-20 00:00:00 4500
2 2016-06-11 00:00:00 3500

Sample Table – Title


WORKER_REF WORKER_TITLE AFFECTED_FROM
_ID

1 Manager 2016-02-20
00:00:00
2 Executive 2016-06-11
00:00:00
8 Executive 2016-06-11
00:00:00
5 Manager 2016-06-11
00:00:00
4 Asst. 2016-06-11
Manager 00:00:00
7 Executive 2016-06-11
00:00:00
6 Lead 2016-06-11
00:00:00
3 Lead 2016-06-11
00:00:00

17. Write an SQL query to fetch worker


names with salaries >= 50000 and <= 100000.

18. Write an SQL query to print the name of


employees having the highest salary in each
department.

You might also like