You are on page 1of 13

GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

GE3151 - PROBLEM SOLVING AND PYTHON PROGRAMMING


QUESTION BANK
SYLLABUS
UNIT I COMPUTATIONAL THINKING AND PROBLEM SOLVING 9
Fundamentals of Computing – Identification of Computational Problems -Algorithms, building blocks of
algorithms ( statements, state, control flow, functions), notation (pseudo code, flow chart, programming
language), algorithmic problem solving, simple strategies for developing algorithms (iteration, recursion).
Illustrative problems: find minimum in a list, insert a card in a list of sorted cards, guess an integer number in a
range, Towers of Hanoi.
UNIT II DATA TYPES, EXPRESSIONS, STATEMENTS 9
Python interpreter and interactive mode, debugging; values and types: int, float, boolean, string
and list; variables, expressions, statements, tuple assignment, precedence of operators, comments; Illustrative
programs: exchange the values of two variables, circulate the values of n variables, distance between two
points.
UNIT III CONTROL FLOW, FUNCTIONS, STRINGS 9
Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-
else); Iteration: state, while, for, break, continue, pass; Fruitful functions: return values, parameters, local and
global scope, function composition, recursion; Strings:string slices, immutability, string functions and methods,
string module; Lists as arrays. Illustrative programs: square root, gcd, exponentiation, sum an array of numbers,
linear search, binary search.
UNIT IV LISTS, TUPLES, DICTIONARIES 9
Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters;
Tuples: tuple assignment, tuple as return value; Dictionaries: operations and methods;advanced list processing
- list comprehension; Illustrative programs: simple sorting, histogram, Students marks statement, Retail bill
preparation.
UNIT V FILES, MODULES, PACKAGES 9
Files and exceptions: text files, reading and writing files, format operator; command line arguments, errors
and exceptions, handling exceptions, modules, packages; Illustrative programs: word count, copy file, Voter’s
age validation, Marks range validation (0-100).
TOTAL : 45 PERIODS
TEXT BOOKS:
1. 1. Allen B. Downey, “Think Python: How to Think like a Computer Scientist”, 2nd Edition, O’Reilly
Publishers, 2016.
2. Karl Beecher, “Computational Thinking: A Beginner's Guide to Problem Solving and Programming”,
1st Edition, BCS Learning & Development Limited, 2017.
REFERENCES:
1. Paul Deitel and Harvey Deitel, “Python for Programmers”, Pearson Education, 1st Edition, 2021.
2. G Venkatesh and Madhavan Mukund, “Computational Thinking: A Primer for Programmers and Data
Scientists”, 1st Edition, Notion Press, 2021.
3. John V Guttag, "Introduction to Computation and Programming Using Python: With Applications to
Computational Modeling and Understanding Data”, Third Edition, MIT Press, 2021
4. Eric Matthes, “Python Crash Course, A Hands - on Project Based Introduction to Programming”, 2nd
Edition, No Starch Press, 2019.
5. Martin C. Brown, “Python: The Complete Reference”, 4th Edition, Mc-Graw Hill, 2018.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

UNIT I - ALGORITHMIC PROBLEM SOLVING

PART- A (2 Marks)
1. What is an algorithm?(Jan-2018)
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 three numbers.


ALGORITHM : Find Minimum of three numbers
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A,B and A,C. If A is minimum, perform step 4 else perform step 5.
Step 4:Compare B and C. If B is minimum, output “B is minimum” else output “C is minimum”.
Step 5: Stop

3. List the building blocks of algorithm.


The building blocks of an algorithm are
• Statements
• Sequence
• Selection or Conditional
• Repetition or Control flow
• Functions
An action is one or more instructions that the computer performs in sequential order (from first to last). A
decision is making a choice among several actions. A loop is one or more instructions that the computer
performs repeatedly.

4. Define statement. List its types.


The instructions in Python, or indeed in any high-level language, are designed as components for algorithmic
problem solving, rather than as one-to-one translations of the underlying machine language instruction set
of the computer. 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 pseudocode 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
COMPUTE 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?


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. Write the algorithm to calculate the average of three numbers and display it.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

Step 1: Start
Step 2: Read values of X,Y,Z
Step 3: S = X+Y+Z
Step 4: A = S/3
Step 5: Write value of A
Step 6: Stop
8. Give the rules for writing Pseudocode.
• Write one statement per line.
• Capitalize initial keywords.
• Indent to show hierarchy.
• End multiline structure.
• Keep statements language independent.

9. What is a function?
Functions are named sequence of statements 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.

10. Give the difference between flowchart and pseudocode.


Flowchart Pseudocode
• A flowchart is a diagram showing an • Pseudocode is a means of expressing the
overview of the problem. stepwise instructions for solving a
• It is a pictorial representation of how problem without worrying about the
the program will work, and it follows syntax of a particular programming
a standard format. language.
• It uses different kinds of shapes to • Unlike a flowchart, it uses a written
signify different processes involved in format which requires no absolute rules
the problem for writing.
• It can be written in ordinary English, and
we can use some keywords in it too.
11. 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.

12. Give an example of iteration.


a=0
for i in range(4): # i takes the value 0,1,2,3
a=a+i
print(a) #number 6 is printed (0+0;0+1;1+2;3+3)

13. Write down the rules for preparing a flowchart.


While drawing a flowchart, some rules need to be followed:
• 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.

14. List the categories of Programming languages.


GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

Programming Languages are divided into the following categories:


• Interpreted
• Functional
• Compiled
• Procedural
• Scripting
• Markup
• Logic-Based
• Concurrent
• Object-Oriented Programming Languages.
15. Mention the characteristics of 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

16. List out the simple strategies 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. Compare machine language, assembly language and high-level language.


Machine Language Assembly Language High-Level Language

• The language of 0s and 1s is • It is low level • High level languages are


called as machine language. programming language in English like statements and
• The machine language is which the sequence of 0s programs .
system independent because and 1s are replaced by • Written in these languages are
there are different set of mnemonic (ni-monic) needed to be translated into
binary instruction for codes. machine language before to
different types of computer • Typical instruction for their execution using a system
systems addition and subtraction software compiler.
18. Describe algorithmic problem solving.
Algorithmic Problem Solving deals with the implementation and application of algorithms to a variety of
problems. When solving a problem, choosing the right approach is often the key to arriving at the best
solution.

19. Give the difference between recursion and iteration.


Recursion Iteration
Function calls itself until the base condition is Repetition of process until the condition fails.
reached.
In recursive function, base case (terminate Iterative approach involves four steps:
condition) and recursive case are specified. initialization, condition, execution and
updation.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

Recursion is slower than iteration due to overhead Iteration is faster.


of maintaining stack.
Recursion takes more memory than iteration due Iteration takes less memory.
to 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 simpler Recursive calls are expensive (inefficient) as
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.

21. Write an algorithm to accept two numbers, compute the sum and print the result.(Jan-2018)
Step1: Read the two numbers a and b.
Step 2: Calculate sum = a+b
Step 3: Display the sum
22. Write an algorithm to find the sum of digits of a number and display it.
Step 1: Start
Step 2: Read value of N
Step 3: Sum = 0
Step 4: While (N != 0)
Rem = N % 10
Sum = Sum + Rem
N = N / 10
Step 5: Print Sum
Step 6: Stop

23. Write an algorithm to find the square and cube and display it.
Step 1: Start
Step 2: Read value of N
Step 3: S =N*N
Step 4: C =S*N
Step 5: Write values of S,C
Step 6: Stop

24. Explain Tower of Hanoi.


The Tower of Hanoi is a mathematical game. It consists of three rods and a number of disks of different
sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of
size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
• Only one disk can be moved at a time.
• Each move consists of taking the upper disk from one of the stacks and placing it on top of another
stack.
• No disk may be placed on top of a smaller disk.
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a
Tower of Hanoi puzzle is 2n − 1, where n is the number of disks.
25. What is recursion?
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller
subproblems until you get to a small enough problem that it can be solved trivially. Usually recursion
involves a function calling itself. While it may not seem like much on the surface, recursion allows us to
write elegant solutions to problems that may otherwise be very difficult to program.
Example:
defcalc_factorial(x):
if x == 1:
return 1
else:
return (x * calc_factorial(x-1))
num = 4
print("The factorial of", num, "is", calc_factorial(num))
26. Write an algorithm to find minimum in a list. (Jan-2019)
ALGORITHM : To find minimum in a list
Step 1: Start
Step 2: Read the list
Step 3: Assume the first element as minimum
Step 4: Compare every element with minimum. If the value is less than minimum, reassign that value as
minimum.
Step 5: Print the value of minimum.
Step 6: Stop
27. Distinguish between algorithm and program. (Jan-2019)
Algorithm Program

Algorithm is the approach / idea to solve some A program is a set of instructions for the computer
problem. to follow.

It does not have a specific syntax like any of the It is exact code written for problem following all the
programming languages rules (syntax) of the programming language.

It cannot be executed on a computer. It can be executed on a computer

28. List the Symbols used in drawing the flowcart. (May 2019)
Flowcharts are usually drawn using some standard symbols
• Terminator
• Process
• Decision
• Connector
• Data
• Delay
• Arrow
29. Give the P to find the minimum among the list of 10 numbers. (May 2019)
numList = []
Read n
for i in range(0, n):
Read x[i]

maxNum = numList[0]
for i in numList:
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

if i > maxNum:
maxNum = i
print Maximum Element of the Given List is maxNum

30. How will you analysis the efficiency of an algorithm? (Nov / Dec 2019)
Time efficiency, indicating how fast the algorithm runs.
Space efficiency, indicating how much extra memory it uses

31. How do algorithm, flowchart and pseudo code use for problem solving? (Nov / Dec 2019)
Algorithm: A process or set of rules to be followed in calculations or other problem – solving operations,
especially by a computer
Flowchart: Flowchart is diagrammatic representation of the algorithm.
Pseudo code: In Pseudo code normal English language is translated into the programming languages to be
worked on.
All are tools for problem solving independent of programming language. Difference is only in the way of
representing the solution.

PART B (16 MARKS)\

1. Explain various generations of computers with features? (FEB 2009/FEB 2010).


Ans:
Generation of Computers
Each phase of computer development is known as a separate generation of computers. The computer can be
classified into four generations according to their type of electronic circuits such as vacuum tube, transistor, IC
etc.

(a) The First-Generation Computers (1949-55)


Main Features:
1) The computers of this generation used vacuum tubes.
2) These computers used machine language for giving instructions.
3) They used the concept of stored program.
4) These computers were 5000 times faster than the MARK-I.
5) The first-generation computers were welcomed by Government and Universities.
Limitations:
1) These computers were very big in size. The ENIAC machine was 30 x 50 feet in size and 30 tons in weight.
So, these machines required very large space for their workings.
2) Their power consumption was very high.
3) These computers had slow operating speed and small computing capacity.
4) These computers had a very small memory.

(b) The Second-Generation Computers (1956-65)


Main Features:
1) The computers of this generation replaced vacuum tubes with transistors.
2) Magnetic cores were invented for storage.
3) Different magnetic storage devices were developed in this generation.
4) Commercial applications were developed during this period. Eighty percent of these computers were used in
business and industries.

(c) Third Generation Computers (1966-75)


Main Features:
· The third-generation computers replaced transistors with’ Integrated Circuits’. These Integrated Circuits are
also known as chips.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022
· The size of main memory was increased and reached about 4 megabytes.
· Magnetic disk technology had been improved and drive having capacity upto 100 MBPS came into existence.
· The CPU becomes more powerful with the capacity of carrying out 1 million
instructions per second.
· This generation computers were relatively inexpensive and faster.
· The application area also increased in this generation. The computers were used in other areas like education,
small businesses survey, analysis along with their previous usage areas.

(d) The Fourth Generation Computers (1976-Present)


Main Features:
• The fourth-generation computers replaced small scale integrated circuits and medium scale integrated
circuits with the microprocessors chip.
• Semiconductor memories replaced magnetic core memories.
• The hard-disks are available of the sizes upto 200 GB. The RAID technology (Redundant Array of
Inexpensive Disks) gives storage upto thousands of GB.
• Computer cost came down rapidly in this generation.
• Application of computers is increased in various areas like visualization,
parallel computing, multimedia etc.

(e) The Fifth Generation Computers

• Mankind along with the advancement in science and technology is working hard to bring the Vth
Generation of computer.

• These computers will have the capability of thinking on their own like a man with the help of Artificial
Intelligence (AI), the 21st century will be better, faster, smaller and smarter computers.

2. Explain the fundamental units of a computer with a block diagram?


(Or)
Explain the basic computer organization in detail? (JAN 2009\MAY 2009)
Ans:
A computer can process data, pictures, sound and graphics. They can solve highly complicated problems
quickly and accurately.

Input Unit:
Computers need to receive data and instruction in order to solve any problem. Therefore, we need to input
the data and instructions into the computers. The input unit consists of oneor more input devices. Keyboard is
the one of the most commonly used input devices. Other commonly used input devices are the mouse, floppy
disk drive, magnetic tape, etc. All the input devices perform the following functions.

• Accept the data and instructions from the outside world.

• Convert it to a form that the computer can understand.

• Supply the converted data to the computer system for further processing.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022
Block Diagram of Computer

Storage Unit:
The storage unit of the computer holds data and instructions that are entered through the input unit, before
they are processed. It preserves the intermediate and final results before these are sent to the output devices. It
also saves the data for the later use.

Types of Storage Devices:


1. Primary Storage:
• Stores and provides very fast.
• This memory is generally used to hold the program being currently executed in the computer,
the data being received from the input unit, the intermediate and final resultsof the program.
• The primary memory is temporary in nature. The data is lost, when the computer is switched
off.
• In order to store the data permanently, the data has to be transferred to the secondary
memory. The cost of the primary storage is more compared to the secondary storage.

2. Secondary Storage:
• It stores several programs, documents, data bases etc.
• The programs that run on the computer are first transferred to the primary memory
before it is actually run.
• Whenever the results are saved, again they get stored in the secondary memory.
• The secondary memory is slower and cheaper than the primary memory. Some of the
commonly used secondary memory devices are Hard disk, CD, etc.,

Memory Size:
All digital computers use the binary system, i.e., 0’s and 1’s. Each character or a number is represented by
an 8-bit code. The set of 8 bits is called a byte. A Character occupies 1 byte space. A numeric occupies 2-
byte space. Byte is the space occupied in the memory. The size of the primary storage is specified in KB
(Kilobytes) or MB (Megabyte). One KB is equal to 1024 bytes and one MB is equal to 1000KB. The size of the
primary storage in a typical PC usually starts at 16MB. PCs having 32 MB, 48MB, 128 MB, 256MB memory are
quite common.
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

Output Unit:

The output unit of a computer provides the information and results of a computation to outside world.
Printers, Visual Display Unit (VDU) are the commonly used output devices. Othercommonly used output devices
are floppy disk drive, hard disk drive, and magnetic tape drive.

Arithmetic Logical Unit:

All calculations are performed in the Arithmetic Logic Unit (ALU) of the computer. It also does
comparison and takes decision. The ALU can perform basic operations such as addition, subtraction,
multiplication, division, etc and does logic operations viz, >, <, =, ‘etc. Whenever calculations are required, the
control unit transfers the data from storage unit to ALU once the computations are done, the results are
transferred to the storage unit by the control unit and then it is send to the output unit for displaying results.

Control Unit:
It controls all other units in the computer. The control unit instructs the input unit, where to store the
data after receiving it from the user. It controls the flow of data and instructions from the storage unit to
ALU. It also controls the flow of results from the ALU to the storage unit. The control unit is generally
referred as the central nervous system of the computer that control and synchronizes it’s working.

Central Processing Unit:


The control unit and ALU of the computer are together known as the Central Processing Unit (CPU).
The CPU is like brain performs the following functions:
• It performs all calculations.
• It takes all decisions.
• It controls all units of the computer.
A PC may have CPU-IC such as Intel 8088, 80286, 80386, 80486, Celeron, Pentium, Pentium Pro, Pentium
II, Pentium III, Pentium IV, Dual Core, and AMD etc.

3. What are the building blocks of an algorithm? Explain in detail.


Ans: Refer Unit 1 Notes Page No. 17 to 20

4. Briefly describe iteration and recursion. Explain with algorithm.


Ans: Refer Unit 1 Notes Page No. 34 to 36

5. Explain Algorithmic problem solving.


Ans: Refer Unit 1 Notes Page No. 32 to 34

6. Write an algorithm and draw a flowchart to calculate 24.


GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

7. Illustrate Algorithm, Pseudocode and Flowchart with an example.


Ans: Refer Unit 1 Notes Page No. 36 to 45

8. (a) Describe Pseudocode with its guidelines.


Ans: Refer Unit 1 Notes Page No. 20 to 2z
(b) Give an example for pseudocode.
Ans: Refer Unit 1 Notes Page No. 20 to 23
(c) Write the pseudocode for Towers of Hanoi.
Ans: Refer Unit 1 Notes Page No. 44 to 45

9. (a) What is flowchart?


Ans: Refer Unit 1 Notes Page No. 24 to 29

(b) List down symbols and rules for writing flowchart.


Ans: Refer Unit 1 Notes Page No. 24 to 29

(c) Draw a flowchart to count and print from 1 to 10.

10. (a) Write a program to insert a card in a list of sorted cards. (Jan-2019)
Ans: Refer Unit 1 Notes Page No. 36 to 45
b) Write a program to find the minimum number in a list.
Ans: Refer Unit 1 Notes Page No. 36 to 45

11. a) Draw a flow chart to accept three distinct numbers, find the greatest and print the result. (8) (Jan-2018)
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

c) Draw a flow chart to find the sum of the series 1+2+3+……+100. (8) (Jan-2018)

12. Outline the Towers of Hanoi problem. Suggest a solution to the Towers of Hanoi problem with relevant
diagrams. (16) (Jan-2018)
Ans: Refer Unit 1 Notes Page No. 42 to 45

13. Identify simple strategies for developing an algorithm. (Jan-2019)


Ans: Refer Unit 1 Notes Page No. 34 to 36

14. (a) What is an algorithm? Summaries the characteristics of a good algorithm. (8) (May 2019)
Ans: Refer Unit 1 Notes Page No. 16 to 17

(b) Outline the algorithm for displaying the first n odd numbers. (May 2019)
GE3151 – Problem Solving and Python Programming Common to all Branches 2021-2022

15. (a) What is a programming language? What are its types? Explain them in detail with their advantages and
disadvantages. (8) (Nov / Dec 2019)
Ans: Refer Unit 1 Notes Page No. 29 to 31

(b) Write a function find index (), which returns the index of a number in the Fibonacci sequence, if the
number is an element of this sequence and returns -1, if the number is not contained in it, call this function
using user input and display the result. (8) (Nov / Dec 2019)
Ans:

16. (a) What is recursive function? What are its advantages and disadvantages? Compare it with iterative
function. (6) (Nov / Dec 2019)
Ans: Refer Unit 1 Notes Page No. 35 to 36

(b) Implement a recursive function in python for the sieve of Eratosthenes. The sieve of Eratosthenes is a
simple algorithm for finding all prime numbers up to a specified integer. It was created by the ancient Greek
mathematician Eratosthenes. The algorithm to find all the prime numbers less than or equal to agiven
integer n: (10) (Nov / Dec 2019)

1) Create a list of integers from two to n: 2,3,4,…, n


2) Start with a counter i set to 2, i.e. the first prime number
3) Starting from i+1, count up by I and remove those numbers from the list, i.e. 2*i,3*i, 4*i,..
4) Find the first number of the list following i. This is the next prime number.
5) Set i to the number found in the previous step.
6) Repeat steps 3 and 4 until i is greater than n. (As an improvement: It’s enough to go to the square root
of n)
7) All the numbers, which are still in the list, are prime numbers.

You might also like