0% found this document useful (0 votes)
84 views

Pythonfundamental typeBQ

Uploaded by

deepsun515
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)
84 views

Pythonfundamental typeBQ

Uploaded by

deepsun515
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
You are on page 1/ 22

Type B : Application Bases questions

Question 1

From the following, find out which assignment statement will produce
an error. State reason(s) too.

(a) x = 55
(b) y = 037
(c) z = 0o98
(d) 56thnumber = 3300
(e) length = 450.17
(f) !Taylor = 'Instant'
(g) this variable = 87.E02
(h) float = .17E - 03
(i) FLOAT = 0.17E - 03

Answer

1. y = 037 (option b) will give an error as decimal integer literal


cannot start with a 0.
2. z = 0o98 (option c) will give an error as 0o98 is an octal integer
literal due to the 0o prefix and 8 & 9 are invalid digits in an octal
number.
3. 56thnumber = 3300 (option d) will give an error as 56thnumber is
an invalid identifier because it starts with a digit.
4. !Taylor = 'Instant' (option f) will give an error as !Taylor is an
invalid identifier because it contains the special character !.
5. this variable = 87.E02 (option g) will give an error due to the space
present between this and variable. Identifiers cannot contain any
space.
6. float = .17E - 03 (option h) will give an error due to the spaces
present in exponent part (E - 03). A very important point to note
here is that float is NOT a KEYWORD in Python. The
statement float = .17E-03 will execute successfully without
any errors in Python.
7. FLOAT = 0.17E - 03 (option i) will give an error due to the spaces
present in exponent part (E - 03).

Question 2

How will Python evaluate the following expression ?

(i) 20 + 30 * 40

Answer

20 + 30 * 40
⇒ 20 + 1200
⇒ 1220

(ii) 20 - 30 + 40

Answer

20 - 30 + 40
⇒ -10 + 40
⇒ 30

(iii) (20 + 30) * 40


Answer

(20 + 30) * 40
⇒ 50 * 40
⇒ 2000

(iv) 15.0 / 4 + (8 + 3.0)

Answer

15.0 / 4 + (8 + 3.0)
⇒ 15.0 / 4 + 11.0
⇒ 3.75 + 11.0
⇒ 14.75

Question 3

Find out the error(s) in following code fragments:

(i)

temperature = 90
print temperature

Answer

The call to print function is missing parenthesis. The correct way to call
print function is this:
print(temperature)

(ii)

a = 30
b=a+b
print (a And b)
Answer

There are two errors in this code fragment:

8. In the statement b=a+b variable b is undefined.


9. In the statement print (a And b), And should be written as and.
(iii)

a, b, c = 2, 8, 9
print (a, b, c)
c, b, a = a, b, c
print (a ; b ; c)

Answer

In the statement print (a ; b ; c) use of semicolon will give error. In


place of semicolon, we must use comma like this print (a, b, c)

(iv)

X = 24
4 = X

Answer

The statement 4 = X is incorrect as 4 cannot be a Lvalue. It is a Rvalue.

(v)

print("X ="X)

Answer

There are two errors in this code fragment:

10. Variable X is undefined


11. "X =" and X should be separated by a comma like this
print("X =", X)
(vi)

else = 21 - 5

Answer

else is a keyword in Python so it can't be used as a variable name.

Question 4

What will be the output produced by following code fragment (s) ?

(i)

X = 10
X = X + 10
X = X - 5
print (X)
X, Y = X - 2, 22
print (X, Y)

Output

15
13 22

Explanation

12. X = 10 ⇒ assigns an initial value of 10 to X.


13. X = X + 10 ⇒ X = 10 + 10 = 20. So value of X is now 20.
14. X = X - 5 ⇒ X = 20 - 5 = 15. X is now 15.
15. print (X) ⇒ print the value of X which is 15.
16. X, Y = X - 2, 22 ⇒ X, Y = 13, 22.
17. print (X, Y) ⇒ prints the value of X which is 13 and Y which
is 22.
(ii)
first = 2
second = 3
third = first * second
print (first, second, third)
first = first + second + third
third = second * first
print (first, second, third)

Output

2 3 6
11 3 33

Explanation

18. first = 2 ⇒ assigns an initial value of 2 to first.


19. second = 3 ⇒ assigns an initial value of 3 to second.
20. third = first * second ⇒ third = 2 * 3 = 6. So variable third is
initialized with a value of 6.
21. print (first, second, third) ⇒ prints the value of first,
second, third as 2, 3 and 6 respectively.
22. first = first + second + third ⇒ first = 2 + 3 + 6 = 11
23. third = second * first ⇒ third = 3 * 11 = 33
24. print (first, second, third) ⇒ prints the value of first,
second, third as 11, 3 and 33 respectively.
(iii)

side = int(input('side') ) #side given as 7


area = side * side
print (side, area)

Output

side7
7 49
Explanation

25. side = int(input('side') ) ⇒ This statements asks the user


to enter the side. We enter 7 as the value of side.
26. area = side * side ⇒ area = 7 * 7 = 49.
27. print (side, area) ⇒ prints the value of side and area as 7
and 49 respectively.

Question 5

What is the problem with the following code fragments ?

(i)

a = 3
print(a)
b = 4
print(b)
s = a + b
print(s)

Answer

The problem with the above code is inconsistent indentation. The


statements print(a), print(b), print(s) are indented but they are not
inside a suite. In Python, we cannot indent a statement unless it is
inside a suite and we can indent only as much is required.

(ii)

name = "Prejith"
age = 26
print ("Your name & age are ", name + age)

Answer
In the print statement we are trying to add name which is a string to
age which is an integer. This is an invalid operation in Python.

(iii)

a = 3
s = a + 10
a = "New"
q = a / 10

Answer

The statement a = "New" converts a to string type from numeric type


due to dynamic typing. The statement q = a / 10 is trying to divide a
string with a number which is an invalid operation in Python.

Question 6

Predict the output:

(a)

x = 40
y = x + 1
x = 20, y + x
print (x, y)

Output

(20, 81) 41

Explanation

28. x = 40 ⇒ assigns an initial value of 40 to x.


29. y = x + 1 ⇒ y = 40 + 1 = 41. So y becomes 41.
30. x = 20, y + x ⇒ x = 20, 41 + 40 ⇒ x = 20, 81. This makes x a
Tuple of 2 elements (20, 81).
31. print (x, y) ⇒ prints the tuple x and the integer variable y
as (20, 81) and 41 respectively.
(b)

x, y = 20, 60
y, x, y = x, y - 10, x + 10
print (x, y)

Output

50 30

Explanation

32. x, y = 20, 60 ⇒ assigns an initial value of 20 to x and 60 to y.


33. y, x, y = x, y - 10, x + 10
⇒ y, x, y = 20, 60 - 10, 20 + 10
⇒ y, x, y = 20, 50, 30
First RHS value 20 is assigned to first LHS variable y. After that
second RHS value 50 is assigned to second LHS variable x. Finally
third RHS value 30 is assigned to third LHS variable which is again
y. After this assignment, x becomes 50 and y becomes 30.
34. print (x, y) ⇒ prints the value of x and y as 50 and 30
respectively.
(c)

a, b = 12, 13
c, b = a*2, a/2
print (a, b, c)

Output
12 6.0 24

Explanation

35. a, b = 12, 13 ⇒ assigns an initial value of 12 to a and 13 to


b.
36. c, b = a*2, a/2 ⇒ c, b = 12*2, 12/2 ⇒ c, b = 24, 6.0. So c has a
value of 24 and b has a value of 6.0.
37. print (a, b, c) ⇒ prints the value of a, b, c as 12, 6.0 and 24
respectively.
(d)

a, b = 12, 13
print (print(a + b))

Output

25
None

Explanation

38. a, b = 12, 13 ⇒ assigns an initial value of 12 to a and 13 to


b.
39. print (print(a + b)) ⇒ First print(a + b) function is called
which prints 25. After that, the outer print statement prints the
value returned by print(a + b) function call. As print function does
not return any value so outer print function prints None.

Question 7

Predict the output


a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)

Output

a, b, c : 10 20 30p, q, r : 25 13 16

Explanation

40. a, b, c = 10, 20, 30 ⇒ assigns initial value of 10 to a, 20 to b


and 30 to c.
41. p, q, r = c - 5, a + 3, b - 4
⇒ p, q, r = 30 - 5, 10 + 3, 20 - 4
⇒ p, q, r = 25, 13, 16.
So p is 25, q is 13 and r is 16.
42. print ('a, b, c :', a, b, c, end = '') ⇒ This statement
prints a, b, c : 10 20 30. As we have given end = '' so output of
next print statement will start in the same line.
43. print ('p, q, r :', p, q, r) ⇒ This statement prints p, q, r
: 25 13 16

Question 8

Find the errors in following code fragment

(a)

y = x + 5
print (x, Y)

Answer

There are two errors in this code fragment:


44. x is undefined in the statement y = x + 5
45. Y is undefined in the statement print (x, Y). As Python is
case-sensitive hence y and Y will be treated as two different
variables.
(b)

print (x = y = 5)

Answer

Python doesn't allow assignment of variables while they are getting


printed.

(c)

a = input("value")
b = a/2
print (a, b)

Answer

The input( ) function always returns a value of String type so variable a


is a string. This statement b = a/2 is trying to divide a string with an
integer which is invalid operation in Python.

Question 9

Find the errors in following code fragment : (The input entered is XI)

c = int (input ( "Enter your class") )


print ("Your class is", c)

Answer

The input value XI is not int type compatible.


Question 10

Consider the following code :

name = input ("What is your name?")


print ('Hi', name, ',')
print ("How are you doing?")

was intended to print output as

Hi <name>, How are you doing ?

But it is printing the output as :

Hi <name>,
How are you doing?

What could be the problem ? Can you suggest the solution for the same
?

Answer

The print() function appends a newline character at the end of the line
unless we give our own end argument. Due to this behaviour of print()
function, the statement print ('Hi', name, ',1) is printing a newline at
the end. Hence "How are you doing?" is getting printed on the next line.
To fix this we can add the end argument to the first print() function like
this:

print ('Hi', name, ',1, end = '')

Question 11

Find the errors in following code fragment :


c = input( "Enter your class" )
print ("Last year you were in class") c - 1

Answer

There are two errors in this code fragment:

46. c - 1 is outside the parenthesis of print function. It should


be specified as one of the arguments of print function.
47. c is a string as input function returns a string. With c - 1, we
are trying to subtract a integer from a string which is an invalid
operation in Python.
The corrected program is like this:

c = int(input( "Enter your class" ))


print ("Last year you were in class", c - 1)

Question 12

What will be returned by Python as result of following statements?

(a) >>> type(0)

Answer

<class 'int'>

(b) >>> type(int(0))

Answer

<class 'int'>

(c) >>>.type(int('0')

Answer
SyntaxError: invalid syntax

(d) >>> type('0')

Answer

<class 'str'>

(e) >>> type(1.0)

Answer

<class 'float'>

(f) >>> type(int(1.0))

Answer

<class 'int'>

(g) >>>type(float(0))

Answer

<class 'float'>

(h) >>> type(float(1.0))

Answer

<class 'float'>

(i) >>> type( 3/2)

Answer

<class 'float'>
Question 13

What will be the output produced by following code ?

(a) >>> str(print())+"One"

Output

NoneOne

Explanation

print() function doesn't return any value so its return value is None.
Hence, str(print()) becomes str(None). str(None) converts None into
string 'None' and addition operator joins 'None' and 'One' to give the
final output as 'NoneOne'.

(b) >>> str(print("hello"))+"One"

Output

hello
NoneOne

Explanation

First, print("hello") function is executed which prints the first line of the
output as hello. The return value of print() function is None i.e. nothing.
str() function converts it into string and addition operator joins 'None'
and 'One' to give the second line of the output as 'NoneOne'.

(c) >>> print(print("Hola"))

Output
Hola
None

Explanation

First, print("Hola") function is executed which prints the first line of the
output as Hola. The return value of print() function is None i.e. nothing.
This is passed as argument to the outer print function which converts it
into string and prints the second line of output as None.

(d) >>> print (print ("Hola", end = " "))

Output

Hola None

Explanation

First, print ("Hola", end = " ") function is executed which prints Hola. As
end argument is specified as " " so newline is not printed after Hola.
The next output starts from the same line. The return value of print()
function is None i.e. nothing. This is passed as argument to the outer
print function which converts it into string and prints None in the same
line after Hola.

Question 14

Carefully look at the following code and its execution on Python shell.
Why is the last assignment giving error ?

>>> a = 0o12
>>> print(a)
10
>>> b = 0o13
>>> c = 0o78
File "<python-input-41-27fbe2fd265f>", line 1
c = 0o78
^
SyntaxError : invalid syntax

Answer

Due to the prefix 0o, the number is treated as an octal number by


Python but digit 8 is invalid in Octal number system hence we are
getting this error.

Question 15

Predict the output

a, b, c = 2, 3, 4
a, b, c = a*a, a*b, a*c
print(a, b, c)

Output

4 6 8

Explanation

48. a, b, c = 2, 3, 4 ⇒ assigns initial value of 2 to a, 3 to b and


4 to c.
49. a, b, c = a*a, a*b, a*c
⇒ a, b, c = 2*2, 2*3, 2*4
⇒ a, b, c = 4, 6, 8
50. print(a, b, c) ⇒ prints values of a, b, c as 4, 6 and 8
respectively.
Question 16

The id( ) can be used to get the memory address of a variable. Consider
the adjacent code and tell if the id( ) functions will return the same
value or not(as the value to be printed via print() ) ? Why ?
[There are four print() function statements that are printing id of
variable num in the code shown on the right.

num = 13
print( id(num) )
num = num + 3
print( id(num) )
num = num - 3
print( id(num) )
num = "Hello"
print( id(num) )

Answer

num = 13
print( id(num) ) # print 1
num = num + 3
print( id(num) ) # print 2
num = num - 3
print( id(num) ) # print 3
num = "Hello"
print( id(num) ) # print 4

For the print statements commented as print 1 and print 3 above, the
id() function will return the same value. For print 2 and print 4, the
value returned by id() function will be different.

The reason is that for both print 1 and print 3 statements the value of
num is the same which is 13. So id(num) gives the address of the
memory location which contains 13 in the front-loaded dataspace.
Question 17

Consider below given two sets of codes, which are nearly identical,
along with their execution in Python shell. Notice that first code-
fragment after taking input gives error, while second code-fragment
does not produce error. Can you tell why ?

(a)

>>> print(num = float(input("value1:")) )


value1:67

TypeError: 'num' is an invalid keyword argument for this function

(b)

>>> print(float(input("valuel:")) )
value1:67

67.0

Answer

In part a, the value entered by the user is converted to a float type and
passed to the print function by assigning it to a variable named num. It
means that we are passing an argument named num to the print
function. But print function doesn't accept any argument named num.
Hence, we get this error telling us that num is an invalid argument for
print function.

In part b, we are converting the value entered by the user to a float type
and directly passing it to the print function. Hence, it works correctly
and the value gets printed.
Question 18

Predict the output of the following code :

days = int (input ("Input days : ")) * 3600 * 24


hours = int(input("Input hours: ")) * 3600
minutes = int(input("Input minutes: ")) * 60
seconds = int(input("Input seconds: "))
time = days + hours + minutes + seconds
print("Total number of seconds", time)

If the input given is in this order : 1, 2, 3, 4

Output

Input days : 1
Input hours: 2
Input minutes: 3
Input seconds: 4
Total number of seconds 93784

Question 19

What will the following code result into ?

n1, n2 = 5, 7
n3 = n1 + n2
n4 = n4 + 2
print(n1, n2, n3, n4)

Answer

The code will result into an error as in the statement n4 = n4 + 2,


variable n4 is undefined.
Question 20

Correct the following program so that it displays 33 when 30 is input.

val = input("Enter a value")


nval = val + 30
print(nval)

Answer

Below is the corrected program:

val = int(input("Enter a value")) #used int() to convert input


value into integer
nval = val + 3 #changed 30 to 3
print(nval)

You might also like