0% found this document useful (0 votes)
100 views20 pages

CIA-1 Answer Key Sec-A

The document provides information about an internal assessment test conducted for the subject GE3151 Problem Solving and Python Programming. It includes details like the course code, name, outcomes being assessed, date and duration of the test. The test contains two parts - Part A consists of 10 short answer questions assessing basic concepts of algorithms, flowcharts, pseudocode etc. Part B contains 5 long answer questions assessing topics like flowcharts, algorithmic problem solving, writing algorithms and code to solve problems like Fibonacci series and finding minimum number from a list.

Uploaded by

akileshwari R
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)
100 views20 pages

CIA-1 Answer Key Sec-A

The document provides information about an internal assessment test conducted for the subject GE3151 Problem Solving and Python Programming. It includes details like the course code, name, outcomes being assessed, date and duration of the test. The test contains two parts - Part A consists of 10 short answer questions assessing basic concepts of algorithms, flowcharts, pseudocode etc. Part B contains 5 long answer questions assessing topics like flowcharts, algorithmic problem solving, writing algorithms and code to solve problems like Fibonacci series and finding minimum number from a list.

Uploaded by

akileshwari R
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/ 20

Kathir College of Engineering

[Approved by AICTE |Affiliated to Anna University | Accredited by NAAC]


Neelambur, Avinashi Road, Coimbatore-62

CONTINUOUS INTERNAL ASSESSMENT – I


YEAR FIRST Sem. I Date 12.1.2023 Duration 3 Hrs.
GE3151 Problem Solving and Python
Subject code & Name Max. Marks 100
Programming
Course Outcomes: RBT

CO1 To understand the basics of algorithmic problem solving. AP


CO2 To learn to solve problems using Python conditionals and loops. AP
CO3 To define Python functions and use function calls to solve problems. AP
SEO RBT CO Marks
Part – A (10 x 02 = 20 Marks)
Answer All Questions
List the building blocks of algorithm.
 Statements
 State
1  Control flow R 1 2
 Sequence
 Selection
 Iteration
 Functions
Write down the rules for preparing a flowchart.
 The flowchart should be clear, neat and easy to follow.
2  The flowchart must have a logical start and finish. R 1 2
 Only one flow line should come out from a process symbol.
 Only one flow line is used with a terminal symbol.

Write the algorithm to calculate the average of three numbers and display it.
Step 1: Start.
Step 2 : Read a,b,c from the user
3 Step 3 : sum = a+b+c AP 1 2
Step 4: Avg=sum/3
Step 5:Display "sum " and "Avg"
Step 6 :End

Give the rules for writing Pseudo code.


 Write one statement per line
4  Capitalize initial keyword R 1 2
 Indent to hierarchy
 End multiline structure
 Keep statements language independent
Write an algorithm to find the sum of digits of a number and display it.
Step 1: Start
5 Step 2: Input N AP 2 2
Step 3: Assign Sum=0
Step 4: While (N! =0) go to step 5, else go to step 6.
Step 5: Rem = N%10;
Sum = Sum +Rem;
N = N/10;
Step 6: Print Sum
Step 7: Stop
Define a variable and write down the rules for naming a variable.
It is the name for memory location that may be used to store a data value. Rules for
6 naming variable R 2 2
 A variable name must start with a letter or the underscore character.
 A variable name cannot start with a number.
 A variable name can only contain alpha-numeric characters and underscores.

Compare Interpreter and Compiler


Interpreter Compiler

The compiler scans the whole Translates the program one


program in one go. statement at a time.

7 As it scans the code in one go, the It scans code one line at a time; U 2 2
error are shown at the end together. errors are shown line by line.

Execution of the program takes place Execution of the program happens


only after the whole program is after every line is checked or
compiled. evaluated.

What is the purpose of using comment in python program?


 Comments are non-executable statements which explain what program
does.
8  The lines in the code that are ignored by the interpreter during the U 2 2
execution of the program.
 Comments enhance the readability of the code
 It helps the programmers to understand the code very carefully.
Write the output for the following code:
X=10 y=12
 Print(‘x>y is’,x>y) = False
9 AP 3 2
 print(‘x>=y is’,x>=y)= False
 print(‘x<y is’,x<y)= True
 Print(‘x==y is’,x==y) = False
Define Boolean Expression with example.
Boolean Expressions
 The Boolean values in Python are True and False, typically used to control if-
statements and while-loops.
Examples:
10 x=10 y=5 U 3 2
x == y # Produce True if x is equal to y
x != y # produce false if x is not equal to y
Part – B (05 x 16 = 80 Marks) SEO RBT CO Marks

Answer All Questions


(i)What is flow chart? Explain in detail about each control structure in flow chart.
Flowchart:
 Flow chart is defined as graphical representation of the logic for problem
solving.
 The purpose of flowchart is making the logic of the program clear in a visual
representation.
TYPES:
 Sequential control structure
 Selection control structure.
 Iteration control structure.
1. Sequential control structure
 Statements are executed in a specified order
 No statement is skipped and no statement is executed more than once
STRUCTURE:

11(a) AP 1 12
Example: Flowchart for finding area of a rectangle:

2. Selection control structure


 It selects a statement to execute on the basis of condition.
 Statement is executed when the condition is true and ignored when it is
false
 e.g if, if else, switch structures.
STRUCTURE:

Example:
Flowchart for finding the greatest of three numbers.

3. Iteration control structure


 In this structure the statements are executed more than one time.
 It is also known as iteration or loop
 e.g while loop, for loop do-while loops etc.
Example: Flow chart for finding leap year or not.
(i) Explain algorithmic problem solving.

1.Understanding the Problem


 It is the process of finding the input of the problem that the algorithm
solves.
11(b) 2.Ascertaining the Capabilities of the Computational Device U 1 6
 If the instructions are executed one after another, it is called sequential
algorithm.
3.Choosing between Exact and Approximate Problem Solving
 The next principal decision is to choose between solving the problem
exactly or solving it approximately.
4.Deciding a data structure:
 Data structure plays a vital role in designing and analysis the algorithms
5.Algorithm Design Techniques
 An algorithm design is a general approach to solving problems
algorithmically.
6.Methods of Specifying an Algorithm
 Pseudocode is usually more precise than natural language, and its usage
often yields more succinct algorithm descriptions.
7.Proving an Algorithm’s Correctness
 Once an algorithm has been specified, you have to prove its correctness
8. Analyzing an Algorithm
1. Efficiency. 2
 Time efficiency, indicating how fast the algorithm runs,
 Space efficiency, indicating how much extra memory it uses.

2. Simplicity.
 An algorithm should be precisely defined and investigated with
mathematical expressions.
9.Coding an Algorithm
 Most algorithms are destined to be ultimately implemented as computer
programs.

(ii) Write algorithm, pseudo code, flowchart to find the Fibonacci series for
the first n terms
Algorithm:
Step 1: START
Step 2: Declare variable n1, n2, sum, n, i
Step 2: Initialize variables: n1 = 0, n2 = 1, i = 2
Step 3: Read n
Step 4: Repeat this step until i <= n:
sum = n1 + n2
print sum
n1 = n2
n2 = sum
i=i+1
Step 5: STOP
Pseudocode:
BEGIN
DECLARE variable n1, n2, sum, n, i
INITIALIZE variables: n1 = 0, n2 = 1, i = 2
READ n
REPEAT this step until i <= n:
SUM = n1 + n2
PRINT SUM AP 1 10
n1 = n2
n2 = SUM
i=i+1
END
Flowchart:
12(a) Write an algorithm pseudo code and flow chart for finding the minimum number AP 1 16
from the given list A:[50,40,5,9,45]
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then go to step 4.1, 4.2 else go to step 5
4.1: Read a[i]
4.2: i=i+1 go to step 4
Step 5: Compute min=a[0]
Step 6: Initialize i=1
Step 7: If i<n, then go to step 8 else go to step 10
Step 8: If a[i]<min, then go to step 8.1,8.2 else go to 8.2
Step 8.1: min=a[i]
Step 8.2: i=i+1 go to 7
Step 9: Print min
Step 10: Stop
Pseudo code:
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:
Outline the Towers of Hanoi problem. Suggest a solution to the Towers of Hanoi
problem with relevant diagrams.

Procedure to solve Tower of Hanoi


The goal of the puzzle is to move all the disks from leftmost peg to rightmost peg.
Rules:
1. Move only one disk at a time.
2
2. A larger disk may not be p1aced on top of a smaller disk. For example,
consider n=3 disks

Algorithm:
Step 1: Start
Step 2: Read n
Step 3: Calculate move=pow(2,n)-1
12(b) AP 1 16
Step 4: Function call T(n,Beg,Aux,End) recursively until n=0
Step 4.1: If n=0, then goto step 5 else go to step 4.2
Step4.2: T(n-1,Beg,End,Aux) T(1,Beg,Aux,End) ,
Move disk from source to destinationT(n-1,Aux,Beg,End)
Step 5: Stop
Pseudo code:
BEGIN
READ n
CALCULATE move=pow(2,n)-1
FUNCTION T(n,Beg,Aux,End) Recursively until n=0
PROCEDURE
IF n=0 then,
No disk to move
Else
T(n-1,Beg,End,Aux)
T(1,Beg,Aux,End), move disk from source to destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END
Flowchart:
(i) Write a python program to rotate a list by right n times with and without slicing
techniques
1. Right rotate a list by n using slicing Technique
 One way of slicing list is by using len () method.
# Python program to right rotate
# a list by n using list slicing
n=3
list_1 = [1, 2, 3, 4, 5, 6]
list_1 = (list_1[len(list_1) - n:len(list_1)] + list_1[0:len(list_1) - n])
print(list_1)
Output
[4, 5, 6, 1, 2, 3]
2. Right Rotate a List by n (Without Slicing)
 To right rotate a list by n positions, you can use the rotate () method of the
deque object.
 This method rotates the elements of the list by the specified number
ofpositions to the right.
For example:
from collections import deque
13(a) # Create a deque object from the list AP 2 10
list_1 = [1, 2, 3, 4, 5, 6]
deque_1 = deque(list_1)
# Rotate the deque by 3 positions to the right
deque_1.rotate(3)
# Convert the deque back to a list
list_1 = list(deque_1)
print(list_1)
Output
[4, 5, 6, 1, 2, 3]
(ii)Demonstrate debugging and its types with relevant example
Debugging
 Errors occurring in programming are called as bugs.
 The process of tracking these bugs is called as debugging.
 There are three types of errors that can occur while coding :
 Syntax Error, Runtime Error and Semantic Error.
1. Syntax Error
 The syntax is a defined structure or set of rules while writing a program.
EXAMPLE:
>>> a=1+2
>>> a
3
>>> 1+2=a
Syntax Error: can't assign to operator
2. Runtime Error
 In this, program is executed successfully without syntax or logical error and
this type oferror occurs at runtime i.e. while accepting values from user.
EXAMPLE:
>>>a=2
b=3
c=4
d=input("enter a number")
sum=a+b+c+d
print(sum)
OUTPUT
enter a number5
Traceback (most recent call last):
File "C:/Python34/runtime error.py", line 5, insum=a+b+c+d
3. Semantic Error
 In this program will be executed without any error on prompt but will not
give desired output.
 Such type of error is called Semantic error.

EXAMPLE: Program to find sum of two digits
AP 2 6
num1=int(input("Enter first number|:"))
num2=int(input("Enter second number:"))
add = num1 - num2
print("sum of two nums is ", add )
OUTPUT:
Enter first number:200
Enter second number:57
sum of two nums is 143
 In above example sum of two numbers is executed.
 Still desired output is not achieved due to logical error.
 Instead of “+” symbol “-” is used.
(i)Explain statements and its types with example.
Statement:
 A statement is an instruction that a Python interpreter can execute.
 There are mainly four types of statements in Python
 Print Statements
 Assignment Statements
 Conditional Statements
 Looping Statements
Print Statements:
The result of a print statement is a value.
Example:
Print(‘Hello)
Output:
Hello
Assignment Statements:
 Assignment statements don’t produce a result it just assigns a value to the
operand on its left side.
Example:
a=10
 This is an assignment statement, wherea is a variable name and 10 is its
value. 10 is stored in a’s memory location. 10 is assigned to variable a.
Conditional Statements:
 if statement: It is a control flow statement that will execute statements
under it, if the condition is true. It is also known as a conditional statement.
Example:
13(b) if a>b: AP 2 8
print(“a is greater”)
else:
print(“b is greater”)
Looping Statements:
 while statements: The while loop statement repeatedly executes a code
block while a particular condition is true.
 for statement:Using for loop statement, we can iterate any sequence or
iterable variable.
 The sequence can be string, list, dictionary, set, or tuple..
Example:
for i in range(1,11):
print(i)
Output:
1 2 3 4 5 6 7 8 9 10

(ii)Explain the following for vowel letters by


(a) Defining a Tuple
>>> tuple=(“a”,”e”,”i”,”o”,”u”)
Print(tuple)
Output:
(‘a’,’e’,’i’,’o,’u’)
(b) How to display the elements in the Tuple
>>> tuple=(“a”,”e”,”i”,”o”,”u”)
Print(tuple)
Output:
(‘a’,’e’,’i’,’o,’u’)
(c) How to select the range of element from the Tuple
>>>print(tuple[1:4]) # It displays first elements till fourth element
Print(tuple)
Output:
(‘e’,’i’,’o’,’u’)
(d) Assign the above Tuple with numbers
>>>tuple = ('a','e','i','o','u')
print(t)
(s1, s2, s3, s4, s5) = tuple
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)
Output: AP 2 8
(‘a’,’e’,’I,’o’,’u,)
a
e
i
o
u

14.(a) What is the role of an interpreter? Give a detailed note on python interpreter
and interactive mode of operation.
Role of an interpreter:
 Python Interpreter translates high level instructions into an
machine level language. It executes instructions directly without compiling.

Source Interpreter or Machine


code Code
Intermediate

python interpreter mode:


 The Python interpreter is
 Installed as /usr/local/bin/python3.11 on those machines
 Putting /usr/local/bin in your Unix shell’s search path
 typing the command:
 Python 3.0 to the shell.
On Windows machines where we have installed
 Python from the Microsoft Store, Python 3.11 command
will be available.
 If you have the py.exe launcher installed, use the py command.
 The interpreter operates somewhat like the Unix shell
 when called with standard input connected to a device, it reads d and
executes commands interactively
 When called with a file name argument/with a file as
standard input, it reads and executes a script from that file.
 A second way of starting the interpreter is:
python -c command [arg] ...,
 which executes the statement(s) in command
 analogous to the shell’s -c option
 Since Python statements often contain spaces
or other characters that are special to the shell,
 It is usually advised to quote command in its entirety.
Interactive mode:
 Interactive mode is a command line which gives
immediate feedback for each statement.
 Python interactive mode can be start by two methods :
 - Python 3.6 (Shell) and PythonIDLE.
 Python 3.6 (Shell), A prompt will appear and it usually have
 3 greater than signs (>>>).
 Each Statements can be enter after this (>>>) symbol.
 For continuous lines three dots (...)will represent the multiple lines.
 Python IDLE (Integrated Development for Learning Environment)
which provides a user friendly console to the python users.
 Different colors are used to represent different keywords.
 IDLE starts by python version, after a line (>>>) three greater than symbols will
be displayed. The statements will be executed in that line.
 Example:
 >>> 1+1
 2
 >>>5+10
 15
(i)Illustrate all arithmetic operators for x=25 and y=5

1. + (Addition)
x + y= 25+5 = 30
2. - (Subtraction)
x-y= 25-5 = 20
3. * (Multiplication)
x*y= 25*5 = 125 AP
4. / (Division)
x/y= 25/5 = 5
5. // Floor division)
15(a)
x//y= 25//5 = 5
6. ℅ (Modulus)
x % y= 25%5 = 0

(iii) Illustrate all bitwise operators for x=8 and y=64


1. & Bitwise and = x & y = 8 & 64
2. | Bitwise or = x || y = 8 || 64 AP 3
3. ^ Bitwise xor = x^y= 8^64
4. Bitwise left shift << = 8<<64

(OR)

Write a syntax, program and draw a flow chart for chained condition execution and
nested conditionals.
Chained condition execution
Chained if...elif...else U 3
 The elif is short for else if.
 This is used to check more than one condition.
 If the condition1 is False, it checks the condition2 of the elif block.
 If all the conditions are False, then the else part is executed.
15(b)
 The if block can have only one else block.
 But it can have multiple elif blocks.
Syntax:
Flowchart:

Example:
Student mark system
mark=int(input("enter your mark:"))
if(mark>=90):
print("grade:S")
elif(mark>=80):
print("grade:A")
elif(mark>=70):
print("grade:B")
elif(mark>=50):
print("grade:C")
else:
print("fail")
Output
enter your mark:78
grade:B
Nested conditionals
 One conditional can also be nested within another.
 Any number of conditions can be nested inside one another.
 In this, if the condition is true it checks another if condition1.
 If both the conditions are true statement1 get executed otherwise
statement2 get execute.
 If the condition is false statement3 gets executed.
Syntax:
If (condition):
If(condition 1):
Statement 1
else:
statement 2
else:
statement 3
Flowchart:

Example:
Greatest of three numbers
a=int(input(“enter the value of a”))
b=int (input(“enter the value of b”))
c=int (input(“enter the value of c”))
if(a>b):
if(a>c):
print(“the greatest no is:”,a)
else:
print(“the greatest no is:”,c)
else:
if(b>c):
print(“the greatest no is:”,b)
else:
print(“the greatest no is:”,c)
Output
enter the value of a: 9
enter the value of b: 1
enter the value of c: 8
the greatest no is 9

You might also like