0% found this document useful (0 votes)
228 views9 pages

Ge3151 Unit1 2marks

The document discusses algorithms and their components. It defines algorithms, lists their characteristics, and provides examples of writing algorithms to solve problems like finding the minimum number or calculating sums and products. It also discusses concepts like flowcharts, pseudo-code, recursion, and control flow statements.

Uploaded by

dhasamalika
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)
228 views9 pages

Ge3151 Unit1 2marks

The document discusses algorithms and their components. It defines algorithms, lists their characteristics, and provides examples of writing algorithms to solve problems like finding the minimum number or calculating sums and products. It also discusses concepts like flowcharts, pseudo-code, recursion, and control flow statements.

Uploaded by

dhasamalika
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/ 9

Unit-1

2marks
1.What is an algorithm? (University question)
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.

1. Write an algorithm to find minimum of 3 numbers in a list. (University question)


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

2. List the building blocks of an algorithm.


• Statements
• Sequence
• Selection or Conditional
• Repetition or Control flow
• Functions

3. Define statements. List its type


Statements are instructions in Python designed as components for algorithmic problem solving. For
example. 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.
4. Write the pseudo code to calculate the sum and product of two numbers and display.
BEGIN
INITIALIZE variables sum, product, number1,number2
READ number1, number2 sum = number1+ number2
PRINT “The sum is “,sum

COMPUTE product = number1 * number2

PRINT “The Product is “, product


END
5. 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 again. Functions can be "called" from the inside of other functions.

6. Write the pseudo code to calculate the sum and product displaying the answer on the
monitor screen.
BEGIN
INITIALIZE variables sum, product, number1, number2 of type real

READ number1, number2 sum = number1 +number2


PRINT “The sum is “, sum
COMPUTE product = number1 * number2

PRINT “The Product is “, product


END program
7. Give the rules for writing
o Write one statement per line.
o Capitalize initial keywords.
o Indent to show hierarchy.
o End multi-line structure.
o Keep statements to be language independent
8. Give the difference between flowchart and pseudo code.
• Flowchart is a graphical representation of the algorithm.
• Pseudo code is a readable, formally English like language representation.
10 . Define a flowchart.
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.

11.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)

12.Write down the rules for preparing a flowchart.


• A flowchart should have a start and end,
• The direction of flow in a flowchart must be from top to bottom and left to right
• The relevant symbols must be used while drawing a flowchart.

13.Mention the characteristics of an algorithm.


• 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.

14.Define the two modes in Python.


Python has two basic modes: script and interactive.
The normal mode is the mode where the scripted and finished .py files are run in the Python
interpreter. Interactive mode is a command line shell which gives immediate feedback for each
statement, while running previously fed statements in active memory

15.Give the various data types in Python


Python has five standard data types
• Numbers
• String
• List
• Tuple
• Dictionary
16.List out the simple steps to develop an algorithm.

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.

17.Give the differences between recursion and iteration

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.

18. 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.

19. Define control flow statement.


A program's control flow is the order in which the program's code executes. The
controlflow of a Python program is regulated by conditional statements, loops, and function
calls.
20.Write an algorithm to accept two number. compute the sum and print the result.
(University question)

Step 1: Start
Step 2: READ the value of two numbers
Step 3:Compute sum of two numbers
Step 4 :Print the sum
Step 5:Stop
21.Write an algorithm to find the minimum number in a given list of numbers.
(Universityquestion)
• Step 1: Start
• Step 2:Read the total number of element in the list as N
• Step3:Read first element as E
• Step 4:MIN=E
• Step 5:Set i=2
• Step6:IF i>n goto Step 11 ELSE goto Step 7
• Step 7:Read i th element as E
• Step 8:IF E<MIN then set MIN=e
• Step 9:i=i+1
• Step 10:goto step 6
• Step 11:print MIN
• Step12:Stop

22. Outline the logic to swap the contents of two identifiers without using the third variable
(University question)
• Step1: Start
• Step 2:Read A,B
• Step 3 :A=A+B
• Step 4:B=A-B
• Step5:A=A-B
• Step 6:Print A ,B
• Step 7:Stop
Unit-2
TWO MARKS
1. What is a value? What are the different types of values? (University question)
A value is one of the fundamental things – like a letter or a number – that a program manipulates.
Its types are: integer, float, , strings , lists and Dicitionary.

2. Define a variable and write down the rules for naming a variable.
A name that refers to a value is a variable. Variable names can be arbitrarily long. They can contain
both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is good
to begin variable names with a lowercase letter.

3. Define keyword and enumerate some of the keywords in Python. (University question)
A keyword is a reserved word that is used by the compiler to parse a program. Keywords cannot be
used as variable names. Some of the keywords used in python are: and, del, from, not, while, is, continue.

4. Define statement and what are its types?


A statement is an instruction that the Python interpreter can execute. There are two types of
statements: print and assignment statement.

5. What do you meant by an assignment statement?


An assignment statement creates new variables and gives them values:

Eg 1: Message = “hello”
Eg 2: n = 17

6. What is tuple? (University question)


A tuple is a sequence of immutable Python objects. Tuples are sequences, like lists. The differences between
tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use
square brackets. Creating a tuple is as simple as putting different comma-separated values. Comma-separated
values between parentheses can also be used.
Example: tup1 = ('physics', 'chemistry', 1997, 2000);
7. What is an expression?
An expression is a combination of values, variables, and operators. An expression is
evaluated using assignment operator.
Examples: Y=x + 17
8. What do you mean by an operand and an operator? Illustrate your answer with relevant
example.
An operator is a symbol that specifies an operation to be performed on the operands. The data
items that an operator acts upon are called operands. The operators +, -, *, / and ** perform addition,
subtraction, multiplication, division and exponentiation.
Example: 20+32
In this example, 20 and 32 are operands and + is an operator.

9. What is the order in which operations are evaluated? Give the order of precedence.
The set of rules that govern the order in which expressions involving multiple operators and
operands are evaluated is known as rule of precedence. Parentheses have the highest precedence
followed by exponentiation. Multiplication and division have the next highest precedence followed by
addition and subtraction.

10. Illustrate the use of * and + operators in string with example.


The * operator performs repetition on strings and the + operator performs concatenationon
strings.
Example:
>>> ‘Hello*3’
Output: HelloHelloHello
>>>’Hello+World
’ Output:
HelloWorld

11. What is the symbol for comment? Give an example.


# is the symbol for comments in python.
Example:
# compute the percentage of the hour that has elapsed
12. What is function call?
A function is a named sequence of statements that performs a computation. When we define a
function, we specify the name and the sequence of statements. Later, we can “call” the function by its
name called as function call.

13.Identify the parts of a function in the given example.


>>> betty = type("32")
>>> print betty
The name of the function is type, and it displays the type of a value or variable. The valueor variable,
which is called the argument of the function, is enclosed in parentheses. The argument is 32. The
function returns the result called return value. The return value is stored in betty.

14.What is a local variable?


A variable defined inside a function. A local variable can only be used inside its function.

15.What is the output of the following?


a. float(32)
b.
float("3.14159")
Output:
a. 32.0 The float function converts integers to floating-point numbers.
b. 3.14159 The float function converts strings to floating-point numbers.

16.What do you mean by flow of execution?


In order to ensure that a function is defined before its first use, we have to know the orderin which
statements are executed, which is called the flow of execution. Execution always begins at the first
statement of the program. Statements are executed one at a time, in order from top to bottom.
17.Write down the output for the following program.
first = 'throat'
second = 'warbler' print first + second
Output:
throatwarbler
18.Give the syntax of function definition.
def NAME( LIST OF PARAMETERS ):
STATEMENTS

19.Explain the concept of floor division.


The operation that divides two numbers and chops off the fraction part is known as floor division.

20.What is type coercion? Give example.


Automatic method to convert between data types is called type coercion. For mathematical operators,
if any one operand is a float, the other is automatically converted to float.
Eg:
>>> minute = 59
>>> minute / 60.00.983333333333

21.Write a math function to perform √2 / 2.


>>> math.sqrt(2) / 2.0

0.70710678118
22.What is meant by traceback?
A list of the functions that tells us what program file the error occurred in, and what line, and what
functions were executing at the time. It also shows the line of code that caused the error.

23.Write a program to accept two numbers multiply them and print them. (University
question)
A
=1
0
B
=2
multiplication=A*B
print (“Multiplication of two numbers :” ,multiplication)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy