考SCJP的感受

考SCJP的感受 

       考过SCJP已经快一个月了,也快拿到证书了,呵呵。在开始考的时候也问了别人到底有没有这个必要考SCJP证书,也忧郁也一段时间;后来还是拿主意一定要考出来证书。其主要原因是:1、可以巩固一下基础知识;2、到毕业的时候找工作也好找些(虽然有些人不信证书)。

      为了考这个证书准备了差不多有一个半月的时间,前后的找SCJP的 资料,什么教程和模拟器了,在网上也是问,后来就定下了考证书需要的资料:SUN公司培训中文教程、Thinking in java,模拟器刚开始用的是SCJP2006模拟系统,不过后来就以J@Whiz1.4为主了。

     放假后一个月也没有回家,在学校里租了间房子住下了,复习了一个多月就准备考试了,刚开始做J@Whiz1.4里的题通过率为50%(很低哟),后来10%的提高,到最后有一次做了95%。J@Whiz1.4里一共8套练习题和一套final练习,也可以选择单项练习;做完一套题后会给你答案和解释(这也是我选择它的一个原因),如果哪一个题错了就根据解释再学习,要多次巩固知识点才能记的牢。

    考完SCJP后的确学了不少的东西,后来写“银行管理系统”的时候很少出错误了,写一些类得心应手了,少走了不少弯路。现在J2EE是很热,大型企业也都在用它做系统,有好多人也想学J2EE去赚钱,但是没有J2SE的基础根本不可能做一个好系统的,里面的那么多的类不都是J2SE里的吗?还有在写JavaBean的时候也会用到J2SE的知识呀,还没有学会爬怎么能走路呢?  有许多人不想学基础,只想学一些高级技术感觉比别人高一等,以为会一些struts,sping等先进技术就很了不起,如果让你看里的类你能看懂了吗?如果让你写一个应用系统你能很快就写出来吗?总之一句话,基础很重要。

    现在有不少人是自学成才的,建议你以考SCJP为由学学理论知识,对你以后写程序会有很大帮助的。 

    我说这些可能有人会骂我,想扁我,但是这是我的心里话,我不在意,只要大家能受益就行了。

   我在做J@Whiz1.4的时候有一些难题和典型题选择了一些放到这里供大家参考:

  不是很全(Test4没有写)

Test1
1、 要想让assertion对整个包都有效用:java -ea:包名... <MAIN_CLASS_NAME>
2、 AssertionError有七个构造方法,参数类型有:int,char,long,boolean,double,float和Object
3、 char is the only unsigned integral primitive type in Java.
4、 接口不能用abstract修饰
5、 子类要覆盖父类中的方法,其权限不能低于父类的访问权限,可以和父类方法的访问权限一样或高于父类的访问权限
6、 把64转换为16进制输入:(1)System.out.println(Integer.toString(64, 16));(2)System.out.println(Integer.toHexString(64));
7、 Finalize()的原形:protected void finalize() throws Throwable
8、 在线程中执行yield()方法,如果没有比当前线程优先级高的线程则继续执行当前线程
9、 本地方法不能有方法体(和抽象方法、接口中的方法一样不能有方法体)
10、              Substring(begin,end)取的是end – begin个字符,字符是从0开始计的
11、              抽象方法不能用abstract的final修饰
12、              wait()       sleep()     suspend()              yield()均可以引起一个线程的停止
13、              trim()是消除字符中前导和后导空格
14、              两个线程同时运行,顺序依赖于操作系统和JVM
15、              Null可以做为Boolean的参数,输入的Boolean值为false,还可以用非true和false的字符进行初始化,默认值为false
16、               
 
Test2
1、如果在一个Thread类中找不到public void run(){},启动线程没有任何错误,但也没有任何输出
2、在switch语句块里,不管default放在哪,和执行顺序没有任何关系
3、当类里没有任何构造方法的时候才会调用默认的无参构造方法
4、static-->static(OK)、static-->nonstatic(error)、nonstatic-->static(error)
5、HashMap is thread-safe whereas Hashtable is not
10、Which of the following assignment statements is invalid?
A、double D = 45456.444;
B、long L = 45784;
C、int I = L;
D、int J = (int)D;
答案:C
因为不能将高精度的类型转换到低精度的类型
 
14、What will happen when you attempt to compile and run the following code snippet?
 
Hashtable table = new Hashtable();
       table.put("Java", "Platform Independence");
       table.put("Java", "Great Language!");
       System.out.println("Java : " + table.get("Java"));
 
A、The code will not compile
B、It will print-Java : Platform Independence
C、It will print-Java : Great Language!
D、It will print-Java : Platform IndependenceGreat Language!
E、Runtime Exception
 
答案:C
解释:
C is the correct choice. Hashtable implements interface Map. The put method specified by Map states that if the map previously contained a mapping for a particular key ("Java" here), the old value would be replaced by the new value specified. Hence, "Great Language!" replaces the value "Platform Independence" for the key "Java".
Also, the put method of Hashtable returns previous value associated with specified key, or null if there was no mapping for key.
中文解释:因为key(“Java”, "Great Language!”)覆盖了前面的("Java", "Platform Independence");
 
15、AssertionError是继承于Error类,属于lang包,lang包是自动引入的,所以不用再引入什么包了,记住了???
 
16、Given the following code segment. In what order will the output be produced?
 
class classC extends classA
{
       public classC()
       {
               System.out.println("C");
        }
}
 
class classA
{
       public classA()
       {
               System.out.println("A");
        }
}
 
class classB extends classC
{
       public classB()
       {
              System.out.println("B");
       }
}
 
public class ABC
{
       public static void main(String args[])
       {
              System.out.println("Main");
              classB c = new classB();
 
       }
}
A、C, B, B, Main
B、Main, A, C, B
C、Main, A, B, C
D、Main, A, B, A
正确答案:B
解释:B is correct. Java's construction order is from bottom to up, from the child class constructor the parent's constructor will be called. Thus, the order would be Main, A, C, B.
中文解释:这样的题只要记住一句话:在new一个父类之前,要首先初始化父类(只执行父类的构造方法),然后才初始化子类(执行构造方法)。多简单呀
 
20、Given the following code, what will be the output?
class Value
{
       public int i = 15;
}
 
public class Test2
{
       public static void main(String argv[])
       {
              Test2 t = new Test2();
              t.first();
         }
 
       public void first()
       {
                int i = 5;
                Value v = new Value();
              v.i = 25;
              second(v, i);
                  System.out.println(v.i);
 
       }
 
       public void second(Value v, int i)
       {
              /*if(v instanceof Value)
                     System.out.println("true");*///这里将会输出true
              i = 0;
                v.i = 20;
              Value val = new Value();
                v = val;
              System.out.println(v == val);//这里将会输入true,看来val和v是指向的一个内在地址了,怪不得把val赋给v后,输入v.i还是15呢,呵呵
              但在输出i的时候怎么输出的不是5呢,JAVA里的引用都传值,不是传的地址,下面这行语句输出的是局部变量i的值I = 0
               System.out.println(v.i + " " + i);
                
       }
}
 
21、Which of the following are static methods of Thread class?
A、sleep
B、yield
C、wait
D、notify
答案:AB
解释:sleep和yield属于Thread的方法,但wait和notify属于Object包,Thread继承于Object,怪不得在Thread类里可以用wait和notify呢。^_^
 
关于interface:
1.interface前面只能有两种修饰符:abstract和public
2.varibles前面只能有3种修饰符:final、static和public。默认为final、static和public(如果interface为public的话)
3.method起面只能有两种修饰符:abstract和public.默认为abstract和public(如果interface为public的话)
4.Interfaces 没有 constructors。
5.因为varibles默认为final,所以必须要赋初值。
 
23、Given the following code:
 
public interface MyClass
{
     void myMethod();
}
 
The class which implements MyClass
A、Should have myMethod which must necessarily be public.
B、Should have myMethod which could be "friendly" or public.
C、Should have myMethod which should not throw any checked exceptions.
D、Should have myMethod which cannot be synchronized.
 
解释:A and C are correct answers. A is correct as by default methods in an interface are public. You can't override a method to another method which is more private then the original method, i.e. a public method can be overridden to only public. B is incorrect for the same reason. Also the overridden method can't throw any Exception if its not thrown in the original method. D is incorrect as you can implement a non synchronized method to synchronized method.
*******************************************************************************
Static 方法总结:
一个final类不能定义为abstract的,因为abstract的类需要被其他类继承的,晕了吧。
再记一条:static 方法不能被非static 方法覆盖(或重写),static方法也不能覆盖非static的方法
 
34、What will be printed out if this code is run with the following command line?
 
java AnyProg Well Done
 
public class AnyProg
{
       public static void main(String argv[])
       {    
              System.out.println(argv[2]);
       }
}
答案:Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"
数组的大小是 定义时的数减一,一共才两个值你让它显示第三个,找不到呀它就去现异常了
 
 
35、1. String s = "abcd";
2. Integer x = new Integer(3);
3. String s2 = s + 4;
4. s2 = null;
5. s = null;
问在s2在第几行符合垃圾收集的条件??
答:在第4行时,没有任何对象引用s2了
*********************************************************
关于内部类的几点:
(1)    Variables defined inside inner classes cannot be static unless the inner class itself is static
如果你非要在非静态内部类里定义的话,编译提示: 内部类不能有静态声明
(2)    Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables.
(3)    An inner class can actually be a subclass of the outer class
(4)    Inner classes can be declared as private. Top level, outer classes cannot.
(5)    如果在一个方法中定义了一个内部类,想让内部类能够存取包装它的方法中的变量的话,就必须把变量设为 最终类型 (final),否则将会出现错误: 从内部类中访问局部变量;需要被声明为最终类型
 
**************************************
Static的方法不能被非Static 的方法所覆盖
 
关于集合框架的东东:
(1)       Hashtable和HashMap都继承于Map,他们能够存取所有Object类型的对象数据
Map中的方法:
public Object put(Object key, Object value)
public Object get(Object key)
HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。
HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。
HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。
Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。
最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。
Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。
 
关于线程:
(1)    如果一个Thread类没有写run()的话,程序编译通过OK,调用start()方法的时候没有任何动作,正常运行。
 
关于同步的解释: Ensures only one thread at a time may access a method or object.
 
Test3
1、Vector v = new Vector(100);
v.addElement("99");
2、在定义float型数据时,如果是一个没有带小数点的可以不在数后加f ,但如果有小数点则要加上f ,否则编译不通过,提示可能损失精度
3、hashCode()原形:public int hashCode()
4、再说一次,接口不能有构造方法
5、不能在定义前使用一个变量
6、如果是一个类成员变量(static int I = 10),无论修改多少次只保存最后一次修改的值
7、"transient" is a keyword used for preventing variables in an object from being serialized. It cannot be applied to methods. "volatile" is used to inform the compiler that a variable may be changed asynchronously with regard to its current definition (e.g., from a native method outside the JVM).
8、内部类可以存取包装它的类中的所有成员,无论是什么修饰符修饰的。
9、关于Assertion:
       assert statements in a program may not always be executed
       Assertions can be enabled or disabled programmatically
10、两数相除,如果除数是int 型的0则会抛出AirthmeticException异常,但除数是浮点型的数据时不会抛出异常,会输出 Infinity
11、Byte只有两个构造方法:public Byte(byte value)            
public Byte(String s)
     throws NumberFormatException
12、八进制、十进制、十六进制混合相加,把各个进制化成对应的十进制进行相加
13、关于try..catch…finally的几点:
       (1)A try block can be followed either by a catch block or a finally block, or both
       (2) A catch block must always be associated with a try block
       (3) A finally block can never stand on its own (that is, without being associated with try block)
14、What will be printed when you execute the following code?
class C
{
       C()
       {
              System.out.print("C");
       }
}
 
class A
{
        C c = new C();
       A()
       {
              this("A");
              System.out.print("A");
       }
        A(String s)
       {
             System.out.print(s);
       }
}
 
class B extends A
{
        B()
       {
              super("B");
              System.out.print("B");
       }
 
        public static void main(String[] args)
       {
                new  B();
        }
}
你可能会认为他输出CAB,但结果是CBB
如果在子类中存在super()语句的话,就会调用父类的有参构造函数
 
21、What will happen when you compile and run the following code?
 
public class Test
{
       public void myMethod(Object o)
       {
              System.out.println("My Object");
       }
 
       public void myMethod(String s)
       {
              System.out.println("My String");
       }
 
       public static void main(String args[])
       {
              Test t = new Test();
              t.myMethod(null);
       }
 
}
A、The code does not compile.
B、The code compiles cleanly and shows "My Object".
C、The code compiles cleanly and shows "My String"
D、The code throws an Exception at Runtime.
答案:
C is correct as when null is passed as an argument to the method myMethod(String s) will be called. Had myMethod(String s) not present , myMethod(Object o) would be called.
 
24、What will happen when you compile and run the following code?
 
public class MyClass
{
       static int x;
      
       public static void main(String args[])
       {
              x = 5;
              MyClass m1 = new MyClass();
              MyClass m2 = new MyClass();
              MyClass m3 = new MyClass();
 
              m1.x = 10;
              m2.x = 20;
              m3.x = 30;
 
              System.out.println(m1.x);
              System.out.println(m2.x);
              System.out.println(m3.x);
 
       }
}
输出结果是:
30
30
30
因为x 是类成员变量,所以对象m1,m2,m3都指导向一个x
 
 
Test4
Test5
1、 Thread类中sleep()方法的睡眠时间是用毫秒表示的,不是用的秒
2、 Math.random()的返回类型是double
3、 一个带有私有构造方法的类是不能被其他类继承的,因为在子类初始化的时候会调用父类的构造方法,而父类的构造方法是私有的,将出现错误。
4、 不能强行进行垃圾回收,但可以用System.gc()或者Runtime.getRuntime().gc()来建议垃圾回收
5、 八进制数据是从0~7,如果在八进制数中出现7以上的数则出现错误
6、 在方法头部用throws 异常名,然后再在方法体内抛出异常没有错误
7、 StringBuffer没有覆盖equals方法
 
 
 
 
 
Test6
1、 如果在方法内定义一个变量不赋予它初值,则编译会出错;但如果在类中定义变量则编译通过。
2、 获得数组的大小是用length,而获得字符串的长度用length(),已经错了不是一次了
3、 Vector和Hashtable是安全的,HashMap没有Hashtable安全
4、 构造方法不通常都是public的
5、 0.0 == -0.0编译OK        0 == -0编译OK
6、 在求余和除法运算时,如果除数为0编译没有错误,在运行时会抛出ArithmeticException。
7、 Local variables cannot be declared as static.
8、 有父类Parent和子类Child,Parent p = new Child();p属于父类(Parent)
9、 移位运算:int i = 1;        i << 32和没有移位结果一样,(32 % 32 = 0)
i << 33 和 i << 1结果一样,因为33减int型的模(32)等于1
 
Test7
1、 当定义一个Thread的子类时,MyThread mt = new MyThread();类中有run(),但在调用的时候却调用了mt.run();没有调用start()启动线程,会调用MyThread类中的run()方法
2、 本地方法没有方法体,可以定义static的
3、 类中如果有静态块(static codes),当类被载入到JVM时自动执行。
4、 在一个Thread的子类中同时存在start()和run(),start()被重写
5、 可以把一个子类强制类型转换后赋给父类,但不能把一个父类赋给子类
6、  
 
 
Test8
1、 定义一个二维数组,如果第二维没有初始化,在用第二维数组的时候会抛出异常
2、 要覆盖Object下的public boolean equals(Object obj){}方法,必须再覆盖public int hashCode()才能够完全覆盖equals()方法的
 
Flow Control And Assertion
1、What results from the following code fragment?
1. int i = 5;
2. int j = 10;
3. System.out.println(i + ~j);
结果是多少呢,你可能要算了会,一个简单的方法
~j = (-j)-1
也就是说,输出 5 + (-11) = -6
2、What will be the result when you attempt to compile and run the following code?
 
public class Conv
{
    public static void main(String argv[])
    {
        Conv c = new Conv();
        String s = new String("ello");
        c.amethod(s);
    }
   
    public void amethod(String s)
    {
        char c = 'H';
        c += s;
        System.out.println(c);
    }
}
此程序在编译时会有错误,因为 c是char型的,而s是String型的,char型的长度小于String的,不行把String强制类型转换到char
3、What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)
     
    public class AssertTest
    {
        public void methodA(int i)
        {
            assert i >= 0 : methodB();
            System.out.println(i);
        }
   
        public void methodB()
        {
            System.out.println("The value must not be negative");
        }
 
        public static void main(String args[])
        {
            AssertTest test = new AssertTest();
            test.methodA(-10);
        }  
    }
此程序会编译错误
assert Expression1;
assert Expression1 : Expression2;
而程序中 assert i >= 0 : methodB();中的methodB()是一个void返回类型,Expresstion2必须是一个表达式才可以,所以会导致编译错误
 
4、在除法、求余运算中,如果除数为0则会抛出AirthmticException
5、0.0 == -0.0 编译OK,但5 == -5  NOT OK
6、int i = (int)(Math.random() * 100); i的值只能是0~99,不可能到100
 
7、What results from compiling and running the following code fragment?
int a = 3;
int b = 5;
System.out.println(a + ' ' + b);//可以这样写System.out.println(3 + 32 + 5);
执行结果为 40                ' '对应的ASCII码为32
***在这样的运算中,把低位类型的数据转换为在此表达式中的高位类型
 
 
 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值