`
Odysseus_110
  • 浏览: 120348 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

SCJP notes

    博客分类:
  • J2SE
阅读更多

The default constructor has the same access as its class

 

 

which gets the name of the parent directory file “file.txt”?

String name = (new File(“file.txt”)).getParent();

 

 

which of these are valid JavaBeans method signatures?

A  public void removeWibbleListener(BurbleListener w)

B  public void addElement(Element e)

C  public boolean getActive()

D  public boolean isActive()

E   int getFooNumber()

F   public long size()

The correct options are C and D.With regards to the JavaBeans naming conventions:

E is incorrect because getter and setter methods must be public

B is incorrect because an add… or remove… method must end in ….Listener

A is incorrect because a removeWibbleListener method must take a WibbleListener as its argument

 

 

which methods can be declared at the position indicated and not cause a compile-time error?

A  protected void drawSting()

B  void drawSting();

C  public abstract void drawSting();

D   public static void drawSting();

E  strictfp void drawSting();

The correct options are B and C

Interface method declarations are implicitly public and abstract,and these modifiers can be omitted.Interface methods cannot be static(ruling out D),nor can they be marked strictfp(ruling out E) ,final or native.Marking A protected conflicts with the restriction that interface methods must be public.

 

 

Fill in the table to indicate which Exception and Error classes are typically thrown programmatically and which by the JVM.

 

Exception/Error class                                   Typically thrown…?

ArrayIndexOutOfBoundsException                 By the JVM

ClassCastException                                         By the JVM

IllegalArgumentException                                 Programatically

IllegalStateException                                        Programatically

NullPointerException                                      By the JVM

NumberFormatException                                 Programatically

AssertionError                                                Programatically

ExceptionInInitalizerError                                By the JVM

StackOverflowError                                        By the JVM

NoClassDefFoundError                                  By the JVM

 

 

How many lines don't compile ?

byte a = 127 + 1 ;

byte b = ( byte ) 25768 ;

char c = 127 ;

c+= 12345 ;

Answer: line 1 does not compile

127 is the max value of a byte. Casting makes everything ok. Remember += does an automatic cast.

 

 

class test {
 public static void main ( String [] args ) {
      final Integer x4 = 8; 
      final int x = 10; 
      switch ( x ) { 
                 case x4: 
                     System.out.println("4");
                     break; 
                 case x: 
                     System.out.println("x");
                     break; 
      } 
}
}
 

 

 

compile error

Final objects are not allowed in a case statement. A final object's value can be changed whereas a final variables value cannot be changed.

 

 

 

 

 

an interface may extend multiple interfaces

 

 

an ArithmeticException is  a checked exception.

Answer:false

ArithmeticException are considered programmer problems ,and are Not checked by the compiler.A divide-by-zero error,for example,should be checked for by your own code.

 

 

 

Public class Demo{ 
public static void main(String[] args){ 
for(int i =0;i < 3;i++){
           label1:
               switch(i){ 
                       case 0: 
                           System.out.println(“34”);
                       case 1: 
                           System.out,println(“23”); 
                           break; 
                       case 2: 
                           System.out.println(“12”);
                       case 3: 
                           System.out.println(“01”); 
                           break label1; 
                       default: 
                           System.out.println(“40”); 
               } 
} 
} 
}
 

 

 

A The code is correct and it compiles.

 

label1: can lay before switch?

you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:

 

 label1:
System.out.println("in label1");//here scope of this label is
                                //this one statement so you can't
                                //break
 
label2:{  //here starts scope of label2
   System.out.println("label 2 started");
   break label2;  // here you can use break in scope of this label
   System.out.println("label 2 ended");  //but it wont compile because compiler
                                         //knows this statement is unreachable
}   //here ends scope of label2
 
break label2; // you cant do this here because you are out of scope
 
for(int i = 0; i < 5; i++){
   // bla bla bla
   break label2; // you cant do this here because you are out of scope
 

 

 

which of these are legal escape sequences?

A \n

B \f

C \m

D \n

E \e

F \r

G \t

ABDFG

 

Remember the sentence:”Big farms need red tractors

only “b” “f” “n” “r” and “t” are legal,no other characters!

 

 

 

Which of the following statements are not true?

java.lang.Character does not extend java.lang.Number

java.lang.Void can't be extended but it can only be instantiated

java.lang.Integer can't be extended

java.lang.Character does not have a constructor that takes a String

java.lang.Byte does not extend java.lang.Number

 

B and E are correct. Only these two statements are NOT true. The class java.lang.Void is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void. This class is final and has a private constructor. Hence, it can neither be extended nor can it be instantiated.

The class java.lang.Byte does indeed extend java.lang.Number.

The class java.lang.Character does not extend java.lang.Number, and has only one constructor that takes a char value.

All wrapper classes are final, and hence they can not be extended.

 

 

It is perfectly legal in Java to create arrays of size zero.

 

 

 

 

在定义一个 native method 时,并不提供实现体(有些像定义一个 java interface ),因为其实现体是由非 java 语言在外面实现的。,下面给了一个示例:    
    public class IHaveNatives
    {
      native public void Native1( int x ) ;
      native static public long Native2() ;
      native synchronized private float Native3( Object o ) ;
      native void Native4( int[] ary ) throws Exception ;
    }

标识符 native 可以与所有其它的 java 标识符连用,但是 abstract 除外。这是合理的,因为 native 暗示这些方法是有实现体的,只不过这些实现体是非 java 的,但是 abstract 却显然的指明这些方法无实现体。

 

 

 

What will happen when you attempt to compile and run the following code?

 

class MyClass 
{
  	static String myName = "SCJP";
  
	MyClass getMyClass() 
  	{     
     		System.out.println(myName);
     		return null;     
  	}

  	public static void main(String[ ] args) 
  	{
    		System.out.println( new MyClass().getMyClass().myName );
  	}
}
 

 

 

 

Answer: Prints SCJP twice

C is the correct choice. The above method will not give any compilation error as at compile time, compiler sees the return type of getMyClass method as MyClass and calling myName on MyClass is valid at compile time.

 

Also it doesn't give any runtime error which is not very obvious. Please note that a null reference may be used to access a class (static) variable without causing an exception . The static variable is called on the class itself and not on the object. Had the myName variable non-static, the above code at run time would give NullPointerException.

 

 

 

 

 

 

  • scjpnote.rar (283 KB)
  • 描述: scjp学习笔记
  • 下载次数: 8
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics