0% found this document useful (0 votes)
128 views13 pages

Descriptive Questions

The document provides definitions and examples related to computational thinking and Python programming. It defines computational thinking as formulating problems and solutions in a way that allows computers to solve them. It then lists four features of the Python programming language including being free/open-source, cross-platform, having easier syntax than other languages, and being able to create desktop and web apps. The document also provides sample code and evaluates logical expressions to demonstrate Python concepts.

Uploaded by

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

Descriptive Questions

The document provides definitions and examples related to computational thinking and Python programming. It defines computational thinking as formulating problems and solutions in a way that allows computers to solve them. It then lists four features of the Python programming language including being free/open-source, cross-platform, having easier syntax than other languages, and being able to create desktop and web apps. The document also provides sample code and evaluates logical expressions to demonstrate Python concepts.

Uploaded by

Rohain Koul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Descriptive Questions:

1. Define the term Computational Thinking.


Computational thinking is the thought process involved in
formulating a problem and expressing its solution(s) in such a
way that a computer—human or machine—can effectively carry out.

2. Write any four features of Python.


一 Four features of Python:
一 (i) Python is free to use, even for commercial products,
because of its OSI-approved open source license.
一 (ii) Python is a cross-platform language. It runs on
Windows, Linux/Unix, Mac OS X, and has been ported to the Java
and .NET virtual machines.
一 (iii) Python’s syntax is easier to learn than most of the
other programming languages.
一 (iv) Python can be used to create desktop applications and
web applications.

3. Write any four application areas of Python.


一 Four application areas of Python:
一 (i) Web and Internet development
一 (ii) Database access
一 (iii) Desktop GUIs
一 (iv) Data Sciences

4. What is a text editor? Name any two text editors.


A text editor is an application software used to create and
manage text files. Two text editors are Notepad and Notepad++.

5. What is an interpreter?
一 An interpreter is a software which converts a high-level
language program into machine language and gets it executed line
by line.

6. What is IDLE? What are the two modes of working with IDLE?
Ans:
IDLE is the default IDE of Python. It stands for Integrated
Development and Learning Environment. Two modes of working with
IDLE are: Interactive mode and Script mode.
7. What is print() in Python?
print() is a function to display the specified content on
screen.
8. Name the keyword arguments of print() function.

The keyword arguments of print() are: end, sep.


9. What is a data type? What are the fundamental data types in
Python?

Every value belongs to a specific data type in Python. Data type


identifies the type of data which a variable can hold and the
operations that can be performed on those data.
Fundamental data types in Python are:

10. Identify the data types of the following data objects:

(i) 7 (ii) '7' (iii) "7" (iv) 7.0 (v) '''7''' (vi) -0.7 (vii) -
29 (viii) -15.38
(ix) "29 acres" (x) ". & #"

Ans: (i) int (ii) str (iii) str (iv) float (v) str (vi) float
(vii) int (viii) float (ix) str (x) str

11. Write Python statements to:


(i) Find the data type of number 12.
print(“Datatype of 12=”,type(12))
(ii) Find the data type of string ‘Hello’
print(“Datatype of hello=”,type(“hello”))
(iii) Find the data type of value represented by a variable
Hello.
print(“Datatype of hello=”,type(Hello))

(iv) Find the address of memory location represented by a


variable amount.
print(id(amount))
(v) Display the string ‘-*-‘ 25 times in a line.
print('-*-'*25)
12. What are unary operators and binary operators? Give two
examples of each.
Ans: An operator which takes one operand is called a unary
operator. Examples of unary operators are unary + and unary -.
An operator which takes two operands is called a binary
operator. Examples of binary operators are * and /.

13. What is the operator precedence of arithmetic operators in


Python?
Ans: Operator precedence of arithmetic operators in Python is as
follows:
(i) (**) > (unary +, unary -) > (*, /, //, %) > (binary +,
binary -)

14. Evaluate the following expressions:

(i) (2 + 3) ** 3 - 6 / 2 (ii) (2 + 3) * 5//4+(4 + 6)/ 2 (iii) 12


+ (3 * 4 – 6) / 3
(iv) 12 + (3 * *4 – 6)// 2 (v) 12 * 3 % 5 + 2 * 6//4 (vi) 12 % 5
*3 +(2*6)//4

Ans: (i) 122.0 (ii) 11.0 (iii) 14.0 (iv) 49 (v) 4 (vi) 9

15. What is a variable? What are the rules for forming valid
variable names in Python?
A variable is the name given to a data object stored in memory.
Rules for forming valid variable names in Python:
• A variable name must start with an alphabet or an underscore
(_).
• A variable name can consist of UNICODE alphabets, digits, and
underscore(_). No other character is allowed.
• A Python keyword cannot be used as a variable name.

16. Which function is used to know the address of the memory


location to which a variable is referring?
id()
17. Write Python statements to display a list of Python
keywords.
import keyword
keyword.kwlist

18. Why is Python called a dynamic type language?


Python is called a dynamic type language because in Python a
variable does not have any fixed data type and its data type
keeps changing depending upon the value it represents.

19. What is an augmented assignment operator? Give an example to


explain.
Ans: An augmented assignment evaluates the target (which, unlike
normal assignment statements, cannot be an unpacking) and the
expression list, performs the binary operation specific to the
type of assignment on the two operands, and assigns the result
to the original target. The target is only evaluated once.

20. Identify invalid variable names from the following, give


reason for each:
Group, if, int, total marks, S.I., volume, tot_strength, 9t,
tag$

Invalid variable name Reason


if if is a keyword, and a
keyword cannot be used as
a variable name.
total marks A variable name cannot
have a space.
S.I. A variable name cannot
have a dot (.)
9t A variable name cannot
start with a digit
tag$ A variable name cannot
have $

22. Write Python expressions to:


(a) represent the Integral part of the quotient when 63 is
divided by 29.
(b) represent the Remainder when 63 is divided by 29.
(c) represent the Fourth root of 3
(d) Add the remainder of 6/5 to the product of 6 and 5.
(e) represent Square root of the sum of 8 and 43.
(f) represent Sum of the square roots of 8 and 43.
(a) 63//29 (b) 63%29 (c) 3**(1/4)

(d) 6*5+6%5 (e) (8+4**3)* (f) 8**0.5+(4


*0.5 **3)
23. What are comments in a program? How are comments given in a
Python script? Give an example.
Ans: Comments are statements in a script which are ignored by
the Python interpreter, and therefore, have no effect on the
actual output of the code.
A comment in Python starts with a # anywhere in a line and
extends till the end of the line. The following code contains
two comments:
# This is a comment line
n=1
m=n+1 # value of n+1 is assigned to m
print(m)

24. Define syntax error, run time error, and logical error. Give
an example of each.
Syntax error: A syntax error is a grammatical error in a
script. Example: mismatching parentheses.

Run time error: A run time error is an error which occurs during
program execution due to incorrect or illegal values used in
some operations. Example: division by zero.
Logical errors: These are the errors in the logic of the script.
Example: incorrect sequence of statements in the script.

25. What is a token? What are different categories of tokens in


Python? Give two examples of tokens of each category.
A token is the smallest meaningful element of a Python
script. Different categories of tokens are:
Category Examples
Identifiers Print, sum
Keywords if, and
Literals 56, "abc"
Operators +, -
Delimiters (, )
/Seperator/Punctuator

26. Observe the following script and enlist all the tokens used
in it. For each token, write its category too:
#Identify tokens in the script
name="Arjun"
age=eval(input("Enter age: "))
print(name,"is",age,"years old")
Token Category Token Category

name Identifi , Delimite


er r
age Identifi ( Delimite
er r
eval Identifi ) Delimite
er r
input identifi "Arjun" Literal
er
print identifi "Enter Literal
er age: "
= Delimite "is" Literal
r /
Operator
" Delimite "years Literal
r old"

27. Evaluate the following logical conditions assuming that


a=10, b=20, c=’three’, d=’four’

(i) a>b/2 (ii) a<=b/2 (iii) a!=b (iv) b==a**2 (v) a+b > -b*-a
(vi) b*2 > ord("2")(vii) c==d (viii) c>d (ix) c<d (x) "c"<"d"
(xi) "c"==c (xii) d != chr(ord("d"))

Ans: (i) False (ii) True (iii) True (iv) False (v) False
(vi) False (vii) False (viii) True (ix) False (x) True (xi)
False (xii) True

28. What are relational operators? What relational operators are


available in Python?
Ans: Relational operators are the operators which are used to
compare two values. Different relational operators available in
Python are: ==, !=, >, >=, <, <= . Return type of relational
operator is always Boolean.

29. What are logical operators? What logical operators are


available in Python?
Ans:
Logical operators are the operators which are used to make
complex conditions by combining and/or negating simple
conditions. Different logical operators available in Python are:
not, and, or
31. Write Python expression for each of the following conditions
(assuming that num1, num2 are numbers):
(i) num1 is equal num2.
(ii) num1 is unequal to num2.
(iii) num1 is a factor of num2.
(iv) num1 is a multiple of num2.
(v) both num1 and num2 are even.
(vi) both num1 and num2 are odd.
(vii) one of these numbers (num1 and num2) is even and the other
is odd.
(viii) the sum of num1 and num2 is greater than the product of
num1 and num2.
(ix) num1 is a multiple of either 3 or 5 or both
(x) num1 is a multiple of 3 and 5.
Ans:
(i) num1==num2
(ii) num1 != num2
(iii) num2%num1==0
(iv) num1%num2==0

(v) num1%2==0 and num2%2==0


(vi) num1%2!=0 and num2%2!=0
(vii) num1%2==0 and num2%2!=0 or num21%2==0 and num1%2!=0
(num1%2==0 and num2%2!=0) or(num1%2!=0 and num2%2==0)
(viii) (num1+num2)>num1*num2
(ix) num1%3==0 or num1%5==0
(x) num1%3==0 and num1%5==0 (OR num1%15==0)

(OR (num1+num2)%2 != 0)

32. What is a loop? What are the looping constructs available in


Python?

Loop is repetition of a set of statements.

There are 2 looping constructs available in Python. These are:


while and for

33. What is the main difference between a for loop and a while
loop?
Ans:
for loop iterates over the elements of an iterable whereas a
while loop iterates while a specified condition is true.
34. When does the else clause of a loop get executed? Is else
clause mandatory in a loop?
Ans:
else clause of a loop gets executed after the normal completion
of the loop. else clause is not mandatory in a loop.
Example
for i in range(1,9):
if i%3==0:
print(i)
else:
print("program over")

35. What is the use of break and continue statements in a loop?


Give a suitable example to explain.
Ans: The break statement prematurely ends execution of the
current while or for loop. It brings the control to the
statement immediately following the current control structure,
skipping the optional "else" clause if the loop has one.

The continue statement is used to skip the execution of the


current iteration of a loop, and continue with the next.
continue does not terminate the loop, but continues with the
next iteration of the loop. Example:
#break and continue
for i in range(1,11):
if (i%3 == 0):
continue
if (i*3 > 25):
break
print(i,end=' ')
else:
print("Else part of loop")
print()
print(i)
36. What is an infinite loop? Give an example of an infinite
loop.
Ans: A loop which never terminates is called an infinite loop.
Example:
i=1
while i<5:
print(i)

37. What is meant by nesting of loops? Give a suitable example


to explain.
Ans:
Nesting of loops means creating a loop within the body of
another loop. The contained loop is called the nested loop or
inner loop and the container loop is called the outer loop.
Example::

38. What is an exception?


Ans:
An exception is a run-time error in a script. Few examples of
exceptions in Python are:
SyntaxError, NameError, ValueError, TypeError

39. By default, what happens when an exception occurs in a


script?
Ans:
On the occurrence of an error, the script abnormally terminates
by default.

40. What is meant by exception handling?

Ans: Exception handling refers to writing the code in such a way


that on the occurrence of an exception ,the code gives some
message to the user and/or adjusts itself so that the script
does not terminate abnormally.
41. Give any four examples of exceptions in Python. Also write
the situation when these exceptions occur.
Ans:
Exception Occurs when
TypeError an operation or function is
applied to an object of
inappropriate type.
ValueError a built-in operation or
function receives an
argument that has the right
type but an inappropriate
value.
ZeroDivisionError the second argument of a
division or modulo
operation (%) is zero.
NameError an undefined identifier is
referenced.

42. What mechanism for exception handling is provided in Python?


Ans:
Python provides try statement (with except, else, and finally
clauses) for exception handling.

43. Write a python script to demonstrate the handling of


NameError exception. Also mention when will NameError occur in
the script

Ans:

try:
n=int(input("Enter an integer to find its reciprocal: "))
print("reciprocal=",1/n)
except NameError: print("Variable not found")
NameError will occur in the script when the user enters a
variable name instead of an integer.

44. Write a python script to demonstrate the handling of


NameError and ZeroDivisionError exceptions. Also mention when
will these exceptions occur in the script.
Ans:
try:

n=int(input("Enter an integer to find its reciprocal: "))


print("reciprocal=",1/n)
except NameError: print("Variable not found")
except ZeroDivisionError: print("Reciprocal of 0 is not
defined")
NameError will occur when the user enters a variable name
instead of an integer. ZeroDivisionError will occur when
the user enters 0 as the value of n.

45. Write a python script to demonstrate the handling of


ZeroDivisionError and ValueError exceptions. Also mention when
will these exceptions occur in the script.
Ans:
try:

n=int(input("Enter an integer to find its reciprocal: "))


print("reciprocal=",1/n)
except ZeroDivisionError: print("Reciprocal of 0 is not
defined")
except ValueError: print("It was not an integer")
ZeroDivisionError will occur when the user enters 0 as the value
of n. ValueError will occur when the user enters a non-integer
as the value of n.

46. Write a python script to demonstrate the handling of


ValueError and TypeError exceptions. Also mention when will
these exceptions occur in the script.
Ans:
print("To find quotient and remainder")
try:
n1=eval(input("Enter dividend: "))
n2=eval(input("Enter divisor: "))
q=n1//n2
r=n1%n2
print("Quotinet =",q, ",Remainder =",r)
except ValueError: print("It was not an integer")
except TypeError: print("Incompatible values for floor
division and remainder.")

47. What is the use of finally clause in exception handling?


Demonstrate with the help of an example.
Ans:
finally clause of try statement is used to specify the code
which has to be executed in any case - irrespective of
whether an exception occurred or not. Example:

print("To find quotient and remainder")


err=False
try:
n1=eval(input("Enter dividend: "))
n2=eval(input("Enter divisor: "))
q=n1//n2
r=n1%n2
print("Quotinet =",q, ",Remainder =",r)
except:
err=True
finally:
if err:
print("Code encountered some error")
else: print("Code executed without any error")

48. What is the use of else clause in exception handling?


Demonstrate with the help of an example.
Ans:
In the else clause of try statement, we can specify the
code which has to be executed in case no exception occurs.
Example:

print("To find quotient and remainder")


try:
n1=eval(input("Enter dividend: "))
n2=eval(input("Enter divisor: "))
q=n1//n2
r=n1%n2
print("Quotinet =",q, ",Remainder =",r)
except:
print("Some error occurred")
else: print("Code executed without any error")

49. What is a bug with reference to programming? What is meant


by debugging the code?
Ans: A bug is an error in a script. Debugging is the process of
removing the errors from a script.

50. What is pdb? What is its use?

Ans: pdb (Python DeBugger) is a debugging tool provided by


Python. pdb is a module which is used to debug scripts by
tracing the scripts, inserting/removing breakpoints, inspecting
the values of variables etc.
***********************************************************

You might also like