True or False: A method with a return type of void must include a return statement.
True or False: You can pass multiple parameters into a method.
True
What range of numbers can
ran.nextInt(5) produce?
0 - 4
What is the difference between = and == in Java?
= assigns a value; == compares values.
How many times does this loop run? for (int i = 0; i < 5; i++)
5 times
What happens if you write a return statement before the end of the method body?
The method stops executing immediately and exits.
What is printed?
public static void greet(String name) { System.out.println("Hi " + name);
}
greet("Sthiti");
"Hi Sthiti"
What does this print?
String a = "hi";
String b = "hi";
System.out.println(a == b);
true
What data type would you use for a decimal number?
double or float
What are the three parts inside a for loop header?
Initialization, condition, update
What will this method return?
public static int multiply(int a, int b) {
return a * b;
}
when called with multiply(2, 3)
6
What happens if you call a method without passing all required arguments in the correct order?
You get a compile-time error.
What is printed? System.out.println("Java".substring(1, 3));
"av" — substring starts at 1 and ends before 3.
What happens if the loop condition is always true and never updated?
Infinite loop
What happens if a method with a non-void return type doesn’t have a return statement in all paths?
Compilation error — Java requires a return statement for every possible execution path.
What is the output?
System.out.println(Math.max(5, Math.min(2, 10)));
5 — Math.min(2,10) is 2, then Math.max(5,2) is 5.
Can a method have more than one return statement?
Yes, but only one will execute, depending on the condition.
What is it called when two methods have the same name but different parameter types or counts?
Method overloading
What is the result of Math.pow(2, 3) == 8?
true but first looks at it as a double