java面試寶典帶答案
- 面試
- 關注:3.54K次
一、 寫出下面代碼的執行結果 (分值10分) public class Foo {
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(); }
- 文章版權屬於文章作者所有,轉載請註明 https://xuewengu.com/flzc/mianshi/61o22.html