Java topic written answer wrong - java code View
1.
Abstract class Name (
Private String name;
Public abstract boolean isStupidName (String name) ()
)
Daxia have, that is wrong?
Answer: wrong. Abstract method must be a semicolon at the end, not with flowers brackets.
2.
(Public class Something
Void doSomething () (
Private String s = "";
Int l = s.length ();
)
)
Wrong?
Answer: wrong. Local variables can not be placed before any visit Xiuchifu (private, public, and protected). Final modified local variables can be used to
(Final strictfp as abstract and are non-Xiuchifu visit, strictfp only modified class and method rather than variable).
3.
Abstract class Something (
Private abstract String doSomething ();
)
This seems nothing wrong?
Answer: wrong. Abstract to the private methods can not modified. The abstract category of methods is to implement (it) specific details, how can be used to abstract private
Method cordoned off? (By the same token, abstract method can not increase before final).
4.
(Public class Something
Public int addOne (final int x) (
Return x + +;
)
)
This relatively clear.
Answer: wrong. Int x modified as final, means that x can not be changed in 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;
)
And is very similar to the above are on the final issues, which is wrong?
Answer: True. In addOne method, the parameters were modified o-final. If addOne method, we have amended the reference o the
(For example: o = new Other ();), then on cases like this and that is wrong. But here is o amend the member vairable
(Member variables), and o the reference and have not changed.
6.
Class Something (
Int i;
Public void doSomething () (
System.out.println ( "i =" + i);
)
)
What is wrong? Could not see ah.
Answer: True. Output is "i = 0." Int i belong instant variable (instance variables, or the member variable). Instant variable is default value. Int the default value is 0.
7.
Class Something (
Final int i;
Public void doSomething () (
System.out.println ( "i =" + i);
)
)
And that there is only one above a different place, is more than a final. Is this the wrong?
Answer: wrong. Final int i is the final instant variable (instance variables, or the member variable). The final instant variable no default value must be in the constructor (structure) was given before the end of a clear value. Can be adapted 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. Appear in the main, call doSomething no problem, after all, the two methods are in the same class. But a close look at, the main is static. Static method can not directly call non-static methods. Can be changed to "System.out.println (" s.doSomething () returns "+ s.doSomething ());". Similarly, static method can not access non-static instant variable.
9.
Here, the paper called Something like OtherThing.java
Class Something (
Private static void main (String [] something_to_do) (
System.out.println ( "Do something …");
)
)
This seems very obvious.
Answer: True. No one has ever said that Java-Class and its name must be the same as the file name. But public class and the name of the file name must be the same.
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. Will be compiled in error (wrong description of the JVM have different information, the meaning is not clearly call x, x is two matches (at the same time as import java.util and two java.sql package directly statement Date the same). father of the variables can be used super.x to clear, and the properties of the default interface for the implicit public static final. therefore can Ax to clear.
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 mistake is not easy to find.
Answer: wrong. "Interface Rollable extends Playable, Bounceable" there is no problem. Gejicheng multiple interfaces interface, right here. The problem is the interface Rollable, "Ball ball = new Ball (" PingPang ");." Any statement in the interface interface variable (variable interface, but also can be said to the members of variables), the default public static final. In other words, "Ball ball = new Ball (" PingPang ");" is in fact "public static final Ball ball = new Ball (" PingPang ");." In the category of Play Ball () method, the "ball = new Ball (" Football ");" change the reference of the ball, and here the ball from Rollable interface, Rollable interface, the ball is public static final, the final object is the reference can not be changed. So the compiler will be in the "ball = new Ball (" Football ");" shown here is wrong.
Tags: java code






