Java 经典笔试题

时间:2021-03-06 18:20:00 面试笔试 我要投稿

Java 经典笔试题2016

  Java笔试会考什么?以下是yjbys小编整理的经典试题内容,快来阅读看看吧。

Java 经典笔试题2016

  Java 经典笔试题

  1.public class ReturnIt{

  returnType methodA(byte x, double y){ //line 2

  return (short)x/y*2;

  }

  }

  what is valid returnType for methodA in line 2?

  答案:返回double类型,因为(short)x将byte类型强制转换为short类型,与double类型运算,将会提升为double类型.

  2.

  1) class Super{

  2) public float getNum(){return 3.0f;}

  3) }

  4)

  5) public class Sub extends Super{

  6)

  7) }

  which method, placed at line 6, will cause a compiler error?

  A. public float getNum(){return 4.0f;}

  B. public void getNum(){}

  C. public void getNum(double d){}

  D. public double getNum(float d){return 4.0d;}

  Answer:B

  A属于方法的重写(重写只存在于继承关系中),因为修饰符和参数列表都一样.B出现编译错误,如下:

  Sub.java:6: Sub 中的 getNum() 无法覆盖 Super 中的 getNum();正在尝试使用不

  兼容的返回类型

  找到: void

  需要: float

  public void getNum(){}

  ^

  1 错误

  B既不是重写也不是重载,重写需要一样的返回值类型和参数列表,访问修饰符的限制一定要大于被重写方法的访问修饰符(public>protected>default>private);

  重载:必须具有不同的参数列表;

  可以有不同的返回类型,只要参数列表不同就可以了;

  可以有不同的访问修饰符;

  把其看做是重载,那么在java中是不能以返回值来区分重载方法的,所以b不对.

  3.

  public class IfTest{

  public static void main(String args[]){

  int x=3;

  int y=1;

  if(x=y)

  System.out.println("Not equal");

  else

  System.out.println("Equal");

  }

  }

  what is the result?

  Answer:compile error 错误在与if(x=y) 中,应该是x==y; =是赋值符号,==是比较操作符

  4.

  public class Foo{

  public static void main(String args[]){

  try{return;}

  finally{ System.out.println("Finally");}

  }

  }

  what is the result?

  A. print out nothing

  B. print out "Finally"

  C. compile error

  Answer:B java的finally块会在return之前执行,无论是否抛出异常且一定执行.

  5.

  public class Test{

  public static String output="";

  public static void foo(int i){

  try {

  if(i==1){

  throw new Exception();

  }

  output +="1";

  }

  catch(Exception e){

  output+="2";

  return;

  }

  finally{

  output+="3";

  }

  output+="4";

  }

  public static void main(String args[]){

  foo(0);

  foo(1);

  24)

  }

  }

  what is the value of output at line 24? Answer:13423 如果你想出的答案是134234,那么说明对return的理解有了混淆,return是强制函数返回,本题就是针对foo(),那么当执行到return的话,output+="4"; 就不再执行拉,这个函数就算结束拉.

  6.

  public class IfElse{

  public static void main(String args[]){

  if(odd(5))

  System.out.println("odd");

  else

  System.out.println("even");

  }

  public static int odd(int x){return x%2;}

  }

  what is output?

  Answer:Compile Error

  7.

  class ExceptionTest{

  public static void main(String args[]){

  try{

  methodA();

  }

  catch(IOException e){

  System.out.println("caught IOException");

  }

  catch(Exception e){

  System.out.println("caught Exception");

  }

  }

  }

  If methodA() throws a IOException, what is the result? (其实还应该加上:import java.io.*;)

  Answer:caught IOException 异常的匹配问题,如果2个catch语句换个位置,那就会报错,catch只能是越来越大,意思就是说:catch的从上到下的顺序应该是:孙子异常->孩子异常->父亲异常->老祖先异常.这么个顺序.

  8.

  int i=1,j=10;

  do{

  if(i++>--j) continue;

  }while(i<5); (注意不要丢了这个分号呦)

  After Execution, what are the value for i and j?

  A. i=6 j=5

  B. i=5 j=5

  C. i=6 j=4

  D. i=5 j=6

  E. i=6 j=6

  Answer:D

  9.

  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) System.out.println(oa[0]);

  9) }

  10) }

  which line is the earliest point the object a refered is definitely elibile

  to be garbage collectioned?

  A.After line 4 B. After line 5 C.After line 6

  D.After line 7 E.After line 9(that is,as the method returns)

  Answer:D

  如果 6) o=null 变成 o=9f ,并且把7)去掉,那么8)将会输出什么呢?

  10.

  1) interface Foo{

  2) int k=0;

  3) }

  4) public class Test implements Foo{

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

  6) int i;

  7) Test test = new Test();

  8) i = test.k;

  9) i = Test.k;

  10) i = Foo.k;

  11) }

  12) }

  what is the result? Answer:compile successed and i=0 接口中的int k=0虽然没有访问修饰符,但在接口中默认是static和final的

  11. what is reserved words in java?

  A. run

  B. default

  C. implement

  D. import

  Answer:B,D

  12.

  public class Test{

  public static void main(String[] args){

  String foo=args[1];

  Sring bar=args[2];

  String baz=args[3];

  }

  }

  java Test Red Green Blue

  what is the value of baz?

  A. baz has value of ""

  B. baz has value of null

  C. baz has value of Red

  D. baz has value of Blue

  E. baz has value of Green

  F. the code does not compile

  G. the program throw an exception

  Answer:G

  分析:感觉原应该多一些语句吧,至少应该有红绿蓝的`赋值语句之类的,才能叫java Test Red Green Blue 才能有后面的选项,所以现在感觉很奇怪,不过就这个样子吧.这个问题在于:数组参数的理解,编译程序没有问题,但是运行这个程序就会出现问题,因为参数args没有给他分配空间那么他的长度应该是0,下面却用拉args[1]........等等的语句,那么定会出现越界错误.

  错误如下:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

  at Test.main(Test.java:4)

  13.

  int index=1;

  int foo[]=new int[3];

  int bar=foo[index];

  int baz=bar+index;

  what is the result?

  A. baz has a value of 0

  B. baz has value of 1

  C. baz has value of 2

  D. an exception is thrown

  E. the code will not compile

  Answer:B

  分析:《thinking in java》中的原话:若类的某个成员是基本数据类型,即使没有进行初始化,java也会确保它获得一个默认值,如下表所示:

基本类型默认值
booleanfalse
char'/u0000'(null)
byte(byte)0
short(short)0
int0
long0L
float0.0f
double0.0d

  千万要小心:当变量作为类的成员使用时,java才确保给定其默认值,。。。。。(后面还有很多话,也很重要,大家一定要看完成,要不然还是不清楚)

  14. which three are valid declaraction of a float?

  A. float foo=-1;

  B. float foo=1.0;

  C. float foo=42e1;

  D. float foo=2.02f;

  E. float foo=3.03d;

  F. float foo=0x0123;

  Answer:A,D,F 分析:B错误,因为1.0在java中是double类型的,C,E错误同样道理,都是double类型的

  15.

  public class Foo{

  public static void main(String args[]){

  String s;

  System.out.println("s="+s);

  }

  }

  what is the result?

  Answer:compile error 分析:需要对s进行初始化,和13题是不是矛盾呢:不矛盾,因为它不是基本类型,也不是类的成员,所以不能套用上述的确保初始化的方法。

  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) ;异或的逻辑定义:真^真=假 真^假=真 假^真=真 假^假=假


【Java 经典笔试题2016】相关文章:

java英文面试笔试题03-19

java面试笔试题分享08-22

2017java笔试题及答案07-20

java程序员面试笔试试题08-22

2017年java中高级笔试面试题及答案07-26

2016经典求职故事汇总08-13

名人经典求职故事201608-11

2016小升初英语笔试题目06-29

2016经典英文求职信07-18

2016求职陷阱经典案例分析07-24