Q. 1

What is the output of the following ?

>>> a = 4.5
>>> b = 2
>>> print( a // b)

Options:

Explanation

Correct answer is : D

// is a floor division operator which truncates the remainder i.e. 0.25 in this case

Discuss It

Q. 2

What is the output of the following code ?

print(int(76.66+9/4))

Options:

Explanation

Correct answer is : B

"/" has higher precedence than "+". So 76.66+9/4 will be evaluated as 76.66 + 2.25results in 78.91
int(78.91) will convert 78.91 to integer type

Discuss It

Q. 3

What is the output of the following code ?

print(float(6+int(6.77)% 5))

Options:

Explanation

Correct answer is : D

int(6.77)will be explicitly converted to 6 and the expression will be evaluated as float(6 + 6 % 5)
"%" has higher precedence than "+" so 6 % 5 will be evaluated first results in 1 and then added to 6 becomes 7
Then 7 will be explicitly converted to float type using float() results in 7.0 finally

Discuss It

Q. 4

Which is an example of explicit type conversion ?

Options:

Explanation

Correct answer is : B

In explicit conversion user has to use methods like float(), int(), str() to convert one data type to another.
In this case python does not converts the type automatically
For example:

>>> "123" + 456
Traceback (most recent call last):
File "", line 1, in
TypeError: must be str, not int
>>>

Here python cannot convert 456 to str type automatically
We need to str() for this:
>>> "123" + str(456)
'123456'
>>>

Discuss It

Q. 5

Which is an example of implicit type conversion ?

Options:

Explanation

Correct answer is : C

in case of 7.0 + 5 , python automatically converts integer type 5 to float type 5.0 which is know as implicit conversion

Discuss It

Q. 6

What is the output of the following code ?

print(int('101010', 2))

Options:

Explanation

Correct answer is : C

int(x, base=10) - Return an integer object constructed from a number or string x, or return 0 if no arguments are given
The default base is 10. The allowed values are 0 and 2–36
Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, whileint('010')is, as well asint('010', 8).

Discuss It

Q. 7

Which of the following results in an error ?

Options:

Explanation

Correct answer is : B

int() can't convert non-string with explicit base.
We need to provide the first argument to int() as string if base is passed to it.
Correct syntax:

int('101010', 2)
int(101010) # base value not provided . So no error

Discuss It

Q. 8

What is the output of the following code ?

print(bin(23-3)+bin(35-5))

Options:

Explanation

Correct answer is : D

bin(20) will be converted to 0b110010 and bin(30) to 0b11110
Concatenating both gives 0b101000b11110 on console

Discuss It

Q. 9

What is the output of the following code ?

>>> a = 13
>>> b = 14
>>> a & b

 

Options:

Explanation

Correct answer is : D

& is bitwise AND operator.

The binary form of  13 is '1101' and 14 is '1110' .

So bit wise AND will give '1100' which is nothing but 12

Discuss It

Q. 10

What is the output of the following code ?

>>> 0x23 | 0x45

 

Options:

Explanation

Correct answer is : D

| is bit wise OR operator.

Binary form of 0x23 is 100011 and of 0x45 is 1000101.

Bit wise OR to will give us 1100111 which is 103

 

Discuss It
Submit Your Answers

Interview Questions

on
Data Types

Tutorial

on
Data Types

Programs

on
Data Types
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description