nerdexam
Oracle

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.

Java Basics

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)
  • A
    91% (29)
  • C
    6% (2)
  • E
    3% (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.

AjCorrect

Variable 'j' is declared directly within the `ScopeTest` class, outside of any method or constructor, making it an instance variable or field.

BkCorrect

Variable 'k' is declared directly within the `ScopeTest` class, outside of any method or constructor, making it an instance variable or field.

Cx

Variable 'x' is declared inside the `doStuff()` method, making it a local variable, not a field.

Dy

Variable 'y' is declared inside the `doStuff2()` method, making it a local variable, not a field.

Ez

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

#variable scope#instance variables#local variables#fields

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice