Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

1. What will be the output of the following Python code?

  1. class father:
  2.     def __init__(self, param):
  3.         self.o1 = param
  4.  
  5. class child(father):
  6.     def __init__(self, param):
  7.         self.o2 = param
  8.  
  9. >>>obj = child(22)
  10. >>>print "%d %d" % (obj.o1, obj.o2)

a) None None
b) None 22
c) 22 None
d) Error is generated

View Answer

Answer: d
Explanation: self.o1 was never created.




2 - Question

2. What will be the output of the following Python code?

  1. class tester:
  2.     def __init__(self, id):
  3.         self.id = str(id)
  4.         id="224"
  5.  
  6. >>>temp = tester(12)
  7. >>>print(temp.id)

a) 224
b) Error
c) 12
d) None

View Answer

Answer: c
Explanation: Id in this case will be the attribute of the class.




3 - Question

3. What will be the output of the following Python code?

  1. >>>example = "snow world"
  2. >>>print("%s" % example[4:7])

a) wo
b) world
c) sn
d) rl

View Answer

Answer: a
Explanation: Execute in the shell and verify.




4 - Question

4. What will be the output of the following Python code?

  1. >>>example = "snow world"
  2. >>>example[3] = 's'
  3. >>>print example

a) snow
b) snow world
c) Error
d) snos world

View Answer

Answer: c
Explanation: Strings cannot be modified.




5 - Question

5. What will be the output of the following Python code?

  1. >>>max("what are you")

a) error
b) u
c) t
d) y

View Answer

Answer: d
Explanation: Max returns the character with the highest ascii value.




6 - Question

6. Given a string example=”hello” what is the output of example.count(‘l’)?
a) 2
b) 1
c) None
d) 0

View Answer

Answer: a
Explanation: l occurs twice in hello.




7 - Question

7. What will be the output of the following Python code?

  1. >>>example = "helle"
  2. >>>example.find("e")

a) Error
b) -1
c) 1
d) 0

View Answer

Answer: c
Explanation: Returns lowest index.




8 - Question

8. What will be the output of the following Python code?

  1. >>>example = "helle"
  2. >>>example.rfind("e")

a) -1
b) 4
c) 3
d) 1

View Answer

8. What will be the output of the following Python code?

  1. >>>example = "helle"
  2. >>>example.rfind("e")

a) -1
b) 4
c) 3
d) 1




9 - Question

9. What will be the output of the following Python code?

  1. >>>example="helloworld"
  2. >>>example[::-1].startswith("d")

a) dlrowolleh
b) True
c) -1
d) None

View Answer

Answer: b
Explanation: Starts with checks if the given string starts with the parameter that is passed.




10 - Question

10. 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

View Answer

Answer: c
Explanation: __add__ is another method that can be used for concatenation.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners