Java Interview Trap Second wave

Source: Internet
Author: User
Tags abstract final modifiers modify reference variable
1.

Abstract class Name {

private String name;

Public abstract Boolean Isstupidname (String name) {}

}

Heroes, what's wrong with that?

Answer: wrong. Abstract method must end with a semicolon with no curly braces.

2.

public class Something {

void DoSomething () {

Private String s = "";

int L = s.length ();

}

}

Is it wrong?

Answer: wrong. You cannot place any access modifiers (private,public, and protected) before a local variable. Final can be used to modify local variables
(final, like abstract and STRICTFP, are non-access modifiers, STRICTFP can only modify class and method rather than variable).

3.

Abstract class Something {

Private abstract String dosomething ();

}

There seems to be nothing wrong with that, right?

Answer: wrong. The methods of abstract cannot be decorated with private. Abstract methods is to let subclass implement (achieve) specific details, how can use private to abstract
method to seal it up? (Similarly, abstract method cannot be added before final).

4.

public class Something {

public int AddOne (final int x) {

return ++x;

}

}

This is more obvious.

Answer: wrong. int x is modified to final, meaning that 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, is the final question, is this wrong?

Answer: Right. In AddOne method, the parameter o is decorated to final. If we modify the reference of O in AddOne method
(for example: o = new Other ();), so it is wrong to do so as in the previous example. But the modified here is O's member vairable
(member variable), and O's reference does not change.


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.