QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - INTRODUCTION TO PYTHON

INTRODUCTION TO PYTHON

00:00:00

What will be the output of the following Python code?

print("Hello {0} and {1}".format(('foo', 'bin')))

Option A. Hello foo and bin
Option B. Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
Option C. Error
Option D. None of the mentioned
True Answer C

Explanation :
IndexError, the tuple index is out of range.

What will be the output of the following Python code?

print("Hello {0} and {1}".format(('foo', 'bin')))

Question No# : 01 out of 20



What will be the output of the following Python code?

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

Option A. dlrowolleh
Option B. True
Option C. -1
Option D. None
True Answer B

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

What will be the output of the following Python code?

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

Question No# : 02 out of 20



What will be the output of the following Python code?

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

Option A. wo
Option B. world
Option C. sn
Option D. rl
True Answer A

Explanation :
Execute in the shell and verify.

What will be the output of the following Python code?

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

Question No# : 03 out of 20



What will be the output of the following Python code snippet?

print('cd'.partition('cd'))

Option A. (‘cd’)
Option B. (”)
Option C. (‘cd’, ”, ”)
Option D. (”, ‘cd’, ”)
True Answer D

Explanation :
The entire string has been passed as the separator hence the first and the last item of the tuple returned are null strings.

What will be the output of the following Python code snippet?

print('cd'.partition('cd'))

Question No# : 04 out of 20



What arithmetic operators cannot be used with strings?

Option A. +
Option B. *
Option C. –
Option D. All of the mentioned
True Answer C

Explanation :
+ is used to concatenate and * is used to multiply strings.

What arithmetic operators cannot be used with strings?

Question No# : 05 out of 20



What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy"))

Option A. 1
Option B. True
Option C. 3
Option D. 2
True Answer B

Explanation :
The function returns True if the given string ends with the specified substring.

What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy"))

Question No# : 06 out of 20



Which of the following is true for variable names in Python?

Option A. unlimited length
Option B. all private members must have leading and trailing underscores
Option C. underscore and ampersand are the only two special characters allowed
Option D. none of the mentioned
True Answer A

Explanation :
Variable names can be of any length.

Which of the following is true for variable names in Python?

Question No# : 07 out of 20



What will be the output of the following Python expression if the value of x is 34?

print(“%f”%x)

Option A. 34.00
Option B. 34.0000
Option C. 34.000000
Option D. 34.00000000
True Answer C

Explanation :
The expression shown above normally returns the value with 6 decimal points if it is not specified with any number. Hence the output of this expression will be: 34.000000 (6 decimal points).

What will be the output of the following Python expression if the value of x is 34?

print(“%f”%x)

Question No# : 08 out of 20



What will be the output of the following Python code snippet?

print('11'.isnumeric())

Option A. True
Option B. False
Option C. None
Option D. Error
True Answer A

Explanation :
All the character are numeric.

What will be the output of the following Python code snippet?

print('11'.isnumeric())

Question No# : 09 out of 20



What will be the output of the following Python code?

print("Hello {0} and {1}".format('foo', 'bin'))

Option A. Hello foo and bin
Option B. Hello {0} and {1} foo bin
Option C. Error
Option D. Hello 0 and 1
True Answer A

Explanation :
The numbers 0 and 1 represent the position at which the strings are present.

What will be the output of the following Python code?

print("Hello {0} and {1}".format('foo', 'bin'))

Question No# : 10 out of 20



What will be the output of the following Python statement?(python 3.xx)

  1. >>>print(format("Welcome", "10s"), end = '#')
  2. >>>print(format(111, "4d"), end = '#')
  3. >>>print(format(924.656, "3.2f"))

Option A. Welcome# 111#924.66
Option B. Welcome#111#924.66
Option C. Welcome#111#.66
Option D. Welcome # 111#924.66
True Answer D

Explanation :
Execute in the shell to verify.

What will be the output of the following Python statement?(python 3.xx)

  1. >>>print(format("Welcome", "10s"), end = '#')
  2. >>>print(format(111, "4d"), end = '#')
  3. >>>print(format(924.656, "3.2f"))

Question No# : 11 out of 20



What is the maximum possible length of an identifier?

Option A. 31 characters
Option B. 63 characters
Option C. 79 characters
Option D. none of the mentioned
True Answer D

Explanation :
Identifiers can be of any length.

What is the maximum possible length of an identifier?

Question No# : 12 out of 20



What will be the value of the following Python expression?

float(4+int(2.39)%2)

Option A. 5.0
Option B. 5
Option C. 4.0
Option D. 4
True Answer C

Explanation :
The above expression is an example of explicit conversion. It is evaluated as: float(4+int(2.39)%2) = float(4+2%2) = float(4+0) = 4.0. Hence the result of this expression is 4.0.

What will be the value of the following Python expression?

float(4+int(2.39)%2)

Question No# : 13 out of 20



What will be the output of the following Python code snippet?

print('abcd'.translate('a'.maketrans('abc', 'bcd')))

Option A. bcde
Option B. abcd
Option C. error
Option D. bcdd
True Answer D

Explanation :
The output is bcdd since no translation is provided for d.

What will be the output of the following Python code snippet?

print('abcd'.translate('a'.maketrans('abc', 'bcd')))

Question No# : 14 out of 20



What will be the output of the following Python code snippet?

print('abcd'.translate({'a': '1', 'b': '2', 'c': '3', 'd': '4'}))

Option A. abcd
Option B. 1234
Option C. error
Option D. none of the mentioned
True Answer A

Explanation :
The function translate expects a dictionary of integers. Use maketrans() instead of doing the above.

What will be the output of the following Python code snippet?

print('abcd'.translate({'a': '1', 'b': '2', 'c': '3', 'd': '4'}))

Question No# : 15 out of 20



What will be the output of the following Python code snippet?

print('abc'.islower())

Option A. True
Option B. False
Option C. None
Option D. Error
True Answer A

Explanation :
There are no uppercase letters.

What will be the output of the following Python code snippet?

print('abc'.islower())

Question No# : 16 out of 20



What will be the output of the following Python code?

print('ab12'.isalnum())

Option A. True
Option B. False
Option C. None
Option D. Error
True Answer A

Explanation :
The string has only letters and digits.

What will be the output of the following Python code?

print('ab12'.isalnum())

Question No# : 17 out of 20



What will be the output of the following Python code?

print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))

Option A. Hello foo and bin
Option B. Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
Option C. Error
Option D. None of the mentioned
True Answer A

Explanation :
The elements of the tuple are accessed by their indices.

What will be the output of the following Python code?

print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))

Question No# : 18 out of 20



Which of the following cannot be a variable?

Option A. __init__
Option B. in
Option C. it
Option D. on
True Answer B

Explanation :
in is a keyword.

Which of the following cannot be a variable?

Question No# : 19 out of 20



What will be the output of the following Python code?

print("ab\tcd\tef".expandtabs())

Option A. ab  cd  ef
Option B. abcdef
Option C. ab cd ef
Option D. ab cd ef
True Answer A

Explanation :
Each is converted to 8 blank spaces by default.

What will be the output of the following Python code?

print("ab\tcd\tef".expandtabs())

Question No# : 20 out of 20