Written questions-----Java code error checking

Java code error checking

abstract class Name {
   private String name;
   public abstract boolean isStupidName(String name) {}
}
     Heroes, what is wrong?
     Answer: wrong. abstract methods must end with a semicolon without curly braces.
2.
public class Something {
   void doSomething () {
       private String s = "";
       int l = s.length();
   }
}
     Is it wrong?
     Answer: wrong. Local variables cannot be preceded by any access modifiers (private, public, and protected). Final can be used to modify local variables
(final, like abstract and strictfp, are non-access modifiers, and strictfp can only modify class and method instead of variable).
3.
abstract class Something {
   private abstract String doSomething ();
}
     This seems to be nothing wrong, right?
     Answer: wrong. Abstract methods cannot be modified with private. The abstract methods are to let the subclass implement the specific details. How can the abstract
method be blocked with private? (Similarly, final cannot be added before the abstract method).
4.
public class Something {
   public int addOne(final int x) {
       return ++x;
   }
}
     This is more obvious.
     Answer: wrong. int x is modified as final, which means that x cannot be modified in the 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;
}
     Similar to the above, it's all about final, is this wrong?
     Answer: True. In the addOne method, the parameter o is modified as final. If we modify the reference of o in the addOne method
(eg: o = new Other();), then this question is also wrong as in the above example. But what is modified here is o's member vairable
(member variable), and o's reference has not changed.
6.
class Something {
    int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
}
     What's wrong? I can't see it.
     Answer: Correct. The output is "i = 0". int i belongs to instant variable (instance variable, or member variable). An instant variable has a default value. The default value of int is 0.
7.
class Something {
    final int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
}
     There is only one difference from the above question, that is, there is an additional final. Is this wrong?

     Answer: Wrong. final int i is a final instant variable (instance variable, or member variable). A final instant variable has no default value and must be assigned an explicit value before the constructor ends. Can be modified to "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 ...";
    }
}
     looks perfect.
     Answer: wrong. It seems that there is no problem with calling doSomething in main, after all, both methods are in the same class. But look closely, main is static. Static methods cannot directly call non-static methods. Can be changed to "System.out.println("s.doSomething() returns " + s.doSomething());". Similarly, static methods cannot access non-static instant variables.
9.
Here, the file name of the Something class is OtherThing.java
class Something {
    private static void main(String[] something_to_do) {       
        System.out.println("Do something ...");
    }
}
This seems obvious.
     Answer: Correct. No one ever said that Java's class name must be the same as its file name. But the name of the public class must be the same as the file name.
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();
   }
}
     Answer: False. An error will occur at compile time (the error describes that different JVMs have different information, which means that the unspecified x is called, and both x's match (like directly declaring Date when importing both java.util and java.sql packages at the same time) The same). For the variables of the parent class, you can use super.x to make it clear, and the properties of the interface are implicitly public static final by default. So you can make it clear through Ax.
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());
    }
}
this Errors are not easy to spot.
     Answer: wrong. "interface Rollable extends Playable, Bounceable" is no problem. An interface can inherit from multiple interfaces, so this is correct. The problem is "Ball ball = new Ball("PingPang");" in interface Rollable. Any interface variable (interface variable, also called member variable) declared in the interface defaults to public static final. That is, "Ball ball = new Ball("PingPang");" is actually "public static final Ball ball = new Ball("PingPang");". In the Play() method of the Ball class, "ball = new Ball("Football");" changes the reference of the ball, and the ball here comes from the Rollable interface, the ball in the Rollable interface is public static final, the final object The reference cannot be changed. So the compiler will start at "ball = new Ball("Football");"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326678960&siteId=291194637