作各种猥琐的Java笔试/面试题目

 原题 http://blog.csdn.net/smcwwh/article/details/7315041,自己做的一些心得体会

 1.

 public static void main(String[] args) {  
        int k = 0;  
        int ret = ++k + k++ + ++k + k;  
        // ret的值为多少   
        System.err.println(ret);  

1+1+3+3=8

2.

int i1 = 10, i2 = 10;  
        System.err.println("i1 + i2 = " + i1 + i2);  
        System.err.println("i1 - i2 = " + i1 - i2);  
        System.err.println("i1 * i2 = " + i1 * i2);  
        System.err.println("i1 / i2 = " + i1 / i2);  

i1 + i2 = 1010(String)

异常(好猥琐 String-in t)
i1 * i2 = 100
i1 / i2 = 1

3.

public void myMethod(String str) {  
        System.err.println("string");  
    }  
      
    public void myMethod(Object obj) {  
        System.err.println("object");  
    }  
      
    public static void main(String[] args) {  
        Test t = new Test();  
        t.myMethod(null);  
    }  

String (子类优先匹配的原则)

4.

Date date = new Date();  
        System.err.println(date.getMonth() + " " + date.getDate());  

8 8 

getMonth返回的是0-11

5.

 double val = 11.5;  
        System.err.println(Math.round(val));  
        System.err.println(Math.floor(val));  
        System.err.println(Math.ceil(val));  

12 

11.0

12.0

Math.round返回最接近参数的 long

Math.floor返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。

Math.ceil 返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。

6.编程输出一个目录下的所有目录及文件名称,目录之间用tab

public static void main(String[] args) {  
        new Test().read("D:/test", "");  
    }  
      
    public void read(String path, String tab) {  
        File file = new File(path);  
        File[] childFiles = file.listFiles();  
        for (int i = 0; childFiles != null && i < childFiles.length; i++) {  
            System.err.println(tab + childFiles[i].getName());  
            if (childFiles[i].isDirectory()) {  
                read(childFiles[i].getPath(), tab + "\t");  
            }  
        }  
    }  


7.从键盘读入10个整数,然后从大到小输出

 public static void main(String[] args) {  
        Scanner in = new Scanner(System.in);  
        // 注意这里的数组,不是int的   
        Integer[] arr = new Integer[10];  
        for (int i = 0; i < 10; i++) {  
            arr[i] = in.nextInt();  
        }  
        Arrays.sort(arr, new Comparator<Integer>() {  
            @Override  
            public int compare(Integer o1, Integer o2) {  
                if (o1 > o2) return -1;  
                if (o1 < o2) return 1;  
                return 0;  
            }  
              
        });  
        System.err.println(Arrays.toString(arr));  
    }  


8.

public static void main(String[] args) {  
        Base b = new Test();  
        b.method();  
          
        Test t = new Test();  
        t.method();  
    }  
  
    @Override  
    public void method() {  
        System.err.println("test");  
    }  
      
}  
  
class Base {  
    public void method() throws InterruptedException {  
        System.err.println("base");  
    }  


test

test

9.

public class Test extends Base {  
  
    public static void main(String[] args) {  
        new Test().method();  
    }  
  
    public void method() {  
        System.err.println(super.getClass().getName());  
        System.err.println(this.getClass().getSuperclass().getName());  
    }  
      
}  
  
class Base {  
}  

Test

Base

在test方法中,直接调用getClass().getName()方法,返回的是Test类名
由于getClass()在Object类中定义成了final,子类不能覆盖该方法,所以,在
test方法中调用getClass().getName()方法,其实就是在调用从父类继承的getClass()方法,等效于调用super.getClass().getName()方法,所以,super.getClass().getName()方法返回的也应该是Test。
如果想得到父类的名称,应该用如下代码:
getClass().getSuperClass().getName();
————————————————————
以上是题目和分析,我也做了测试,结果确实是Test,但是我看不明白下面的分析,我觉得他说的模棱两可,没有说明白实质问题。
为此,我查阅了Object的getClass()方法的API,解释是:返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。
看了这个,我很清楚this.getClass()是返回运行时对象的类型,但是super.getClass()为何返回的还是子类的类型,还是不明白。
后来在网上查了super的含义,有一个帖子说super不是超类的引用,而是表示在子类中调用父类的方法或属性而已,并且给出例子
class B extends A{
  public void print(){
  System.out.println(super.getClass());//调用A类中的getclass()方法,A是Object的子类,A中的getClass()是Object中的,运行时期的实例是B类,所以输出的依然是Class B
  System.out.println(this.getClass()); //调用B类中的getclass()方法,此方法从A继承的,A从Object继承的,运行时期的实例是B类,所以输出的是Class B

 
}
我认为后面的解释更加说的通一些,但是我不确定解释是不是正确,所以,发到版上来,请版上的高手帮忙看一下,给出合理的解释。因为这道笔试题的所谓答案也已经被转载了很多次了,如果解释真是不正确了,那就蒙蔽了很多人了。。。

原文链接http://blog.sina.com.cn/s/blog_60cad3500102dx11.html

10.

public static void main(String[] args) {  
        String str1 = new String("abc");  
        String str2 = new String("abc");  
        System.err.println(str1.equals(str2));  
          
        StringBuffer sb1 = new StringBuffer("abc");  
        StringBuffer sb2 = new StringBuffer("abc");  
        System.err.println(sb1.equals(sb2));  
    }  

true

false

String类提供了一些方法,用来进行字符串的比较。这个类实现了Object父类的equals()方法,用来比较两种字符串的值是否相等。

但是StringBuffer类并没有实现Objcet类的Equals方法,所以不能用这个方法来比较两个StringBuffer类的字符串是否相等

详细情况连接http://www.west263.com/info/html/chengxusheji/Javajishu/20080403/57254.html

11.

public static void main(String[] args) {  
        System.err.println(new Test().method1());  
        System.err.println(new Test().method2());  
    }  
      
    public int method1() {  
        int x = 1;  
        try {  
            return x;  
        } finally {  
            ++x;  
        }  
    }  
      
    public int method2() {  
        int x = 1;  
        try {  
            return x;  
        } finally {  
            return ++x;  
        }  
    }  

1

2

 finally 块必须与 try 或 try/catch 块配合使用。此外,不可能退出 try 块而不执行其 finally 块。如果 finally 块存在,则它总会执行。(无论从那点看,这个陈述都是正确的。有一种方法可以退出 try 块而不执行 finally 块。如果代码在 try 内部执行一条 System.exit(0); 语句,则应用程序终止而不会执行 finally 执行。另一方面,如果您在 try 块执行期间拨掉电源,finally 也不会执行。)

finally 结构使代码总会执行,而不管有无异常发生其次try, catch, finally中fianlly的throw/return的级别最高
12.

 synchronized  与static synchronized  的区别

:A: synchronized static是某个类的范围,synchronized static cSync{}防止多个线程同时访问这个    类中的synchronized static 方法。它可以对类的所有对象实例起作用。
  
               B: synchronized 是某实例的范围,synchronized isSync(){}防止多个线程同时访问这个实例中的synchronized 方法。



     2.synchronized方法与synchronized代码快的区别
      synchronized methods(){} 与synchronized(this){}之间没有什么区别,只是
synchronized methods(){} 便于阅读理解,而synchronized(this){}可以更精确的控制冲突限制访问区域,有时候表现更高效率。


     3.
synchronized关键字是不能继承的
     这个在http://www.learndiary.com/archives/diaries/2910.htm一文中看到的,我想这一点也是很值得注意的,继承时子类的覆盖方法必须显示定义成synchronized

原文http://www.cnblogs.com/shipengzhi/articles/2223100.html

13.

 public static void main(String[] args) {  
        Integer i1 = 127;  
        Integer i2 = 127;  
        System.err.println(i1 == i2);  
          
        i1 = 128;  
        i2 = 128;  
        System.err.println(i1 == i2);  
    }  

true

false

对于整数,Integer类中设置了缓存,保存了(-128)~127, 所以当创建小于128的整数对象时不是每次都分配一个新对象,而是先判断的*
14.

 public static void main(String[] args) {  
        String str1 = "a";  
        String str2 = "a";  
        String str3 = new String("a");  
          
        System.err.println(str1 == str2);  
        System.err.println(str1 == str3);  
        str3 = str3.intern();  
        System.err.println(str1 == str3);  
    }  

true
false
true

intern()返回字符串对象的规范化表示形式。

15. 

 System.err.println(12 - 11.9 == 0.1);  

false

0.5在计算机中可以精确表示,而0.1无法精确用二进制表示.同理0.75等计算起来应该都是对的
深入http://blog.csdn.net/smcwwh/article/details/7080878

16.

 BigInteger one = new BigInteger("1");  
        BigInteger two = new BigInteger("2");  
        BigInteger three = new BigInteger("3");  
        BigInteger sum = new BigInteger("0");  
        sum.add(one);  
        sum.add(two);  
        sum.add(three);  
        System.out.println(sum.toString());  

0

最后输出的还是sum,这破名起的

BigInteger c= sum.add(three);  
		        System.out.println(c);  

这样输出c才是求得和

17.

Set<String> set = new HashSet<String>();  
        set.add("one");  
        set.add("two");  
        set.add("three");  
        set.add("four");  
        set.add("five");  
        for (Iterator<String> it = set.iterator(); it.hasNext();) {  
            System.err.println(it.next());  
        }  

无序



 


 






 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值