Java Interview Core Basics (1)

Source: Internet
Author: User
Tags throwable

1. Running results of the following code

String S1 = "HelloWorld";

String s2 = "Hello" + New Stirng ("World");

System.out.println (S1 = = s2);

Analysis:false,S1 = = S2 This compares the address of two objects, not the value, there isa new stirng inS2 ( "World"), the statement will open a new piece of memory to hold the World string, sos1 and S2 The address is different

2. The following statement is correct (C)

constructors in A.class cannot be omitted

B. The constructor must have the same name as class , and the method cannot have the same name as Class

C. The constructor is executed when an object is new

D. A class can have only one constructor

Analysis: The constructor in class does not write, there is an empty constructor (no parameter) by default , and the method in class is to drink the name of class, but few people will The method name in class is defined as a class name, and the only difference between a method and a constructor is that the constructor does not return a value ; a class There can be multiple constructors, which are distinguished by the different parameters.

The 3.java.lang.exception class is inherited from the (throwable Class)

Analysis:java.lang.Exceptionand thejava.lang.EcxceptionAre inherited fromThrowableof the class,Throwableclass is allJavathe base class of the exception class;Javaexceptions are classified as run-time exceptions (which are difficult to detect during program writing and can only be found at the stage in which the program is actually running, such as: array out of bounds, null pointer, denominator0and compile-time exceptions (which need to be handled by the programmer during program writing,Try/catchstatement block, do not add the compilation does not pass, such as:FileNotFoundException, etc.).

4. Running results of the following code

String []a = new STRING[10];

System.out.println (A[0]);

int []b = new INT[10];

System.out.println (b[1]);

Results:

Null

0

Analysis: After the first sentence is compiled, a piece of memory is created in the stack to place the string array variable a, but the elements in the array do not initialize any values, so a[0]...a[9] are the default values Null, similarly,the elements in an int array are not initialized, sob[0]...b[9] is the default value 0

5. Running results of the following code

Class a{

public static void Main (String args[]) {Thread t = new Thread () {public void run () {TE            St ();        }        };        T.run ();     System.out.print ("Test");    } public static void Test () {System.out.print ("test"); }

}

Results:testtest

Parse: First instantiate a thread object and implement Span style= "Font-family:times New Roman" >run method (default when a thread starts execution run method), and then t.run () execution is just thread in Object run method execution, rather than opening a thread, opening a thread with t.start () method, After execution print test test

6. The following statement is correct (AB)

A LinkedList inherits from List

B HashSet inherit from abstractset

C abstractset inherit from Set

D Weakmap inherit from HashMap

Analysis: See inheritance Diagram

http://img.blog.csdn.net/20140907123055043?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhlbmcwNTE4/font/ 5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/southeast

7. Does the existence of I make The establishment of i+1<i? Established

Analysis: Suppose i is of type int , when the value range (overflow) of type int is exceeded after i+1 , at this time i+1 will become negative, obviously negative < positive

the data type of the 8.0.123 is (double)

Analysis:0.123 Of course is double ,0.123f or 0.123F is Float

9. Which of the following is the character-oriented output stream (A)

A.bufferedwriter

B.fileinputstream

C.objectinputstream

D.inputstreamreader

Analysis: java io stream is divided into characters ( character ) and bytes ( byte read write , Byte-oriented operations are 8

character streams end with Reader/writer , and byte flows end with Inputstream/outputstream .

See Java Io flow graph:

http://img.blog.csdn.net/20140907123109536?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhlbmcwNTE4/font/ 5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/southeast

10. Talk about the understanding of interface (interface) in Java

Analysis: The interface is a kind of things of the attribute and behavior of the abstract, is a unified abstraction , since it is unified, it can not exist variables, therefore, the interface properties are public static final , the behavior is Public Abstract Of

(1) The interface indicates the external public service, so that the properties and behavior of the interface are publicly, who can use;

(2) The interface just tells the outside world an abstraction to describe what can be done, and the concrete inside the implementation is not explained, so the interface behavior is abstract ;

(3) The interface does not contain specific implementation details, and therefore cannot be instantiated , there is no instance variable exists.

(4) If there is only a constant in the interface and the value must be initialized .

How many ways to create objects in 11.Java? What's the difference?

Analysis: A total of 4 species, as follows:

(1) New statement to create an object (call constructor)

(2) Reflection mechanism, invoking the class class or newinstance () instance method of the Constructor class (call constructor)

(3) Call the Clone method (which is the cloneof the object on memoryand does not call the constructor)

(4) deserialization (call the ReadObject () method of the objectinputstream object to restore the class object from the file)

12. Is there a problem with the following code, if no problem, the result of the operation?

public class NULL {public static void T () {System.out.println ("TTT"); public static void Main (string[] args) {(null) null). T ();

SYSTEM.OUT.PRINTLN (((null) null)); }}

Results:

Ttt

Null

Analysis:

(1) Null is not a keyword or reserved word in Java, so null can be a class name;

(2)null is a keyword in Java, anda null value can be converted to any type, but is still null after conversion (Invalid object) value In additionTfunction isStatic, the class name can be directly.function name to invoke, so theMainthe second statement in the function can execute normally, printingNULL; (If the functionTis notStatic, the program will be an error)

13. Talk about static code blocks, construct code blocks, and execute order of constructors

Static code blocks are executed first, and finally the construction code blocks and constructors are executed (both bound, with the internal order of first constructing the code block, and the post constructor)

Analysis: When a class is loaded, from the parent class to the subclass, from top to bottom, execute the static code block, and then execute the main function , if there is a statement of the new object, The construction code block and constructor of the binding are executed.

Note: static code blocks have and execute only once

14. What is the result of the following code?

public class a{

public static void Main (string[] args) {

String str;

System.out.println ("" + str);

}

}

Analysis: The above code will not be compiled through, if str is not initialized, is not printed output,java Basic types or objects need to be data, this is different from the class's properties (member variables).

Then look at the following code:

public class a{

Static String str;

public static void Main (string[] args) {

System.out.println ("" + str);

}

}

Parsing: The code compiles through, because str is the property of the class, so print it out as The default value of str NULL

There are no reference passes in Java, only values are passed. (Whether it is an object, a primitive type, or an array, the actual address cannot be changed in the function, only the value can be changed)

Can the String class in 16.Java inherit? Not to

Analysis: TheString class is final and therefore cannot be inherited

can a constructor in 17.Java beoverridden (rewritten)? Not to

Analysis:Java Constructors cannot inherit, so overrideis not possible, but can be overload(overloaded)

Java Interview Core Basics (1)

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.