Java Interview Core Basics (1)

Source: Internet
Author: User
Tags instance method throwable

1. Execution results of the following code

String S1 = "HelloWorld";

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

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

Analysis:false,S1 = = S2 This comparison is the address of two objects, not the value. There is a new stirng ("World") in S2 ,which will open up a piece of memory to hold the World string, so S1 with the 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 runs 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 by default (no participation) , the method in class is able to drink The name of the class, Only very few people Define the method names in class as class names, and the only difference between a method and a constructor is that the constructor has no return value ; class there can be multiple constructors, which are distinguished by the difference in the number of 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 divided into execution-time exceptions (which are difficult to detect during program writing, but can be found only at the stage in which the program actually executes, such as: array out-of-bounds. Null pointer, denominator is0etc.) and compile-time Exceptions (program apes are required to process during program writing.) The need to joinTry/catchstatement block, do not join the compilation does not pass, such as:FileNotFoundException, etc.).

4. Execution 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 are uninitialized regardless of the value, so a[0]...a[9] is the default value null Empathy the elements in an int array are also uninitialized values. Therefore,b[0]...b[9] is the default value of 0

5. Execution 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

Analysis : First instantiate a thread object and implement the Run method (which runs the Run method by default when a thread starts running ), and then T.run () runs without running the Run method in the Thread object , rather than opening a thread. Open thread with t.start () method. print testafter run, and then print test immediately thereafter .

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: If i is of type int , when the value range (overflow) of type int is exceeded after i+1 . At this point the i+1 becomes negative, obviously negative < positive

the data type of the 8.0.123 is (double)

Analysis:0.123 is of course double . 0.123f or 0.123F is float

9. Which of the following is a 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: An interface is an abstraction of the properties and behavior of a class of things, and is a unified abstraction . Since it is unified, there is no variable, so the properties in the interface are public static final , and the behavior is public abstract .

(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 merely tells the outside world an abstraction to describe what the narrative can do. The implementation in detail is not explained, so the behavior in the interface is abstract ;

(3) The interface does not include detailed implementation details, so it cannot be instantiated and there is no instance variable.

(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 is each?

Analysis: A total of 4 kinds. For example, the following:

(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. The following code is a problem, if no problem. What is the execution result?

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. a null value can be converted to a type that is no matter what, but is still a null(Invalid object) value after conversionIn additionTfunction isStatic. Can direct class name.function name to invoke, so theMainthe second statement in the function will work correctly. PrintNULL; (If the functionTis notStatic, the program will be an error)

13. Talk about static code blocks and construct blocks of code. The order in which constructors are run

A static code block runs first, and finally constructs a code block and a constructor run (both bound, with the inner order of building the code block first, and the post constructor)

Parse: When a class is loaded and finished, from the parent class to the child class. From top to bottom. Run the static code block first, then run the main function , and if there is a statement for the new object, run the bound construction code block and constructor.

Note: static code blocks are available and run only once

14. What is the execution 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 type or object needs to be data, this is different from the class's properties (member variables).

Next, look at the following code:

public class a{

Static String str;

public static void Main (string[] args) {

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

}

}

Analysis: The code is compiled and passed. Because str is a property of the class, it prints the default value of str to null

There is no reference pass in Java. Only value is passed.

(Regardless of object, primitive type, or array, the actual address cannot be changed in the function.) Can only change the value)

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

Analysis: TheString class is final and cannot be inherited

can a constructor in 17.Java be overridden (rewritten)? Not be able to

Analysis:Java Constructors are not able to inherit and therefore cannot override, but can overload(overloaded)

Java Interview Core Basics (1)

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.