# 面试题集锦

2011/04/15 23:40

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

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++){
}
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++){
}
System.out.println(Arrays.deepToString(a.toArray()));
System.out.println(Arrays.deepToString(b.toArray()));
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++){
}
System.out.println(Arrays.deepToString(a.toArray()));
System.out.println(Arrays.deepToString(b.toArray()));
a.removeAll(b);
System.out.println(Arrays.deepToString(a.toArray()));
}``````

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() {

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

}``````

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() {
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
+ 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