12.Java 面试题整理(JAVA 代码查错)

1.下面代码有什么错误

abstract class Name {

private String name;

public abstract boolean isStupidName(String name) {}

}

答案: 抽象方法不应该有实现,abstract method 必须以分号结尾,且不带花括号。

2.下面代码有什么错误

public class Something {

void doSomething () {

private String s = "";

int l = s.length();

}

}

答案:

局部变量前不能放置任何访问修饰符 (privatepublic,和 protected)final 可以用来修饰局部变量 。(final 如同 abstract strictfp,都是非访问修饰符, strictfp 只能修饰 class method 而非variable)

3.下面代码有什么错误

abstract class Something {

private abstract String doSomething ();

}

答案:

abstract methods 不能以 private 修饰。 abstract methods 就是让子类 implement(实现 )具体细节的,怎么可以用 private abstract method 封锁起来呢 ? (同理, abstract method 前不能加 final)

  1. 下面代码有什么错误

public class Something {

public int addOne(final int x) {

return ++x;

}

}

答案:

int x 被修饰成 final,意味着 x 不能在 addOne method 中被修改。

5.下面代码有什么错误

public class Something {

public static void main(String[] args) {

Other o = new Other();

new Something().addOne(o);

}

public void addOne(final Other o) {

o.i++;

}

}

class Other {

public int i;

}

答案:

正确。在 addOne method 中,参数 o 被修饰成 final。如果在 addOne method 里我们修改了 o

reference(引用) 。(比如 : o = new Other();),那么如同上例这题也是错的。但这里修改的是 o member vairable (成员变量 ),而 o reference 并没有改变。

6.下面代码有什么错误

class Something {

int i;

public void doSomething() {

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

}

}

答 案: 正确。输出的是 "i = 0"int i 属於 instant variable (实例变量,或叫成员变量 )instant

variable default valueint default value 0

7.下面代码有什么错误

class Something {

final int i;

public void doSomething() {

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

}

}

答 案:

final int i 是个 final instant variable (实例变量,或叫成员变量 )final instant variable 没有default value,必须在 constructor (构造器 )结束之前被赋予一个明确的值。可以修改为 "final int i = 0;"

8.下面代码有什么错误

public class Something {

public static void main(String[] args) {

Something s = new Something();

System.out.println("s.doSomething() returns " + doSomething());

}

public String doSomething() {

return "Do something ...";

}

}

答 案:

错。看上去在 main call doSomething 没有什么问题,毕竟两个 methods 都在同一个 class 里 。

但仔细看, main static 的。 static method 不能直接 call non-static methods。可改成

"System.out.println("s.doSomething() returns " + s.doSomething());"。同理, static method 不能访问 non-static instant variable

9.此处, Something 类的文件名叫 OtherThing.java 里面的代码,如下:

class Something {

private static void main(String[] something_to_do) {

System.out.println("Do something ...");

}

}

答案:

正确。从来没有人说过 Java Class 名字必须和其文件名相同。但 public class 的名字必须和文件名相同。 ??

10下面代码有什么错误

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}

答 案:

错误。在编译时会发生错误 (错误描述不同的 JVM 有不同的信息,意思就是未明确的 x 调用,两个 x

匹配(就象在同时 import java.util java.sql 两个包时直接声明 Date 一样)。对于父类的变量 ,可以用 super.x 来明确,而接口的属性默认隐含为 public static final.所以可以通过 A.x 来明确。

11.下面代码有什么错误

interface Playable {

void play();

}

interface Bounceable {

void play();

}

interface Rollable extends Playable, Bounceable {

Ball ball = new Ball("PingPang");

}

class Ball implements Rollable {

private String name;

public String getName() {

return name;

}

public Ball(String name) {

this.name = name;

}

public void play() {

ball = new Ball("Football");

System.out.println(ball.getName());

}

}

答 案:

错。 "interface Rollable extends Playable, Bounceable"没有问题。 interface 可继承多个

interfaces,所以这里没错。问题出在 interface Rollable 里的 "Ball ball = new Ball("PingPang");"任何在 interface 里声明的 interface variable (接口变量,也可称成员变量 ),默认为 public static final。也就是说 "Ball ball = new Ball("PingPang");"实际上是 "public static final Ball ball = new Ball("PingPang");"。在 Ball 类的 Play()方法中, "ball = new Ball("Football");"改变了 ball reference,而这里的 ball 来自 Rollable interfaceRollable interface 里的 ball public static final 的, final object 是不能被改变 reference 的。因此编译器将在 "ball = new Ball("Football");" 这里显示有错。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值