0% found this document useful (0 votes)
5 views16 pages

Question 2

The document provides a comprehensive overview of various Python concepts, including data types, operators, and expressions. It covers integer types, string types, mutable vs immutable types, and the differences between equality and identity operators. Additionally, it includes examples and explanations of type conversions, boolean expressions, and evaluation of expressions.

Uploaded by

jebajesudas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Question 2

The document provides a comprehensive overview of various Python concepts, including data types, operators, and expressions. It covers integer types, string types, mutable vs immutable types, and the differences between equality and identity operators. Additionally, it includes examples and explanations of type conversions, boolean expressions, and evaluation of expressions.

Uploaded by

jebajesudas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 2

How many integer types are supported by Python? Name them.

Answer

Two integer types are supported by Python. They are:

1. Integers (signed)

2. Booleans

Question 3

How are these numbers different from one another (with respect to Python)? 33, 33.0, 33j, 33 + j

Answer

The number 33 is an integer whereas 33.0 is a floating-point number. 33j represent the imaginary
part of a complex number. 33 + j is a complex number.

Question 4

The complex numbers have two parts : real and imaginary. In which data type are real and imaginary
parts represented ?

Answer

In Python, the real and imaginary parts of a complex number are represented as floating-point
numbers.

Question 5

How many string types does Python support? How are they different from one another?

Answer

Python supports two types of strings — Single-line strings and Multi-line strings. Single line strings
are enclosed in single or double quotes and terminate in one line. Multi-line strings store multiple
lines of text and are enclosed in triple quotes.

Question 6

What will following code print?

str1 = '''Hell

o'''

str2 = '''Hell\

o'''

print(len(str1) > len(str2))

Answer

This code will print:

True
len(str1) is 6 due to the EOL character. len(str2) is 5 as backslash (\) character is not counted in the
length of string. As len(str1) is greater than len(str2) so the output is True.

Question 7

What are Immutable and Mutable types in Python? List immutable and mutable types of Python.

Answer

Mutable types are those whose values can be changed in place whereas Immutable types are those
that can never change their value in place.

Mutable types in Python are:

1. Lists

2. Dictionaries

3. Sets

Immutable types in Python are:

1. Integers

2. Floating-Point numbers

3. Booleans

4. Strings

5. Tuples

Question 8

What are three internal key-attributes of a value-variable in Python ? Explain with example.

Answer

The three internal key-attributes of a value-variable in Python are:

1. Type

2. Value

3. Id

For example, consider this:

a=4

The type of a is int which can be found with the built-in function type() like this:
type(a).

Value can be found using the built-in function print() like this:
print(a)

It will give the output as 4 which is value contained in variable a.

Id is the memory location of the object which can be determined using built-in function id() like this:
id(a)
Question 9

Is it true that if two objects return True for is operator, they will also return True for == operator?

Answer

Yes, if is operator returns true, it implicitly means that the equality operator will also return True. is
operator returning true implies that both the variables point to the same object and hence ==
operator must return True.

Question 10

Are these values equal? Why/why not?

1. 20 and 20.0

2. 20 and int(20)

3. str(20) and str(20.0)

4. 'a' and "a"

Answer

1. The type of 20 is int whereas the type of 20.0 is float so they are two different objects. Both
have the same value of 20. So, as values are same equality (==) operator return True but as
objects are different is operator returns False.

2. The value and type of both 20 and int(20) are the same and both point to the same object so
both equality (==) and is operator returns True.

3. For str(20) and str(20.0), both equality (==) and is operator returns False as their values are
different and they point to two different objects.

4. For 'a' and "a", both equality (==) and is operator returns True as their values are same and
they point to the same object.

Question 11

What is an atom in Python? What is an expression?

Answer

In Python, an atom is something that has a value. Identifiers, literals, strings, lists, tuples, sets,
dictionaries, etc. are all atoms. An expression in Python, is any valid combination of operators and
atoms. It is composed of one or more operations.

Question 12

What is the difference between implicit type conversion and explicit type conversion?

Answer
Implicit Type Conversion Explicit Type Conversion

An implicit type conversion is automatically performed by the compiler An explicit type conversion is user-defin
when differing data types are intermixed in an expression. that forces an expression to be of specifi

An implicit type conversion is performed without programmer's An explicit type conversion is specified e
intervention. programmer.

Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)

Question 13

Two objects (say a and b) when compared using == ,return True. But Python gives False when
compared using is operator. Why? (i.e., a == b is True but why is a is b False?)

Answer

As equality (==) operator returns True, it means that a and b have the same value but as is operator
returns False, it means that variables a and b point to different objects in memory. For example,
consider the below Python statements:

>>> a = 'abc'

>>> b = input("Enter a string: ")

Enter a string: abc

>>> a == b

True

>>> a is b

False

Here, both a and b have the same value 'abc' but they point to different objects.

Question 14

Given str1 = "Hello", what will be the values of:

(a) str1[0]

(b) str1[1]

(c) str1[-5]

(d) str1[-4]

(e) str1[5]

Answer

(a) H
(b) e

(c) H

(d) e

(e) IndexError: string index out of range

Explanation

H 0−5 e 1−4 l 2−3 l3−2 o 4−1


Question 15

If you give the following for str1 = "Hello", why does Python report error?

str1[2] = 'p'

Answer

Python reports error because strings are immutable and hence item assignment is not supported.

Question 16

What will the result given by the following?

(a) type (6 + 3)

(b) type (6 -3)

(c) type (6 *3)

(d) type (6 / 3)

(e) type (6 // 3)

(f) type (6 % 3)

Answer

⇒ int + int
(a) type (6 + 3)

⇒ int
So the result is int.

⇒ int - int
(b) type (6 -3)

⇒ int
So the result is int.

⇒ int * int
(c) type (6 * 3)

⇒ int
So the result is int.

⇒ int / int
(d) type (6 / 3)
⇒ float
So the result is float.

⇒ int // int
(e) type (6 // 3)

⇒ int
So the result is int.

⇒ int % int
(f) type (6 % 3)

⇒ int
So the result is int.

Question 17

What are augmented assignment operators? How are they useful?

Answer

Augmented assignment operators combine the impact of an arithmetic operator with an assignment
operator. For example, to add the value of b to the value of a and assign the result back to a then
instead of writing:

a=a+b

we can write

a += b.

Augmented assignment operators are useful as they provide a shorthand way by combining the
arithmetic and assignment operators.

Question 18

Differentiate between (555/222)**2 and (555.0/222)**2.

Answer

In the first expression, 555 is of int type whereas in the second expression 555.0 is of float type.

Question 19

Given three Boolean variables a, b, c as : a = False, b = True, c = False. Evaluate the following Boolean
expressions:

(a) b and c

(b) b or c

(c) not a and b

(d) (a and b) or not c

(e) not b and not (a or c)

(f) not ((not b or not a) and c) or a

Answer
⇒ False and True
(a) b and c

⇒ False

⇒ True or False
(b) b or c

⇒ True

⇒ not False and True


(c) not a and b

⇒ True and True


⇒ True

⇒ (False and True) or not False


(d) (a and b) or not c

⇒ False or not False


⇒ False or True
⇒ True

⇒ not True and not (False or False)


(e) not b and not (a or c)

⇒ not True and not False


⇒ False and True
⇒ False

⇒ not ((not True or not False) and False) or False


(f) not ((not b or not a) and c) or a

⇒ not ((False or True) and False) or False


⇒ not (True and False) or False
⇒ not False or False
⇒ True or False
⇒ True

Question 20

What would following code fragments result in? Given x = 3.

(a) 1 < x

(b) x >= 4

(c) x == 3

(d) x == 3.0

(e) "Hello" == "Hello"

(f) "Hello" > "hello"

(g) 4/2 == 2.0

(h) 4/2 == 2

(i) x < 7 and 4 > 5.


Answer

(a) True

(b) False

(c) True

(d) True

(e) True

(f) False

(g) True

(h) True

(i) False

Question 21

Write following expressions in Python:

1 2
(a) b h
3
Answer

1/3*b*b*h

(b) π r 2 h

Answer

[Link] * r * r * h

1 2
(c) πr h
3
Answer

1 / 3 * [Link] * r * r * h

(d) √ ¿ ¿

Answer

[Link]((x2 - x1) ** 2 + (y2 - y1) ** 2)

(e) ¿

Answer

(x - h) ** 2 + (y - k) ** 2 == r ** 2

−b+ √ b 2−4 ac
(f) x=
2a
Answer
x = (-b + [Link](b * b - 4 * a * c)) / (2 * a)

(g) a n × am =an +m

Answer

a ** n * a ** m == a ** (n + m)

(h) ( a n ¿ m=a nm

Answer

(a ** n) ** m == a ** (n * m)
n
a n−m
(i) m
=a
a
Answer

a ** n / a ** m == a ** (n - m)

−n 1
(j) a = n
a
Answer

a ** -n == 1 / a ** n

Question 22

int('a') produces error. Why ?

Answer

int() converts its argument into an integer. As 'a' is a letter, it cannot be converted into a valid integer
hence int('a') produces an error.

Question 23

int('a') produces error but following expression having int('a') in it, does not return error. Why?

len('a') + 2 or int('a')

Answer

The or operator tests the second operand only if the first operand is false otherwise it ignores it. In
the expression, the first operand of or operator is len('a') + 2. It evaluates to 3 and as it is a non-zero
value hence it is True for or operator. As first operand of or operator is True so it doesn't evaluate its
second argument int('a') and no error is returned.

Question 24

Write expression to convert the values 17, len('ab') to (i) integer (ii) str (iii) boolean values

Answer

(i) int(17), int(len('ab'))

(ii) str(17), str(len('ab'))


(iii) bool(17), bool(len('ab'))

Question 25

Evaluate and Justify:

(i) 22 / 17 = 37 / 47 + 88 /83

(ii) len('375')**2

Answer

(i) It produces an error as LHS value in this case is an expression that evaluates to a literal whereas
LHS value should be a variable.

⇒ 3 ** 2 [∵ len('375') = 3]
(ii) len('375')**2

⇒9 [∵ 3 * 3 = 9]

Question 26

Evaluate and Justify:

(i) 22.0/7.0 - 22/7

(ii) 22.0/7.0 - int(22.0/7.0)

(iii) 22/7 - int (22.0)/7

Answer

⇒0
(i) 22.0/7.0 - 22/7

As values of 22.0/7.0 and 22/7 are equal, subtracting them will give the result as 0.0.

⇒ 3.142857142857143 - 3
(ii) 22.0/7.0 - int(22.0/7.0)

⇒ 0.142857142857143

⇒ 0.0
(iii) 22/7 - int (22.0)/7

int (22.0) gives 22 so the expression becomes 22/7 - 22/7 which results in 0.0

Question 27

Evaluate and Justify:

(i) false and None

(ii) 0 and None

(iii) True and None

(iv) None and None

Answer
(i) This produces an error as false is an invalid literal in Python. It should be False. Had the expression
being False and None, the return value will be False.

(ii) This logical expression evaluates to 0. As first operand of and operator is false so it will return the
first operand itself.

(iii) This logical expression evaluates to None. As first operand of and operator is True so it will return
the second operand.

(iv) This logical expression evaluates to None. As first operand of and operator is false so it will return
the first operand itself.

Question 28

Evaluate and Justify:

(a) 0 or None and "or"

(b) 1 or None and 'a' or 'b'

(c) False and 23

(d) 23 and False

(e) not (1 == 1 and 0 != 1)

(f) "abc" == "Abc" and not (2 == 3 or 3 == 4)

(g) False and 1 == 1 or not True or 1 == 1 and False or 0 == 0

Answer

⇒ 0 or None [∵ and has higher precedence than or]


(a) 0 or None and "or"

⇒ None

⇒ 1 or None or 'b'
(b) 1 or None and 'a' or 'b'

⇒ 1 or 'b'
⇒1

⇒ False
(c) False and 23

⇒ False
(d) 23 and False

⇒ not (True and True)


(e) not (1 == 1 and 0 != 1)

⇒ not True
⇒ False

⇒ "abc" == "Abc" and not (False or False)


(f) "abc" == "Abc" and not (2 == 3 or 3 == 4)

⇒ "abc" == "Abc" and not False


⇒ False and not False
⇒ False and True
⇒ False

⇒ False and True or not True or True and False or True


(g) False and 1 == 1 or not True or 1 == 1 and False or 0 == 0

⇒ False and True or False or True and False or True


⇒ False or False or False or True
⇒ False or False or True
⇒ False or True
⇒ True

Question 29

Evaluate the following for each expression that is successfully evaluated, determine its value and
type for unsuccessful expression, state the reason.

(a) len("hello") == 25/5 or 20/10

(b) 3 < 5 or 50/(5 - (3 + 2))

(c) 50/(5 - (3 + 2)) or 3 < 5

(d) 2 * (2 * (len("01")))

Answer

⇒ 5 == 25/5 or 20/10
(a) len("hello") == 25/5 or 20/10

⇒ 5 == 5 or 2
⇒ True or 2
⇒ True

The type of result is Boolean.

⇒ True or 50/(5 - (3 + 2)) [∵ first operand is True, second operand is not evaluated so no division by
(b) 3 < 5 or 50/(5 - (3 + 2))

⇒ True
0 error happens]

The type of result is Boolean.

⇒ 50/(5 - 5) or 3 < 5
(c) 50/(5 - (3 + 2)) or 3 < 5

⇒ 50/0 or 3 < 5
⇒ Division by Zero Error

As the denominator of or operator's first operand is 0, Division by Zero error occurs.

⇒ 2 * (2 * 2)
(d) 2 * (2 * (len("01")))

⇒2*4
⇒8

The type of result is Integer.


Question 30

Write an expression that uses exactly 3 arithmetic operators with integer literals and produces result
as 99.

Answer

9 * 10 + 21 % 12

Question 31

Add parentheses to the following expression to make the order of evaluation more clear.

y % 4 == 0 and y % 100 != 0 or y % 400 == 0

Answer

((y % 4) == 0) and ((y % 100) != 0) or ((y % 400) == 0)

Question 32

A program runs to completion but gives an incorrect result. What type of error would have caused it?

Answer

Logical errors can make a program run till completion but give incorrect result.

Question 33

In Python, strings are immutable while lists are mutable. What is the difference?

Answer

In Python, strings are immutable means that individual letter assignment for strings is not allowed.
For example:

name='hello'

name[0] = 'p'

The above Python code will cause an error as we are trying to assign some value to an individual
letter of a string.

Lists are mutable in Python means that we can assign values to individual elements of a list. For
example:

a = [1, 2, 3, 4, 5]

a[0] = 10

The above Python code will work correctly without any errors as Lists are mutable in Python.

Question 34

How does the // operator differ from the / operator? Give an example of where // would be needed.

Answer

The Division operator (/) divides its first operand by second operand and always returns the result as
a float value whereas Floor Division operator (//) divides its first operand by second operand and
truncates the fractional part of the result and returns it as an int value. Floor Division operator is
useful in situations where we only need the integer part of the division operation result. For
example, to determine how many minutes are there in some given number of seconds:

secs = 2565

mins = 2565 // 60

Question 35

MidAir Airlines will only allow carry-on bags that are no more than 22 inches long, 14 inches wide,
and 9 inches deep. Assuming that variables named length, width, and depth have already been
assigned values, write an expression combining the three that evaluates to True if bag fits within
those limits, and False otherwise.

Answer

length <= 22 and width <= 14 and depth <= 9

Question 36

What are main error types? Which types are most dangerous and why?

Answer

The types of errors are:

1. Compile Time Errors (Syntax errors and Semantic Errors)

2. Runtime Errors

3. Logical Errors

Logical Errors are the most dangerous as they are hardest to prevent, find and fix.

Question 37

Correct any false statements:

(a) Compile-time errors are usually easier to detect and to correct than run-time errors.

(b) Logically errors can usually be detected by the compiler.

Answer

(a) The statement is correct.

(b) The statement is incorrect. The correct statement should be:

Logical errors cannot be detected by the compiler.

Question 38

Differentiate between a syntax error and a semantics error.

Answer
Syntax Error Semantics Error

Syntax errors occurs when the rules of the programming language are Semantic errors occur when the stateme
violated. meaningful.

Example: Example:
x = false x*y=z

Question 39

Differentiate between a syntax error and a logical error in a python program. When is each type of
error likely to be found?

Answer

Syntax Error Logical Error

Syntax Errors occur when we violate the rules of writing the Logical Errors occur due to our mistakes
statements of the programming language. logic.

Program compiles and executes but doe


Program fails to compile and execute.
desired output.

Logical errors need to be found and corr


Syntax Errors are caught by the compiler.
working on the program.

Syntax errors are found at compile type whereas Logical errors are found when the program starts
executing.

Question 40

What is the difference between an error and exception?

Answer

An Error is a bug in the code that causes irregular output or stops the program from executing
whereas an Exception is an irregular unexpected situation occurring during execution on which
programmer has no control.

Type B: Application Based Questions

Discover more

Book

Practice tests platform

Doubt resolution service

Question 1

What is the result produced by (i) bool (0) (ii) bool (str(0))? Justify the outcome.
Answer

(i) bool (0)

The result is False as truth value of 0 is falsetval

(ii) bool (str(0))

The result is True as str(0) converts 0 to string "0". As it becomes a non-empty string so its truth
value is truetval

You might also like