Blog

6/recent/ticker-posts

Python Questions and Answers



General Python Quiz


Q 1 - What is the correct file extension for Python files?

A ) .pyt

B ) .pt

C ) .py

D ) .pyth

Answer : C

the correct file extension for Python files is .py

Q 2 - Which of the following expressions is an example of type conversion?

A ) 4.0 + float(3)

B ) 5.3 + 6.3

C ) 5.0 + 3

D ) 5.3 + 6.3

Answer : A

Explanation

Type conversion is nothing but explicit conversion of operands to a specific type. Options 5.3 + 6.3 and 5.0 + 3 are examples of implicit conversion whereas option 4.0 + float(3) is an example of explicit conversion or type conversion.

Q 3 - The expression 2**2**3 is evaluates as: (2**2)**3.

A - True

B - False

Answer : B

Explanation

The value of the expression (2**2)**3 = 4**3 = 64. When the expression 2**2**3 is evaluated in python, we get the result as 256, because this expression is evaluated as 2**(2**3). This is because the associativity of exponentiation operator (**) is from right to left and not from left to right.

Q 4 - What is the output of the following function call

def outer_fun(a, b):
    def inner_fun(c, d):
        return c + d

    return inner_fun(a, b)
    return a

result = outer_fun(5, 10)
print(result)

A ) 15

B ) 5

C ) Syntax Error

D ) (15, 5)

Answer : A

Explanation

Adding multiple return statements doesn’t perform any task. Once function execution is encountered with the return statement, it stops the execution by returning whatever specified by the return statement.

Answer : D

Explanation

The code ‘There are %d %s birds.’ %(4, blue) results in the output: There are 4 blue birds. When we insert more than one value, we should group the values on the right in a tuple.

Q 6 - What is the output of the following code?

sampleList = ["Junior", "Mason", "Nelson"]
sampleList.append(2, "Gideon")
print(sampleList)

A ) [‘Junior’, ‘Mason’, ‘Gideon’, ‘Nelson’]

B ) Error

C ) [‘Junior’, ‘Mason’, ‘Nelson’, ‘Gideon’]

D ) [‘Junior’, ‘Gideon’, ‘Mason’, Nelson’]

Answer : B

Explanation

The append() method appends an item to the end of the list. Therefore, we cannot pass the index number to it.

Q 7 - The formatting method {1:<10} represents the ___________ positional argument, _________ justified in a 10 character wide field.

A ) first, left

B ) second, right

C ) first, right

D ) second, left

Answer : D

Explanation

The formatting method {1:<10} represents the second positional argument, left justified in a 10 character wide field.

Q 8 - The ______ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.

A ) #

B ) @

C ) $

D ) &

Answer : B

Explanation

The @ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.

Q 9 - What arithmetic operators cannot be used with strings?

A ) +

B ) –

C ) *

D ) All of the above mentioned

Answer : B

Explanation

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

Q 10 - What is the value of x

x = 0
while (x < 100):
  x+=2
print(x)

A ) 99

B ) 101

C ) 100

D ) None of the above

Answer : C

python_questions_answers.html

Advertisements

Post a Comment

0 Comments

Ad Code

Responsive Advertisement

Hot Post