面试题

1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。
2.继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
3.封装:封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
4. 多态性:多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。

1:What will happen when you attempt to compile and run the following code?

class Base

{

int i = 99;

public void amethod()

{

System.out.println("Base.amethod()");

}

Base()

{

amethod();

}

}

public class Derived extends Base

{

int i = -1;



public static void main(String argv[])

{

Base b = new Derived();

System.out.println(b.i);

b.amethod();

}

public void amethod()

{

System.out.println("Derived.amethod()");

}

}

Choices:

A.Derived.amethod() -1 Derived.amethod()
B.Derived.amethod() 99
C.Compile time error
D.Derived.amethod()


2:
Give this class outline:
class Example{
private int x;
//rest of class body…
}
Assuming that x invoked by the code java Example, which statement can made x be directly accessible in main() method of Example.java?

A.Change private int x to public int x
B.change private int x to static int x
C.Change private int x to protected int x
D.change private int x to final int x


3:Which statement about listener is true?
A.Most component allow multiple listeners to be added.
B.If multiple listener be add to a single component, the event only affected one listener.
C.Component don?t allow multiple listeners to be add.
D.none


4:
1. public class X {
2. public object m () {
3. object o = new float (3.14F);
4. object [] oa = new object [1];
5. oa[0]= o;
6. o = null;
7. oa[0] = null;
8.return o;
9. }
10.}
When is the float object created in line 3, eligible for garbage collection?

A.Just after line 5
B.Just after line 6
C.Just after line 7
D.Just after line 8(that is, as the method returns)


5:
Given the following class definition:
class A{
protected int i;
A(int i){
this.i=i;
}
}
which of the following would be a valid inner class for this class?
Select valid answer:

A.class B{ }
B.class B extends A{ }
C.class B extends A{ B(){System.out.println(“i=”+i);} }
D.class B{ class A{} }


6:
What will be the result of executing the following code?

// Filename; SuperclassX.java

package packageX;

public class SuperclassX

{

protected void superclassMethodX()

{

}

int superclassVarX;

}



// Filename SubclassY.java

1.package packageX.packageY;

2.

3.public class SubclassY extends SuperclassX

4.{

5.SuperclassX objX = new SubclassY();

6.SubclassY objY = new SubclassY();

7.void subclassMethodY()

8.{

9.objY.superclassMethodX();

10.int i;

11.i = objY.superclassVarX;

12.}

13.}

Choices:

A.Compilation error at line 5
B.Compilation error at line 9
C.Runtime exception at line 11
D.None of these


7:Which modifier should be applied to a method for the lock of object this to be obtained prior to excution any of the method body?
A.synchronized
B.abstract
C.final
D.static


8:In the following pieces of code, which one will compile without any error?
A.StringBuffer sb1 = "abcd";
B.Boolean b = new Boolean("abcd");
C.C: byte b = 255;
D.float fl = 1.2;


9: Which of the following statements are true?
A.The automatic garbage collection of the JVM prevents programs from ever running out of memory
B.A program can suggest that garbage collection be performed and force it
C.Garbage collection is platform independent
D.An object becomes eligible for garbage collection when all references denoting it are set to null.


10:A class design requires that a member variable should be accessible only by same package, which modifer word should be used?
A.protected
B.public
C.no modifer
D.private


11:Which are not Java keywords?

A.TRUE
B.const
C.super
D.void


12:A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class. but otherwise not by classes which are not members of the same package. What should be done to achieve this?

A.The variable should be marked public
B.The variable should be marked private
C.The variable should be marked protected
D.The variable should have no special access modifier


13:public class Parent {
  int change() {…}
}

class Child extends Parent {

}

Which methods can be added into class Child?
A.public int change(){}
B.abstract int chang(){}
C.private int change(){}
D.none


14:
In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:
1.public class Test {

2. public static void main (String args []) {

3. Employee e = new Employee("Bob", 48);

4. e.calculatePay();

5. System.out.println(e.printDetails());

6. e = null;

7. e = new Employee("Denise", 36);

8. e.calculatePay();

9. System.out.println(e.printDetails());

10. }

11.}
Only One:

A.Line 10
B.Line 11
C.Line 7
D.Line 8


15:
What is the result when you compile and run the following code?

public class Test

{

public void method()

{

for(int i = 0; i < 3; i++)

{

System.out.print(i);

}

System.out.print(i);

}

}

Choices:

A.0122
B.0123
C.Compilation error
D.None of these


16:
下述程序代码中有语法错误的行是( )。
int i,ia[10],ib[10]; /*第一行*/
for (i=0;i<=9;i++) /*第2行*/
ia[i]=0; /*第3行*/
ib=ia; /*第4行*/

A.第1行
B.第2行
C.第3行
D.第4行


17:
String s=”Example String”;Which operation is not legal?


A.int i=s.length();
B.s[3]=”x”;
C.String short_s=s.trim();
D.String t=”root”+s;


18:
What happens when you try to compile and run the following program?
class Mystery{
String s;
public static void main(String[] args){
Mystery m=new Mystery();
m.go();
}
void Mystery(){
s=”constructor”;
}
void go(){
System.out.println(s);
}
}


A.this code compliles but throws an exception at runtime
B.this code runs but nothing appears in the standard output
C.this code runs and “constructor” in the standard output
D.this code runs and writes ”null” in the standard output


19:
What will happen when you attempt to compile and run the following code?

public class Static

{

static

{

int x = 5;

}

static int x,y;

public static void main(String args[])

{

x--;

myMethod();

System.out.println(x + y + ++x);

}

public static void myMethod()

{

y = x++ + ++x;

}

}

Choices:

A.prints : 2
B.prints : 3
C.prints : 7
D.prints : 8


20:Math.round(-11.5)等於多少?
A.-11
B.-12
C.-11.5
D.none


简答题
21:到底在哪里使用inverse="true"

22:假设你有一个用1001个整数组成的数组,这些整数是任意排列的,但是你知道所有的整数都在1到1000(包括1000)之间。此外,除一个数字出现两次外,其他所有数字只出现一次。假设你只能对这个数组做一次处理,用一种算法找出重复的那个数字。如果你在运算中使用了辅助的存储方式,那么你能找到不用这种方式的算法吗?

23:正向最大匹配分词,怎么做最快?

24:什么是序列化?什么是反序列化?为什么要序列化对象?

25:给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含),指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
* 比如,A=[1,0] K=21 那么输出结构应该为100

26:你所知道的集合类都有哪些?主要方法?


Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap

Collection接口
  Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements)。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java SDK不提供直接继承自Collection的类,Java SDK提供的类都是继承自Collection的“子接口”如List和Set。
  所有实现Collection接口的类都必须提供两个标准的构造函数:无参数的构造函数用于创建一个空的Collection,有一个Collection参数的构造函数用于创建一个新的Collection,这个新的Collection与传入的Collection有相同的元素。后一个构造函数允许用户复制一个Collection。
  如何遍历Collection中的每一个元素?不论Collection的实际类型如何,它都支持一个iterator()的方法,该方法返回一个迭代子,使用该迭代子即可逐一访问Collection中每一个元素。典型的用法如下:
    Iterator it = collection.iterator(); // 获得一个迭代子
    while(it.hasNext()) {
      Object obj = it.next(); // 得到下一个元素
    }
  由Collection接口派生的两个接口是List和Set。

List接口
  List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。
和下面要提到的Set不同,List允许有相同的元素。
  除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素,还能向前或向后遍历。
  实现List接口的常用类有LinkedList,ArrayList,Vector和Stack。

LinkedList类
  LinkedList实现了List接口,允许null元素。此外LinkedList提供额外的get,remove,insert方法在LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈(stack),队列(queue)或双向队列(deque)。
  注意LinkedList没有同步方法。如果多个线程同时访问一个List,则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List:
    List list = Collections.synchronizedList(new LinkedList(...));

ArrayList类
  ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。
size,isEmpty,get,set方法运行时间为常数。但是add方法开销为分摊的常数,添加n个元素需要O(n)的时间。其他的方法运行时间为线性。
  每个ArrayList实例都有一个容量(Capacity),即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加,但是增长算法并没有定义。当需要插入大量元素时,在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。
  和LinkedList一样,ArrayList也是非同步的(unsynchronized)。

Vector类
  Vector非常类似ArrayList,但是Vector是同步的。由Vector创建的Iterator,虽然和ArrayList创建的Iterator是同一接口,但是,因为Vector是同步的,当一个Iterator被创建而且正在被使用,另一个线程改变了Vector的状态(例如,添加或删除了一些元素),这时调用Iterator的方法时将抛出ConcurrentModificationException,因此必须捕获该异常。

Stack 类
  Stack继承自Vector,实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop方法,还有peek方法得到栈顶的元素,empty方法测试堆栈是否为空,search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。

Set接口
  Set是一种不包含重复的元素的Collection,即任意的两个元素e1和e2都有e1.equals(e2)=false,Set最多有一个null元素。
  很明显,Set的构造函数有一个约束条件,传入的Collection参数不能包含重复的元素。
  请注意:必须小心操作可变对象(Mutable Object)。如果一个Set中的可变元素改变了自身状态导致Object.equals(Object)=true将导致一些问题。

Map接口
  请注意,Map没有继承Collection接口,Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个value。Map接口提供3种集合的视图,Map的内容可以被当作一组key集合,一组value集合,或者一组key-value映射。

Hashtable类
  Hashtable继承Map接口,实现一个key-value映射的哈希表。任何非空(non-null)的对象都可作为key或者value。
  添加数据使用put(key, value),取出数据使用get(key),这两个基本操作的时间开销为常数。
Hashtable通过initial capacity和load factor两个参数调整性能。通常缺省的load factor 0.75较好地实现了时间和空间的均衡。增大load factor可以节省空间但相应的查找时间将增大,这会影响像get和put这样的操作。
使用Hashtable的简单示例如下,将1,2,3放到Hashtable中,他们的key分别是”one”,”two”,”three”:
    Hashtable numbers = new Hashtable();
    numbers.put(“one”, new Integer(1));
    numbers.put(“two”, new Integer(2));
    numbers.put(“three”, new Integer(3));
  要取出一个数,比如2,用相应的key:
    Integer n = (Integer)numbers.get(“two”);
    System.out.println(“two = ” + n);
  由于作为key的对象将通过计算其散列函数来确定与之对应的value的位置,因此任何作为key的对象都必须实现hashCode和equals方法。hashCode和equals方法继承自根类Object,如果你用自定义的类当作key的话,要相当小心,按照散列函数的定义,如果两个对象相同,即obj1.equals(obj2)=true,则它们的hashCode必须相同,但如果两个对象不同,则它们的hashCode不一定不同,如果两个不同对象的hashCode相同,这种现象称为冲突,冲突会导致操作哈希表的时间开销增大,所以尽量定义好的hashCode()方法,能加快哈希表的操作。
  如果相同的对象有不同的hashCode,对哈希表的操作会出现意想不到的结果(期待的get方法返回null),要避免这种问题,只需要牢记一条:要同时复写equals方法和hashCode方法,而不要只写其中一个。
  Hashtable是同步的。

HashMap类
  HashMap和Hashtable类似,不同之处在于HashMap是非同步的,并且允许null,即null value和null key。,但是将HashMap视为Collection时(values()方法可返回Collection),其迭代子操作时间开销和HashMap的容量成比例。因此,如果迭代操作的性能相当重要的话,不要将HashMap的初始化容量设得过高,或者load factor过低。

WeakHashMap类
  WeakHashMap是一种改进的HashMap,它对key实行“弱引用”,如果一个key不再被外部所引用,那么该key可以被GC回收。

总结
  如果涉及到堆栈,队列等操作,应该考虑用List,对于需要快速插入,删除元素,应该使用LinkedList,如果需要快速随机访问元素,应该使用ArrayList。
  如果程序在单线程环境中,或者访问仅仅在一个线程中进行,考虑非同步的类,其效率较高,如果多个线程可能同时操作一个类,应该使用同步的类。
  要特别注意对哈希表的操作,作为key的对象要正确复写equals和hashCode方法。
  尽量返回接口而非实际的类型,如返回List而非ArrayList,这样如果以后需要将ArrayList换成LinkedList时,客户端代码不用改变。这就是针对抽象编程。


27:请阐述一下你对JAVA多线程中“锁”的概念的理解。

28:Solve this cryptic equation, realizing of course that values for M and E could be interchanged. No leading zeros are allowed.
  WWWDOT - GOOGLE = DOTCOM

29:如何现实servlet的单线程模式?

30:写一个程序,把一个100以内的自然数分解因数。(自然数分解因数就是将一个自然数分解为几个素数的乘积,提示,由于该数不是很大,所以可以将质数保存在数组中,以加快计算速度)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值