All Flashcards
What are the steps to call a method with parameters?
- Write the method name. 2. Include parentheses. 3. Pass arguments inside the parentheses that match the parameter types and order.
What are the steps to define a method with parameters?
- Declare the method signature (access modifier, return type, method name). 2. Add parameters inside the parentheses (type and name). 3. Write the method body with code that uses the parameters.
What are the steps to overload a method?
- Create multiple methods with the same name. 2. Ensure each method has a unique parameter list (different types, number, or order of parameters). 3. Implement each method's specific functionality.
What are the steps to trace the execution of a method call?
- Identify the method call. 2. Substitute the argument values for the corresponding parameters. 3. Execute the code within the method. 4. Note any output or side effects.
What are the steps to handle a division by zero error in a method?
- Check if the divisor is zero. 2. If the divisor is zero, return a predefined value (e.g., 0) or print an error message. 3. If the divisor is not zero, perform the division.
What are the steps to pass arguments to a method?
- Identify the method's parameters. 2. Ensure the arguments match the parameters in type and order. 3. Place the arguments inside the parentheses when calling the method.
What are the steps to ensure a method handles invalid input?
- Identify potential invalid inputs. 2. Add conditional statements to check for these inputs. 3. Provide appropriate error handling (e.g., return an error value or throw an exception).
What are the steps to determine which overloaded method is called?
- Check the number of arguments. 2. Check the data types of the arguments. 3. The compiler selects the method whose parameters match the arguments in number and type.
What are the steps to debug a method with incorrect output?
- Review the method's code. 2. Check parameter values. 3. Use print statements or a debugger to trace the execution and identify the source of the error.
What are the steps to test a method with different inputs?
- Identify a range of input values. 2. Call the method with each input value. 3. Verify that the output matches the expected result for each input.
What does the following code output?
java
public static void printValue(int x) {
System.out.println(x + 5);
}
public static void main(String[] args) {
printValue(10);
}
15
What does the following code output?
java
public static void printSum(double a, double b) {
System.out.println(a + b);
}
public static void main(String[] args) {
printSum(2.5, 3.5);
}
6.0
What does the following code output?
java
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Alice");
}
Hello, Alice!
Identify the error in the following code:
java
public static void square(int x) {
return x * x;
}
public static void main(String[] args) {
int result = square(5);
System.out.println(result);
}
The square method is declared as void but attempts to return a value. It should be declared with a return type of int.
What does the following code output?
java
public class Example {
public static void modify(int num) {
num = num * 2;
}
public static void main(String[] args) {
int x = 5;
modify(x);
System.out.println(x);
}
}
5
What does the following code output?
java
public class Overload {
public static void print(int x) {
System.out.println("Integer: " + x);
}
public static void print(double x) {
System.out.println("Double: " + x);
}
public static void main(String[] args) {
print(5);
print(5.0);
}
}
Integer: 5 Double: 5.0
What does the following code output?
java
public class Test {
public static void method(int a, double b) {
System.out.println("int, double");
}
public static void method(double a, int b) {
System.out.println("double, int");
}
public static void main(String[] args) {
method(5, 5.0);
}
}
int, double
Identify the error in the following code:
java
public class ErrorExample {
public static void calculate(int a, int b) {
int result = a + b;
}
public static void main(String[] args) {
System.out.println(calculate(5, 3));
}
}
The calculate method is void and does not return a value. The main method attempts to print the result of calculate(5, 3), which is not possible.
What does the following code output?
java
public class ScopeTest {
static int x = 10;
public static void modify(int x) {
x = x + 5;
System.out.println("Method x: " + x);
}
public static void main(String[] args) {
modify(x);
System.out.println("Main x: " + x);
}
}
Method x: 15 Main x: 10
What does the following code output?
java
public class Division {
public static void divide(double a, double b) {
if (b == 0) {
System.out.println("Cannot divide by zero");
} else {
System.out.println(a / b);
}
}
public static void main(String[] args) {
divide(10.0, 0.0);
}
}
Cannot divide by zero
What is a method parameter?
Input passed into a method, used to customize its behavior.
What is a method signature?
The method's name and the types and order of its parameters.
What does void mean in a method signature?
The method does not return any value.
What is method overloading?
Having multiple methods with the same name but different parameter lists.
What is the purpose of parameters?
To make methods more flexible and reusable by allowing them to operate on different data.
What are actual parameters?
The values passed to a method when it is called.
What are formal parameters?
The parameters declared in the method signature.
What is the scope of a parameter?
The region of the program where the parameter is accessible, typically within the method's body.
What is a default parameter?
A parameter that has a default value specified in the method signature, used if no value is provided when the method is called.
What is the return type of a method?
The data type of the value that the method returns after its execution. If a method does not return a value, its return type is void.