Python 2 Marks
Python 2 Marks
QUESTION BANK
UNIT I
ALGORITHMIC PROBLEM SOLVING
PART- A (2 Marks)
1. What is an algorithm? (Nov/Dec 2017)
Algorithm is an ordered sequence of finite, well defined, unambiguous instructions
for completing a task. It is an English-like representation of the logic which is used to solve
the problem. It is a step- by-step procedure for solving a task or a problem. The steps must
be ordered, unambiguous and finite in number.
2. Write an algorithm to find minimum of 3 numbers in a list.
(Apr/May 2017)
ALGORITHM : Find Minimum of 3 numbers in a list
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step
5. Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
3. List the building blocks of an algorithm.(Nov/Dec 2021)
Statements
Sequence
Selection or Conditional
Repetition or Control flow
Functions
4. Define statement. List its types.
Statements are instructions in Python designed as components for algorithmic problem
solving, rather than as one-to-one translations of the underlying machine language instruction
set of the computer.
There are three types of high-level programming language statements Input/output
statements make up one type of statement. An input statement collects a specific value from
the user for a variable within the program. An output statement writes a message or the value
of a program variable to the user’s screen.
5. Write the pseudo code to calculate the sum and product of two numbers and display
it. INITIALIZE variables sum, product, number1, number2 of type real
PRINT “Input two numbers”
READ number1, number2
sum = number1 + number2
PRINT “The sum is “, sum
COMPUTE product = number1 * number2
PRINT “The Product is “, product
END program
6. How does flow of control work? (Nov/Dec 2017)
Control flow (or flow of control) is the order in which individual statements, instructions or
function calls of an imperative program are executed or evaluated. A control flow statement is
a statement in which execution results in a choice being made as to which of two or more
paths to follow.
7. What is a function?
Functions are "self-contained" modules of code that accomplish a specific task. Functions
usually "take in" data, process it, and "return" a result. Once a function is written, it can be used
over and over and over again. Functions can be "called" from the inside of other functions.
8. Write the pseudo code to calculate the sum and product displaying the answer on the
monitor screen. (Apr/May 2017)
INITIALIZE variables sum, product, number1, number2 of type real
PRINT “Input two numbers”
READ number1, number2
sum = number1 + number2
PRINT “The sum is “, sum
COMPUTE product = number1 * number2
PRINT “The Product is “, product
END program
9. Give the rules for writing Pseudo codes.
Write one statement per line.
Capitalize initial keywords.
Indent to show hierarchy.
End multiline structure.
Keep statements to be language independent.
10. Give the difference between flowchart and pseudo code. (Nov/Dec 2017)
Flowchart and Pseudo code are used to document and represent the algorithm. In other words,
an algorithm can be represented using a flowchart or a pseudo code. Flowchart is a graphical
representation of the algorithm. Pseudo code is a readable, formally styled English like language
representation of the algorithm.
11. Define a flowchart. (Nov/Dec 2021)
A flowchart is a diagrammatic representation of the logic for solving a task. A flowchart is
drawn using boxes of different shapes with lines connecting them to show the flow of control. The
purpose of drawing a flowchart is to make the logic of the program clearer in a visual form.
12. Give an example of
iteration. a = 0
for i from 1 to 3 // loop three times
{
a=a+i // add the current value of i to a
}
print a // the number 6 is printed (0 + 1; 1 + 2; 3 + 3)
13. Write down the rules for preparing a flowchart. (Nov/Dec 2017)
While drawing a flowchart, some rules need to be followed—
(1) A flowchart should have a start and end,
(2) The direction of flow in a flowchart must be from top to bottom and left to right, and
(3) The relevant symbols must be used while drawing a flowchart.
14. List the categories of Programming languages.
Programming languages are divided into the following categories:
Interpreted, Functional, Compiled, Procedural, Scripting, Markup, Logic-Based,
Concurrent and Object-Oriented Programming Languages
15. Mention the characteristics of an algorithm. (Nov/Dec 2017)
Algorithm should be precise and unambiguous.
Instruction in an algorithm should not be repeated infinitely.
Ensure that the algorithm will ultimately terminate.
Algorithm should be written in sequence.
Algorithm should be written in normal English.
Desired result should be obtained only after the algorithm terminates.
16. Compare machine language, assembly language and high-level language.
Machine language is a collection of binary digits or bits that the computer reads
and interprets. This language is not easily understandable by the human.
An assembly language directly controls the physical hardware. A program written in
assembly language consists of a series of instructions mnemonics that correspond to a stream of
executable instructions, when translated by an assembler can be loaded into memory and
executed. The programs written in this language are not portable and the debugging process is
also not very easy.
A high level language is much more abstract, that must be translated or compiled in to
machine language. It is easily understandable and the programs are portable. Debugging the code is
easy and the program written is not machine dependent.
17. What is the difference between algorithm and pseudo code?
An algorithm is a systematic logical approach used to solve problems in a computer
while pseudo code is the statement in plain English that may be translated later to a
programming language. Pseudo code is the intermediary between algorithm and program.
18. List out the simple steps to develop an algorithm.
(Apr/May 2017)
Algorithm development process consists of five steps,
Step 1: Obtain a description of the problem.
Step 2: Analyze the problem.
Step 3: Develop a high-level algorithm.
Step 4: Refine the algorithm by adding more detail.
Step 5: Review the algorithm.
19. Give the differences between recursion and iteration. (Nov/Dec 2017)
Recursion Iteration
Function calls itself until the base condition is Repetition of process until the condition fails.
reached.
Only base condition (terminating condition) is It involves four steps: initialization, condition,
specified. execution and updation.
It keeps our code short and simple. Iterative approach makes our code longer.
It is slower than iteration due to overhead of Iteration is faster.
maintaining stack.
It takes more memory than iteration due to Iteration takes less memory.
overhead of maintaining stack.
20. What are advantages and disadvantages of recursion?
Advantages Disadvantages
Recursive functions make the code look clean Sometimes the logic behind recursion is hard to
and elegant. follow through.
A complex task can be broken down into Recursive calls are expensive (inefficient) as
simpler sub-problems using recursion. they take up a lot of memory and time.
Sequence generation is easier with recursion Recursive functions are hard to debug.
than using some nested iteration.
elif is an abbreviation of “else if.” Again, exactly one branch will be executed. There is no limit on
the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t
have to be one.
9. Explain ‘for loop’ with example. (Nov/Dec 2017)
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.
Eg:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
14. Explain global and local scope.
The scope of a variable refers to the places that we can see or access a variable. If we define
a variable on the top of the script or module, the variable is called global variable. The variables
that are defined inside a class or function is called local variable.
Eg:
def my_local():
a=10
print(“This is local variable”)
Eg:
a=10
def my_global():
print(“This is global variable”)
15.Compare string and string slices. (Nov/Dec 2017)
A string is a sequence of character.
Eg: fruit = ‘banana’
String Slices :
A segment of a string is called string slice, selecting a slice is similar to selecting a
character. Eg: >>> s ='Monty Python'
> print
s[0:5] Monty
> print
s[6:12] Python
16. Define string immutability. (Apr/May 2017)
Python strings are immutable. ‘a’ is not a string. It is a variable with string value. We can’t
mutate the string but can change what value of the variable to a new string.
Program: Output:
a = “foo” #foofoo
# a now points to foo #foo
b=a It is observed that ‘b’ hasn’t changed even
# b now points to the same foo that a points to though ‘a’ has changed.
a=a+a
# a points to the new string “foofoo”, but b
points to the same old “foo”
prin
t a
prin
tb
17.Mention a few string functions. (Nov/Dec 2017)
s.captilize() – Capitalizes first character of string
s.count(sub) – Count number of occurrences of sub in
string
s.lower() – converts a string to lower case
s.split() – returns a list of words in string
18. What are string methods?
A method is similar to a function—it takes arguments and returns a value—but the
syntax is different. For example, the method upper takes a string and returns a new string with all
uppercase letters:
Instead of the function syntax upper(word), it uses the method syntax
word.upper() .>>> word = 'banana'
> new_word = word.upper()
> print new_word
BANANA
19. Explain about string module. (Nov/Dec 2017)
The string module contains number of useful constants and classes, as well as some
deprecated legacy functions that are also available as methods on strings.
Eg:
Program: Output:
import string upper => MONTY PYTHON'S FLYING
text = "Monty Python's Flying Circus" CIRCUS
print "upper", "=>", string.upper(text) lower => monty python's flying circus
print "lower", "=>", string.lower(text) split => ['Monty', "Python's", 'Flying', 'Circus']
print "split", "=>", string.split(text) join => Monty+Python's+Flying+Circus
print "join", "=>", string.join(string.split(text), replace => Monty Java's Flying Circus
"+") find => 6 -1
print "replace", "=>",string.replace(text, count => 3
"Python", "Java")
print "find", "=>", string.find(text, "Python"),
string.find(text, "Java")
print "count", "=>", string.count(text, "n")
23. Write a python script to display the current date and time. (Apr/May 2017)
Program:
import datetime
now=datetime.datetime.now()
print(“Current date and time using str method of datetime object:”)
print(str(now))
Output:
Current date and time using str method of datetime object:
2022-10-25 21:44:33.737559
24. What is reusability? (Nov/Dec 2017)
Functionality defined in a single module can be easily reused (through an appropriately defined
interface) by other parts of the application. This eliminates the need to duplicate code.
25. What is scoping. Modules typically define a separate namespace, which helps avoid collisions
between identifiers in different areas of a program.