Test Series - python

Test Number 24/108

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

class father:
    def __init__(self, param):
        self.o1 = param
 
class child(father):
    def __init__(self, param):
        self.o2 = param
 
>>>obj = child(22)
>>>print "%d %d" % (obj.o1, obj.o2)
A. None None
B. None 22
C. 22 None
D. Error is generated
Solution:  self.o1 was never created.
Q: What will be the output of the following Python code?

class tester:
    def __init__(self, id):
        self.id = str(id)
        id="224"
 
>>>temp = tester(12)
>>>print(temp.id)
A. 224
B. Error
C. 12
D. None
Solution: Id in this case will be the attribute of the class.
Q: What will be the output of the following Python code?

>>>example = "snow world"
>>>print("%s" % example[4:7])
A. wo
B. world
C. sn
D. rl
Solution: Execute in the shell and verify.
Q: What will be the output of the following Python code?

>>>max("what are you")
A. error
B. u
C. t
D. y
Solution: Max returns the character with the highest ascii value.
Q: Given a string example=”hello” what is the output of example.count(‘l’)?
A. 2
B. 1
C. None
D. 0
Solution: l occurs twice in hello.
Q:  What will be the output of the following Python code?

>>>example = "helle"
>>>example.find("e")
A. Error
B. -1
C. 1
D. 0
Solution: Returns lowest index.
Q: What will be the output of the following Python code?

>>>example = "helle"
>>>example.rfind("e")
A. -1
B. 4
C. 3
D. 1
Solution: Returns highest index.
Q: What will be the output of the following Python code?

>>>example="helloworld"
>>>example[::-1].startswith("d")
A. dlrowolleh
B. True
C. -1
D. None
Solution: Starts with checks if the given string starts with the parameter that is passed.
Q: To concatenate two strings to a third what statements are applicable?
A. s3 = s1 . s2
B. s3 = s1.add(s2)
C. s3 = s1.__add__(s2)
D. s3 = s1 * s2
Solution: __add__ is another method that can be used for concatenation.

You Have Score    /9