Java Foundations 5 Flashcards

(14 cards)

1
Q

What is the standard signature of the main method in Java?

A

public static void main(String[] args)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why must the main method be static?

A

So the JVM can call it without creating an object of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does String[] args represent in the main method?

A

It holds command-line arguments passed to the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can a method with a void return type use the return keyword?

A

Yes, but only as return; without a value to exit early.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is type inference in Java?

A

Letting the compiler determine the variable’s type using var.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When was var introduced in Java?

A

Java 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can var be used for method parameters or return types?

A

No, var is only allowed for local variables (inside methods or blocks).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of using var for type inference.

A

var list = new ArrayList<String>();</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When should you use var in Java?

A

When the type is obvious or to reduce redundancy in declarations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why should you avoid overusing var?

A

It can reduce code readability if the type is not clear from context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does continue do inside a loop?

A

It skips the current iteration and moves to the next one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In which loops can continue be used?

A

for, while, and do-while loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does continue differ from break?

A

continue skips to the next iteration; break exits the loop entirely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example: What is the output?

for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i);
}

A

0134 (2 is skipped)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly