Write a correct void method named sayHello that prints “Hello, world!”
public static void sayHello() {
System.out.println("Hello, world!");
}How many parameters are allowed in a method?
Essentially, as many as you want.
What symbol is used to enclose a method body?
curly brackets { }
What is a local variable?
A variable declared inside a method
What word is used at the end of some methods that sends a value back to the statement that called the method
return
Is this a method header or method call?
calcTotal();
Method call
What happens when a double is passed into a method expecting an int?
It causes an error because Java cannot narrow automatically
What is wrong with this method?
public int add(int a, int b) {
System.out.println(a + b);
}
It is missing a return statement (it says int but doesn’t return a value).
Where can a local variable be used?
Only inside the method where it is declared.
What does the keyword void mean in a method?
The method does not return a value
Name the following parts of the method header:
public static double hey(int thing)
public → access modifier
static → thing you need
double → return type
hey → method name
(int thing) → parameter
int = parameter data type
thing = parameter variable name
What's wrong with this method call?
displayValue(int x);
You do not include the data type in a method call
What is wrong with this method header?
public static calcTotal(int a, int b)
Missing return type
Why can two methods use local variables with the same name?
Because each method has its own separate scope.
What is my daughter's name?
Hayden Jo Curtis
What is the purpose of writing methods in a program? List two advantages of using methods.
Code reuse - You can write a method once and call it multiple times instead of rewriting the same code.
Easier to manage and debug (modularity) - Breaking a program into smaller parts makes it easier to read, test, and fix problems.
What is the difference between parameter and argument?
A parameter is a variable in the method definition that receives a value, while an argument is the actual value that is passed into the method when it is called.
What's wrong with this method:
public static int nums() {
return 10.5
}
return statement doesn't match return type
What's wrong with this code:
public static void main(String[] args) {
System.out.println(x);
}
public static int mystery(int n) {
int x = n*n;
return x;
}
x is not defined in main
Which method is a return type method, and which is a void method? Explain
print("Mrs. Schullo);
System.out.println(average(a, b));
print() → Void method
average() → Value-returning method
What planet is known as the “Red Planet”?
Mars
False
What is the difference between correlation and causation?
Correlation means two variables are related or change together; causation means one directly causes the other.
How can you become a millionaire by the time you retire?
Contribute to your 403b or 401k early!!!!!
Why do we have seasons on Earth?
Because Earth is tilted on its axis, causing different parts of Earth to receive varying amounts of sunlight throughout the year as it orbits the Sun