六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

一个简单的java测试题

[复制链接]

升级  72%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
36
 楼主| 发表于 2013-1-27 06:19:52 | 显示全部楼层 |阅读模式
  今天做了一份看似简单的java测试题,但。。。看来java基础知识还是很重要,要加强。 第9题还要编译测试下,第10题可参考下sevlet线程安全研究文章。
<o:p></o:p>
<o:p></o:p>
            
1.   <o:p></o:p>

            
            Consider the following code. Please list which lines below will not compile. (choose all that apply)
             <o:p></o:p>
            
                   
  • public class acd {               
  •     public static void main(String arg[])               
  •     {            
                  4.        Object obj1 =   new Object();
                  5.        char c       =   3;
                  6.         short    s    =       41;
                  7.         byte b     =   41;
                  8.         s    =   b*b;
                  9.         Double      d       =     new Double(4.12f);
                10.        Float        f     = new Double(2.33);
                11.        d  =    obj1;
                12.        Float []ff     =   new Float[5];
                 13.       float  f1      =    ff[0];
                 14.       obj1 = ff;
                 15.      obj1   = ff[5];
                 16.     }
            17. }
             <o:p></o:p>
            8,10,11
            另一类似题: 
            Which of the following statements are legal?
              A. long l = 4990;
              B. int i = <st1:chmetcnv w:st="on" tcsc="0" unitname="l" sourcevalue="4" negative="False" numbertype="1" hasspace="False">4L</st1:chmetcnv>;
              C. float f = 1.1;
              D. double d = 34.4;
              E. double t = <st1:chmetcnv w:st="on" tcsc="0" unitname="F" sourcevalue=".9" negative="False" numbertype="1" hasspace="False">0.9F</st1:chmetcnv>.
            
              题目:下面的哪些声明是合法的。
                答案: (ade)
              此题的考点是数字的表示法和基本数据类型的类型自动转换,没有小数点的数字被认为是int型数,带有小数点的数被认为是double型的数,其它的使用在数字后面加一个字母表示数据类型,加l或者L是long型,加d或者D是double,加f或者F是float,可以将低精度的数字赋值给高精度的变量,反之则需要进行强制类型转换,例如将int,short,byte赋值给long型时不需要显式的类型转换,反之,将long型数赋值给byte,short,int型时需要强制转换(int a=(int)<st1:chmetcnv w:st="on" tcsc="0" unitname="l" sourcevalue="123" negative="False" numbertype="1" hasspace="False">123L</st1:chmetcnv>;)。<o:p></o:p>
             
            
                  
            
            
2.   <o:p></o:p>

            
            Consider the source code of the following class UIDTest
            1.         class UIDTest
            2.         {
            3.           long testing (float a, float b) {return 0;}
            4.   
            5.    }
             <o:p></o:p>
            Which one of the following methods would be legal, at line 4 in class UIDTest. (choose all that apply)
             <o:p></o:p>
            
                   
  • String testing (float a, float b){ return “acd”;}               
  • long testing (float a, float b, int i ) { return 0;}               
  • long testing (float a, float b ) throws Exception { return ‘0’;}               
  • float testing (float a, int b ) { return 0.01;}               
  • double testing (float a, int b, int c ) { return 1e10;}            
            a,b,d,e
            答案是 b ,e
            方法重载的规则是:一、参数列表必须不同,个数的不同完全可以,如果个数相同则参数类型的不同不能引起歧意,例如int 和long,float和double就不能作为唯一的类型不同;二、返回值可以不同,但是不能是重载时唯一的不同点(这点和c++中不同,c++中返回类型必须一致)。<o:p></o:p>
            
             <o:p></o:p>
            
            
3.   <o:p></o:p>

            
            True or False: In the code fragment below, line 4 is executed.
            
                   
  • String s1 = “UID”               
  • String s2 = “UID”               
  • if(s1 == s2)               
  •   System.out.println(“ UID is printed”);            
             <o:p></o:p>
            A. True
            B.  False
            B
            
            <span />
            答案应该是A。参考一类似题:
            String s= "hello";
              String t = "hello";
              char c[] = {'h','e','l','l','o'} ;
              Which return true?
              A. s.equals(t);
              B. t.equals(c);
              C. s==t;
              D. t.equals(new String("hello"));
              E. t==c.
            
              题目:哪些返回true。
                答案: (acd)
              这个在前面第10题的equals()方法和==操作符的讨论中论述过。==操作符比较的是操作符两端的操作数是否是同一个对象,而String的equals()方法比较的是两个String对象的内容是否一样,其参数是一个String对象时才有可能返回true,其它对象都返回假。需要指出的是由于s和t并非使用new创建的,他们指向内存池中的同一个字符串常量,因此其地址实际上是相同的(这个可以从反编译一个简单的测试程序的结果得到,限于篇幅不列出测试代码和反编译的分析),因此答案c也是正确的。
            
                  
            
            
4.   <o:p></o:p>

            
            Given a string constructed by calling s = new String(“Hung”), which of the call listed below modify the string? (choose all that apply)
            
                   
  • s. append(“xxx”);               
  • s.trim();               
  • s.substring(3)               
  • s.replace(‘z’.’a’);               
  • s.concat(s);               
  • Non of the above            
            F<o:p></o:p>
            
             <o:p></o:p>
            
            
5.   <o:p></o:p>

            
            Which would be the most suitable for storing data elements that must not appear in the store more than once, if searching is not a priority? 
            
                   
  • java.util.Collection               
  • java.util.List               
  • java.util.Set               
  • java.util.Map               
  • java.util.Vector               
  • java.util.HashSet            
            C,F<o:p></o:p>
            
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
            
            
6.   <o:p></o:p>

            
            Which one of the following statements is true? (choose all that apply)
            
                   
  • Transient methods may not be overridden.               
  • Transient classes may not be serialized.               
  • Transient variables must be static.               
  • Transient variables are not serialized.            
             <o:p></o:p>
            D
            
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
             <o:p></o:p>
            
            
7.   <o:p></o:p>

            
            Consider the source code of the following source code, which is in UIDTest.java
             <o:p></o:p>
            1.        class Test
            2.        {
            3.               long result (float a, float b) {return 0;}
            4.        }
             <o:p></o:p>
            5.        public class UIDTest extends Test
            6.        {
            7.                 <o:p></o:p>
            8.        }
             <o:p></o:p>
            Which of the following method would be legal (individual), at line 7.  (choose all that apply)
             <o:p></o:p>
            
                   
  • public long result (float a, float b) {return 0;}                 
  • public long result (float a, float b) throws Exception {return 0;}               
  • public long result (float a, float b, int c) {return 0;}               
  • public long result (float a, float b, int c) throw Exception {return 0;}               
  • public String result2 (float a, float b) throw Exception {return "0";}               
  • public  String  result2(float a, float b) {return "0"; }            
            a,b,c,d,e,f     
            惭愧,上面答案b是错误的,重写的方法比父类的方法抛出更多的异常了! 注:java中方法重写的一个重要而且容易被忽略的规则是重写的方法的访问权限不能比被重写的方法的访问权限低!重写的另一个规则是重写的方法不能比被重写的方法抛弃(throws)更多种类的异常,其抛弃的异常只能少,或者是其子类,不能以抛弃异常的个数来判断种类,而应该是异常类层次结果上的种类。
              另一类似题:
                  public class Parent {
              public int addValue( int a, int b) {
              int s;
              s = a+b;
              return s;
              }
              }
              class Child extends Parent {
              }
              Which methods can be added into class Child?
              A. int addValue( int a, int b ){// do something...}
              B. public void addValue (){// do something...}
              C. public int addValue( int a ){// do something...}
              D. public int addValue( int a, int b )throws MyException {//do something...}
              题目:哪些方法可以加入类Child中。
                答案: (bc)
              此题涉及方法重载(overload),方法重写(override)以及类派生时方法重写的规则。方法重载的规则是:一、参数列表必须不同,个数的不同完全可以,如果个数相同则参数类型的不同不能引起歧意,例如int 和long,float和double就不能作为唯一的类型不同;二、返回值可以不同,但是不能是重载时唯一的不同点(这点和c++中不同,c++中返回类型必须一致)。方法重写发生在类继承时,子类可以重写一个父类中已有的方法,必须在返回类型和参数列表一样时才能说是重写,否则就是重载,java中方法重写的一个重要而且容易被忽略的规则是重写的方法的访问权限不能比被重写的方法的访问权限低!重写的另一个规则是重写的方法不能比被重写的方法抛弃(throws)更多种类的异常,其抛弃的异常只能少,或者是其子类,不能以抛弃异常的个数来判断种类,而应该是异常类层次结果上的种类。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表