25 Java questions selected by netizens

Source: Internet
Author: User
Tags finally block

1. Is float f = 3.4 correct?

A: incorrect. It should be float f = 3.4f.

2. after an object is passed as a parameter to a method, this method can change the properties of this object and return the changed result. Is it a value transfer or a reference transfer?

A: It is a reference transfer.

3. in java, a class is declared as the final type. What does it mean?

A: It indicates that the class cannot be inherited. It means that it cannot generate a new subclass and cannot be inherited as a parent class. It is a top-level class.

4. What is the difference between Error and Exception?

A: Error indicates a system-level Error or an exception that the program does not need to handle. It is an internal Error or a hardware problem in the java Runtime Environment, for example, insufficient memory resources. For this Error, the program is basically powerless and has no choice but to quit.

Exception (Violation) indicates an Exception that needs to be captured or processed by a program. It processes general problems caused by flaws in program design or external input, is required by the program.

5. Let's talk about the differences between final and finally.

A: final is a modifier (keyword). If a class is declared as final, it means that it cannot generate a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final. Declare variables or methods as final to ensure that they are not changed during use. Variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be overloaded.

Finally provides finally blocks for troubleshooting. If an exception is thrown, the matched catch clause will be executed, and the control will enter the finally block (if any). As long as there is a finally statement block, no matter how the program runs, it must be executed.

6. String s = new String ("xyz"); how many String objects are created?

A: Two character objects and one referenced object are created.

7. short s1 = 1; s1 = s1 + 1; what is the error? Short s1 = 1; s1 + = 1; what is the error?

A: The two errors are the same. The latter is different in addition. Both errors are caused by the fact that s1 is a short type and 1 is an integer type. Then, the Data Type changes to an integer type, there is a conflict with s1 defined as the short type. It must be forced to convert the type to be correct.

8. Does the array have the length () method? Does String have the length () method?

A: No array, and no String.

9. Differences between Overload and Override

A: Overriding and Overloading are different manifestations of Java polymorphism. Overriding is a manifestation of polymorphism between parent class and Child class, and Overloading is a manifestation of polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ). The Overloaded method can change the type of the returned value.

10. What is the difference between abstract class and interface?

A: The class that declares the existence of a method without implementing it is called abstract class. It is used to create a class that reflects some basic behaviors and declare a method for this class, however, this class cannot be implemented in this class. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of Abstract classes provide implementation for all Abstract methods in their parent classes. Otherwise, they are also Abstract classes. Instead, implement this method in the subclass. Other classes that know their behavior can implement these methods in the class.

An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. All methods in the interface are abstract, and none of them have a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.

11. Can Constructor be overwritten?

A: Yes.

12. Can I inherit the String class?

A: The String class is a final class, so it cannot be inherited.

13. The questions are as follows:

Parent class:

Public class FatherClass

{

Public FatherClass ()

{

System. out. println ("FatherClass Create ");

}

}

Subclass:

Public class ChildClass extends FatherClass

{

Public ChildClass ()

{

System. out. println ("ChildClass Create ");

}

Public static void main (String [] args)

{

FatherClass fc = new FatherClass ();

ChildClass cc = new ChildClass ();

}

}

The output result is as follows:

FatherClass Create +

FatherClass Create

ChildClass Create

14. public class OuterClass {

Private double d1 = 1.0;

// Insert code here ~~ Line 3

}

Select two correct answers from the following options and place them in line 3 (c e)

A. class InnerOne {

Public static double methoda () {return d1 ;}

}

B. public class InnerOne {

Static double methoda () {return d1 ;}

}

C. private class InnerOne {

Double methoda () {return d1 ;}

}

D. static class InnerOne {

Protected double methoda () {return d1 ;}

}

E. abstract class InnerOne {

Public abstract double methoda ();

}

15. Differences between STRING and STRINGBUFFER

A: The length of a STRING is unchangeable. Once a String object is created, the content ("STRING") contained in this instance cannot be changed until the object is destroyed, therefore, the variable pointing to a String object is essentially a constant, and the String object is also known as a constant object. The length of STRINGBUFFER is variable and can be obtained through the append () and insert () of StringBuffer (), reverse (), setCharAt (), setLength () and other methods, you can modify the content in this string. If you often operate on the content in the String, especially when the content is to be modified, use StringBuffer. If the String is needed at last, use the toString () method of StringBuffer.

16. What is the method for JAVA class implementation to be prefixed?

A: to serialize JAVA classes, the corresponding classes must first implement the Serializable and Externalizable interfaces, and then they can call the witeObject () of ObjectOutputStream () to save the object and read the saved object through the readObjeact () method of the ObjeatInputStream.

17. What interfaces should be implemented for comparison in the Collection framework?

A: To implement the Comparable interface and implement the unique method cpmparaTo () of this interface, accept an Object and define the Object sorting rules in this method.

18. Introduce and draw the Collection framework structure in JAVA

<> Collection

<> Set

<> List

HashSet

Tree set

ArrayList

Vector

19. Programming: list all files in a folder (enter the folder from the command line)

Solution:

Import java. io .*;

Public class listFile

{

Public static void main (String [] args)

{

String s = "";

InputStreamReader ir = new InputStreamReader

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.