- 相关推荐
java面试宝典带答案
一、 写出下面代码的执行结果 (分值10分) public class Foo {
public static void main(String[] args) { String strValue="ABCDEFG"; strValue.substring(3); strValue.concat("123"); System.out.println("result=" + strValue);//ABCDEFG String value = new String ("ABCDEFG"); System.out.println(strValue== value);//false
} }
二、 写出下面代码的执行结果 public class Foo{ (分值5分)
public static void main(String args[]) { int x = 100; int y = 200; if (x = y)//=是赋值运算符。不是比较运算符。 编译失败。 System.out.println("Not equal"); else System.out.println("Equal"); } }
三、 写出下面代码的执行结果 (此题需写出分析过程,没有分析过程不得分,分值10分)
import java.io.IOException; public class ExceptionTest {
public static void main(String args[]) { try { new ExceptionTest().methodA(5); } catch (IOException e) { System.out.println("caught IOException"); } catch (Exception e) { System.out.println("caught Exception"); }finally{ System.out.println("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() { System.out.println("isTrue"); return true; }
static boolean isFalse() { System.out.println("isFalse"); return false; }
public static void main(String[] args) { if (isTrue() || isFalse()) { System.out.println(" || operate return true"); } if (isFalse() & isTrue()) { System.out.println(" & operate return true"); } } }
五、 写出下面代码执行的结果(此题需写出分析过程,没有分析过程不得分,分值10分)
class MyThread extends Thread{
public void run(){ try { Thread.currentThread().sleep(3000); } catch (InterruptedException e) { } System.out.println("MyThread running"); } }
public class ThreadTest{
public static void main(String argv[]) { MyThread t = new MyThread(); t.run(); t.start(); System.out.println("Thread Test"); } }
六、 执行B.main的结果是什么?(此题需写出分析过程,没有分析过程不得分,分值10分) class A {
void fun1() {
System.out.println(this.fun2()); }
int fun2() { return 123; } }
class B extends A { int fun2() { return 456; }
public static void main(String argv[]) { A a;
B b = new B(); b.fun1(); a = b; a.fun1(); } }
、 执行ListTest.main的结果是什么?(此题需写出分析过程,没有分析过程不得分,分值10分) class Data { int val;
int getVal() { return val; }
void setVal(int val) { this.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++) { data.setVal(i); list.add(data); }
int j = 0;
while (j < list.size()) {
Data tmp = (Data )list.get(j);
System.out.println("list(" + j + ") = " + tmp.getVal()); 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 = s.length(); } } 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面试宝典带答案】相关文章:
面试宝典07-13
Java集合面试问题和答案07-13
2015java面试题及答案06-23
unity 面试宝典07-11
Java常见面试题(含答案)07-13
高效面试技巧宝典06-23
32道java程序员面试题及答案07-13
程序员面试宝典07-09
职场女性英语面试宝典06-29
英语面试宝典在校表现06-29