Docsity

BCA Semester 1 Python Programming Complete Notes, Lecture notes of Computer Applications

Toggle sidebar
bg1
Q1: What will be the output of the following Python code?
class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()
a) Invalid syntax for inheritance
b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()
Answer: d
Q2: What will be the output of the following Python code?
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
a) Error because class B inherits A but variable x isn’t inherited
b) 0 0
c) 0 1
d) Error, the syntax of the invoking method is wrong
Answer: c
Q3: class is a _______
a) template
b) blue print
c) both a and b
d)None of the above
Answer: c
Q4: Which of the following is not a type of inheritance?
a) Double-level
b) Multi-level
c) Single-level
d) Multiple
Answer: a
Q5: What type of inheritance is illustrated in the following Python code?
class A():
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download BCA Semester 1 Python Programming Complete Notes and more Lecture notes Computer Applications in PDF only on Docsity!

Q1: What will be the output of the following Python code?

class A():

def disp(self):

print("A disp()")

class B(A):

pass

obj = B()

obj.disp()

a) Invalid syntax for inheritance

b) Error because when object is created, argument must be passed

c) Nothing is printed

d) A disp()

Answer: d

Q2: What will be the output of the following Python code?

class Test:

def init(self):

self.x = 0

class Derived_Test(Test):

def init(self):

Test.init(self)

self.y = 1

def main():

b = Derived_Test()

print(b.x,b.y)

main()

a) Error because class B inherits A but variable x isn’t inherited

b) 0 0

c) 0 1

d) Error, the syntax of the invoking method is wrong

Answer: c

Q3: class is a _______

a) template

b) blue print

c) both a and b

d)None of the above

Answer: c

Q4: Which of the following is not a type of inheritance?

a) Double-level

b) Multi-level

c) Single-level

d) Multiple

Answer: a

Q5: What type of inheritance is illustrated in the following Python code?

class A():

pass

class B():

pass

class C(A,B):

pass

a) Multi-level inheritance

b) Multiple inheritance

c) Hierarchical inheritance

d) Single-level inheritance

Answer: b

Q6: What will be the output of the following Python code?

class A:

def one(self):

return self.two()

def two(self):

return 'A'

class B(A):

def two(self):

return 'B'

obj1=A()

obj2=B()

print(obj1.two(),obj2.two())

a) A A

b) A B

c) B B

d) An exception is thrown

Answer: b

Q7:What will be the output of the following Python code?

class A:

def init(self):

self.__i = 1

self.j = 5

def display(self):

print(self.__i, self.j)

class B(A):

def init(self):

super().init()

self.__i = 2

self.j = 7

c = B()

c.display()

a) 2 7

b) 1 5

test of A called

b) test of C called

test of B called

c) test of B called

test of C called

d) Error, all the three classes from which D derives has same method test()

Answer: a

Q11. The assignment of more than one function to a particular operator is _______

a) Operator over-assignment

b) Operator overriding

c) Operator overloading

d) Operator instance

Answer: c

Q 12. What is delattr(obj,name) used for?

a) To print deleted attribute

b) To delete an attribute

c) To check if an attribute is deleted or not

d) To set an attribute

Answer: b

Q 13. del method is used to destroy instances of a class.

a) True

b) False

Answer: a

Q 14. What will be the output of the following Python code?

class stud:

'Base class for all students'

def init(self, roll_no, grade):

self.roll_no = roll_no

self.grade = grade

def display (self):

print("Roll no : ", self.roll_no, ", Grade: ", self.grade)

print(stud.doc)

a) Exception is thrown

b) main

c) Nothing is displayed

d) Base class for all students

Answer: d

Q 15. What does print(Test.name) display (assuming Test is the name of the class)?

a) ()

b) Exception is thrown

c) Test

d) main

Answer: c

Q 16:What will be the output of following code?

print(False=={})

a)False

b)True

Answer: a

Q 17. What will be the output of the following Python code?

x=

def f1(a,b=x):

print(a,b)

x=

f1(4)

a) Error

b) 12 4

c) 4 12

d) 4 15

Answer: c

Q18: What will be the output of the following Python code?

def f1(a,b=[]):

b.append(a)

return b

print(f1(2,[3,4]))

a) [3,2,4]

b) [2,3,4]

c) Error

d) [3,4,2]

Answer: d

Q 19: What will be the output of the following Python code?

def f(p, q, r):

global s

p = 10

q = 20

r = 30

s = 40

print(p,q,r,s)

p,q,r,s = 1,2,3,

c) x, y and z

d) Neither x, nor y, nor z

Answer: c

Q 23) Which of these is a private data field?

class Demo:

def init(self,x,y,z):

self.a=x

self._b=y

self.__c=z

a) a

b) __c

c) _b

d) x

Answer: b

Q 24) What will be the output of the following Python code?

class fruits:

def init(self):

self.price = 100

self.__bags = 5

def display(self):

print(self.__bags)

obj=fruits()

obj.display()

a) The program has an error because display() is trying to print a private class member

b) The program runs fine but nothing is printed

c) The program runs fine and 5 is printed

d) The program has an error because display() can’t be accessed

Answer: c

Q 25) What will be the output of the following Python code?

class student:

def init(self):

self.marks = 97

self.__cgpa = 8.

def display(self):

print(self.marks)

obj=student()

print(obj._student__cgpa)

a) The program runs fine and 8.7 is printed

b) Error because private class members can’t be accessed

c) Error because the proper syntax for name mangling hasn’t been implemented

d) The program runs fine but nothing is printed

Answer: a

if not a or b: print ("a") elif not a or not b and c: print ("b") elif not a or b or not b and a: print ("c") else: print ("d") (A) a (B) b (C) c (D) d Ans B Q8: What is the output of following code? class test: def init(self): print ("Hello World") def init(self): print ("Bye World") obj=test() (A) Hello World (B) Compilation Error (C) Bye World (D) Ambiguity Ans: C Q9: Which of the following is an invalid statement? (A) abc = 1,000, (B) a b c = 1000 2000 3000 (C) a,b,c = 1000, 2000, 3000 (D) a_b_c = 1,000, Ans: B Q10: Is it possible to use round function without any argument like round() (A) Yes (B) No Ans:: B Q11: What is an exception (A) Error (B) Compile time error (C) Run time error (D) None Ans: C Q12: Predict the output of following Python Programs. class Acc: def init(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) A) B) C)

D)

Ans: A Q13:Predict the output of following Python Programs. counter = {} def addToCounter(country): if country in counter: counter[country] += 1 else: counter[country] = 1 addToCounter('China') addToCounter('Japan') addToCounter('china') print (len(counter)) A) B) C) D) Ans 3 Q 14: Predict the output of following Python Program count = 1 def doThis(): global count for i in (1, 2, 3): count += 1 doThis() print (count) A) B) C) D) ANS: A Q 15: dictionary = {1:'1', 2:'2', 3:'3'} del dictionary[1] dictionary[1] = '10' del dictionary[2] print (len(dictionary)) A) B) C) D) ANS: C Q 16:What is the output of the following piece of code? class A: def init(self):

main() a) 1 b) 0 c) 1 0 d) Error Ans: d Q21:Which of the following best describes polymorphism? a) Ability of a class to derive members of another class as a part of its own definition b) Means of bundling instance variables and methods in order to restrict access to certain class members c) Focuses on variables and passing of variables to functions d) having more than one form. Ans d Q22: What is the output of the following piece of code? class A: def init(self,x=3): self._x = x class B(A): def init(self): super().init(5) def display(self): print(self._x) def main(): obj = B() obj.display() main() a) 5 b) 3 c) Error, class member x has two values d) Error, protected class member can’t be accessed in a subclass Ans: a Q23: What is the output of the following piece of code? class A: def str(self): return '1' class B(A): def init(self): super().init() class C(B): def init(self): super().init() def main(): obj1 = B()

obj2 = A() obj3 = C() print(obj1, obj2,obj3) main() a) 1 1 1 b) 1 2 3 c) ‘1’ ‘1’ ‘1’ d) An exception is thrown Ans:a Q 24:A class in which one or more methods are only implemented to raise an exception is called an abstract class. True or False? a) True b) False Ans: b Q25: Inheritance shows. a) IS A Relationship b) HAS A Relationship c) USES Relationship d)None of the above. Ans: a

f.writelines(txt, list) f.seek(0,0) line = f.readlines() print ("Read Line: %s" % (line)) f.close() A. ['This', 'is', '1st', 'line ]� B. [] C. Error D. None Answer: C

  1. What Will Be The Output Of The Following Code Snippet? try: f = open("testfile", "r") f.write("This is the test file for exception handling!!") except IOError: print ("Error: could not find a file or read data") finally: print ("In finally part") else: print ("content is written in the file successfully") A. This is the test file for exception handling!! In finally part B. Error: could not find a file or read data In finally part C. In finally part content is written in the file successfully D. SyntaxError Answer: D
  1. What Will Be The Output Of The Following Code Snippet? colors = ['red\n', 'yellow\n', 'blue\n'] f = open('colors.txt', 'w') f.write(colors) f.seek(0,0) for line in f: print (line) f.close() A. red yellow blue B. [ red\n ,� � �yellow\n , � �blue\n ] � C. Error D. Compilation error Answer: C
  1. What gets printed? import re list1 = ['astronaut', 'astrology', 'astronomy', 'archeology'] from the list, if we have to get the strings starting with "astro", which one is the right way to use? A. for i in list1: if re.match("(astro)", i): print (i) B. for i in list1:

if re.findall("^(astro)", i): print (i) C. print (re.findall("^(astro)", list1)) D. Both A and B Answer: D

  1. What will be the output of the following Python code? import re sentence = 'we are humans and we are just humans' matched = re.search('(.*(humans))', sentence) print(matched.group()) A. we are humans and we are just humans B. ('we are humans', 'humans') C. ('we are humans and we are just humans') D. 'we are humans' Answer: A
  1. What will be the output of the following Python code? import re sentence = 'we are humans and we are just humans' matched = re.match('(.*?humans)', sentence) print(matched.group()) A. ('we', 'are', 'humans') B. (we, are, humans) C. ('we', 'humans') D. we are humans Answer: D
  1. What will be the output of the following Python code? import re sentence = 'we are humans' matched = re.match(r'(.) (.)', sentence) print(matched.groups()) A. ('we are', 'humans') B. 'are' C. 'humans' D. ('we', 'are', 'humans') Answer: A
  1. What will be the output of the following Python code? import re sentence = 'we are humans' matched = re.match(r'(.?) (.)', sentence) print(matched.groups(2)) A. ('we', 'are humans') B. ('we', 'humans') C. 'we are' D. 'are' Answer: A
  1. What will happen when the following Python code is executed? import re sentence = 'horses are fast. deers are fastest' matched = re.sub("horses", "deers", sentence) print(matched)
  1. What is the output of the following code? string = "This is python" id1 = id(string) string = string.replace("python", "java") id2 = id(string) print (id1 == id2) A. True B. False C. 0 D. 1 Answer: B
  1. What is the output of the following code? string = "This is python" print (string.rindex("is")) A. 2 B. 1 C. Error D. None of the above Answer: D
  1. What is the output of the following code? string = "65 66 67 68 69 70 71" for i in string: print (chr(i), end = " ") A. ABCDEFG B. Error C. acdefg D. None of the above Answer: B
  1. What will be the output of the following code? string = "12345678" s = "" for i in range(len(string)): s = string[i] + s print (s) A. 87654321 B. 12345678 C. NameError D. TypeError Answer: A
  1. What will be the output? print (('hello' + 'world') + ('how are you?') * 0) A. Error B. prints nothing C. helloworld D. 0 Answer: C
  1. What will be printed from the following code? string = "PYTHON TEST" for i in string: if i == "N": pass print (i, end = "")

A. PYTHO TEST

B. PYTHON TEST

C. PYTHONTEST

D. Error Answer: B

  1. What will the output of the following line? >>>"PYTHON TEST"[-11:-1:-1] A. >>>'TSET NOHTY' B.>>> 'PYTHON TES' C.>>> PYTHON TEST D.None of the above Answer: D