Objective Type Questions
Question 1
Which statement/function will you use to display something on screen ?
- Display
- display
Answer
Reason — The print statement is used to display something on screen.
Question 2
Which statement/function will you use to get value for a variable from keyboard ?
- get
- cin
- raw_input
- raw_in
Answer
raw_input
Reason — The raw_input function is used to get value for a variable from keyboard.
Question 3
Which of these is not a Python data type ?
- int
- long
- float
- NUMBER
Answer
NUMBER
Reason — NUMBER is not a Python data type. Numbers are stored using int, float and long data types.
Question 4
In the following code a = '5'
. a is a ............... :
- int
- string
- float
- number
Answer
string
Reason — A string literal is always enclosed in single quotes or double quotes.
Question 5
What is the output of following code ?
print 8 >= 8
- 8 >= 8
- False
- True
- Error
Answer
True
Reason — a >= b
compares a and b and returns true if a is greater than or equal to b, else returns false. Since 8 is equal to 8, the given expression results in 'true'.
Question 6
What is the output of following code ?
print "8 >= 8"
- 8 >= 8
- False
- True
- Error
Answer
8 >= 8
Reason — Since 8 >= 8
is enclosed in quotes, it is treated as a String and printed on the output terminal as it is.
Question 7
What will be the output of following code ?
print(8 >= 8)
- 8 >= 8
- False
- True
- Error
Answer
True
Reason — The given expression (8 >= 8)
results in true as 8 is equal to 8. Thus, 'true' is printed on the output terminal.
Question 8
What will be the output of
print 3*2**2
- 81
- 18
- 36
- 12
Answer
12
Reason — The given expression 3*2**2
is evaluated as follows:
= 3 * 2 ** 2
= 3 * 4 (Since exponentiation (**) has higher precedence than multiplication (*))
= 12
Thus, 12 is printed as output.
Question 9
What will be the output of
print (3*2)**2
- 81
- 18
- 36
- 12
Answer
36
Reason — The given expression (3*2)**2
is evaluated as follows:
= (3 * 2) ** 2
= 6 ** 2 (Since parentheses has higher precedence than exponentiation (**))
= 36
Thus, 36 is printed as output.
Question 10
Which of the following has the highest precedence in an expression ?
- Exponentiation (**)
- Multiplication (*)
- Division (/)
- Parenthesis (())
Answer
Parenthesis (())
Reason — Parenthesis (()) has the highest precedence in an expression.
Theoretical Questions
Question 1
What are tokens in Python ? How many types of tokens are allowed in Python ? Examplify your answer.
Answer
The smallest individual unit in a program is known as a Token. Python has following tokens:
- Keywords — Examples are import, for, in, while, etc.
- Identifiers — Examples are MyFile, DS, DATE9777, etc.
- Literals — Examples are "abc", 5, 28.5, etc.
- Operators — Examples are +, -, >, or, etc.
- Punctuators — ' " # () etc.
Question 2
How are keywords different from identifiers?
Answer
Keywords are reserved words carrying special meaning and purpose to the language compiler/interpreter. For example, if, elif, etc. are keywords. Identifiers are user defined names for different parts of the program like variables, objects, classes, functions, etc. Identifiers are not reserved. They can have letters, digits and underscore. They must begin with either a letter or underscore. For example, _chk, chess, trail, etc.
Question 3
What are literals in Python ? How many types of literals are allowed in Python ?
Answer
Literals are data items that have a fixed value. The different types of literals allowed in Python are:
- String literals
- Numeric literals
- Boolean literals
- Special literal None
- Literal collections
Question 4
What are operators ? What is their function ? Give examples of some unary and binary operators.
Answer
Operators are tokens that trigger some computation/action when applied to variables and other objects in an expression.
Unary plus (+), Unary minus (-), Bitwise complement (~), Logical negation (not) are a few examples of unary operators.
Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/).
Question 5
What all components can a Python program contain ?
Answer
A Python program can contain various components like expressions, statements, comments, functions, blocks and indentation.
Question 6
What are data types ? How are they important ?
Answer
Data types are used to identify the type of data a memory location can hold and the associated operations of handling it.
The data that we deal with in our programs can be of many types like character, integer, real number, string, boolean, etc. hence programming languages including Python provide ways and facilities to handle all these different types of data through data types. The data types define the capabilities to handle a specific type of data such as memory space it allocates to hold a certain type of data and the range of values supported for a given data type, etc.
Question 7
How many integer types are supported by Python ? Name them.
Answer
Two integer types are supported by Python. They are:
- Integers (signed)
- Booleans
Question 8
Write a program to input a number n and print n, n2, n3.
Answer
Solution
n = int(input("Enter n: "))
n2, n3 = n ** 2, n ** 3
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
Output
Enter n: 2
n = 2
n^2 = 4
n^3 = 8
Question 9
What is meant by token ? Name the tokens available in Python.
Answer
The smallest individual unit in a program is known as a Token. Python has following tokens:
- Keywords — Examples are import, for, in, while, etc.
- Identifiers — Examples are MyFile, DS, DATE9777, etc.
- Literals — Examples are "abc", 5, 28.5, etc.
- Operators — Examples are +, -, >, or, etc.
- Punctuators — ' " # () etc.
Question 10
What are keywords ? Can keywords be used as identifiers ?
Answer
Keywords are words having special meaning reserved by programming language.
No, keywords cannot be used as identifiers.
Question 11
What is an identifier ? What are the identifier forming rules of Python ?
Answer
Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program viz variables, objects, classes, functions, lists, dictionaries etc. For example, Myfile, _DS, _name_01, H34CAP etc.
The identifier forming rules of Python are:
- An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
- Python does not allow punctuation characters such as @, $, and % within identifiers.
- Reserved words (keywords) must not be used as identifiers.
Question 12
What are literals ? How many types of literals are available in Python ?
Answer
Literals are data items that have a fixed value. The different types of literals allowed in Python are:
- String literals
- Numeric literals
- Boolean literals
- Special literal None
- Literal collections
Question 13
How many types of integer literals are allowed in Python ? How are they written ?
Answer
The types of integer literals allowed in Python are as follows:
- Decimal form — Integers written in decimal form do not begin with a zero and contain only 0 to 9 digits.
- Octal form — Integers written in octal form begin with a 0 and contain only 0 to 7 digits.
- Hexadecimal form — Integers written in hexadecimal form begin with a 0x and contain only 0 to 9 digits and A to F or a to f letters.
Question 14
What kind of literals are these : 011, 0X2A, 17, 014, 0XBC1 ?
Answer
011 | Octal Literal |
0X2A | Hexadecimal Literal |
17 | Decimal Literal |
014 | Octal Literal |
0XBC1 | Hexadecimal Literal |
Question 15
How are nongraphic characters represented in Python ?
Answer
Python allows us to have certain non-graphic characters in String values. These non-graphic characters can be represented by using escape sequences — represented by a backslash ( \ ) followed by one or more characters.
Some examples of non-graphic characters and their escape sequences are as follows:
Escape Sequence | Non-Graphic Character |
---|---|
\a | ASCII Bell (BEL) |
\n | New line character |
\t | Horizontal tab |
Question 16
What are string-literals in Python ? How many ways, can you create String literals in Python ?
Answer
We can form string literals by enclosing text in both forms of quotes — single quotes or double quotes. For example, 'Astha', "Rizwan", 'Hello World', "112FB0291", etc.
Python allows us to have two string types :
- Single line Strings — These strings terminate in single line. For example, "I am a single line string" , "Hello World!"
- Multiline Strings — These strings spread across multiple lines. These strings are created in following two ways :
- by adding a backslash at the end of physical line, for example,
multiline_string_variable = "Hello \
World !" - by enclosing the multiple lines of string in triple quotes, for example,
Str1 = ''' Hello
World.
There I Come ! ! !
Cheers.
'''
- by adding a backslash at the end of physical line, for example,
Question 17
What is meant by a floating-point literal in Python ? How many ways can a floating literal be represented into ?
Answer
Floating point literals represent real numbers and are written with a decimal point dividing the integer and fractional parts.
Floating literals are represented in Python in two forms —
- Fractional Form — 2.0, 17.5, -13.0, -0.00625
- Exponent form — 152E05, 1.52E07, 0.152E08, -0.172E-3
Question 18
What are the two Boolean literals in Python ?
Answer
The two Boolean literals in Python are true and false.
Question 19
What kind of program elements are these: 'a', 4.38925, "a", main( ) ?
Answer
'a' | String Literal |
4.38925 | Floating point Literal |
"a" | String Literal |
main( ) | Function |
Application Oriented Questions
Question 1
Aashna has written following code :
a = 5
b = '5'
c = "5"
d = 5.0
(a) What are the data types of a, b, c, d ?
(b) Are a, b, c, d storing exactly the same values ? Why/why not ?
(c) What will be the output of following code based on values of a, b, c and d ?
print a == b
print b == c
print a == d
(d) Discuss the reasons behind the output of part (c).
Answer
(a) The data types are as follows:
a | int |
b | String |
c | String |
d | float |
(b) No, a, b, c, and d are not storing exactly the same values. While the values of b and c look like integers (numerical 5), they are enclosed within quotes, making them strings. On the other hand, a is an integer number, and d is a floating-point number with a decimal point.
So, although they all represent the number 5, they are stored as different data types.
(c) The output of the following code will be :
False
True
True
(d) The reasons behind the output of part c are as follows:
1. print a == b
This comparison checks if the value of a is equal to the value of b. Even though the values of a and b are both 5, they are not of the same data type (int vs. str). Python strictly checks for both value and type equality in this case, so the comparison evaluates to False.
2. print b == c
This comparison checks if the value of b is equal to the value of c. Since both b and c are strings with the same content ("5"), the comparison evaluates to True.
3. print a == d
This comparison checks if the value of a is equal to the value of d. Although they have different data types (int vs. float), their numerical values are the same (both represent 5). Python performs type coercion in this case and considers them equal, so the comparison evaluates to True.
Question 2
Anant has written following expressions in Python 2.7 :
a = - 4 % 1.5
b = - 4 // 1.5
c = 4.0 / 5
d = 4 / 5
(a) Find out the values stored by a, b, c and d.
(b) What are the datatypes of a, b, c and d ?
(c) Why are the values of c and d not the same.
Answer
(a) The values stored in a, b, c, d are as follows:
Variable | Value | Reason |
---|---|---|
a | -0.5 | Remainder of -4 divided by 1.5 is -0.5 |
b | -3.0 | Floor division of -4 by 1.5 is -3.0 as floor division divides and truncates the fractional part from the result. |
c | 0.8 | Division of 4.0 by 5 is 0.8, which is a floating-point value |
d | 0 | Division of 4 by 5 is 0, and the fractional part is truncated |
(b) The data types of a, b, c and d are as follows:
Variables | Datatypes |
---|---|
a | float |
b | float |
c | float |
d | int |
(c) The values of 'c' and 'd' are not the same because of the difference in the division operation performed in each case:
'c' is the result of dividing two floating-point numbers, which preserves the decimal point and gives a floating-point result. 'd' is the result of dividing two integers, which truncates the fractional part and gives an integer result.