1Z0-803 · Question #87
Given: public class ScopeTest { int j, int k; public static void main(String[] args) { ew ScopeTest().doStuff(); } void doStuff() { nt x = 5; oStuff2(); System.out.println("x"); } void doStuff2() { nt
The correct answer is A. j B. k. Fields, also known as instance variables, are declared directly within a class, whereas local variables are declared inside methods or blocks and have a more limited scope.
Question
Given:
public class ScopeTest { int j, int k; public static void main(String[] args) { ew ScopeTest().doStuff(); } void doStuff() { nt x = 5; oStuff2(); System.out.println("x"); } void doStuff2() { nt y = 7; ystem.out.println("y"); or (int z = 0; z < 5; z++) { ystem.out.println("z"); ystem.out.println("y"); } Which two items are fields?
Options
- Aj
- Bk
- Cx
- Dy
- Ez
How the community answered
(32 responses)- A91% (29)
- C6% (2)
- E3% (1)
Why each option
Fields, also known as instance variables, are declared directly within a class, whereas local variables are declared inside methods or blocks and have a more limited scope.
Variable 'j' is declared directly within the `ScopeTest` class, outside of any method or constructor, making it an instance variable or field.
Variable 'k' is declared directly within the `ScopeTest` class, outside of any method or constructor, making it an instance variable or field.
Variable 'x' is declared inside the `doStuff()` method, making it a local variable, not a field.
Variable 'y' is declared inside the `doStuff2()` method, making it a local variable, not a field.
Variable 'z' is declared within the `for` loop inside `doStuff2()`, making it a local variable scoped to that loop, not a field.
Concept tested: Java fields vs. local variables
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
Topics
Community Discussion
No community discussion yet for this question.