面试题集锦

原创
2011/04/15 23:40
阅读数 454

将自己这段时间遇到的想得起来的面试题摘录如下,希望对参加面试的朋友们有帮助:

A 打印1到100,如果是3的倍数,打印"Three",如果是5的倍数打印"five",如果既是3的倍数又是5的倍数,打印"three&five".

http://www.oschina.net/bbs/thread/17460 解法见这里

B 给定两个集合,求两个集合的交集/并集/差集

static void intersection(){
		Random r= new Random();
		HashSet<Integer> a = new HashSet<Integer>();
		HashSet<Integer> b = new HashSet<Integer>();
		int cnt = 20;
		int value = 25;
		for(int i = 0;i<cnt;i++){
		  a.add(r.nextInt(value));
		  b.add(r.nextInt(value));
		}
		System.out.println(Arrays.deepToString(a.toArray()));
		System.out.println(Arrays.deepToString(b.toArray()));
		a.retainAll(b);
		System.out.println(Arrays.deepToString(a.toArray()));
	}
	static void union(){
		Random r= new Random();
		Set<Integer> a = new HashSet<Integer>();
		Set<Integer> b = new HashSet<Integer>();
		int cnt = 20;
		int value = 25;
		for(int i = 0;i<cnt;i++){
		  a.add(r.nextInt(value));
		  b.add(r.nextInt(value));
		}
		System.out.println(Arrays.deepToString(a.toArray()));
		System.out.println(Arrays.deepToString(b.toArray()));
		a.addAll(b);
		System.out.println(Arrays.deepToString(a.toArray()));		
	}
	static void subtraction(){
		Random r= new Random();
		Set<Integer> a = new HashSet<Integer>();
		Set<Integer> b = new HashSet<Integer>();
		int cnt = 20;
		int value = 25;
		for(int i = 0;i<cnt;i++){
		  a.add(r.nextInt(value));
		  b.add(r.nextInt(value));
		}
		System.out.println(Arrays.deepToString(a.toArray()));
		System.out.println(Arrays.deepToString(b.toArray()));
		a.removeAll(b);
		System.out.println(Arrays.deepToString(a.toArray()));		
	}

主要利用了集合的三个方法.具体自己去查API.

C  有一条绳子1000米长,一天去掉一半,问多少天以后,它的长度少于5米。

static void sf()
	{
		double length = 1000;
		int day =1;
		do{
			double leng = length / 2.0;
			length = leng;
			System.out.println("No. " + day +" day rope is "+ leng +" m");
			day++;
		}while(length/2.0 >5);
		System.out.println(day+" day(s) rope will less than 5 meters");
	}

D  有一个集合,数组吧,里面存一系列对象,当数据量很大时,如何查找年龄大于40的对象出来。

static void testHugeRecords() {
		BlockingQueue<Person> queue = new LinkedBlockingQueue<Person>();
		BlockingQueue<Person> selectedQueue = new LinkedBlockingQueue<Person>();

		Random random = new Random();
		int nums = 8000000;
		{
			for (int i = 1; i <= nums; i++) 
			{

				int age = random.nextInt(45);
				Person person = new Person(age);
				queue.offer(person);
			}
		}

		long size = queue.size();
		System.out.println(size);
		for (int i = 0; i < size; i++) {
			Person p = queue.poll();
			if (p.age > 40) {
				selectedQueue.offer(p);
			}
		}
		System.out.println(selectedQueue.size());
	}


class Person {
	int age;

	public Person(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

}

该题在我PC机上测试只能达到上面哪个数,所以这个方法是最优。肯定还有其它的解决方案,请大家指教。

E  singleton pattern

public class TestNumber {
	static class Helper {
		static final TestNumber instance = new TestNumber();
	}

	private TestNumber() {
		System.out.println("in private constructor");
	}

	public static void testSingleton() {
		ExecutorService executor = Executors.newCachedThreadPool();
		for (int i = 0; i < 10; i++) {
			executor.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + " : "
							+ TestNumber.getInstance());
				}

			});
		}
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		executor.shutdown();
	}

	public static TestNumber getInstance() {
		return Helper.instance;
	}

F java 参数的传递是传值还是引用及考查For loop的execute step

/**
	 * 包装类在传递参数中是按其基本类型来传递的。String 类似 char[] 的包装类。
	 */
	static void testSendValueOrReference()
	{
		String name="wenth";
		change(name);
		System.out.println(name);
		
		String[] s = new String[5];
		change(s);
		for(String st : s){
			System.out.print(st);
		}
		System.out.println();
		Integer in = new Integer(0);
		change(in);
		System.out.println(in);
		
		Integer[] ins = new Integer[5];
		change(ins);
		System.out.println(java.util.Arrays.deepToString(ins));
		
		
	}
	/**
	 * @param ins
	 */
	private static void change(Integer[] ins) {
		for(int i=0;i<ins.length;i++){
			ins[i] = new Integer(i);
		}
	}
	/**
	 * @param in
	 */
	private static void change(Integer in) {
		int v = in.intValue();
		v++;
	}
	static void change(String s){
		s = " abcde ";
	}
	static void change(String[] str){
		for(int i = 0 ; i < str.length;i++){
			str[i] = String.valueOf(i);
		}
	}
	
	/**
	 * @param c
	 * @return
	 */
	static boolean foo(char c){
		System.out.print(c);
		return true;
	}

	 /**
	 * 考for循环执行顺序,三个条件,第一个条件执行一次,后面从第二个条件循环执行,然后执行方法体内的程序,最后执行第三个条件。然后依次从第二个条件开始执行,只到不满足条件退出。		
	 */
	static void testForLoopRunSteps(){
		int i = 0;
		for(foo('A');foo('B') && i <4;foo('C')){
			i++;
			foo('D');
		}
	}

console output is

wenth
01234
0
[0, 1, 2, 3, 4]
ABD
CBD
CBD
CBD
CB

 

 

未完待续

 

 

 

 

 

展开阅读全文
打赏
0
7 收藏
分享
加载中
更多评论
打赏
0 评论
7 收藏
0
分享
返回顶部
顶部