πŸ“Š Problem Solving and Python Programming
Q. What will be the output of the following
Python code?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]
  • (A) olleh
  • (B) hello
  • (C) h
  • (D) o
πŸ’¬ Discuss
βœ… Correct Answer: (D) o

Explanation: -1 corresponds to the last index.

πŸ“Š Problem Solving and Python Programming
Q. What will be the output of the following Python code? >>>print (r"\nhello")
  • (A) a new line and hello
  • (B) \\nhello
  • (C) the letter r and then hello
  • (D) error
πŸ’¬ Discuss
βœ… Correct Answer: (B) \\nhello

Explanation: when prefixed with the letter ‘r’ or ‘r’ a string literal becomes a raw string and the escape sequences such as \n are not converted.

πŸ“Š Problem Solving and Python Programming
Q. What will be the output of the following Python code? >>>print('new' 'line')
  • (A) error
  • (B) output equivalent to print ‘new\\nline’
  • (C) newline
  • (D) new line
πŸ’¬ Discuss
βœ… Correct Answer: (C) newline

Explanation: string literal separated by whitespace are allowed. they are concatenated.

πŸ“Š Problem Solving and Python Programming
Q. What will be the output of the following
Python code?
1. >>>str1="helloworld"
2. >>>str1[::-1]
  • (A) dlrowolleh
  • (B) hello
  • (C) world
  • (D) helloworld
πŸ’¬ Discuss
βœ… Correct Answer: (A) dlrowolleh

Explanation: execute in shell to verify.

πŸ“Š Problem Solving and Python Programming
Q. What will be the output of the following
Python code?
print(0xA + 0xB + 0xC)
  • (A) 0xa0xb0xc
  • (B) error
  • (C) 0x22
  • (D) 33
πŸ’¬ Discuss
βœ… Correct Answer: (D) 33

Explanation: 0xa and 0xb and 0xc are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. there sum is 33.

πŸ“Š Problem Solving and Python Programming
Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (D) error is generated

Explanation: self.o1 was never created.

πŸ“Š Problem Solving and Python Programming
Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (C) 12

Explanation: id in this case will be the attribute of the class.

πŸ“Š Problem Solving and Python Programming
Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (A) wo

Explanation: execute in the shell and verify.

πŸ“Š Problem Solving and Python Programming
Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (C) error

Explanation: strings cannot be modified.

Jump to