Java code Error Test Set

Source: Internet
Author: User

1.

Abstract class name {

Private string name;

Public Abstract Boolean isstupidname (string name ){}

}

What's wrong with this?

Answer: Yes. 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: Yes. 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: Yes. 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: Yes. Int X is modified to final, meaning x cannot be modified 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 we modify the reference of o in addone method

(For example, O = new other. But here we modify the member vairable of O.

(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: Yes. Final int I is a final instant variable (instance variable, or member variable ). The instant variable of final has no default value and must be assigned a clear value before the constructor ends. It can be changed 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: Yes. 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 ("do something ...");

}

}

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 Java at the same time. util and Java. when two SQL packages are declared, the date is the same ). The variables of the parent class can be identified using super. X, and the interface property is implicitly public static final by default. Therefore, A. X can be used to specify the variables.

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 error is not easy to find.

answer: Yes. "Interface rollable extends playable, bounceable" is correct. The interface can inherit multiple interfaces, so this is correct. The problem lies in "ball = new ball (" pingpang ");" in interface rollable ");". The default value of interface variable declared in the interface is public static final. That is to say, "ball = new ball (" pingpang ");" is actually "public static final 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 rollable interface is public static final, and the final object cannot be changed by reference. Therefore, the compiler will display errors in "ball = new ball (" football.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.