Deekshith Xerox
Deekshith Xerox
MCQ
1. What is the first step in problem solving as per the document?
A) Testing the solution B) Coding the algorithm
C) Analyzing the problem D) Debugging the program
Answer: C) Analyzing the problem
2.What is an algorithm?
A) A set of exact steps to solve a problem B) A programming language
C) A type of computer hardware D) A method of debugging
Answer: A) A set of exact steps to solve a problem
3.What does the term "computerisation" refer to?
A) The use of computers to develop software for automating tasks
B) The physical components of a computer system
C) The process of debugging a program
D) The network connectivity of computers
Answer: A) The use of computers to develop software for automating tasks
4.Which of the following is NOT a characteristic of a good algorithm?
A) Precision B) Uniqueness C) Infinite steps D) Finiteness
Answer: C) Infinite steps
5.What does the process of 'coding' involve?
A) Writing an algorithm B) Testing the program D) Debugging the code
C) Converting the algorithm into a format understood by the computer
Answer: C) Converting the algorithm into a format understood by the computer
6.What shape is used to represent a process in a flowchart?
A) Parallelogram B) Rectangle C) Diamond D) Circle
Answer: B) Rectangle
7.In a flowchart, which symbol represents the start or end?
A) Diamond B) Rectangle C) Oval D) Parallelogram
Answer: C) Oval
8.What is the purpose of developing an algorithm before writing code?
A) To debug the program B) To convert it into machine language
C) To have a clear solution plan D) To test the program
Answer: C) To have a clear solution plan
9.Which step comes immediately after developing an algorithm?
A) Debugging B) Testing C) Coding D) Analyzing the problem
Answer: C) Coding
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
28. Which step in problemsolving involves identifying the logical steps to reach a solution?
A) Analyzing the problem B) Developing the algorithm
C) Coding D) Testing and debugging
Answer: B) Developing the algorithm
29. Which shape in a flowchart represents input or output data?
A) Oval B) Rectangle C) Parallelogram D) Diamond
Answer: C) Parallelogram
30. Why is uniqueness important in an algorithm?
A) To ensure each step is uniquely defined B) To make the algorithm run faster
C) To reduce the number of steps D) To avoid using highlevel language
Answer: A) To ensure each step is uniquely defined
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
2 Marks Questions
1. Define an algorithm.
An algorithm is a finite set of instructions or a step-bystep procedure for solving a
problem or performing a task.
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
2. What is pseudocode?
Pseudocode is a non-formal language that helps programmers to write algorithms. It is
a detailed description of instructions that a computer must follow in a particular order
and is intended for human reading.
3. Explain the term "flowchart."
A flowchart is a graphical representation of the steps in an algorithm or a process, using
various symbols to denote different types of actions or steps.
4. What are conditionals in programming?
Conditionals are statements that allow a program to take different actions based on whether a
certain condition is true or false. They are used to check possibilities and perform operations
depending on the outcome of the condition.
5. What is the purpose of a flowchart?
The purpose of a flowchart is to visually represent the sequence of steps in a process or
algorithm, making it easier to understand and analyze the flow of control and the logic of the
process.
6. Describe the term "sequence" in the context of algorithms.
In the context of algorithms, "sequence" refers to the execution of steps one after the other in
a specific order, without any branching or repetition.
7. What does iterative mean in algorithm design?
In algorithm design, "iterative" refers to the process of repeating a set of instructions a
certain number of times or until a specific condition is met. This repetition is known as a
loop.
8. Why is it important to verify an algorithm?
It is important to verify an algorithm to ensure that it correctly and efficiently solves the
problem it is intended to address and to confirm that it produces the correct output for all
possible inputs.
9. What is a loop in programming?
A loop in programming is a control structure that allows the repeated execution of a block of
code as long as a specified condition is met
10. What is the significance of decision making in algorithms?
Decision making in algorithms is significant because it allows the algorithm to choose
different paths of execution based on certain conditions, enabling it to handle a variety of
situations and inputs.
11. Write pseudocode to add two numbers.
Pseudocode for adding two numbers:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
8. Explain how to handle time addition in pseudocode when minutes exceed 60.
o Pseudocode:
INPUT hours1, minutes1
INPUT hours2, minutes2
total_minutes = minutes1 + minutes2
extra_hours = total_minutes / 60
remaining_minutes = total_minutes % 60
total_hours = hours1 + hours2 + extra_hours
PRINT total_hours, remaining_minutes
9. Discuss the significance of decision-making algorithms with a real-life example.
o Significance:
▪ Decision-making algorithms allow systems to choose between different actions based on
conditions.
▪ Example: In an e-commerce application, a recommendation system suggests products
based on user preferences and past purchases, improving user experience and sales.
10. What steps should be taken if an algorithm has multiple approaches?
o Steps:
▪ Analyze each approach for correctness and efficiency.
▪ Compare the approaches based on their time and space complexity.
▪ Test each approach with various inputs to identify the best one.
▪ Choose the most efficient and robust approach.
▪ Document the reasons for choosing that particular approach.
11. Explain the importance of verifying an algorithm with an example.
o Importance:
▪ Verifying ensures the algorithm produces correct results for all inputs.
▪ Example: In a sorting algorithm, verification involves checking that the output list is sorted
for different input lists, including edge cases like empty lists or lists with repeated
elements.
12. Describe the pseudocode to classify numbers as "Single Digit," "Double Digit," or
"Big."
o Pseudocode:
INPUT number
IF number >= 0 AND number <= 9 THEN
PRINT "Single Digit"
ELSE IF number >= 10 AND number <= 99 THEN
PRINT "Double Digit"
ELSE
PRINT "Big"
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
17. How do you write an algorithm to read the marks of three subjects and
calculate the aggregate?
o Algorithm:
STEP 1. Start
STEP 2. Input marks1, marks2, marks3
STEP 3. aggregate = marks1 + marks2 + marks3
STEP 4. Print aggregate
STEP 5. End
18. What is the significance of iterative steps in an algorithm?
o Significance:
▪ Iterative steps allow the repetition of certain actions until a condition is met.
▪ They help in simplifying complex problems by breaking them down into repetitive tasks.
▪ Improve the efficiency of algorithms by reducing redundancy.
▪ Essential for tasks like searching, sorting, and processing data in loops.
▪ Enable handling of large datasets and continuous processes.
19. Describe the process of designing an algorithm to add hours and minutes.
o Process:
STEP 1. Start
STEP 2. Input hours1, minutes1
STEP 3. Input hours2, minutes2
STEP 4. total_minutes = minutes1 + minutes2
STEP 5. extra_hours = total_minutes / 60
STEP 6. remaining_minutes = total_minutes % 60
STEP 7. total_hours = hours1 + hours2 + extra_hours
STEP 8. Print total_hours, remaining_minutes
STEP 9. End
5 Marks Questions (15 questions)
1. Write a detailed algorithm to determine the winner in a coin-flipping game.
o Algorithm:
STEP 1. Start
STEP 2. Input the number of coin flips (n)
STEP 3. Initialize counters: player1_wins = 0, player2_wins = 0
STEP 4. For each flip from 1 to n:
▪ Flip the coin and get the result (heads or tails)
▪ If the result is heads, increment player1_wins
▪ If the result is tails, increment player2_wins
STEP 5. If player1_wins > player2_wins, then:
▪ Print "Player 1 wins"
STEP 6. Else if player2_wins > player1_wins, then:
▪ Print "Player 2 wins"
STEP 7. Else:
▪ Print "It's a tie"
STEP 8. End
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
3. Describe the algorithm for adding hours and minutes, including the necessary
conditionals.
o Algorithm:
STEP 1. Start
STEP 2. Input hours1, minutes1
STEP 3. Input hours2, minutes2
STEP 4. total_minutes = minutes1 + minutes2
STEP 5. extra_hours = total_minutes / 60
STEP 6. remaining_minutes = total_minutes % 60
STEP 7. total_hours = hours1 + hours2 + extra_hours
STEP 8. If total_hours >= 24, then:
▪ total_hours = total_hours % 24
STEP 9. Print total_hours, remaining_minutes
STEP 10. End
4. Write the pseudocode and draw the flowchart for calculating the area and perimeter
of a rectangle.
o Pseudocode:
INPUT length
INPUT width
COMPUTE area = length * width
COMPUTE perimeter = 2 * (length + width)
PRINT area
PRINT perimeter
o Flowchart:
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
STEP 5. Example: If a sorting algorithm fails for an array containing negative numbers,
modify the algorithm to include handling of negative values, then test it with arrays
containing negative numbers.
9. Write a detailed pseudocode to calculate the factorial of a number.
o Pseudocode:
INPUT number
IF number == 0 THEN
factorial = 1
ELSE
factorial = 1
FOR i FROM 1 TO number
factorial = factorial * i
PRINT factorial
10. Describe the process of writing an algorithm that accepts only positive integers up
to 100.
o Process:
STEP 1. Start
STEP 2. Input number
STEP 3. If number < 1 or number > 100, then:
▪ Print "Invalid input"
STEP 4. Else:
▪ Print "Valid input"
STEP 5. End
11. Explain the steps involved in designing an algorithm for a coin-flipping game with
detailed conditionals.
o Steps:
STEP 1. Start
STEP 2. Input the number of flips
STEP 3. Initialize counters for heads and tails
STEP 4. For each flip:
▪ If heads, increment heads counter
▪ If tails, increment tails counter
STEP 5. If heads counter > tails counter, then:
▪ Print "Heads win"
STEP 6. Else if tails counter > heads counter, then:
▪ Print "Tails win"
STEP 7. Else:
▪ Print "It's a tie"
STEP 8. End
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
12. Write a detailed algorithm to print all multiples of 5 between 10 and 25.
o Algorithm: STEP 1. Start
STEP 2. For number from 10 to 25:
▪ If number % 5 == 0, then:
▪ Print number
STEP 3. End
13. Describe the process of writing an algorithm to read the marks of three subjects and
calculate the percentage.
o Process: STEP 1. Start
STEP 2. Input marks for subject1, subject2, and subject3
STEP 3. total_marks = subject1 + subject2 + subject3
STEP 4. percentage = (total_marks / 300) * 100
STEP 5. Print percentage
STEP 6. End
14. Explain the significance of iterative and branching steps in algorithm design with
examples.
o Significance:
▪ Iterative Steps: Allow repetitive actions, simplifying the process of handling multiple data
elements. Example: Looping through an array to calculate the sum of its elements.
▪ Branching Ste: Enable decision-making by allowing different actions based on conditions.
Exapsmple:
rative
steps reducUsing if-else statements to handle different cases in a grading system.
▪ Enhance Efficiency: Itee redundancy, while branching steps handle diverse scenarios,
making the algorithm robust and efficient.
▪ Improve Clarity: Clearly defined iterative and branching steps make the algorithm easier
to understand and maintain.
▪ Adapt to Complex Problems: Combine iterative and branching steps to solve complex
problems by breaking them into manageable parts.
15. Explain the steps involved in Problem Solving.
1. Analysing the Problem:
o Understand the problem thoroughly before seeking a
solution.
o Carefully read and analyze the problem statement.
o List the principal components and decide the core
functionalities of the solution.
o Identify the inputs and expected outputs.
2. Developing an Algorithm:
o Devise a solution plan represented in natural language, called an algorithm.
o Refine the algorithm until it captures all aspects of the desired solution.
o Select the most suitable algorithm if multiple solutions are possible.
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
3. Coding:
o Convert the finalized algorithm into a format understood by the computer using a high-
level programming language.
o Record coding procedures and document the solution for future reference and maintenance.
4. Testing and Debugging:
o Test the program to ensure it meets user requirements and responds within the expected
time.
o Verify that the program generates correct output for all possible inputs.
o Fix any syntactical or logical errors encountered during testing.
o Follow standard testing methods like unit, integration, system, and acceptance testing to
ensure the software meets all business and technical requirements.
o Debug the program to rectify any defects found during testing.
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Answer 2:
Start
Initialize player1_wins to 0
Initialize player2_wins to 0
For i from 1 to 5 do
Input flip_result
If flip_result is 1 then
Increment player1_wins
Else If flip_result is 2 then
Increment player2_wins
End If
If player1_wins is 3 then
Print "Player 1 wins the cake"
Exit
Else If player2_wins is 3 then
Print "Player 2 wins the cake"
Exit
End If
End For
End
Question 3: Write the pseudocode to print all multiples of 5 between 10 and 25
(including both 10 and 25).
Answer 3:
Start
For number from 10 to 25 do
If number mod 5 is 0 then
Print number
End If
End For
End
Question 4: Give an example of a loop that is to be executed a certain number of times.
Answer 4:
Start
For i from 1 to 10 do
Print "This is iteration number " + i
End For
End
Question 5: Suppose you are collecting money for something. You need ₹200 in all.
You ask your parents, uncles, and aunts as well as grandparents. Different people may
give either ₹10, ₹20, or even ₹50. You will collect till the total becomes 200. Write the
algorithm.
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Answer 5:
Start
Initialize total_collected to 0
While total_collected < 200 do
Input amount_given
If amount_given is 10 or 20 or 50 then
total_collected = total_collected + amount_given
End If
End While
Print "Total collected: " + total_collected
End
Question 6: Write the pseudocode to print the bill depending upon the price and
quantity of an item. Also print Bill GST, which is the bill after adding 5% of tax in the
total bill.
Answer 6:
Start
Input price
Input quantity
Set total_bill = price * quantity
Set gst = total_bill * 0.05
Set bill_gst = total_bill + gst
Print "Total Bill: " + total_bill
Print "Bill after GST: " + bill_gst
End
Question 7: Write pseudocode that will perform the following: a) Read the marks of
three subjects: Computer Science, Mathematics, and Physics, out of 100. b) Calculate
the aggregate marks. c) Calculate the percentage of marks.
Answer 7:
Start
Input computer_science_marks
Input mathematics_marks
Input physics_marks
Set aggregate_marks = computer_science_marks + mathematics_marks +
physics_marks
Set percentage_marks = (aggregate_marks / 300) * 100
Print "Aggregate Marks: " + aggregate_marks
Print "Percentage Marks: " + percentage_marks
End
Question 8: Write an algorithm to find the greatest among two different numbers
entered by the user.
Answer 8:
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Start
Input number1
Input number2
If number1 > number2 then
Print "The greatest number is " + number1
Else
Print "The greatest number is " + number2
End
Question 9: Write an algorithm that performs the following: Ask a user to enter a
number. If the number is between 5 and 15, write the word GREEN. If the number is
between 15 and 25, write the word BLUE. If the number is between 25 and 35, write
the word ORANGE. If it is any other number, write that ALL COLOURS ARE
BEAUTIFUL.
Answer 9:
Start
Input number
If number >= 5 and number <= 15 then
Print "GREEN"
Else If number > 15 and number <= 25 then
Print "BLUE"
Else If number > 25 and number <= 35 then
Print "ORANGE"
Else
Print "ALL COLOURS ARE BEAUTIFUL"
End
Question 10: Write an algorithm that accepts four numbers as input and find the
largest
and smallest of them.
Answer 10:
pseudocode
Copy code
Start
Input number1
Input number2
Input number3
Input number4
Set largest = number1
Set smallest = number1
If number2 > largest then
Set largest = number2
If number3 > largest then
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Question 14: Following is an algorithm for going to school or college. Can you suggest
improvements in this to include other options? Reach_School_Algorithm a) Wake up
b) Get ready c) Take lunch box d) Take bus e) Get off the bus f) Reach school or college
Answer 14:
Reach_School_Algorithm
a) Wake up
b) Get ready
c) Take lunch box
d) If going by bus then
Take bus
Get off the bus
Else If going by car then
Get in the car
Get out of the car
Else If walking then
Walk to school or college
End If
f) Reach school or college
Question 15: Write a pseudocode to calculate the factorial of a number. (Hint: Factorial
of 5, written as 5! = 5 × 4 × 3 × 2 × 1).
Answer 15:
Start
Input number
Set factorial = 1
For i from 1 to number do
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
factorial = factorial * i
End For
Print "Factorial of " + number + " is " + factorial
End
Question 16: Draw a flowchart to check whether a given number is an Armstrong
number. An Armstrong number of three digits is an integer such that the sum of the
cubes of its digits is equal to the number itself.
Answer 16:
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
c) Prompts the user to enter their name and stores it in the variable name
Answer: c) Prompts the user to enter their name and stores it in the variable name
28. Which of the following is an invalid variable name in Python?
a) var1 b) _var c) 1var d) var_1 Answer: c) 1var
29. What is the output of the following code: `print(10 / 3)`?
a) 3.3333333333333335 b) 3 c) 3.0 d) 10 Answer: a) 3.3333333333333335
30. Which of the following is NOT an arithmetic operator in Python?
a) + b) / c) * d) && Answer: d) &&
31. What is the correct way to create a string in Python?
a) `string s = "Hello"` b) `s = 'Hello'` c) `s = Hello`
d) `string s = 'Hello'` Answer: b) `s = 'Hello'`
32. Which of the following methods can be used to convert a string to a list in Python?
a) list( ) b) split( ) c) convert( ) d) str( ) Answer: b) split( )
33. Which of the following is NOT a valid Python data type?
a) List b) Tuple c) Dictionary d) Array Answer: d) Array
34. What is the output of the following code: `print("Hello" + " " + "World")`?
a) Hello World b) HelloWorld c) "Hello World" d) Hello + World
Answer: a) Hello World
35. Which operator is used for exponentiation in Python?
a) ^ b) ** c) exp( ) d) pow( ) Answer: b)
36. Which of the following is a mutable data type in Python?
a) String b) Tuple c) List d) Integer Answer: c) List
37. What is the output of the following code: `print(2**3)`?
a) 5 b) 6 c) 8 d) 9 Answer: c) 8
38. Which function is used to read input from the user in Python 3.x?
a) input( ) b) raw_input( ) c) scan( ) d) read( ) Answer: a) input( )
39. Which of the following statements will create a tuple in Python?
a) t = [1, 2, 3] b) t = {1, 2, 3} c) t = (1, 2, 3) d) t = 1, 2, 3 Answer: c) t = (1, 2, 3)
40. What is the output of the following code: `print(type([ ]))`?
a) <class 'tuple'> b) <class 'list'> c) <class 'set'> d) <class 'dict'> Answer: b) <class 'list'>
41. Which of the following operators is used for string concatenation in Python?
a) + b) & c) . d) concat() Answer: a) +
42. What is the correct way to declare a variable in Python?
a) var x = 5 b) x := 5 c) int x = 5 d) x = 5 Answer: d) x = 5
43. What will be the output of the following code: `print(10 % 3)`?
a) 1 b) 3 c) 10 d) 0.3 Answer: a) 1
44. Which method is used to remove an item from a list in Python by its value?
a) remove( ) b) pop( ) c) delete( ) d) discard( ) Answer: a) remove( )
45. What is the output of the following code: `print(5 == 5)`?
a) True b) False c) Syntax Error d) 5 Answer: a) True
46. Which method can be used to convert a list into a tuple in Python?
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Python being an interpreted language means that its programs are executed by an interpreter,
which processes each statement one by one.
11. How does Python handle case sensitivity?
Python is case-sensitive, meaning identifiers like NUMBER and number would be
considered different.
12. Mention any two features of Python.
Python is portable and platformindependent, and it has a rich library of predefined functions.
13. What is the extension of a Python script file?
The extension of a Python script file is .py.
14. Explain the interactive mode in Python.
In interactive mode, individual statements can be executed instantaneously, and results are
displayed immediately.
15. What is script mode in Python?
In script mode, a Python program is written in a file, saved, and then executed as a whole.
16. What are Python keywords?
Python keywords are reserved words that have specific meanings to the interpreter and
cannot be used as identifiers.
17. Define identifiers in Python.
Identifiers are names used to identify variables, functions, or other entities in a program.
18. What are variables in Python?
Variables in Python are names that refer to objects stored in memory.
19. How are comments added in Python?
Comments in Python are added using the # symbol; everything following the # till the end of
the line is treated as a comment.
20. What is an object in Python?
An object in Python is an instance of a data type that has a unique identity, attributes, and
behaviour.
21. What is implicit conversion?
Implicit conversion is the automatic conversion of one data type to another by the interpreter.
22. What is explicit conversion?
Explicit conversion is the manual conversion of one data type to another using functions like
int(), float(), str(), etc.
3-Mark Questions
1. Explain the process of program execution in Python using an interpreter.
o Python uses an interpreter to execute programs. The interpreter processes the program
statements one by one, translating and executing each statement sequentially until an error
is encountered or the entire program is executed. If an error occurs, execution stops. This
contrasts with a compiler, which translates the entire source code into object code before
execution .
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
8. Explain the concept of high-level and low-level programming languages and their
differences.
o High-level Languages: These languages are closer to human languages and abstract away
most of the hardware details. Examples include Python, Java, and C++.
o Low-level Languages: These languages are closer to machine code and provide little
abstraction from the hardware. Examples include Assembly and Machine Code.
o Ease of Use: High-level languages are easier to learn and use, while low-level languages
require more detailed knowledge of the hardware.
o Portability: High-level languages are generally more portable across different hardware
platforms, whereas low-level languages are specific to a particular type of hardware.
o Performance: Programs written in low-level languages can be more efficient and faster
but are more complex to write and maintain compared to high-level languages.
9. Discuss the use of Python in web development and provide examples of popular web
services built with Python.
o Frameworks: Python has powerful web frameworks like Django and Flask that simplify
web development.
o Backend Development: Python is widely used for server-side programming, handling
requests, and connecting to databases.
o APIs: Python is commonly used to develop RESTful APIs, facilitating
communication between different parts of a web service.
o Popular Web Services: Examples of popular web services built with Python include
Instagram, Pinterest, and Dropbox.
o Integration: Python can easily integrate with other technologies and services, making it a
versatile choice for web development.
10. Explain with examples the different types of operators available in Python.
o Arithmetic Operators: Used for mathematical operations, e.g., +
(addition), - (subtraction), * (multiplication), / (division).
x = 10 + 5 # 15
y = 10 - 5 # 5
o Comparison Operators: Used to compare values, e.g., == (equal to),
!= (not equal to), > (greater than).
a=5
b = 10
result = (a == b) # False
o Logical Operators: Used to combine conditional statements, e.g., and,
or, not.
x = True
y = False
result = x and y # False
o Assignment Operators: Used to assign values to variables, e.g., =, +=, -=.
a=5
a += 3 # 8
o Bitwise Operators: Used to perform bit-level operations, e.g., &
(AND), | (OR), ^ (XOR).
a = 10 # 1010 in binary
b = 4 # 0100 in binary result = a & b # 0000 (0 in decimal)
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
11. Discuss the role of Python in education and why it is a good choice for learning
programming.
o Ease of Learning: Python's simple and readable syntax makes it an excellent choice for
beginners.
o Versatility: Python can be used for various applications, from web development to data
science, providing a broad learning experience.
o Active Community: Python's large and active community offers extensive resources,
tutorials, and support for learners.
o Immediate Feedback: Python's interactive mode allows learners to test code snippets and
receive immediate feedback, facilitating learning.
o Real-world Applications: Learning Python provides practical skills that are in high
demand in the job market, enhancing career prospects.
12. Explain various types of Errors.
o Syntax Errors: Errors in the structure of the code, such as missing colons, parentheses, or
incorrect indentation.
o Runtime Errors: Errors that occur during program execution, such as division by zero or
accessing a variable that has not been defined.
o Logical Errors: Errors in the logic of the code that produce incorrect results, even though
the code runs without crashing.
EXERCISES (CHAPTER END QUESTIONS)
Question 1
Which of the following identifier names are invalid and why?
1. Serial_no.
2. 1st_Room
3. Hundred$
4. total-Marks
5. True
Answer:
• Identifier name can’t contain dot.
• 1st_Room: Invalid because identifiers cannot start with a digit.
• $ symbol is special character that can’t be part of an identifier name.
• total-Marks: Invalid because identifiers cannot contain hyphens (-).
• True: Invalid because True is a keyword in Python and cannot be used as an identifier.
Question 2
Write the corresponding Python assignment statements:
1. Assign 10 to variable length and 20 to variable breadth.
2. Assign the average of values of variables length and breadth to a variable sum.
3. Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable stationery.
4. Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first,
middle and last.
5. Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately between
different parts of names.
Answer:
1. length = 10
breadth = 20
2. sum = (length + breadth) / 2
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
num1, num2 = 2, 3
num3, num2 = num1, num3 + 1
print(num1, num2, num3)
Answer:
1. 2 5
2. 6 4
3. This code will result in an error because num3 is not defined.
Question 6
Which data type will be used to represent the following data values and why?
1. Number of months in a year
2. Resident of Delhi or not
3. Mobile number
4. Pocket money
5. Volume of a sphere
6. Perimeter of a square
7. Name of the student
8. Address of the student
Answer:
1. int - The number of months is a whole number.
2. bool - It is a binary state (True or False).
3. str - Mobile numbers can contain leading zeros and are not used for calculations.
4. float - Pocket money can have decimal values.
5. float - Volume can be a decimal value.
6. float - Perimeter can be a decimal value.
7. str - Names are textual data.
8. str - Addresses are textual data.
Question 7
Give the output of the following when num1 = 4, num2 = 3, num3 = 2:
1. += num2 + num3
print(num1)
2. num1 = num1 ** (num2 + num3)
print(num1)
3. num1 **= num2 + num3
4. num1 = '5' + '5'
print(num1)
5. print(4.00 / (2.0 + 2.0))
6. num1 = 2 + 9 * ((3 * 12) - 8) / 10
print(num1)
7.
num1 = 24 // 4 // 2
print(num1)
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
8. num1 = float(10)
print(num1)
9. num1 = int('3.14')
print(num1)
10. print('Bye' == 'BYE')
11. print(10 != 9 and 20 >= 20)
12. print(10 + 6 * 2 ** 2 != 9 // 4 - 3 and 29 >= 29 / 9)
13. print(5 % 10 + 10 < 50 and 29 <= 29)
14. print((0 < 6) or (not(10 == 6) and (10 < 0)))
Answer:
1. 9
2. 4096
3. 4096
4. 55
5. 1.0
6. 25.4
7. 3
8. 10.0
9. Error (cannot convert '3.14' to an integer)
10. False
11. True
12. True
13. True
14. True
Question 8
Categorise the following as syntax error, logical error or runtime error:
1. 25 / 0
2. num1 = 25; num2 = 0; num1/num2
Answer:
1. Runtime error (division by zero)
2. Runtime error (division by zero)
Question 9
A dartboard of radius 10 units and the wall it is hanging on are represented using a two-
dimensional coordinate system, with the board’s center at coordinate (0,0). Variables x and
y store the x-coordinate and the y-coordinate of a dart that hits the dartboard. Write a
Python expression using variables x and y that evaluates to True if the dart hits (is within)
the dartboard, and then evaluate the expression for these dart coordinates:
1. (0,0)
2. (10,10)
3. (6, 6)
4. (7,8)
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
Answer:
(x**2 + y**2) <= 100
1. True (0,0 is at the center)
2. False (10,10 is outside the dartboard)
3. True (6,6 is within the dartboard)
4. True (7,8 is within the dartboard)
Question 10
Write a Python program to convert temperature in degree Celsius to degree Fahrenheit. If
water boils at 100 degree C and freezes at 0 degree C, use the program to find out what is
the boiling point and freezing point of water on the Fahrenheit scale.
Answer:
boiling_point_C = 100
freezing_point_C = 0
# Conversion formula: F = C * 9/5 + 32
boiling_point_F = boiling_point_C * 9/5 + 32
freezing_point_F = freezing_point_C * 9/5 + 32
print(f"Boiling point of water: {boiling_point_F}°F")
print(f"Freezing point of water: {freezing_point_F}°F")Output:
Boiling point of water: 212.0°F
Freezing point of water: 32.0°F
Question 11
Write a Python program to calculate the amount payable if money has been lent
on simple interest. Principal or money lent = P, Rate of interest = R% per annum
and Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable
= Principal + SI. P, R and T are given as input to the program.
Answer:
P = float(input("Enter the principal amount: "))
R = float(input("Enter the rate of interest: "))
T = float(input("Enter the time in years: "))
SI = (P * R * T) / 100
amount_payable = P + SI
print(f"Simple Interest: {SI}")
print(f"Amount Payable: {amount_payable}")
Question 12
Write a program to calculate in how many days a work will be completed by three
persons A, B and C together. A, B, C take x days, y days and z days respectively
to do the job alone. The formula to calculate the number of days if they work
together is xyz/(xy + yz + xz) days where x, y, and z are given as input to the program.
Answer:
x = float(input("Enter the number of days A takes to complete the work: "))
y = float(input("Enter the number of days B takes to complete the work:"))
DILIP KUMAR H A
PRAGATHI CO-ED PU COLLEGE, Vijayapura INTRODUCTION TO PROBLEM SOLVING
DILIP KUMAR H A