PSPP Two Marks Faq-First-Two-Units-2022

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PSPP FAQ’s

PSPP TWO MARKS FAQ with ANSWER


1. What is an algorithm?
 An algorithm is a step by step method of solving a problem
 An algorithm is a procedure that consists of finite set of unambiguous instructions to solve the
problem, which takes some value(s) as input and produces some value(s) as output
 An algorithm to accept two number, computer the sum and print the result:
Step1: START
Step2: READ a, b
Step3: COMPUTE sum=a + b Step4: DISPLAY sum
Step5: STOP

2. Justify the reasons to divide programs into functions


 The following reason we go for User defined functions:
 supports code reusability
 makes easier to write, modify, understand, and debug your program
 reduce the length of the program
 supports modular programming

3. What is the difference between algorithm and program?


Algorithm Program
Algorithm is the general description of how to Program is a implementation of an algorithm
solve the problem. in specific programming language

An algorithm helps human to understand how Program helps Computer to understand how
to solve the problem in computer to solve the problem
Algorithm cannot be directly executed in the Program can be executed in the computer
computer
Algorithms can be written using flowchart and Programs are written using different
pseudo code programming language

4. Write an algorithm to find largest of three given numbers?


Answer:
Step 1: START
Step 2: READ a,b,c
Step 3: IF a>b and a>c THEN
PRINT “The largest is”,a
GOTO Step 6
END IF
Step 4: IF b>c THEN
PRINT “The largest is”,b
GOTO Step 6
END IF
Step 5: PRINT “The largest is”,c
Step 6: STOP
PANIMALAR ENGINEERING COLLEGE Page: - 1 -
PSPP FAQ’s

5. Whether following function definition is correct? If not correct then explain the reason:
def def show(a=10,b):
c=a*b print(c)
#main program
mul(10,20)
Answer:
The above function definition is not correct. The reason given below:
 Syntax Error: Non-default argument cannot appear after default-argument

6. Differentiate mutable and immutable objects/variables with example?


Answer:
Immutable object
 Immutable object cannot be modified, after its creation
 Example of immutable type includes:
o Number(int,float,complex)
o Boolean
o String
o tuple

mutable object
 Mutable object can be modified or altered, after its creation
 Example of mutable type includes:
o list
o set
o dictionary

7. What are the two mode python supports?


Answer:
1. Interactive mode
2. Script mode

8. What is output of the following python code: X=[1,2,3,4,5,6,7,8,9]


(i) print(X[-3:]) (ii) print(X[:-1]) (iii) print(X[2:6]) (iv) print(X[::-1])
Answer:
[7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8]
[3, 4, 5, 6]
[9, 8, 7, 6, 5, 4, 3, 2, 1]

9. What is the difference between break and continue?


Answer:
Break Continue
When break is encountered within the body of When continue is encountered with the body of
loop, it forces to terminate the loop, without loop, it forces it forces next iteration to take

PANIMALAR ENGINEERING COLLEGE Page: - 2 -


PSPP FAQ’s

executing rest of the iteration of loop place, without executing rest of the instructions in
the the body of loop
Example: Example:
for i in for i in range(1,10):
range(1,10): if i==5:
if i==5: continue
break print(i,end=”\t”)
print(i,end=”\t”) output:
output: 1 2 3 4 6 7 8 9
1 2 3 4

10. Differentiate global and local scope of a variable


Answer:
Break Continue
 Variables defined outside of all  Variables defined within the
function definitions are global function definitions are local
variable variable
 Global variables are accessible  Local variable are not accessible
within the function definition outside of the function definition
Also
 Life time of global variable  Local variables are created when
throughout the program, once it is function is called and destroyed
created when function call ends

11. What is the output of the following python code:


>>> def fun1():
pass
>>> result=fun1()
>>> print(result)

Answer:
None

Reason: All non-fruitful functions return special value called None to the calling program

12. Find the output of following python expression using operator precedence:
>>>100//5+2-2*min(10,20,5,3)**2
Answer:
4

What are keywords? Give examples


PANIMALAR ENGINEERING COLLEGE Page: - 3 -
PSPP FAQ’s

 Keywords are the reserved words in Python that have specific meanings and
purposes
 We cannot use a keyword as a variable name, function name or any other
identifier.
 In Python, keywords are case sensitive 

What are the rules and guidelines used for writing pseudo code?

i) Write only one statement per line: Each stmt in your pseudo code should express just one
action for the computer
ii) Capitalize initial keyword: The following are just a few keywords we will use in pseudo code:
READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL . In the following pseudo
code, READ and WRITE are in capital letters:
READ name, hourlyRate, hoursWorked, deductionPercent
iii) indent the statements to show hierarchy: This will improve the readability of pseudo code to
a great extent
 SEQUENCE: keep all statements in the same column
 SELECTION: indent the statements that fall inside the selection structure
 ITERATION: indent the statements that fall inside the loop
iv) End multiline structures: Multiline structures like control structures and looping structure
must end properly. For example IF structure should ends with END IF
Example:
• IF...END IF
• WHILE ... END WHILE
• FOR ... NEXT
v) Keep statement independent of programming language

13. Draw flow chart for printing first N natural numbers?

PANIMALAR ENGINEERING COLLEGE Page: - 4 -


PSPP FAQ’s

14. Write an algorithm to swap value of two variables without using third variable

Step1: START
Step2: READ x, y
Step3: SET x := x + y
Step4: SET y := x - y
Step5: SET x := x - y
Step6: PRINT “After swap x=”, x , “y=”, y
Step7: STOP

15. What is a module? Give example

• A module is a python file containing definitions of functions, classes, and variables and
executable statements, that can be reused from other programs
• The file name and the module name must be same, extension of file must be .py
• Grouping related code into a module makes the code easier to understand and reuse.
• It also makes the code logically organized
• Example:
>>>import math
>>>print(math.pow(2,5))
32

16. Why string data type is called immutable data type ? Justify your answer?
String is immutable, since once object of string is created, it value cannot be further modified.
Consider the following example:
>>> s="hello"
>>> print(s[0])
h
>>> s[0]='C'
TypeError: 'str' object does not support item assignment
>>>
The above statement will produce TypeError, string is immutable.

17. What is fruitful function? Give example


 The function that returns some result to the calling program is called fruitful
function.
 Fruitful function always ends with return expression
 Example:
def add(a,b): return
a+b
#main
PANIMALAR ENGINEERING COLLEGE Page: - 5 -
PSPP FAQ’s

result=add(10,20)
print(“10+20=”,result)
output: 10+20=30

18. Present the flow of execution for a while statement

19. Define recursion with an example


 A function call itself is referred to as recursion.
 Calling the function from within the body of same function for certain number of times is
referred as recursion
 Example Program: computing factorial of given number using recursive function
def factorial(n):
if n==1 or n==0:
return 1
else:
return n*factorial(n-1)
#main program
print("factorial(4)=",factorial(4))
output:
factorial(4)= 24

20. Relate strings and lists


 Both string and list are of sequence type.
 To access individual elements, we can use index operator on both stringand
list object
The difference between both are given below:
List String
 Object of Lists are mutable  Object of String are immutable
 Elements of list are enclosed within  Value for string objects are
square bracket, and separated by enclosed either double or single
Comma quotes
 Example: lst=[23,True,’test’]  Example: name=”Krishna”

PANIMALAR ENGINEERING COLLEGE Page: - 6 -


PSPP FAQ’s

21. How to create a list in python? Illustrate the use of negative indexing of list with an example
 List is one of the sequence data-type, which is used store more than one value
of different types
 Individual elements of list are enclosed in square bracket([]) and separated by
comma.
 An example of list object given below:
lst=[10,True, "python”,12.3]
 An internal representation of List lst is given below:
Negative Index: -4 -3 -2 -1
Lst 10 True Python 12.3
Positive indexing: 0 1 2 3
 Negative indexing are used to individual elements of List from last element to element to first
element:
>>> print(lst[-1])
12.3
>>> print(lst[-4])
10
22. Differentiate list and tuple?

List Tuple
 Lists are mutable sequence type  tuples are immutable sequence
type
 Created using square bracket([])  Created using parenthesis(())
 Example  Example
lst=[23,True,’test’] record=(23,True,’test’)
 Addition, deletion and insertion of  Addition, deletion and insertion of
element possible in list element not possible in tuple

PANIMALAR ENGINEERING COLLEGE Page: - 7 -


PSPP FAQ’s

23. Differentiate list and set?

List Set
 storing of duplicate elements is  Element in the set are unique,
possible in list duplicate elements are not allowed
 Created using square bracket([])  Created using curly bracket({})
 Example:  Example:
lst=[23,True,’test’] set1={23,True,’test’}
 Order of elements is well defined  The order of element is unknown
 List can contain any types of object  Can contain only hashable object
such as number, Boolean, string,
tuple, but not list object
 Object of list are indexable  Object of list are not indexeable

24. Write the syntax for concatenating two lists in python?


 Concatenation operator(+) – is used to concatenate two list in python.
Thesyntax is given below:
List-object3 = list-object1 + list-object2
 Example: 
>>> lst1 = [1,2,3]
>>>lst2 = [4,5]
>>>lst3 = lst1+lst2
>>>print(lst3)
1,2,3,4,5]

25. Compare algorithm, pseudo code, and flowchart?


Algorithm Flowchart Pseudo code
An algorithm is well- Flowchart is a Pseudo code is an informal
defined Systematic diagrammatic graphical high-level description of an
logical approach that representation of an algorithm that written in plain
describe step by step algorithm English
procedure to solve a
problem
Algorithm expressed in Drawn using various Written using natural
various way using natural symbols that are language and mathematical
language, program, connected using flow notation.
flowchart, pseudo code line. Easy to It is more close to
understand programming language

PANIMALAR ENGINEERING COLLEGE Page: - 8 -


PSPP FAQ’s

PART –B QUESTIONS
1. Steps involved in algorithmic problem solving techniques
2. Explain building blocks of an algorithm?
3. Explain the features of a Python.
4. Outline with an example the assignment operators
5. List various symbols used for drawing flowchar?
6. Explain various data types supported by Python?
7. Explain various operator supported by python?
8. What is the use of comment? What are the various way of writing comments in python
9. What is function? What is function proto types? Classify functions based on its prototypes?
10. Explain various parameter and argument types with suitable example
11. Explain various iterative statements supported by python
12. Explain various control statements supported by python
13. Explain various string handling methods supported by python?
14. Explain methods of List object in python?

IMPORTANT PROGRAMS
1. Write the python program to find the distance between two given points?

PROGRAM
import math
x1=int(input("enter x1? "))
y1=int(input("enter y1? "))
x2=int(input("enter x2? "))
y2=int(input("enter y2? "))
xdist=math.fabs( x2 - x1 )
ydist=math.fabs( y2 - y1)
dist =math.sqrt( xdist ** 2 + ydist ** 2 )
print("The distance is ",dist)

OUTPUT
enter x1? 10
enter y1? 20
enter x2? 10
enter y2? 40
The distance is 20.0

PANIMALAR ENGINEERING COLLEGE Page: - 9 -


PSPP FAQ’s

2. Write a Python program to exchange the value of two variables

ROGRAM (using temporary variable):


a=input("Enter value for a:")
b=input("Enter value for b:")
print("Before swapping:\na=",a,"\tb=",b)
temp=a
a=b
b=temp
print("After swapping:\na=",a,"\tb=",b)

PROGRAM (without using temporary variable):


a=input("Enter value for a:")
b=input("Enter value for b:")
print("Before swapping:\na=",a,"\tb=",b)
a,b=b,a
print("After swapping:\na=",a,"\tb=",b)

OUTPUT
Enter value for a:10
Enter value for b:20
Before swapping:
a= 10 b= 20
After swapping:
a= 20 b= 10

3. Write a Python program using function to find the sum of first ‘n’ even numbers and
print the result

PROGRAM:
n=eval(input("Enter the value for N:"))
total=0
for i in range(0,(n*2)+1):
total=total+i

print("sum of first",n," even numbers is",total)

OUTPUT:
Enter the value for N:5

PANIMALAR ENGINEERING COLLEGE Page: - 10 -


PSPP FAQ’s

sum of first 5 even numbers is 54

4. Write a Python program to find the factorial of a given number without recursion and
with recursion.
PROGRAM USING NON-RECURSION:
def factorial(x):
f=1
for i in range(2,x+1):
f=f*i
return f

#MAIN
n=eval(input("Enter the value for N:"))
print(n,"!=",factorial(n))

OUTPUT:
Enter the value for N:5
5 != 120

PROGRAM USING RECURSION:


def factorial(x):
if x==0 or x==1:
return 1
else:
return x*factorial(x-1)

#MAIN
n=eval(input("Enter the value for N:"))
print(n,"!=",factorial(n))

OUTPUT:
Enter the value for N:4
4 != 24

5. Write a Python program to generate first ‘N’ Fibonacci numbers


PROGRAM:
n=eval(input("Enter the value for N:"))
print("the first",n,"fibo series are:")
n1=-1
n2=1
for i in range(n):
n3=n1+n2
print(n3,end="\t")
n1=n2
n2=n3

PANIMALAR ENGINEERING COLLEGE Page: - 11 -


PSPP FAQ’s

OUTPUT:
Enter the value for N:5
the first 5 fibo series are:
0 1 1 2 3

6. Write a python program to find minimum and maximum value in the given array

PROGRAM

lst=eval(input('Enter array?\n'))
minimum=maximum=lst[0]
for x in lst:
if x < minimum:
minimum=x
if x > maximum:
maximum=x
print('The minimum value is',minimum)
print('The maximum value is',maximum)

OUTPUT
Enter array?
[10,1,-3,4,100,45]
The minimum value is -3
The maximum value is 100

7. Write a python program to print all prime numbers between two given numbers

PROGRAM:
n1=eval(input("Enter the value for N1:"))
n2=eval(input("Enter the value for N2:"))
print("All prime numbers between",n1,"and",n2,"are:")
for x in range(n1,n2+1):
for i in range(2,x):
if x%2==0:
break
else:
print(x,end="\t")

OUTPUT:
Enter the value for N1:10
Enter the value for N2:20
All prime numbers between 10 and 20 are:
11 13 15 17 19

PANIMALAR ENGINEERING COLLEGE Page: - 12 -

You might also like