分享:某单位Java笔试题(一)

前段时间做过的一些题目,有些挺恶心的,看似是从JAVA解惑上面拉下来的,还是做错了一些,毕竟有些不常用。

纯手打,有些字删减了,大家权当乐呵乐呵。

代码直接复制就可以跑的,尽量跑跑看结果跟自己想的一样不一样吧。

[list]
  • 1.下面哪个方法是@deprecated?
  • A. java.util.Date: parse(String s)
    B. java.util.Arrays: asList(T... a)
    C. java.util.Properties: loadFromXML(InputStream in)
    D. java.util.Collections binarySearch()
  • 2.下面代码抛什么异常?
  • List<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    for(String s : list){
    if(s.equals("A")){
    list.add("E");
    }
    }
    

  • 3.日期格式的大小写问题,月份和分钟哪个M是大写?
  • 4.HashTable和HashMap关系(多选)
  • A.都实现了Map
    B.都允许null键值
    C.前者同步后者不
    D.Collection视图方法都是快速失败的(这个选项根本没看懂。。。)
    E.都可以自定义容量和加载因子
  • 5.try,catch,finally中,try有个return语句,finally中的语句会在什么时候执行?
  • 6.哪种变量不能用于switch?
  • A.int B.byte C.char D.long
  • 7.抽象类与接口,哪个说法错误?
  • A.一个类可实现多接口
    B.一个类只能继承一个抽象类
    C.抽象类可以提供实现
    D.接口可以私有成员变量
  • 8.看程序写结果
  • public class FakeThread extends Thread {
    	private String name;
    
    	public static void main(String[] args) {
    		FakeThread t = new FakeThread();
    		t.go();
    	}
    
    	public FakeThread() {
    	}
    
    	public FakeThread(String name) {
    		this.name = name;
    	}
    
    	public String getThreadName() {
    		return name;
    	}
    
    	private void go() {
    		FakeThread first = new FakeThread("first");
    		first.start();
    		FakeThread second = new FakeThread("second");
    		second.start();
    	}
    
    	public void start() {
    		for (int i = 0; i < 2; i++) {
    			System.out.println(this.getThreadName() + i);
    			try {
    				Thread.sleep(100);
    			} catch (Throwable e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }

  • 9.下列程序那几行编译出错?
  • StringBuffer sb = new StringBuffer("abc");
    		String s = new String("abc");
    		sb.append("def");
    		s.append("def");
    		sb.insert(1, "zzz");
    		s.concat(sb);
    		s.trim();

  • 10.写个单例
  • 11.看代码写结果
  • public class Null {
    
    	public static void greet() {
    		System.out.println("Hello World");
    	}
    
    	@SuppressWarnings("static-access")
    	public static void main(String[] args) {
    		((Null) null).greet();
    	}
    
    }

  • 12.写结果(这个真回答错了,内部类还是弱项啊。。。回头要恶补一下姿势)
  • public class Twisted {
    
    	private final String name;
    
    	Twisted(String name) {
    		this.name = name;
    	}
    
    	private String name() {
    		return name;
    	}
    
    	private void reproduce() {
    		new Twisted("reproduce") {
    			public void printName() {
    				System.out.println(name());
    			}
    		}.printName();
    	}
    
    	public static void main(String[] args) {
    		new Twisted("main").reproduce();
    	}
    
    }

  • 13.写结果(同样做错了,没想到是先静态成员变量再是静态类)
  • public class StaticClassNField {
    
    	public static void main(String[] args) {
    		System.out.println(X.Y.Z);
    	}
    
    }
    
    class X {
    	static class Y {
    		static String Z = "Black";
    	}
    
    	static C Y = new C();
    }
    
    class C {
    	String Z = "White";
    }

  • 14.看程序写结果。见Java解惑之61--日期游戏
  • public class DatingGame {
        public static void main(String[ ] args) {
            Calendar cal = Calendar.getInstance();
            cal.set(1999, 12, 31); // Year, Month, Day
            System.out.print(cal.get(Calendar.YEAR) + " ");
            Date d = cal.getTime();
            System.out.println(d.getDay());
        }
    }
    

    [/list]


    感觉有点长了,先搞到这里。后面还有几题回头补充。

    突然 发现有种高中时候做卷子的感觉。。。

    猜你喜欢

    转载自wwwcomy.iteye.com/blog/1855251
    今日推荐