Java OCA OCP Practice Question 167

Question

Given the file MyClass.java below.

which of the marked lines can you independently insert the line public String color; into and still have the code compile?

// line a1 
public class MyClass { 
   // line a2 
   public void attach() { 
      // line a3 
   } 
   // line a4 
} 
  • A. a1 and a3
  • B. a2 and a4
  • C. a2, a3, and a4
  • D. a1, a2, a3, and a4


B.

Note

The line of code cannot be inserted at a1.

No variables are allowed outside of the class declaration.

Options A and D are incorrect.

The line of code can not be inserted at a3.

Local variables defined within methods cannot have access modifiers such as public.

Option C is incorrect.

The code can be inserted independently at a2 and a4.

Instance variables can be defined anywhere in the class outside a method.

Option B is the correct choice.




PreviousNext

Related