Shared Flashcard Set

Details

SCJP6 or OCPJP6 Certification
Oracle Certified Professional - JP6 exam flash cards
22
Software
Professional
06/10/2012

Additional Software Flashcards

 


 

Cards

Term

Quantifiers

  • Greedy    Reluctant    Possessive

    x?    x??  x?+

  • Definition
    Greedy quantifiers are considered 'greedy' because they force the matcher to eat entire input string prior to attempting first match
    if the first match (entire input string) fails, the matcher backs off the input string by one character and tries agian, repeating the process until a match is found or there are no more character left. The last is found or there are no more character left. The last thing it will try matching against 0 or 1 character. e.g.,
    .*foo - argument is "xfoo aaa b xfoo xfoo"
    result: "xfoo aaa b xfoo xfoo" since it eats full inut and the last word satisfies the match
    Term
    T/F: Method-local inner class objects can access the enclosing (outer) class's private (or any other) members
    Definition
    True. yes, method local inner classes can access outer class members
    Term
    What is the output?
    a) prints nothing
    b) prints "anonymous popcorn"
    c) compilation error

    public void main(String[] args)
    {
    Popcorn p = new Popcorn() {
    public void pop() {
    System.out.println("anonymous popcorn");
    }
    }
    }
    Definition
    c. Missing semi-colon
    Term
    An inner class can be protected. True or False?
    Definition
  • True Inner class can have any access modifier. it can default, private, protected and public
  • Term
    What members does a method-local inner class declared in a static method, have access to?
    Definition
    Only the static members of the outer class (no access to the instance variables)
    Term
    Which of the following modifiers are acceptable for method-local inner classes:
    a) public
    b) private
    c) protected
    d) static
    e) abstract
    f) transient
    g) final
    Definition
    abstract or final modifier is only acceptable for method-local inner classes
    public class Outer {
    public void any() {
    class MethodLocal1 {}
    abstract class MethodLocal2 {}
    final class MethodLocal3 {}
    }
    }
    Term

    Given the following Static Nested Class:

    class BigOuter {
         static class Nest {}
    }


    What is the syntax for instantiating the static nested class:
    A. BigOuter.Nest n = new BigOuter().new Nest();
    B. BigOuter.Nest n = BigOuter.Nest;
    C. BigOuter.Nest n = new BigOuter.Nest();
    D. BigOuter.Nest n = new BigOuter.Nest;

    Definition
    Answer: C
    BigOuter.Nest n = new BigOuter.Nest();
    Term

    True/False ? A static nested class is simply a class that's a static member of the enclosing class,
    since there is no such thing as a static class
    Definition
    True. It can be accessed without having an instance of the outer class.
    Term

    An outter class instance cannot be referenced from within an inner class - True or False?
    Definition
    False
    Term

    Where can a method-local inner class be instantiated?
    Definition

    Only within the method where the inner class is defined.
    It cannot be instantiated outside the method.
    Term

    //Is this legal?

    class MyRunnable implements Runnable {
         public void run() {
              System.out.println("Im running!");
         }
    }
    public class Tester {
         public static void main(String[] args) {
              MyRunnable inst = new MyRunnable();
              Thread thr = new Thread(inst);
              inst.start();
         }
    }
    Definition
    No,
    start method must be called on the Thread object, not the Runnable class instance since there is no start() method in the class.
    Term

    What happens when the following is run:
         A.   Prints nothing
         B.   Prints "foofy"
         C.   Compilation error
         D.   Exception


    class MyWonderfulClass {
          void go() {
              Bar b = new Bar();
              b.doStuff (new Foo() {
                   public void foof() {
                         System.out.println("foofy");
                   }
              })
         }
    }

    interface Foo {
         void foof();
    }
    class Bar {
         void doStuff(Foo f) { }
    }
    Definition

    Answer:  C,   Argument-defined anonymous inner classes end with "});"
    Term

    True/False?  Method-local inner class objects can access the local variables marked final of the method it is in.
    Definition

    True ,  can access the local variables marked final of the method it is in.
    Term

    class MyOuter {
         class MyInner{ }

         static class MyStatic {}
    }

     When compiled, what are the resulting class file names?
    Definition

    Total three .class files will be created in file system

     MyOuter.class
     MyOuter$MyStatic.class
     MyOuter$MyInner.class

    Term

    public class Orange {
          public void bite() {
                System.out.println("yum");
          }

          public static void main(String...strings) {
                Fruit f = new Fruit();
                f.o.bite();
                f.a();

                Orange o = new Orange();
                o.bite();
          }
    }

    class Fruit {
          Orange o = new Orange() { // 1
                public void bite() { // 2
                      System.out.println("anonymous yum"); // 3
                } // 4
          }// 5

          public void a() {
                o.bite();
          }
    }


    Choose all statements that are correct:

    a)   Line 1: is a legal declaration to an instance of an anonymous subclass
    b)   Line 2: cannot override bite() method.
    c)   Line 4: needs a semi-colon after the curly brace
    d)   Line 5: needs a semi-colon after the curly brace
    e)   None of the above
    Definition

    options a and d are correct
    Term
    True or False?  Method-local inner class objects can access the local variables of the method it is in.
    Definition

    False  - local variables only exist for the lifetime of the method, but a method-local inner class object might exist after the method completes
    Term

    True or False?  - a static nested class does not have access to the instance variables and non-static methods of the outer class.
    Definition

    True  - This also includes no access to the MyOuter.this reference
    Term
    True or False?  - Method-local inner class objects can access the enclosing (outer) class's private (or any other) members
    Definition
    True
    Term

    public class MyExOuterClass {
          private String x = "Outer";

          void doSomething() {
                String differentMethodVar = "local variable";

                class MyExInnerClass {
                      String innerMember = "ram";
                      public void accesOuter() {
                            System.out.println("Outer class variable x is " + x);
                            System.out.println("MyExInnerClass variable innerMember is " + innerMember);
                            System.out.println("enclosing method's variable differentMethodVar is " + differentMethodVar); // Won't Compile!
                      }
                }
          }
    }
    Definition

    Compiling the preceding code makes the compiler spew out this error message:
    MyExOuterClass.java:8: local variable z is accessed from within inner class;
    needs to be declared final
    System.out.println("Local variable z is " + z);
    ^

    Marking the local variable z as final fixes the problem:
    final String z = "local variable";

    What a Method-Local Inner Class Can? and Cannot Do?

    A method-local inner class can be instantiated only within the method where the inner class is defined. In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. Like regular inner class objects, the method-local inner class object shares a special relationship with the enclosing outer class object, and can access its private or any other members. However, the inner class object cannot use the local variables of the method the inner class is in.

    If you ask me, Why not? Think about it. The local variables of the method live on the stack, and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is destroyed and the variable is gone. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them. Unless the local variables are marked final! The following code attempts to access a local variable from within a method-local inner class
    }

    the only modifiers you can apply to a method-local inner class are abstract and final, but as always, never both at the same time

    Exam Tip: Remember that a local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class.
    If you are in a static method there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables
    Term

    Ways to Create Locale instance
    java.util.LocaleLocale loc = Locale.US;
    java.util.LocaleLocale loc1 = new Locale("it");
    java.util.LocaleLocale loc2 = new Locale ("fr", "FR");

    Ways to Create different Date Format instances (package java.text.*)
    java.text.DateFormat d1 = DateFormat.getDateInstance();
    java.text.DateFormat d2 = DateFormat.getDateInstance(java.text.DateFormat.FULL);
    java.text.DateFormat d3 = DateFormat.getDateInstance(java.text.DateFormat.LONG, loc1);
    java.text.DateFormat d4 = DateFormat.getDateTimeInstance();
    java.text.DateFormat d5 =
        DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, java.text.DateFormat.MEDIUM);
    java.text.DateFormat d6 =
        DateFormat.getDateTimeInstance(java.text.DateFormat.LONG, java.text.DateFormat.FULL, loc2);

    Ways to Create different Number Format(DecimalFormat) instances (package java.text.*)
    java.text.NumberFormat nf1 = NumberFormat.getInstance();
    java.text.NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
    java.text.NumberFormat nf3 = NumberFormat.getNumberInstance();
    java.text.NumberFormat nf4 = NumberFormat.getNumberInstance(Locale.US);
    java.text.NumberFormat nf5 = NumberFormat.getIntegerInstance();
    java.text.NumberFormat nf6 = NumberFormat.getIntegerInstance(Locale.US);
    java.text.NumberFormat nf7 = NumberFormat.getCurrencyInstance();
    java.text.NumberFormat nf8 = NumberFormat.getCurrencyInstance(Locale.US);
    java.text.NumberFormat nf9 = NumberFormat.getPercentInstance();
    java.text.NumberFormat nf10 = NumberFormat.getPercentInstance(Locale.US);
    Definition
    from packages
    java.text.*;
    java.util.Locale;
    Term

    Enums
  • Enum constants are implicitly static and final and can not be changed once created.
        For example below code of java enum will result in compilation error:
            Currency.PENNY = Currency.DIME;
            Error, The final field EnumExamples.Currency.PENNY cannot be re assigned.

  • Constructor of enum in java must be private, any other access modifier will result in compilation error.

        CurrencyEnum(int i) { // only private or default access modifier for enum constructors
            this.setConversionValue(i);
        }
        private CurrencyEnum(int i) { // only private or default access modifier for enum constructors
            this.setConversionValue(i);
        }
  • Definition
    Term
    6. Thread States
    Definition
    A thread state. A thread can be in one of the following states:

    NEW
    A thread that has not yet started is in this state.

    RUNNABLE
    A thread executing in the Java virtual machine is in this state.

    BLOCKED
    A thread that is blocked waiting for a monitor lock is in this state.

    WAITING
    A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

    TIMED_WAITING
    A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

    TERMINATED
    A thread that has exited is in this state.
    Supporting users have an ad free experience!