[关闭]
@liayun 2016-05-25T15:19:09.000000Z 字数 5262 阅读 1599

一份面试题

java基础


注:按Java规范书写代码,如果你认为程序有错误,请指出,并说明程序错误原因。
1.写出程序结果

  1. class Demo {
  2. public static void func() {
  3. try {
  4. throw new Exception();
  5. } finally {
  6. System.out.println("B");
  7. }
  8. }
  9. public static void main(String[] args) {
  10. try {
  11. func();
  12. System.out.println("A");
  13. } catch (Exception e) {
  14. System.out.println("C");
  15. }
  16. System.out.println("D");
  17. }
  18. }

2.写出程序结果

  1. class Test {
  2. Test() {
  3. System.out.println("Test");
  4. }
  5. }
  6. class Demo extends Test {
  7. Demo() {
  8. System.out.println("Demo");
  9. }
  10. public static void main(String[] args) {
  11. new Demo();
  12. new Test();
  13. }
  14. }

3.写出程序结果

  1. interface A() {}
  2. class B implements A {
  3. public String func() {
  4. return "func";
  5. }
  6. }
  7. class Demo {
  8. public static void main(String[] args) {
  9. A a = new B();
  10. System.out.println(a.func());
  11. }
  12. }

4.写出程序结果

  1. class Fu {
  2. boolean show(char a) {
  3. System.out.println(a);
  4. return true;
  5. }
  6. }
  7. class Demo extends Fu {
  8. public static void main(String[] args) {
  9. int i = 0;
  10. Fu f = new Demo();
  11. Demo d = new Demo();
  12. for(f.show('A'); f.show('B') && (i < 2); f.show('C')) {
  13. i++;
  14. d.show('D');
  15. }
  16. }
  17. boolean show(char a) {
  18. System.out.println(a);
  19. return false;
  20. }
  21. }

5.写出程序结果

  1. interface A {}
  2. class B implements A {
  3. public String test() {
  4. return "yes";
  5. }
  6. }
  7. class Demo {
  8. static A get() {
  9. return new B();
  10. }
  11. public static void main(String[] args) {
  12. A a = get();
  13. System.out.println(a.test());
  14. }
  15. }

6.写出程序结果

  1. class Super {
  2. int i = 0;
  3. public Super(String a) {
  4. System.out.println("A");
  5. i = 1;
  6. }
  7. public Super() {
  8. System.out.println("B");
  9. i+=2;
  10. }
  11. }
  12. class Demo extends Super {
  13. public Demo(String a) {
  14. System.out.println("C");
  15. i = 5;
  16. }
  17. public static void main(String[] args) {
  18. int i = 4;
  19. Super d = new Demo("A");
  20. System.out.println(d.i);
  21. }
  22. }

7.

  1. interface Inter {
  2. void show(int a, int b);
  3. void func();
  4. }
  5. class Demo {
  6. public static void main(String[] args) {
  7. // 补足代码:调用两个函数,要求用匿名内部类
  8. }
  9. }

8.写出程序结果

  1. class TD {
  2. int y = 6;
  3. class Inner {
  4. static int y = 3;
  5. void show() {
  6. System.out.println(y);
  7. }
  8. }
  9. }
  10. class TC {
  11. public static void main(String[] args) {
  12. TD.Inner ti = new TD().new Inner();
  13. ti.show();
  14. }
  15. }

9.选择题,写出错误答案错误的原因,用单行注释的方式

  1. class Demo {
  2. int show(int a, int b){return 0;}
  3. }

下面哪些函数可以存在于Demo的子类中?

A.public int show(int a, int b){return 0;}
B.private int show(int a, int b){return 0;} 
C.private int show(int a, long b){return 0;} 
D.public short show(int a, int b){return 0;} 
E.static int show(int a, int b){return 0;} 

10.写出this关键字的含义,final有哪些特点?
11.写出程序结果

  1. class Fu {
  2. int num = 4;
  3. void show() {
  4. System.out.println("showFu");
  5. }
  6. }
  7. class Zi extends Fu {
  8. int num = 5;
  9. void show() {
  10. System.out.println("showZi");
  11. }
  12. }
  13. class T {
  14. public static void main(String[] args) {
  15. Fu f = new Zi();
  16. Zi z = new Zi();
  17. System.out.println(f.num);
  18. System.out.println(z.num);
  19. f.show();
  20. z.show();
  21. }
  22. }

12.

  1. interface A {
  2. void show();
  3. }
  4. interface B {
  5. void add(int a, int b);
  6. }
  7. class C implements A, B {
  8. // 程序代码
  9. }
  10. class D {
  11. public static void main(String[] args) {
  12. C c = new C();
  13. c.add(4, 2);
  14. c.show(); // 通过该函数打印以上两个数的和
  15. }
  16. }

13.写出程序结果

  1. class Demo {
  2. public static void main(String[] args) {
  3. try {
  4. showExce();
  5. System.out.println("A");
  6. } catch(Exception e) {
  7. System.out.println("B");
  8. } finally {
  9. System.out.println("C");
  10. }
  11. System.out.println("D");
  12. }
  13. public static void showExce() throws Exception {
  14. throw new Exception();
  15. }
  16. }

14.写出程序结果

  1. class Super {
  2. int i = 0;
  3. public Super(String s) {
  4. i = 1;
  5. }
  6. }
  7. class Demo extends Super {
  8. public Demo(String s) {
  9. i = 2;
  10. }
  11. public static void main(String[] args) {
  12. Demo d = new Demo("yes");
  13. System.out.println(d.i);
  14. }
  15. }

15.写出程序结果

  1. class Super {
  2. public int get(){return 4;}
  3. }
  4. class Demo15 extends Super {
  5. public long get(){return 5;}
  6. public static void main(String[] args) {
  7. Super s = new Demo15();
  8. System.out.println(s.get());
  9. }
  10. }

16.写出程序结果

  1. class Demo {
  2. public static void func() {
  3. try {
  4. throw new Exception();
  5. System.out.println("A");
  6. } catch(Exception e) {
  7. System.out.println("B");
  8. }
  9. }
  10. public static void main(String[] args) {
  11. try {
  12. func();
  13. } catch(Exception e) {
  14. System.out.println("C");
  15. }
  16. System.out.println("D");
  17. }
  18. }

17.

  1. class Demo {
  2. public void func() {
  3. // 位置1
  4. }
  5. class Inner {}
  6. public static void main(String[] args) {
  7. Demo d = new Demo();
  8. // 位置2
  9. }
  10. }
A.在位置1写 new Inner(); 
B.在位置2写 new Inner();
C.在位置2写 new d.Inner();  
D.在位置2写 new Demo.Inner();

18.写出程序结果

  1. class Exc0 extends Exception {}
  2. class Exc1 extends Exc0 {}
  3. class Demo {
  4. public static void main(String[] args) {
  5. try {
  6. throw new Exc1();
  7. } catch(Exception e) {
  8. System.out.println("Exception");
  9. } catch(Exc0 e) {
  10. System.out.println("Exc0");
  11. }
  12. }
  13. }

19.

  1. interface Test {
  2. void func();
  3. }
  4. class Demo {
  5. public static void main(String[] args) {
  6. // 补足代码:(匿名内部类)
  7. }
  8. void show(Test t) {
  9. t.func();
  10. }
  11. }

20.写出程序结果,一道java国际考试题。

  1. class Test {
  2. public static String output = "";
  3. public static void foo(int i) {
  4. try {
  5. if(i == 1)
  6. throw new Exception();
  7. output+="1";
  8. } catch(Exception e) {
  9. output+="2";
  10. return;
  11. } finally {
  12. output+="3";
  13. }
  14. output+="4";
  15. }
  16. public static void main(String[] args) {
  17. foo(0);
  18. System.out.println(output);
  19. foo(1);
  20. System.out.println(output);
  21. }
  22. }

21.补足compare函数内的代码,不许添加其他函数。

  1. class Circle {
  2. private static double pi = 3.14;
  3. private double radius;
  4. public Circle(double r) {
  5. radius = r;
  6. }
  7. public static double compare(Circle[] cir) {
  8. // 程序代码,其实就是在求数组中的最大值
  9. }
  10. }
  11. class TC {
  12. public static void main(String[] args) {
  13. Circle cir[] = new Circle[3]; // 创建一个类类型数组
  14. cir[0] = new Circle(1.0);
  15. cir[1] = new Circle(2.0);
  16. cir[2] = new Circle(4.0);
  17. System.out.println("最大的半径值是:"+Circle.compare(cir));
  18. }
  19. }

22.写出程序结果

  1. public class Demo {
  2. private static int j = 0;
  3. private static boolean methodB(int k) {
  4. j += k;
  5. return true;
  6. }
  7. public static void methodA(int i) {
  8. boolean b;
  9. b = i < 10 | methodB(4);
  10. b = i < 10 || methodB(8);
  11. }
  12. public static void main(String[] args) {
  13. methodA(0);
  14. System.out.println(j);
  15. }
  16. }

23.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递给该方法。如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中,以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
24.补足compare函数内的代码,不许添加其他函数。

  1. class Circle {
  2. private double radius;
  3. public Circle(double r) {
  4. radius = r;
  5. }
  6. public Circle compare(Circle cir) {
  7. // 程序代码
  8. }
  9. }
  10. class TC {
  11. public static void main(String[] args) {
  12. Circle cir1 = new Circle(1.0);
  13. Circle cir2 = new Circle(2.0);
  14. Circle cir;
  15. cir = cir1.compare(cir2);
  16. if(cir1 == cir)
  17. System.out.println("圆1的半径比较大");
  18. else
  19. System.out.println("圆2的半径比较大");
  20. }
  21. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注