学者谷

位置:首页 > 职场范文 > 面试

java面试宝典带答案

面试5.87K

一、 写出下面代码的执行结果 (分值10分) public class Foo {

java面试宝典带答案

public static void main(String[] args) { String strValue="ABCDEFG"; tring(3); at("123"); tln("result=" + strValue);//ABCDEFG String value = new String ("ABCDEFG"); tln(strValue== value);//false

} }

二、 写出下面代码的执行结果 public class Foo{ (分值5分)

public static void main(String args[]) { int x = 100; int y = 200; if (x = y)//=是赋值运算符。不是比较运算符。 编译失败。 tln("Not equal"); else tln("Equal"); } }

三、 写出下面代码的执行结果 (此题需写出分析过程,没有分析过程不得分,分值10分)

import ception; public class ExceptionTest {

public static void main(String args[]) { try { new ExceptionTest()odA(5); } catch (IOException e) { tln("caught IOException"); } catch (Exception e) { tln("caught Exception"); }finally{ tln("no Exception"); } }void methodA(int i) throws IOException { if (i%2 != 0) throw new IOException("methodA IOException"); } }

在主函数中建立本类对象,调用了非静态方法methodA(5);

methodA方法执行方法体时,因为条件满足,抛出了IO异常。 Try就检测到了该异常。

因为有多个catch,会去找匹配的第一个catch。所以catch(IOException e){}就执行了。打印caught IOException。异常被处理后。在执行finally代码块中的语句。 因为finally中的代码一定会执行。Finally代码块通常都用于关闭资源。比如流资源,或者数据库。

四、 写出下面代码执行的'结果(此题需写出分析过程,没有分析过程不得分,分值10分) public class Test {

static boolean isTrue() { tln("isTrue"); return true; }

static boolean isFalse() { tln("isFalse"); return false; }

public static void main(String[] args) { if (isTrue() || isFalse()) { tln(" || operate return true"); } if (isFalse() & isTrue()) { tln(" & operate return true"); } } }

五、 写出下面代码执行的结果(此题需写出分析过程,没有分析过程不得分,分值10分)

class MyThread extends Thread{

public void run(){ try { entThread()p(3000); } catch (InterruptedException e) { } tln("MyThread running"); } }

public class ThreadTest{

public static void main(String argv[]) { MyThread t = new MyThread(); (); t(); tln("Thread Test"); } }

六、 执行的结果是什么?(此题需写出分析过程,没有分析过程不得分,分值10分) class A {

void fun1() {

tln(2()); }

int fun2() { return 123; } }

class B extends A { int fun2() { return 456; }

public static void main(String argv[]) { A a;

B b = new B(); 1(); a = b; 1(); } }

、 执行的结果是什么?(此题需写出分析过程,没有分析过程不得分,分值10分) class Data { int val;

int getVal() { return val; }

void setVal(int val) { = val; } }

public class ListTest {

public static void main(String argv[]) { Data data = new Data();

ArrayList list = new ArrayList();

for (int i=100; i<103; i++) { al(i); (data); }

int j = 0;

while (j < ()) {

Data tmp = (Data )(j);

tln("list(" + j + ") = " + al()); j++; } } }

八、 请指出以下代码有哪些错误(分值15分) 1.

abstract class Name { private String name;

public abstract boolean isStupidName(String name) {} }

2.

public class Something { void doSomething () { private String s = ""; int l = th(); } } 3.

public class Something {

public int addOne(final int x) { return ++x; } }

九、 写出以下正则表达式(分值10分) 1-6位字母或数字:

手机号(只能以139或159开通,11位数字):

十、 写一个方法,实现字符串的反转,如:输入abc,输出cba(分值10分)

十一、 写一个延迟加载的单例模式(Singleton)的例子(分值10分)

十二、

public class OuterClass { private double d1 = 1.0; // code here }

把下列答案存放在指定code位置上,哪两个答案是正确的。阐述原因。 A. class InnerOne{

public static double methoda() {return d1;} }

B. public class InnerOne{

static double methoda() {return d1;} }

C. private class InnerOne{

double methoda() {return d1;} }

D. static class InnerOne{

protected double methoda() {return d1;} }

E. abstract class InnerOne{

public abstract double methoda(); }

标签:java 宝典 面试