1.
Abstract class name {
Private string name;
Public Abstract Boolean isstupidname (string name ){}
}
What's wrong with this?
Answer:
Error. Abstract method 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:
Error. You cannot place any access modifiers (private, public, and protected) before local variables ). Final can be used to modify local variables.
(Like abstract and strictfp, final is not an access modifier. strictfp can only modify class and method rather than variable ).
3.
Abstract class something {
Private abstract string dosomething ();
}
Is there nothing wrong with this?
Answer:
Error. Abstract methods cannot be modified in private mode. Abstract METHODS is to make the sub-class implement (Implementation) specific details, how can I use private to abstract
What about method blocking? (Likewise, final cannot be added before abstract method ).
4.
Public class something {
Public int addone (final int X ){
Return ++ X;
}
}
This is obvious.
Answer:
Error. Int X is modified to final, meaning x cannot be 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;
}
Similar to the above, they are all about final. Is this wrong?
Answer:
Correct. In addone method, the parameter o is modified to final. If
The reference of O is modified in method.
(For example, O = new other. But here we modify the member of O.
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?
It cannot be seen.
Answer:
Correct. The output is "I = 0 ". Int I belongs to instant variable (instance variable, or member variable ). Instant
Variable has default value. The default value of int is 0.
7.
Class something {
Final int I;
Public void dosomething (){
System. Out. println ("I =" + I );
}
}
Unlike the above question, there is only one more final. Is that true?
Answer:
Error. Final int I is a final instant variable (instance variable, or member variable ). Final instant
Variable has no default value and must be assigned a clear value before the constructor ends. It can be changed to "finalint ".
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:
Error. It seems that there is no problem with calling dosomething in Main. After all, both methods are in the same class. But take a closer look, main is static. Static
Method cannot call non-static methods directly. You can change it to "system. Out. println (" S. dosomething () returns "+ S. dosomething ());". Similarly, static
Method cannot access non-static instant variable.
9.
Here, the name of the something class is otherthing. java.
Class something {
Private Static void main (string [] something_to_do ){
System. Out. println ("dosomething ...");
}
}
This seems obvious.
Answer:
Correct. No one has ever said that the Java class name must be the same as its file name. However, the public class name must be the same as the file name.
10.
Interface {
Int x = 0;
}
Class B {
Int x = 1;
}
Class C extends B implements {
Public void PX (){
System. Out. println (X );
}
Public static void main (string [] ARGs ){
New C (). PX ();
}
}
Answer: incorrect. An error occurs during compilation (the error description varies with the JVM, which means that the unspecified x call matches both of them (just like importing at the same time ).
When java. util and Java. SQL packages are declared, date is the same ). You can use super. X to specify the variables of the parent class, and the default property of the interface is
Public static final. Therefore, it can be identified through 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 = newball ("football ");
System. Out. println (ball. getname ());
}
}
This error is not easy to find.
Answer:
Error. "Interface rollableextends playable, bounceable" is correct. The interface can inherit multiple interfaces, so this is correct. The problem lies in Interface
"Ball ball = new ball (" pingpang ");" in rollable ");". Any interface declared in the interface
Variable (interface variable, also known as member variable). The default value is public static final. That is to say, "Ball
Ball = newball ("pingpang"); "is actually" public static final ball = newball ("pingpang ");". In the play () method of the ball class
= Newball ("football"); "changed the ball reference, and the ball here comes from rollable
Interface. The ball in rollable interface is public static.
Final and final objects cannot be changed by reference. Therefore, the compiler will
= Newball ("football"); "an error is displayed.