Java OCA OCP Practice Question 3032

Question

Given:

public class Main {
   static StringBuffer sb;
   StringBuffer sb2;//from  ww  w .  j  av  a2s.  c  om

   public static void main(String[] args) {
      sb = sb.append(new Main().go(new StringBuffer("Python")));
      System.out.println(sb);
   }

   {
      sb2 = new StringBuffer("Ruby ");
   }

   StringBuffer go(StringBuffer s) {
      System.out.print(s + " SQL " + sb2);
      return new StringBuffer("MySQL");
   }

   static {
      sb = new StringBuffer("Java ");
   }
}

What is the result?

  • A. Java MySQL
  • B. Python SQL Ruby
  • C. Python SQL Ruby MySQL
  • D. SQL Ruby Python
  • E. Python SQL Ruby Java MySQL
  • F. Java Python SQL Ruby MySQL
  • G. Compilation fails.
  • H. An exception is thrown at runtime.


E is correct.

Note

The final contents of sb is "Java MySQL".

Remember that the static initializer runs before any instances are created.




PreviousNext

Related