Java Foundations 5 Flashcards
(14 cards)
What is the standard signature of the main method in Java?
public static void main(String[] args)
Why must the main method be static?
So the JVM can call it without creating an object of the class.
What does String[] args represent in the main method?
It holds command-line arguments passed to the program.
Can a method with a void return type use the return keyword?
Yes, but only as return; without a value to exit early.
What is type inference in Java?
Letting the compiler determine the variable’s type using var.
When was var introduced in Java?
Java 10
Can var be used for method parameters or return types?
No, var is only allowed for local variables (inside methods or blocks).
Give an example of using var for type inference.
var list = new ArrayList<String>();</String>
When should you use var in Java?
When the type is obvious or to reduce redundancy in declarations.
Why should you avoid overusing var?
It can reduce code readability if the type is not clear from context.
What does continue do inside a loop?
It skips the current iteration and moves to the next one.
In which loops can continue be used?
for, while, and do-while loops.
How does continue differ from break?
continue skips to the next iteration; break exits the loop entirely.
Example: What is the output?
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i);
}
0134 (2 is skipped)