GE3151-Python-Unit-wise Solved Part-A Questions
GE3151-Python-Unit-wise Solved Part-A Questions
Page 1 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
Page 2 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
9. Define a 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.
A flowchart is a type of diagram that represents a workflow or process.
The flowchart shows the steps as boxes of various kinds, and their order by connecting the
boxes with arrows. This diagrammatic representation illustrates a solution model to a given
problem.
4. Only one flow line should enter a decision symbol. However, two or three flow
Page 4 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
Advantages:
to flowchart.
compared to flowchart.
18. What is a programming language?
A programming language is a set of symbols and rules for instructing a computer to perform specific
task. Each programming language has a flow of execution and a unique syntax to be followed.
It categorizes as, High level language, low level language and assembly language.
19. Differentiate high level, low level and assembly level languages.
Page 5 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
Low level/ Machine level Assembly level language High level language
language
Programs are written only in Programs are wirtten in Programs are written using
0s and 1s ie. binary will be mnuemonics like ADD A,B English words and symbols.
understood by the computer.
There is no conversion The Assembler converts the The compiler and the
required. assembly level language to the interpreter does the conversion
machine language. of the high level languages to
the machine language.
Execution is faster as there is Execution time is fast Takes more execution time
no translation needed. compared to the high level than the assembly level
language but slow compared language and the low level
to the low level language as language.
translation is required.
Debugging is difficult and Debugging is easy when Debugging can be done faster
consumes more time to debug. compared to low level then the other two languages.
language , but not easy as high
level language.
It is translation free. Translation is required Translation is required
It is machine dependent. Machine Dependent Machine Independent
Hard to learn and find errors. Medium difficulty with Easy to learn and debug , but
respect to learning when less efficient as translation
compared to low level takes more time.
language
The steps are ,
Page 6 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
Compiler Interpreter
Compiler takes entire program as input. Interpreter takes single instruction as input.
Intermediate object code is generated. No intermediate object code is generated.
Conditional control statements executes faster. Conditional control statements executes
slower.
Memory requirement is more. Memory requirement is less.
Program need not be compiled every time. Every time a high level program is converted
into a low level program.
Errors are displayed after entire program is Error are displayed after every instuction.
checked.
Ex: C Compiler Ex: Python.
16 Mark questions:
1. Write an algorithm and program to find minimum in a list.
Page 7 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit I - 2 Marks Q & A
2.Discuss in detail about symbols used in flowchart and also summarize the advantage and
disadvantage of flowchart.
3. Discuss in detail Towers of Hanoi Problem
4. Explain in detail about Building Blocks of Algorithm
5. Explain in detail about the steps involved in Algorithmic problem solving.
6.Explain in detail about the simple strategies for developing an algorithm.
7. Write the algorithm, flowchart and pseudocode for inserting a card in a list of sorted cards.
8. Write the algorithm and flowchart and pseudocode for guessing an integer in a range.
Page 8 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING
PART – A (2 MARKS)
as elif if or yield
A comment is text that doesn't affect the outcome of a code, it is just a piece
of text to let someone know what you have done in a program or what is
being done in a block of code. In python we use # special character to start
the comment. Lets take few examples to understand the usage.
# This is just a comment. Anything written here is ignored by Python
1. What a python program to accept two inputs, find the greatest and print the result ?
2. State the reasons to divide programs into functions.
3. Present the flow of execution for a WHILE statement.
4. Comment with an example on the use of local and global variables with the same identifier
name.
5. State about Logical Operators available in Python programming language.
6. Define Boolean expression with example.
A boolean expression is an expression that is either true or false. The values true and false are
called Boolean values.
Eg : >>> 5 == 6
False
True and False are special values that belongs to the type bool; they are not strings:
Page 1 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
For example,
x > 0 and x < 10 is true only if x is greater than 0 and less than 10.
n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is
divisible by 2 or 3.
Finally, the not operator negates a Boolean expression, so not(x > y) is true if x > y is false,
that is, if x is less than or equal to y. Non-zero number is said to be true in Boolean expressions.
Page 2 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Sometimes there are more than two possibilities and we need more than two branches. One
way to express a computation like that is a chained conditional:
Example:
if (x < y):
print("x is less than y")
elif (x > y):
print("x is greater than y")
else:
print("x and y are equal")
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.
Example:
x=4
for i in range(0, x):
print(i)
Page 3 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
When a break statement is encountered inside a loop, the loop is immediately terminated and
the program control resumes at the next statement following the loop.
Example:
while True:
line = raw_input('>')
if (line == "done"):
break
print(line)
print("Done ! ")
Composition:
Calling one function from another is called composition.
Example:
def circle_area(xc, yc, xp, yp):
radius = distance(xc, yc, xp, yp)
result = area(radius)
return result
Page 4 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.
Example:
def factorial(n):
if (n == 1):
return(1)
else:
return(n * factorial(n-1))
Example:
a=10
def my_global():
print(“This is global variable”)
Page 5 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Page 6 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Page 7 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
1. What is a list?
A list is an ordered set of values, where each value is identified by an index.
Values in the list are called elements / items. It can be written as a list of comma-separated
items (values) between square brackets [ ].
Items in the lists can be of different data types and List is a mutable type.
>>>[1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
3. Let list = [’a’, ’b’, ’c’, ’d’, ’e’, ’f’]. Find a) list[1:3] b)list[:4] c) list[3:] .
>>> list = [’a’, ’b’, ’c’, ’d’, ’e’, ’f’]
>>> list[1:3]
[’b’, ’c’]
>>> list[:4]
[’a’, ’b’, ’c’, ’d’]
>>> list[3:]
[’d’, ’e’, ’f’]
Page 1 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
When a list to a function is passed, the function gets a reference to the list.
Passing a list as an argument actually passes a reference to the list, not a copy of the list.
Since lists are mutable, changes made to the elements referenced by the parameter change the
same list that the argument is referencing.
Example
def remove(a):
a.remove(1)
a=[1,2,3,4,5]
remove(a)
print(a)
output
[2,3,4,5]
Page 3 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
Page 4 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
18. How to create a list in python? Illustrate the use of negative indexing of list with example.
List is an ordered sequence of items. Values in the list are called elements / items. It can be
written as a list of comma-separated items (values) between square brackets [ ]. Items in the
lists can be of different data types.
Example:
>>> a = [1,2,3]
>>> print(a)
[1,2,3]
Negative Indexing
Python supports negative indexing for a sequence types like list. The last item on the list
takes the -1 index and second to the last item has the -2 index and so on.
>>> a = [1,2,3]
>>> print(a[-1])
3
>>> print(a[-2])
2
Page 5 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
Using the indexing operator (square brackets [ ]) on the left side of an assignment, one of the
list items can be updated.
Page 6 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit IV - 2 Marks Q & A
Page 7 of 7
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Page 1 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
7. What is an Exception?
Even if a program is syntactically correct, it may cause an error when an attempt is made
to execute it. Errors detected during execution time are called Exceptions and are not
unconditionally fatal.
Whenever a runtime error occurs, it creates an exception. The program stops execution
and prints an error message.
For example, dividing by zero creates an exception:
print(55/0)
Output:
ZeroDivisionError: integer division or modulo
8. What are the error messages that are displayed for the following exceptions?
a. Accessing a non-existent list item
b. Accessing a key that isn’t in the dictionary
c. Trying to open a non-existent file
Answer:
a. IndexError: list index out of range
b. KeyError: 'variablename' is not defined
c. IOError: [Errno 2] No such file or directory: 'filename'
11. How do you handle the exception when you try to open a non-existent file?
filename = input("Enter the file name : ")
try:
f = open (filename, "r")
except IOError:
print("There is no file named " , filename)
13. What is the function of raise statement? What are its two arguments?
The raise statement is used to raise an exception when the program detects an error.
It takes two arguments:
i. The exception type
ii. Specific information about the error.
Example:
raise NameError("Error Message")
Page 3 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Packages are namespaces that contain multiple packages and modules themselves. They
are simply directories.
Syntax:
from <mypackage> import <modulename>
19. What is the special file that each package in Python must contain?
Each package in Python must contain a special file called __init__.py
Files name __init__.py are used to mark directories on disk as Python package
directories.
The __init__.py file is usually empty, but can be used to export selected portions of the
package under more convenient name, hold convenience functions, etc.
21. How do you use command line arguments to give input to the program?
Python sys module provides access to any command-line arguments via sys.argv.
sys.argv is the list of command-line arguments.
len(sys.argv) is the number of command-line arguments.
Page 6 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
isdir() - used to check whether the specified path is an existing directory or not. This method
follows symbolic link, that means if the specified path is a symbolic link pointing to a
directory then the method will return True.
Page 7 of 8
Saranathan College of Engineering, Tiruchirappalli - 12
GE8151 - Problem Solving and Python Programming - Unit V - 2 Marks Q & A
Page 8 of 8
Saranathan College of Engineering, Tiruchirappalli - 12