`
xiongzhenhui
  • 浏览: 205645 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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

 
阅读更多
本文收录各种猥琐的Java笔试/面试题,一些比较容易忘记的,不定期更新。也希望大家在底下留言,贴出自己碰到或看到的各种猥琐笔试、面试题目。

J2EE基础部分
1、运算符优先级问题,下面代码的结果是多少?(笔试)


[java] view plaincopyprint?package test; 
 
public class Test { 
    public static void main(String[] args) { 
        int k = 0; 
        int ret = ++k + k++ + ++k + k; 
        // ret的值为多少  
        System.err.println(ret); 
    } 


package test;

public class Test {
public static void main(String[] args) {
int k = 0;
int ret = ++k + k++ + ++k + k;
// ret的值为多少
System.err.println(ret);
}
}2、运算符问题,下面代码分别输出什么?(笔试)


[java] view plaincopyprint?package test; 
 
public class Test { 
    public static void main(String[] args) { 
        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); 
    } 


package test;

public class Test {
public static void main(String[] args) {
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);
}
}3、下面代码的结果是什么?还是抛出异常?(笔试)


[java] view plaincopyprint?package test; 
 
public class Test { 
     
    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); 
    } 


package test;

public class Test {

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);
}
}
4、假设今天是9月8日,下面代码输出什么?(笔试)


[java] view plaincopyprint?package test; 
 
import java.util.Date; 
 
public class Test { 
 
    public static void main(String[] args) { 
        Date date = new Date(); 
        System.err.println(date.getMonth() + " " + date.getDate()); 
    } 


package test;

import java.util.Date;

public class Test {

public static void main(String[] args) {
Date date = new Date();
System.err.println(date.getMonth() + " " + date.getDate());
}
}
5、下面代码的输出结果是什么?


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    public static void main(String[] args) { 
        double val = 11.5; 
        System.err.println(Math.round(val)); 
        System.err.println(Math.floor(val)); 
        System.err.println(Math.ceil(val)); 
    } 


package test;

public class Test {

public static void main(String[] args) {
double val = 11.5;
System.err.println(Math.round(val));
System.err.println(Math.floor(val));
System.err.println(Math.ceil(val));
}
}
6、编程输出一个目录下的所有目录及文件名称,目录之间用tab。(笔试)


[java] view plaincopyprint?package test; 
 
import java.io.File; 
 
public class Test { 
 
    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"); 
            } 
        } 
    } 


package test;

import java.io.File;

public class Test {

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");
}
}
}
}
不要觉得很简单,最起码你要记得返回当前文件夹下的所有文件的方法是listFiles(),isDirectory别拼错了。

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


[java] view plaincopyprint?package test; 
 
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.Scanner; 
 
public class Test { 
 
    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)); 
    } 
     


package test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Test {

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));
}

}
自己手写排序算法的可以无视此题,如果是Arrays.sort()的,请注意Comparator与Comparable接口的区别,别搞混了。

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


[java] view plaincopyprint?package test; 
 
public class Test extends Base { 
 
    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"); 
    } 


package test;

public class Test extends Base {

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");
}
}
9、以下代码的结果是什么?


[java] view plaincopyprint?package test; 
 
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 { 


package test;

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 {
}
10、true or false?
[java] view plaincopyprint?package test; 
 
public class Test { 
 
    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)); 
    } 


package test;

public class Test {

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));
}
}
11、输出的结果是什么?


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    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; 
        } 
    } 


package test;

public class Test {

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;
}
}
}
这样呢?输出什么


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    public static void main(String[] args) { 
        System.err.println(method()); 
    } 
     
    public static boolean method() {  
         try {  
            return true;  
        } finally {  
          return false; 
        }  
    } 


package test;

public class Test {

public static void main(String[] args) {
System.err.println(method());
}

public static boolean method() {
     try {
        return true;
    } finally {
      return false;
    }
}
}
12、方法m1和m2有区别吗?什么区别


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    public static void main(String[] args) { 
    } 
 
    public synchronized void m1() { 
    } 
 
    public static synchronized void m2() { 
    } 


package test;

public class Test {

public static void main(String[] args) {
}

public synchronized void m1() {
}

public static synchronized void m2() {
}
}
13、true or false?理由


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    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); 
    } 


package test;

public class Test {

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);
}
}
14、true or false?理由


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    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); 
    } 


package test;

public class Test {

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);
}
}
15、true or false?理由


[java] view plaincopyprint?package test; 
 
public class Test { 
 
    public static void main(String[] args) { 
        System.err.println(12 - 11.9 == 0.1); 
    } 


package test;

public class Test {

public static void main(String[] args) {
System.err.println(12 - 11.9 == 0.1);
}
}
16、以下代码输出是什么?


[java] view plaincopyprint?package test; 
 
import java.math.BigInteger; 
 
public class Test { 
 
    public static void main(String[] args) { 
        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()); 
    } 


package test;

import java.math.BigInteger;

public class Test {

public static void main(String[] args) {
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());
}
}
17、输出的结果是什么?12345?根据单词排序?还是?


[java] view plaincopyprint?package test; 
 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.Set; 
 
public class Test { 
 
    public static void main(String[] args) { 
        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()); 
        } 
    } 


package test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Test {

public static void main(String[] args) {
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());
}
}
}
18、如何迭代Map容器,别所大概是......,手写个试试?

19、以下代码输出的结果(笔试选择题)


[java] view plaincopyprint?public class Test { 
 
    public static void main(String[] args) { 
        System.err.println(args.length); 
    } 
     

/*
A. null     B. 0        C. Test
D. Exception in thread "main" java.lang.NullPointerException
*/ 

public class Test {

public static void main(String[] args) {
System.err.println(args.length);
}

}
/*
A. null B. 0 C. Test
D. Exception in thread "main" java.lang.NullPointerException
*/20、下面为一个单例的实现代码,请指出代码中有几个错误或不合理之处,并改正。


[java] view plaincopyprint?public class Test { 
     
    public Test instance = null; 
     
    public static Test getInstance() { 
        if (instance == null) { 
            instance = new Test(); 
            return instance; 
        } 
    } 

分享到:
评论

相关推荐

    java的笔试面试题目

    收录的题目不是很全面,但是重点基本上都收集的差不多了,希望大家可以找到好工作

    C、C 、Java及软件测试的笔试、面试题集合Version2

    {没工作经验的人找C、C++、Java、软件测试方面的工作要看的题,跳槽的也可以看下,都是笔试中...Java笔试题.rar 软件测试.rar 高质量C /C编程指南.rar 最新的是C、C 、Java及软件测试的笔试、面试题集合Version3

    C、C 、Java、软件测试的笔试、面试题集合Version3

    在Version2、Version1基础上修改、增加了一点题; 没工作经验的人找C、C++、Java、软件测试方面的工作要看的题,跳槽的也可以看下...Java笔试题.rar; 软件测试.rar ; 高质量C++/C编程指南.rar; SQL语法手册.rar; unix

    Java就业面试题大全.doc

    说明,为了减轻大家的负担和节省大家的时间,一些过时知识点和被笔试概率极低的题目不再被收录和分析。 回答问题的思路:先正面叙述一些基本的核心知识,然后描述一些特殊的东西,最后再来一些锦上添花的东西。要...

    最新面试和笔试全集——计算机通信相关专业

    这里收录的资料比我刚才的更全,里面除了面试技巧外,有很多专业的笔试题目,可以帮助大家,希望大家早日找到属于自己的好工作!!

    java选择题总结pdf文档

    总结Java面试题之选择题部分,包含【Java]、【数据库】、【框架】、【[Redis]、[Linux】等,包含题目讲解和考点提示,收录公司常考笔试题,收集不易,建议收藏,博文同步PDF文档

    java简答题总结pdf文档

    总结Java面试题之简答题部分,包含【Java]、【数据库】、【框架】、【[Redis]、[Linux】等,包含题目讲解和考点提示,收录公司常考笔试题,收集不易,建议收藏,博文同步PDF文档

    张孝祥正在整理Java就业面试题大全

    说明,为了减轻大家的负担和节省大家的时间,一些过时知识点和被笔试概率极低的题目不再被收录和分析。

    java编程题总结pdf文档

    总结Java面试题之编程题部分,包含【Java]、【数据库】、【框架】、【[Redis]、[Linux】等,包含题目讲解和考点提示,收录公司常考笔试题,收集不易,建议收藏,博文同步PDF文档

    LeetCode判断字符串是否循环-AC_Offer:剑指Offer刷题Java代码

    总所周知,不论你是面试Java/C++/C...的岗位,算法是鉴定一个人基础的重要指标,良好的算法基础也是你能通过笔试的必要条件。剑指Offer是众多企业历年招聘的经典题目,刷上几遍剑指Offer,是你临阵磨枪的最好选择。 ...

Global site tag (gtag.js) - Google Analytics