WorksheetsQuiz 7: IPS_Ch 06
Total questions: 10
Worksheet time: 5mins
Which keyword is used in Java to define a method that does not return any value?
int
void
return
static
What is the term for breaking a program into smaller, reusable pieces in Java?
Compilation
Modularity
Inheritance
Polymorphism
Which of the following is the correct way to declare a method named `calculateSum` that takes two integers and returns their sum?
int calculateSum(int a, int b) { return a + b; }
void calculateSum(int a, int b) { return a + b; }
int calculateSum() { return a + b; }
int calculateSum(int a, int b) { a + b; }
Which of the following best describes the purpose of using methods in Java?
To increase the size of the code
To organize code into reusable blocks
To make code run faster
To avoid using variables
Given the following method signature, what is the return type? ```java public double getArea(double radius) ```
int
double
void
String
Suppose you have a method `public int multiply(int x, int y)`. How would you call this method from another method in the same class to multiply 3 and 4?
multiply(3, 4);
int result = multiply(3, 4);
int multiply = multiply(3, 4);
multiply = multiply(3, 4);
If you want to make a method accessible only within its own class, which access modifier should you use?
public
private
protected
static
Consider the following code snippet: ```java public static int square(int n) { return n * n; } ``` Which of the following statements correctly uses this method to print the square of 5?
System.out.println(square(5));
System.out.println(square());
System.out.println(square(5, 5));
System.out.println(square);
You are designing a Java program to calculate the average of three numbers. Which approach best demonstrates modularity?
Write all code in the main method.
Create a separate method to calculate the average and call it from the main method.
Use global variables for all calculations.
Avoid using any methods.
You are tasked with refactoring a large Java program that contains repeated code for calculating the area of different shapes. How would you use modularity and methods to improve the program’s design?
Copy and paste the calculation code wherever needed.
Create separate methods for each shape’s area calculation and call them as needed.
Write all calculations in a single method regardless of shape.
Avoid using methods and write calculations directly in the main method.
