巨人网络2014校园招聘笔试
21 October 2013
巨人网络2014校园招聘笔试。据说笔试通过的会去上海参加面试。我果断没去成。
###一、改错题
1. 下列代码的错误之处
public class Question1 {
public static boolean isOdd(int i) {
return i % 2 == 1;
}
public static void main(String[] args) {
for (int i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; ++i) {
boolean isOdd = isOdd(i);
System.out.println(String.format("i=%d, isOdd = %b", i, isOdd);
}
}
} // 负数求模,取绝对值取模,然后加符号。此处负数的部分,奇数取模之后都是-1,所以返回都是false。检测负数奇偶性时应先判断正负。
2. 下列代码的错误之处
public class Question2 {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
} // long 64位 范围合适 // 右边做乘法时,是int类型数值,int是32位,乘法结束后,才转成long型付给MICROS_PER_DAY // 改成:final long MICROS_PER_DAY = 24l * 60l * 60l * 1000l * 1000l;
3. 下列代码的错误之处
public class Question3 {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
if (b == 0x90)
System.out.print("Joy!");
}
}
} // byte只有8位,0x90是16位,超出范围,永远不会执行到Joy处。
###二、问答题
1. 请解释一下JVM GC的原理
GC garbage coolection,使用一个线程实现,监控内存,如果该块内存没有被对象引用,则会自动释放该内存块。常见的情况下,a = null;后,a原本引用的内存就会被自动释放。程序员也可以通过System.gc()建议JVM去释放内存。
2. 实现一个高并发的聊天服务器需要克服哪些问题?
###三、编程题
1. 请用Java实现冒泡排序算法
class BubbleSort {
public static void BubbleSort(int [] arr) {
boolean flag;
for (int i = 0; i < arr.length; i++) {
flag = false;
for (int j = i; j < arr.length; j++) {
if (arr[i] > arr[j]) {
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
flag = true;
}
}
if (!flag) {
return ;
}
}
}
public static void main(String argv[]) {
int arr[] = {5, 4, 6, 2, 3, 9};
BubbleSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
2. 请用Java实现二叉树的(中序)遍历算法。
public void inOrder(Node node) {
if (node == null) {
return ;
}
if (node.left != null) {
inOrder(node.left);
}
System.out.println(node.value);
if (node.right != null) {
inOrder(node.right);
}
}
原文链接: 巨人网络2014校园招聘笔试 ,转载请注明来源!
– EOF –