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

招聘笔试题2018-11-22三水老师

  26. class BaseClass{

  private float x=1.0f;

  private float getVar(){return x;}

  }

  class SubClass extends BaseClass{

  private float x=2.0f;

  //insert code

  }

  what are true to override getVar()?

  A.float getVar(){

  B.public float getVar(){

  C.public double getVar(){

  D.protected float getVar(){

  E.public float getVar(float f){

  Answer:A,B,D 分析:返回类型和参数列表必须完全一致,且访问修饰符必须大于被重写方法的访问修饰符.

  27. public class SychTest{

  private int x;

  private int y;

  public void setX(int i){ x=i;}

  public void setY(int i){y=i;}

  public Synchronized void setXY(int i){

  setX(i);

  setY(i);

  }

  public Synchronized boolean check(){

  return x!=y;

  }

  }

  Under which conditions will check() return true when called from a different class?

  A.check() can never return true.

  B.check() can return true when setXY is callled by multiple threads.

  C.check() can return true when multiple threads call setX and setY separately.

  D.check() can only return true if SychTest is changed allow x and y to be set separately.

  Answer:C

  分析:答案是C,但是我想不出来一个测试程序来验证C答案.希望高手们给我一个测试的例子吧,万分感谢..........

  28. 1) public class X implements Runnable{

  2) private int x;

  3) private int y;

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

  5) X that =new X();

  6) (new Thread(that)).start();

  7) (new Thread(that)).start();

  }

  9) public synchronized void run(){

  10) for(;;){

  11) x++;

  12) y++;

  13) System.out.println("x="+x+",y="+y);

  14) }

  15) }

  16) }

  what is the result?

  A.compile error at line 6

  B.the program prints pairs of values for x and y that are always the same on the same time

  Answer:B 分析:我感觉会出现不相等的情况,但是我说不出为什么会相等。线程方面,还有好多路要走啊,咳

  29. class A implements Runnable{

  int i;

  public void run(){

  try{

  Thread.sleep(5000);

  i=10;

  }catch(InterruptedException e){}

  }

  public static void main(String[] args){

  try{

  A a=new A();

  Thread t=new Thread(a);

  t.start();

  17)

  int j=a.i;

  19)

  }catch(Exception e){}

  }

  }

  what be added at line line 17, ensure j=10 at line 19?

  A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); E.t.notify(); F. a.notify(); G.t.interrupt();

  Answer:C

  30. Given an ActionEvent, how to indentify the affected component?

  A.getTarget();

  B.getClass();

  C.getSource(); //public object

  D.getActionCommand();

  Answer:C

  31. import java.awt.*;

  public class X extends Frame{

  public static void main(String[] args){

  X x=new X();

  x.pack();

  x.setVisible(true);

  }

  public X(){

  setLayout(new GridLayout(2,2));

  Panel p1=new Panel();

  add(p1);

  Button b1=new Button("One");

  p1.add(b1);

  Panel p2=new Panel();

  add(p2);

  Button b2=new Button("Two");

  p2.add(b2);

  Button b3=new Button("Three");

  p2.add(b3);

  Button b4=new Button("Four");

  add(b4);

  }

  }

  when the frame is resized,

  A.all change height B.all change width C.Button "One" change height

  D.Button "Two" change height E.Button "Three" change width

  F.Button "Four" change height and width

  Answer:F

  32. 1)public class X{

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

  3) String foo="ABCDE";

  4) foo.substring(3);

  5) foo.concat("XYZ");

  6) }

  7) }

  what is the value of foo at line 6?

  Answer:ABCDE

  33. How to calculate cosine 42 degree?

  A.double d=Math.cos(42);

  B.double d=Math.cosine(42);

  C.double d=Math.cos(Math.toRadians(42));

  D.double d=Math.cos(Math.toDegrees(42));

  E.double d=Math.toRadious(42);

  Answer:C

  34. public class Test{

  public static void main(String[] args){

  StringBuffer a=new StringBuffer("A");

  StringBuffer b=new StringBuffer("B");

  operate(a,b);

  System.out.pintln(a+","+b);

  }

  public static void operate(StringBuffer x, StringBuffer y){

  x.append(y);

  y=x;

  }

  }

  what is the output?

  Answer:AB,B 分析:这道题的答案是AB,B,网上有很多答案给错啦,大家注意啊。

  35. 1) public class Test{

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

  3) class Foo{

  4) public int i=3;

  5) }

  6) Object o=(Object)new Foo();

  7) Foo foo=(Foo)o;

  System.out.println(foo.i);

  9) }

  10) }

  what is result?

  A.compile error at line 6

  B.compile error at line 7

  C.print out 3

  Answer:C

  36. public class FooBar{

  public static void main(String[] args){

  int i=0,j=5;

  4) tp: for(;;i++){

  for(;;--j)

  if(i>j)break tp;

  }

  System.out.println("i="+i+",j="+j);

  }

  }

  what is the result?

  A.i=1,j=-1 B. i=0,j=-1 C.i=1,j=4 D.i=0,j=4

  E.compile error at line 4

  Answer:B

  37. public class Foo{

  public static void main(String[] args){

  try{System.exit(0);}

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

  }

  }

  what is the result?

  A.print out nothing

  B.print out "Finally"

  Answer:A

  system.exit(0) has exit

  38. which four types of objects can be thrown use "throws"?

  A.Error

  B.Event

  C.Object

  D.Excption

  E.Throwable

  F.RuntimeException

  Answer:A,D,E,F

  分析:throw,例如:throw new IllegalAccessException("demo");是一个动作。

  而throws则是异常块儿的声明。所以感觉题目应该是throw

  39. 1)public class Test{

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

  3) unsigned byte b=0;

  4) b--;

  5)

  6) }

  7) }

  what is the value of b at line 5?

  A.-1 B.255 C.127 D.compile fail E.compile succeeded but run error

  Answer:D

  40. public class ExceptionTest{

  class TestException extends Exception{}

  public void runTest() throws TestException{}

  public void test() /* point x */ {

  runTest();

  }

  }

  At point x, which code can be add on to make the code compile?

  A.throws Exception B.catch (Exception e)

  Answer:A

  41. String foo="blue";

  boolean[] bar=new boolean[1];

  if(bar[0]){

  foo="green";

  }

  what is the value of foo?

  A."" B.null C.blue D.green

  Answer:C

  42. public class X{

  public static void main(String args[]){

  Object o1=new Object();

  Object o2=o1;

  if(o1.equals(o2)){

  System.out.prinln("Equal");

  }

  }

  }

  what is result?

  Answer:Equal

相关推荐

猜你喜欢

大家正在看

换一换