Professional Documents
Culture Documents
i Pu Cs Chapter 4 Q&A-1
i Pu Cs Chapter 4 Q&A-1
VSA SA LA E TOTAL
MCQ
2. What is an algorithm?
- D) A method of debugging
- A) Precision
- B) Uniqueness
- C) Infinite steps
- D) Finiteness
2
5. What does the process of 'coding' involve?
- A) Writing an algorithm
- A) Parallelogram
- B) Rectangle
- C) Diamond
- D) Circle
- Answer: B) Rectangle
- A) Diamond
- B) Rectangle
- C) Oval
- D) Parallelogram
3
- Answer: C) Oval
8. What is the purpose of developing an algorithm before writing code?
- A) Debugging
- B) Testing
- C) Coding
- Answer: C) Coding
12. Which of the following is an essential skill for a computer science student?
- A) Problem-solving
- B) Debugging
- C) Coding
- A) Input data
- B) A set of instructions
5
- C) The result of the process
- D) A debugging report
- A) Process
- B) Decision
- C) Input/Output
PRIMUS PU COLLEGE, BANGALORE
- D) Start/End
- Answer: C) Input/Output
15. Which of the following steps is NOT part of the problem-solving process
mentioned in the textbook?
- B) Developing an algorithm
- C) Coding
- Answer: C) Coding
7
19. Which of the following is a key component of analyzing a problem?
- A) Writing the program code
- A) Process
- B) Decision
- C) Input/Output
- D) Start/End
- Answer: B) Decision
8
22. Which programming language can be used to write a program after
developing an algorithm?
- A) Only Python
- C) Only Java
- D) Only C++
28. Which step in problem-solving involves identifying the logical steps to reach
a solution?
- A) Oval
- B) Rectangle
- C) Parallelogram
- D) Diamond
- Answer: C) Parallelogram
- A) Oval
- B) Rectangle
- C) Diamond
- D) Parallelogram
12
- Answer: C) Diamond
33. What should be the next step if an algorithm is not producing the correct
output?
- A) Decision
- B) Input/Output
- C) Start/End
- D) Process
- Answer: D) Process
- C) Coding
- D) Testing
- Answer: D) Testing
- C) Producing a result
16
44. Why is it important to develop an algorithm before coding?
- A) To reduce the size of the program
- A) Rectangle
- B) Parallelogram
- C) Oval
- D) Diamond
- Answer: C) Oval
17
47. Which of the following best describes 'uniqueness' in an algorithm?
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
20
12. Define "space complexity" in algorithm analysis. Space complexity in
algorithm analysis refers to the amount of memory space an algorithm needs to
run to completion, taking into account both the fixed and variable space
requirements.
13. Define "time complexity" in algorithm analysis. Time complexity in
algorithm analysis refers to the amount of time an algorithm takes to run as a
function of the length of the input. It helps to estimate the efficiency of the
algorithm.
14. What are the components of a flowchart? The components of a flowchart
5. Else
▪ Print "number2 is greater"
6. End
2. Explain the importance of pseudocode before actual coding.
o Importance of Pseudocode:
▪ Helps in planning and organizing thoughts.
▪ Ensures all steps of the algorithm are considered.
▪ Easy to read and understand.
▪ Bridges the gap between human logic and programming
language syntax.
▪ Facilitates debugging by identifying logical errors before
coding.
3. Describe the process of writing a flowchart for a given algorithm.
o Process of Writing a Flowchart:
▪ Identify the major steps in the algorithm.
▪ Use standard flowchart symbols (ovals for start/end, rectangles
for processes, diamonds for decisions).
22
▪ Connect the symbols with arrows to show the flow of control.
▪ Ensure the flowchart is logically correct and covers all scenarios.
▪ Review and refine the flowchart for accuracy and clarity.
4. How can you improve an algorithm that fails for certain inputs?
o Improving an Algorithm:
▪ Identify the inputs that cause the algorithm to fail.
▪ Analyze the algorithm to understand why it fails for those inputs.
▪ Modify the algorithm to handle those specific cases.
▪ Test the algorithm with a variety of inputs to ensure robustness.
▪ Optimize the algorithm to improve efficiency and performance.
5. Write pseudocode for calculating the area and perimeter of a rectangle.
INPUT length
INPUT width
COMPUTE area = length * width
COMPUTE perimeter = 2 * (length + width)
PRINT area
PRINT perimeter
7. End
8. Explain how to handle time addition in pseudocode when minutes exceed
60.
o Pseudocode:
typescript
Copy code
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" 25
6. Print bill
7. End
14. What improvements can be made to an algorithm that only accepts positive
integers up to 100?
o Improvements:
▪ Add input validation to check if the input is within the range.
▪ Provide feedback to the user if the input is out of range.
▪ Optimize the algorithm to handle edge cases.
▪ Ensure the algorithm handles different data types correctly.
▪ Include error handling to manage invalid inputs gracefully.
15. Explain the algorithm for determining the winner in a coin-flipping game.
o Algorithm:
1. Start
2. Input number_of_flips
3. Initialize heads_count and tails_count to 0
4. For each flip, do
▪ If the result is heads, increment heads_count
26
▪ Else, increment tails_count
5. If heads_count > tails_count, then
▪ Print "Heads win"
6. Else if tails_count > heads_count, then
▪ Print "Tails win"
7. Else
▪ Print "It's a tie"
8. End
16. Write the pseudocode to print all multiples of 5 between 10 and 25.
o Pseudocode:
17. How do you write an algorithm to read the marks of three subjects and
calculate the aggregate?
o Algorithm:
1. Start
2. Input marks1, marks2, marks3
3. aggregate = marks1 + marks2 + marks3
4. Print aggregate
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.
27
▪ 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:
1. Start
2. Input hours1, minutes1
3. Input hours2, minutes2
4. total_minutes = minutes1 + minutes2
PRIMUS PU COLLEGE, BANGALORE
5. extra_hours = total_minutes / 60
6. remaining_minutes = total_minutes % 60
7. total_hours = hours1 + hours2 + extra_hours
8. Print total_hours, remaining_minutes
9. End
INPUT length
INPUT width
PRIMUS PU COLLEGE, BANGALORE
o Flowchart:
30
PRIMUS PU COLLEGE, BANGALORE
5. Explain the importance of verifying algorithms with multiple examples.
o Importance:
▪ Ensures Correctness: Verification confirms that the algorithm
produces the correct output for various inputs.
▪ Identifies Edge Cases: Helps in discovering and handling edge
cases that might not be apparent initially.
▪ Improves Reliability: Builds confidence in the algorithm's
reliability and robustness.
▪ Enhances Understanding: Multiple examples provide a deeper 31
understanding of how the algorithm works.
▪ Reduces Errors: Early detection of errors prevents issues later in
the development process.
6. Write a detailed algorithm to classify numbers as "Single Digit," "Double
Digit," or "Big."
o Algorithm:
1. Start
2. Input number
3. If number >= 0 and number <= 9, then:
▪ Print "Single Digit"
PRIMUS PU COLLEGE, BANGALORE
32 6. Print bill
7. End
8. Explain the process of improving an algorithm that fails for certain inputs.
o Process:
1. Identify the inputs causing the failure.
2. Analyze the algorithm to understand why it fails for these inputs.
3. Modify the algorithm to handle these specific cases.
4. Test the modified algorithm with a variety of inputs.
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.
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:
1. Start
2. Input number
3. If number < 1 or number > 100, then:
▪ Print "Invalid input"
33
4. Else:
▪ Print "Valid input"
5. End
11. Explain the steps involved in designing an algorithm for a coin-flipping
game with detailed conditionals.
o Steps:
1. Start
2. Input the number of flips
3. Initialize counters for heads and tails
4. For each flip:
▪ If heads, increment heads counter
PRIMUS PU COLLEGE, BANGALORE
35
PRIMUS PU COLLEGE, BANGALORE
37
EXERCISES (CHAPTER END QUESTIONS)
Question 1: Write pseudocode that reads two numbers and divides one by another and
displays the quotient.
Answer 1:
Start
Input number1
Input number2
If number2 is not 0, then
PRIMUS PU COLLEGE, BANGALORE
Question 2: Two friends decide who gets the last slice of a cake by flipping a coin five
times. The first person to win three flips wins the cake. An input of 1 means player 1
wins a flip, and a 2 means player 2 wins a flip. Design an algorithm to determine who
takes the cake?
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
38 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
Answer 3:
Start
For number from 10 to 25 do
If number mod 5 is 0 then
Print number
End If
End For
End
Answer 4:
Start
For i from 1 to 10 do
Print "This is iteration number " + i
End For 39
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.
Answer 5:
Start
Initialize total_collected to 0
While total_collected < 200 do
PRIMUS PU COLLEGE, BANGALORE
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
40 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
Question 8: Write an algorithm to find the greatest among two different numbers
entered by the user.
Answer 8:
Start
Input number1
Input number2
If number1 > number2 then
Print "The greatest number is " + number1
Else
Print "The greatest number is " + number2
End
41
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"
PRIMUS PU COLLEGE, BANGALORE
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
42
Set smallest = number1
If number2 > largest then
Set largest = number2
If number3 > largest then
Set largest = number3
If number4 > largest then
Set largest = number4
If number2 < smallest then
Set smallest = number2
If number3 < smallest then
Set smallest = number3
If number4 < smallest then
Set smallest = number4
Question 11: Write an algorithm to display the total water bill charges of the month
depending upon the number of units consumed by the customer as per the following
criteria:
Answer 11:
Start
Input units_consumed
Set meter_charges = 75
If units_consumed <= 100 then
total_bill = units_consumed * 5 43
Else If units_consumed <= 250 then
total_bill = (100 * 5) + ((units_consumed - 100) * 10)
Else
total_bill = (100 * 5) + (150 * 10) + ((units_consumed - 250) *
20)
End If
total_bill = total_bill + meter_charges
Print "Total Water Bill: " + total_bill
End
Question 12: What are conditionals? When are they required in a program?
Answer 12: Conditionals are statements that perform different actions based on
PRIMUS PU COLLEGE, BANGALORE
whether a given condition is true or false. They are required in a program to make
decisions and execute certain sections of code depending on specific conditions.
Question 13:
Answer 13:
44
→ Flow of control
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
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
factorial = factorial * i
45
End For
Print "Factorial of " + number + " is " + factorial
End
Answer 16:
PRIMUS PU COLLEGE, BANGALORE
46
PRIMUS PU COLLEGE, BANGALORE
47
Question 17: Following is an algorithm to classify numbers as “Single Digit”, “Double
Digit” or “Big”. Verify for (5, 9, 47, 99, 100, 200) and correct the algorithm if required.
Answer 17:
Classify_Numbers_Algo
INPUT Number
IF Number < 10 then
Print "Single Digit"
Else If Number < 100 then
Print "Double Digit"
PRIMUS PU COLLEGE, BANGALORE
Else
Print "Big"
End If
Verification:
For 5, 9, 47, 99, 100, 200:
- 5 and 9 should be classified as "Single Digit"
- 47 and 99 should be classified as "Double Digit"
- 100 and 200 should be classified as "Big"
Question 18: For some calculations, we want an algorithm that accepts only positive
integers up to 100.
Accept_1to100_Algo
INPUT Number
IF (0 < Number) AND (Number <= 100) then
ACCEPT
Else
REJECT
End If
48
a) On what values will this algorithm fail? b) Can you improve the algorithm?
Answer 18: a) This algorithm will fail for the value 0 because 0 is not considered a
positive integer. b) Improved algorithm:
Accept_1to100_Algo
INPUT Number
IF (Number > 0) AND (Number <= 100) then
ACCEPT
Else
REJECT
End If
49