python class test 2 with two mark answer
python class test 2 with two mark answer
QUESTION BANK
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
10 What is the use of algorithm ? Flowchart and Pseudo code in the perspective of problem
solving (Nov/Dec 2019)
i. Simple to understand
ii. Easy to debug
iii. Independent of programming language
iv. Each step can be easy to written in high-level language.
11 What are the factors used to judge the quality of an algorithm ? (Nov/Dec 2020)
I. Accuracy
II. Memory
III. Time
IV. Sequence
V. Result
12 What is control flow? List and define the ways of execution of control. (Nov/Dec 2020)
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.
i. Sequential control flow
ii. Selection control flow
iii. Iterative control flow
13 List the building blocks of algorithm?
The building blocks of an algorithm are
Statements
Sequence
Selection or Conditional
Repetition or Control flow
Functions
14. 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.
15 Give the difference between flowchart and pseudo code.
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.
16 Define Recursion?
Function calls itself until the base condition is
reached.
Only base condition (terminating condition) is
Specified
It takes more memory than iteration due to
overhead of maintaining stack.
17 What is Iteration
Repetition of process until the condition fails
It involves four steps: initialization, condition,
execution and updation
Iterative approach makes our code longer
Iteration is faster.
Iteration takes less memory
18 Write down the steps involved in algorithmic problem?
Algorithm development process consists of five major 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.
NUMBER num
IF num>0 THEN
OUTPUT "Entered number is positive"
ELSE IF num <0 THEN
OUTPUT "Entered number is negative"
ELSE
OUTPUT "Entered number is zero"
ENDIF
END
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
PART-B
1 i. Define flow chart? Draw flowchart and write an algorithm to accept three
distinct numbers and find the greatest, print the result (Nov/Dec 2022)
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.
FLOECHART:
ALGORITHM:
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
ii. Outline the algorithm to display the first n odd numbers(Apr/May 2019)
2 I. Draw a flowchart AND algorithm to find the sum of the series 1+2+3…..
+100 (Nov/Dec 2022)
II.What is an algorithm? Summarize the characteristics of a good algorithm.
(Apr/May 2019)
I. FLOWCHART
ALGORITHM:
Read n value as 100.
declare k variable.
Uss formula n*(n+1)/2 to get sum of first 100 natural numbers and hold it in the
variable k.
print k.
II. ALGORITHM:
CHARECTERISTICS:
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.
3 What is a programming language ? What are its types ? Explain in detail with
their advantages and disadvantages (Nov/Dec 2019)
• A programming language is a set of symbols and rules for instructing a computer
to perform specific tasks.
• The programmers have to follow all the specified rules before writing program
using programming language.
• The user has to communicate with the computer using language which it can
understand
1. Machine language
2. Assembly language
3. High level language
MACHINE LANGUAGE:
The computer can understand only 0’s and 1’s.
• In machine language the different instructions are formed by taking different
combinations of 0’s and 1’s.
Advantages:
Translation free:
• For executing any program written in any programming language, the conversion
to machine language is necessary.
• The conversion process is not required.
High speed
• The machine language program is translation free.
• Conversion time is saved
the execution of machine language program is extremely fast
Disadvantage:
• It is hard to find errors in a program written in the machine language.
• Writhing program in machine language is a time consuming process.
Machine dependent:
• According to architecture used, the computer differs from each other.
• Machine language differs from computer to computer.
• Program developed for a particular type of computer may not run on other type
of computer.
ASSEMBLY LANGUAGE:
• To overcome the issues in programming language and make the programming
process easier, an assembly language is developed which is logically
equivalent to machine language but it is easier for people to read, write and
understand.
• Assembly languages are symbolic programming language that uses symbolic
notation to represent machine language instructions.
• They are called low level language because they are so closely related to the
machines.
ASSEMBLER
Assembler is the program which translates assembly language instruction in to a
machine language.
• Easy to understand and use.
• It is easy to locate and correct errors.
Disadvantage
1. Machine dependent
The assembly language program which can be executed on the machine depends
on the architecture of that computer.
2. Hard to learn
It is machine dependent, so the programmer should have the hardware
knowledge to create applications using assembly language.
3. Less efficient
Execution time of assembly language program is more than machine language
program.
Because assembler is needed to convert from assembly language to machine
language.
HIGH LEVEL LANGUAGE
High level language contains English words and symbols.
The specified rules are to be followed while writing program in high level
language.
The interpreter or compilers are used for converting these programs in to
machine readable form.
Translating high level language to machine language
Compiler:
• A compiler is a program which translates the source code written in a high level
language in to object code which is in machine language program.
• Compiler reads the whole program written in high level language and translates
it to machine language.
• If any error is found it display error message on the screen.
Interpreter
• Interpreter translates the high level language program in line by line manner.
• The interpreter translates a high level language statement in a source program to
a machine code and executes it immediately before translating the next
statement.
• When an error is found the execution of the program is halted and error message
is displayed on the screen.
Advantages
Readability
• High level language is closer to natural language so they are easier to learn and
understand
Machine independent
• High level language program have the advantage of being portable between
machines.
Easy debugging
• Easy to find and correct error in high level language
Disadvantages
Less efficient
• The translation process increases the execution time of the program. Programs
in high level language require more memory and take more execution time to
execute.
They are divided into following categories:
Interpreted programming languages
• Functional programming languages
• Compiled programming languages
• Procedural programming languages
• Scripting programming language
• Markup programming language
• Concurrent programming language
• Object oriented programming language
4 Outline the algorithm, flowchart and pseudocode to display the first n odd
numbers(Apr/May 2019)
ALGORITHM:
Step 1: Start
Step 5: Stop
FLOWCHART:
PSEUDOCODE:
1. INPUT n.
2. remainder = n % 2.
3. IF remainder is not equal to 0.
4. answer = odd.
5. ELSE.
6. answer = even.
7. OUTPUT answer.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop
Selection:
A selection statement causes the program control to be transferred to a specific part of
the program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called
looping or iteration.
Example
Functions:
Def add(a,b):
Sum=a+b
Return sum
Num1=10
Num2=20
Print(‘the sum is’,add(num1,num2))
6 Draw flowchart to print the first n prime numbers(Apr/May 2017)
IS THIS NUMBER PRIME:
7 State the Tower of Hanoi problem. Outline the solution to the Tower of Hanoi problem with r
diagram. (Nov/Dec 2020)
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one
as depicted –
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller o
over the larger one. There are other variations of the puzzle where the number of disks increase,
tower count remains the same.
RULES
The mission is to move all the disks to some another tower without violating the sequence of arrang
A few rules to be followed for Tower of Hanoi are −
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.Following is an animated representation of solving a
And finally, we move the smaller disk from aux to destination peg.
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two dis
divide the stack of disks in two parts. The largest disk (n th disk) is in one part and all other (n-1) d
in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto
can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are −
Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from aux to dest
A RECURSIVE ALGORITHM FOR TOWER OF HANOI CAN BE DRIVEN AS FOLLOW
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
FLOWCHART
1. Iterations:
A sequence of statements is executed until a specified condition is true is called iterations.
1. for loop
2. While loop
Syntax for For:
FOR( start-value to end-value) DO
Statement
...
ENDFOR
9 What is Algorithmic Problem Solving and explain the key steps involved for solving a problem?
Understanding the Problem
v It is the process of finding the input of the problem that the algorithm solves.
v It is very important to specify exactly the set of inputs the algorithm needs to handle.
v A correct algorithm is not one that works most of the time, but one that works c
for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device
v If the instructions are executed one after another, it is called sequential algorithm.
v If the instructions are executed concurrently, it is called parallel algorithm.
Choosing between Exact and Approximate Problem Solving
v The next principal decision is to choose between solving the problem exactly or sol
approximately.
v Based on this, the algorithms are classified as exact algorithm and approximation algorithm.
Deciding a data structure:
v Data structure plays a vital role in designing and analysis the algorithms.
v Some of the algorithm design techniques also depend on the structuring data specifying a pro
instance
v Algorithm+ Data structure=programs.
Algorithm Design Techniques
v An algorithm design technique (or “strategy” or “paradigm”) is a general approach to
problems algorithmically that is applicable to a variety of problems from different areas of computi
v Learning these techniques is of utmost importance for the following reasons.
v First, they provide guidance for designing algorithms for new problems,
2. simplicity.
v An algorithm should be precisely defined and investigated with mathematical expressions.
v Simpler algorithms are easier to understand and easier to program.
v Simple algorithms usually contain fewer bugs.
Coding an Algorithm
v Most algorithms are destined to be ultimately implemented as computer programs. Programm
algorithm presents both a peril and an opportunity.
v A working program provides an additional opportunity in allowing an empirical analysis
underlying algorithm. Such an analysis is based on timing the program on several inputs an
analysing the results obtained.
4 Write algorithm, flowchat and pseudocode for guessing an integer number in a range(binary search
Algorithm:
Step1: Start
Step 2: Declare hidden, guess
Step 3: Compute hidden= Choose a random value in a range
Step 4: Read guess
Step 5: If guess=hidden, then
Print Guess is hit
Else
Print Guess not hit
Print hidden
Step 6: Stop
Pseudocode:
BEGIN
COMPUTE hidden=random value in range
READ guess
IF guess=hidden, then
PRINT Guess is hit
ELSE
PRINT Guess not hit
PRINT hidden
END IF-ELSE
END
FLOWCHART:
10 Identify the simple strategies for developing an algorithm (Nov/Dec 2019)
SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS:
1. iterations
2. Recursions
1. Iterations:
Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
Step4.1: Read a[i]
Step 4.2: i=i+1 goto step 4
Step 5: Read item
Step 6: Calculate i=n-1
Step 7: If i>=0 and item<a[i], then go to step 7.1, 7.2 else goto step 8
Step 7.1: a[i+1]=a[i]
Step 7.2: i=i-1 goto step 7
Step 8: Compute a[i+1]=item
Step 9: Compute n=n+1
Step 10: If i<n, then goto step 10.1, 10.2 lse goto st 11
Step10.1: Print a[i]
Step10.2: i=i+1 goto step 10
Step 11: Stop
Pseudocode:
BEGIN
READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END
Flowchart: