and Programming Chapter 6 (Exercises)
with Complete Solutions
Why is writing pseudocode helpful before coding?
✔✔ It helps outline the logic of the program without worrying about syntax.
What is the purpose of testing a program with different inputs?
✔✔ To ensure the program works correctly for various cases, including edge cases.
How do you debug a program effectively?
✔✔ By using print statements, breakpoints, and debugging tools to trace errors.
What should you do if a program does not behave as expected?
✔✔ Analyze the logic, check for syntax errors, and use debugging techniques.
Why is it important to write comments in your code?
✔✔ To explain the logic and make the code easier to read and maintain.
1
,What is the purpose of using a class in Java?
✔✔ To define a blueprint for objects, organizing data and behavior together.
How do you create an object from a class in Java?
✔✔ By using the `new` keyword followed by the class constructor.
Why should instance variables usually be private?
✔✔ To protect data and enforce encapsulation by allowing controlled access.
How do you access private variables in a class?
✔✔ By using public getter and setter methods.
What is the significance of method parameters?
✔✔ They allow data to be passed into a method for processing.
How do you return a value from a method?
✔✔ By using the `return` keyword followed by the value or variable.
2
, What is an overloaded method?
✔✔ A method that has the same name but different parameter lists.
How can you reuse code efficiently in Java?
✔✔ By creating methods and calling them instead of duplicating logic.
What is a constructor and when is it called?
✔✔ A special method used to initialize an object; it is called when an object is created.
Why is method decomposition useful?
✔✔ It improves readability, reusability, and maintainability by breaking large methods into
smaller ones.
What happens if a method does not specify a return type?
✔✔ It must be declared as `void`, meaning it does not return a value.
How do you create an array of objects in Java?
✔✔ By declaring an array of the class type and initializing each element with `new`.
3