java笔试题整理

4 篇文章 0 订阅

1、运算符优先级问题,下面代码的结果是多少?

 

 
  1. public class Test {

  2. public static void main(String[] args) {

  3. int k = 0;

  4. int ret = ++k + k++ + ++k + k;

  5. // ret的值为多少

  6. System.err.println(ret);

  7. }

  8. }

解答:主要考察++i和i++的区别。++在前则先自增再赋值运算,++在后则先赋值再自增运算。因此,结果为8。

 

 

2、运算符问题,下面代码分别输出什么?

 

 
  1. public class Test {

  2. public static void main(String[] args) {

  3. int i1 = 10, i2 = 10;

  4. System.err.println("i1 + i2 = " + i1 + i2);

  5. System.err.println("i1 - i2 = " + i1 - i2);

  6. System.err.println("i1 * i2 = " + i1 * i2);

  7. System.err.println("i1 / i2 = " + i1 / i2);

  8. }

  9. }

解答:主要考察两点,运算符的优先级、字符串与数字中的+为连接符号。第一条中,都是相加,则从前到后的顺序运算,字符串与数字相加,连接为一个字符串,再与后面的数字相加,再次连接为字符串,因此结果为“i1 + i2 = 1010”。第二条是错误的,字符串无法与数字用减号连接。第三条、第四条中乘除的优先级高,会先运算,而后再与字符串连接,因此结果分别为:“i1 * i2 = 100”、“i1 * i2 = 1”。

 

 

3、下面代码的结果是什么?

 

 
  1. public class Test {

  2.  
  3. public void myMethod(String str) {

  4. System.err.println("string");

  5. }

  6.  
  7. public void myMethod(Object obj) {

  8. System.err.println("object");

  9. }

  10.  
  11. public static void main(String[] args) {

  12. Test t = new Test();

  13. t.myMethod(null);

  14. }

  15. }

解答:这道题考察重载方法参数具有继承关系时的调用问题,还有对null的认识。如果是一般具有继承关系的对象分别作为参数,看对象的引用,如:

 

 

 
  1. class A {

  2. }

  3.  
  4. class B extends A {

  5. }

  6.  
  7. public class Test {

  8.  
  9. public static void main(String[] args) {

  10. A b1 = new B();

  11. B b2 = new B();

  12. get(b1);// A

  13. get(b2);// B

  14. }

  15.  
  16. public static void get(A a) {

  17. System.out.println("A");

  18. }

  19.  
  20. public static void get(B a) {

  21. System.out.println("B");

  22. }

  23. }

这道题中,Object是一切类的父类,具有继承关系,那null是指向什么呢?null是任何引用类型的初始值,String和Object的初始值都是null,但是null会优先匹配引用类型参数为String的方法,因此这道题答案是string。假设这道题中还有其他同是引用类型的重载方法呢?如:

 

 

 
  1. public void myMethod(Integer obj) {

  2. System.err.println("Integer");

  3. }

如果是这样的话,调用这个方法传入参数null时会报错,他不知道选哪个方法进行匹配调用了。

 

 

4、假设今天是9月8日,下面代码输出什么?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. Date date = new Date();

  5. System.err.println(date.getMonth() + " " + date.getDate());

  6. }

  7. }

解答:这道题考察的是日期中获取的月份是从0开始的,因此会比我们日常的月份少1,这个题答案是8 8。

 

 

5、下面代码的输出结果是什么?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. double val = 11.5;

  5. System.err.println(Math.round(val));

  6. System.err.println(Math.floor(val));

  7. System.err.println(Math.ceil(val));

  8. }

  9. }

解答:这个是在考Math取整数的三种方法。round()是四舍五入取证,floor()是舍去小数位,ceil()是向上进一位。floor是地板ceil是天花板,一个在下,则舍去,一个在上,则向上进1。那是不是结果应该为12、11、12呢?还要考虑返回值类型,round()返回值类型为long长整型,floor()和ceil()返回值的是double类型,因此正确的答案应该是12、11.0、12.0。

 

 

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

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. new Test().read("D:/test", "");

  5. }

  6.  
  7. public void read(String path, String tab) {

  8. File file = new File(path);

  9. File[] childFiles = file.listFiles();

  10. for (int i = 0; childFiles != null && i < childFiles.length; i++) {

  11. System.err.println(tab + childFiles[i].getName());

  12. if (childFiles[i].isDirectory()) {

  13. read(childFiles[i].getPath(), tab + "\t");

  14. }

  15. }

  16. }

  17. }

这个主要是考察IO部分知识点了。

 

 

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

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. Scanner in = new Scanner(System.in);

  5. // 注意这里的数组,不是int的

  6. Integer[] arr = new Integer[10];

  7. for (int i = 0; i < 10; i++) {

  8. arr[i] = in.nextInt();

  9. }

  10. Arrays.sort(arr, new Comparator<Integer>() {

  11. @Override

  12. public int compare(Integer o1, Integer o2) {

  13. if (o1 > o2) return -1;

  14. if (o1 < o2) return 1;

  15. return 0;

  16. }

  17.  
  18. });

  19. System.err.println(Arrays.toString(arr));

  20. }

  21.  
  22. }


8、下面代码的结果是什么?

 

 

 
  1. public class Test extends Base {

  2.  
  3. public static void main(String[] args) {

  4. Base b = new Test();

  5. b.method();

  6.  
  7. Test t = new Test();

  8. t.method();

  9. }

  10.  
  11. @Override

  12. public void method() {

  13. System.err.println("test");

  14. }

  15.  
  16. }

  17.  
  18. class Base {

  19. public void method() throws InterruptedException {

  20. System.err.println("base");

  21. }

  22. }

解答:两次调用输出都是test。多态的情况下,尽管是父类的引用,调用方法时,还是调用子类的方法。

 

 

9、以下代码的结果是什么?

 

 
  1. package test;

  2.  
  3. public class Test extends Base {

  4.  
  5. public static void main(String[] args) {

  6. new Test().method();

  7. }

  8.  
  9. public void method() {

  10. System.err.println(super.getClass().getName());

  11. System.err.println(this.getClass().getSuperclass().getName());

  12. }

  13.  
  14. }

  15.  
  16. class Base {

  17. }

解答:第一个输出test.Test、第二个输出test.Base。super很容易让人以为也是调用了父类,实际上还是本类。具体原因也没太搞懂,有懂的同学欢迎留言。

 

 

10、true or false?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. String str1 = new String("abc");

  5. String str2 = new String("abc");

  6. System.err.println(str1.equals(str2));

  7.  
  8. StringBuffer sb1 = new StringBuffer("abc");

  9. StringBuffer sb2 = new StringBuffer("abc");

  10. System.err.println(sb1.equals(sb2));

  11. }

  12. }

解答:第一个true,第二个false。String重写了Object中的equals方法,会将string拆分为字符数组,逐个比较各个字符,代码如下:

 

 

 
  1. public boolean equals(Object anObject) {

  2. if (this == anObject) {

  3. return true;

  4. }

  5. if (anObject instanceof String) {

  6. String anotherString = (String)anObject;

  7. int n = value.length;

  8. if (n == anotherString.value.length) {

  9. char v1[] = value;

  10. char v2[] = anotherString.value;

  11. int i = 0;

  12. while (n-- != 0) {

  13. if (v1[i] != v2[i])

  14. return false;

  15. i++;

  16. }

  17. return true;

  18. }

  19. }

  20. return false;

  21. }

Object中的equests方法如下:

 

 

 
  1. public boolean equals(Object obj) {

  2. return (this == obj);

  3. }


11、输出的结果是什么?

 

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. System.err.println(new Test().method1());

  5. System.err.println(new Test().method2());

  6. }

  7.  
  8. public int method1() {

  9. int x = 1;

  10. try {

  11. return x;

  12. } finally {

  13. ++x;

  14. }

  15. }

  16.  
  17. public int method2() {

  18. int x = 1;

  19. try {

  20. return x;

  21. } finally {

  22. return ++x;

  23. }

  24. }

  25. }

解答:第一个返回1,第二个返回2。finally中的代码是一定会被执行的且在try中的代码执行完之后,因此若在其中return返回,会覆盖掉try中的返回值。

 

如果这样呢?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. System.err.println(method());

  5. }

  6.  
  7. public static boolean method() {

  8. try {

  9. return true;

  10. } finally {

  11. return false;

  12. }

  13. }

  14. }

很明显返回值应该为false。

 

 

12、方法m1和m2有区别吗?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. }

  5.  
  6. public synchronized void m1() {

  7. }

  8.  
  9. public static synchronized void m2() {

  10. }

  11. }

解答:这里考察的是同步方法的问题。synchronized修饰方法时锁定的是调用该方法的对象。它并不能使调用该方法的多个对象在执行顺序上互斥,静态修饰符很有必要。因此当不适用静态时,创建多个对象执行该方法,锁都不一样,还同步什么呢,因此用static修饰后才能实现想要的效果。

 

 

13、true or false?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. Integer i1 = 127;

  5. Integer i2 = 127;

  6. System.err.println(i1 == i2);

  7.  
  8. i1 = 128;

  9. i2 = 128;

  10. System.err.println(i1 == i2);

  11. }

  12. }

解答:这个是对Integer包装类型中应用的享元模式的考察。具体可以看一下http://blog.csdn.net/m0_37240709/article/details/78644341

 

 

14、true or false?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. String str1 = "a";

  5. String str2 = "a";

  6. String str3 = new String("a");

  7.  
  8. System.err.println(str1 == str2);

  9. System.err.println(str1 == str3);

  10. str3 = str3.intern();

  11. System.err.println(str1 == str3);

  12. }

  13. }

解答:这个是对String Pool的考察。参看http://blog.csdn.net/m0_37240709/article/details/78642110

 

 

15、true or false?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

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

  5. }

  6. }

解答:结果为false。这个题我只说下我的想法,12-11.9进行运算后会转换成对象,不在是基本数据类型,因此在进行恒等判断时,就会是false。

 

 

16、以下代码输出是什么?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. BigInteger one = new BigInteger("1");

  5. BigInteger two = new BigInteger("2");

  6. BigInteger three = new BigInteger("3");

  7. BigInteger sum = new BigInteger("0");

  8. sum.add(one);

  9. sum.add(two);

  10. sum.add(three);

  11. System.out.println(sum.toString());

  12. }

  13. }

解答:这个是对大整数的考察。结果是不是6呢?看起来好像没毛病,其实不然。sum.add(one)与我们基本类型的sum+=one可不同,前者不会讲结果赋值给sum对象,结果被赋值了这条语句的返回值。因此不管怎么add,sum对象的值是没有变化的,因此结果为0。

 

 

17、输出的结果是什么?

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. Set<String> set = new HashSet<String>();

  5. set.add("one");

  6. set.add("two");

  7. set.add("three");

  8. set.add("four");

  9. set.add("five");

  10. for (Iterator<String> it = set.iterator(); it.hasNext();) {

  11. System.err.println(it.next());

  12. }

  13. }

  14. }

解答:结果顺序是four  one  two  three  five。有懂的同学欢迎留言评论。

 

 

18、如何迭代Map容器?

 

 
  1. Map<String, String> map = new HashMap<>();

  2. Set<Map.Entry<String, String>> entrySet = map.entrySet();

  3. for(Map.Entry<String, String> entry : entrySet){

  4. String key = entry.getKey();

  5. String value = entry.getValue();

  6. }

  7.  
  8. Set<String> keySet = map.keySet();

  9. Iterator<String> it1 = keySet.iterator();

  10. if(it1.hasNext())

  11. System.out.println(it1.next());

  12.  
  13. Collection<String> values = map.values();

  14. Iterator<String> it2 = values.iterator();

  15. if(it2.hasNext())

  16. System.out.println(it2.next());


19、以下代码输出的结果

 

 

 
  1. public class Test {

  2.  
  3. public static void main(String[] args) {

  4. System.err.println(args.length);

  5. }

  6.  
  7. }

  8. /*

  9. A. null B. 0 C. Test

  10. D. Exception in thread "main" java.lang.NullPointerException

  11. */

解答:0.

 

 

20、下面为一个单例的实现代码,请指出代码中有几个错误或不合理之处,并改正。

 

 
  1. public class Test {

  2.  
  3. public Test instance = null;

  4.  
  5. public static Test getInstance() {

  6. if (instance == null) {

  7. instance = new Test();

  8. return instance;

  9. }

  10. }

  11. }

解答:单例模式要满足三点:1、私有化构造方法;2、创建私有静态对象;3、提供公有静态方法获取唯一实例。因此错误包含,1构造函数没有私有化,2对象非私有静态,3获取实例的方法中return不应包含在条件中。单例模式更详细的讲解,参看http://blog.csdn.net/m0_37240709/article/details/78646814

题目参考了http://blog.csdn.net/smcwwh/article/details/7315041

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值