Java经典笔试题和面试题答案(二)

编辑:李老师高考志愿助手

  16. 1) public class Test{

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

  3) int i =0xFFFFFFF1;

  4) int j=~i;

  5)

  6) }

  7) }

  which is decimal value of j at line 5?

  A. 0 B.1 C.14 D.-15 E. compile error at line 3 F. compile error at line 4

  Answer:C 分析:int是32位的(范围应该在-231~231-1),按位取反后,后4位是1110,前面的全部是0,所以肯定是14

  17. float f=4.2F;

  Float g=new Float(4.2F);

  Double d=new Double(4.2);

  Which are true?

  A. f==g B. g==g C. d==f D. d.equals(f) E d.equals(g) F. g.equals(4.2);

  Answer:B,E(网上的答案是B,E;我测试的结果是:true,true,false,false,fasle,fasle,所以答案是:A,B,还请各位大虾明示)

  分析:以下是我从网络上找到的,但是感觉应用到这个题目上反而不对拉,郁闷中,希望能给大家有所提示,要是你们明白拉,记得给我留言啊!:~

  1.基本类型、对象引用都在栈中; 而对象本身在堆中;

  2.“==“比的是两个变量在栈内存中的值,而即使变量引用的是两个对象,“==”比的依旧是变量所拥有的“栈内存地址值”;

  3.equals()是每个对象与生俱来的方法,因为所有类的最终基类就是Object(除去Object本身);而equals()是Object的方法之一,也就是说equals()方法来自Object类。 观察一下Object中equals()的source code:

  public boolean equals(Object obj) { return (this == obj); }

  注意:“return (this == obj)” this与obj都是对象引用,而不是对象本身。所以equals()的缺省实现就是比较“对象引用”是否一致,所以要比较两个对象本身是否一致,须自己编写代码覆盖Object类里的equals()的方法。来看一下String类的equals方法代码:

  public boolean equals(Object anObject){

  if(this == anObject){

  return true;

  }

  if(anObject instanceof String){

  String anotherString = (String)anObject;

  int n = count;

  if(n == anotherString.count){

  char v1[] = value;

  char v2[] = anotherString.value;

  int i = offset;

  int j = anotherString.offset;

  while(n-- != 0){

  if(v1[i++] != v2[j++])

  return false;

  }

  return true;

  }

  }

  return false;

  }

  18. public class Equals{

  public static void add3(Integer i){

  int val = i.intValue();

  val += 3;

  i = new Integer(val);

  }

  public static void main(String args[]){

  Integer i=new Integer(0);

  add3(i);

  System.out.println(i.intValue());

  }

  }

  what is the result?

  A. compile fail B.print out "0" C.print out "3"

  D.compile succeded but exception at line 3

  Answer:B 分析:java只有一种参数传递方式,那就是值传递.(大家可以看我转载的另一个同名文章,会让大家豁然开朗)

  19. public class Test{

  public static void main(String[] args){

  System.out.println(6^3);

  }

  }

  what is output? Answer:5 分析: ^ is yi huo(计算机器上是Xor) ;异或的逻辑定义:真^真=假 真^假=真 假^真=真 假^假=假

  20. public class Test{

  public static void stringReplace(String text){

  text=text.replace('j','l');

  }

  public static void bufferReplace(StringBuffer text){

  text=text.append("c");

  }

  public static void main(String args[]){

  String textString=new String("java");

  StringBuffer textBuffer=new StringBuffer("java");

  stringReplace(textString);

  bufferReplace(textBuffer);

  System.out.println(textString+textBuffer);

  }

  }

  what is the output?

  Answer:javajavac

  分析:根据我转载的一篇文章可以得出答案,不过还有几个类似的题目,用该文章解释不通,因为本人对java传递参数也一直没有弄明白,所以,还请大虾多多指教.

  21. public class ConstOver{

  public ConstOver(int x, int y, int z){}

  }

  which two overload the ConstOver constructor?

  A.ConstOver(){}

  B.protected int ConstOver(){}  //not overload ,but no a error

  C.private ConstOver(int z, int y, byte x){}

  D.public void ConstOver(byte x, byte y, byte z){}

  E.public Object ConstOver(int x, int y, int z){}

  Answer:A,C

  分析:测试不通过的首先是B,E,因为要求有返回值,这2个选项没有,要想通过编译那么需要加上返回值,请注意如果加上返回值,单纯看选项是没有问题拉,可以针对题目来说,那就是错之又错拉,对于构造器来说是一种特殊类型的方法,因为它没有返回值.对于D选项在91页有详细的介绍,空返回值,经管方法本身不会自动返回什么,但可以选择返回别的东西的,而构造器是不会返回任何东西的,否则就不叫构造器拉.

  22. public class MethodOver{

  public void setVar(int a, int b, float c){}

  }

  which overload the setVar?

  A.private void setVar(int a, float c, int b){}

  B.protected void setVar(int a, int b, float c){}

  C.public int setVar(int a, float c, int b){return a;}

  D.public int setVar(int a, float c){return a;}

  Answer:A,C,D 分析:方法的重载,根据概念选择,B是错误的,因为他们有相同的参数列表,所以不属于重载范围.

  23. class EnclosingOne{

  public class InsideOne{}

  }

  public class InnerTest{

  public static void main(String args[]){

  EnclosingOne eo=new EnclosingOne();

  //insert code here

  }

  }

  A.InsideOne ei=eo.new InsideOne();

  B.eo.InsideOne ei=eo.new InsideOne();

  C.InsideOne ei=EnclosingOne.new InsideOne();

  D.InsideOne ei=eo.new InsideOne();

  E.EnclosingOne.InsideOne ei=eo.new InsideOne();

  Answer:E

  24. What is "is a" relation?

  A.public interface Color{}

  public class Shape{private Color color;}

  B.interface Component{}

  class Container implements Component{ private Component[] children; }

  C.public class Species{}

  public class Animal{private Species species;}

  D.interface A{}

  interface B{}

  interface C implements A,B{}  //syntex error

  Answer:B 我没有明白这个题目的意思,有人能告诉我嘛?

  25. 1)package foo;

  2)

  3)public class Outer{

  4) public static class Inner{

  5) }

  6)}

  which is true to instantiated Inner class inside Outer?

  A. new Outer.Inner()

  B. new Inner()

  Answer:B

  if out of outerclass A is correct 分析:在Outer内部,B方式实例化内部类的方法是正确的,如果在Outer外部进行inner的实例化,那么A方法是正确的.

中国点击率最高的一篇文章 !