Java interview questions compulsory 50 (with answers) 5

41, the date and time:

1) how to get the date, hours, minutes, seconds?

2) How to get the number of milliseconds from at 0:00:00 on January 1, 1970 to the present?

3) How do you get the last day of a month?

4) How to format dates?

A: The operating method is as follows:

1) Create java.util.Calendar instance, calls that get () method passing different parameter values ​​can be obtained corresponding to the parameter

2) The following method can be obtained the number of milliseconds:

Calendar.getInstance().getTimeInMillis();  
System.currentTimeMillis();  
//何问起 hovertree.com

3) the following sample code:

Calendar time = Calendar.getInstance();  
time.getActualMaximum(Calendar.DAY_OF_MONTH);  
//何问起 hovertree.com

Subclass 4) using java.text.DataFormat (e.g. SimpleDateFormat class) in the format (Date) method can be formatted date.

 

42, print the current time yesterday.

answer:

 

public class YesterdayCurrent {  
    public static void main(String[] args){  
        Calendar cal = Calendar.getInstance();  
        cal.add(Calendar.DATE, -1);  
        System.out.println(cal.getTime());  
    }  
}  //何问起 hovertree.com

 

43, compare the Java and JavaSciprt.

Answer: JavaScript and Java are two developed two different products. Sun introduced Java is the original object-oriented programming language, particularly suitable for Internet application development; and JavaScript is Netscape's products - a way to extend the Netscape browser functionality developed can be embedded in Web pages running on objects and event-driven interpretive language, which is the predecessor LiveScript; and the Java language, formerly known as Oak.

Next, the similarities and differences between the two languages ​​to make the following comparison:

1) object-based and object-oriented: Java is a true object-oriented language, even the development of simple procedures, must design objects; JavaScript is kind of scripting language that can be used to make the network independent, user interaction the complex software. It is an object (Object-Based) based and event-driven (Event-Driven) programming language. So it itself provides a very rich internal object for designers to use;

2) interpretation and compilation: Java source code before execution, must be compiled; JavaScript is an interpreted programming language, its source code need not compiled, interpreted by the browser execution;

3) the type of weak and strong type variable variable: Java strongly typed variable checking that all variables before compiling must make a statement; JavaScript in variable declaration, using its weak type. I.e., no need to make the variable declaration before use, but the interpreter checks its data type at runtime;

4) code format is not the same.

Added: four points listed above is the original so-called standard answer given. In fact, Java and JavaScript the most important difference is that a static language, is a dynamic language. Current trends programming language is a functional and dynamic languages. In Java class (class) are first-class citizens, and the JavaScript function (function) is a first-class citizens. For this problem, in the interview or answer in their own words will be more tricky.

 

44, when to use assert?

A: assertion (assertion) is a common debug mode in software development languages ​​support this mechanism for many developers. Generally speaking, assertion assurance procedures for basic and critical correctness. assertion checking is usually open during development and testing. To improve performance, after the software release, assertion checking is usually closed. In implementation, the assertion is a statement that contains a Boolean expression, assumes that the expression is true when the implementation of the sentence; if the expression evaluates to false, then the system will report a AssertionError.

Assertion for debugging purposes:

assert(a > 0); // throws an AssertionError if a <= 0

Assertions can come in two forms:

assert Expression1;

assert Expression1 : Expression2 ;

Expression1 should always produce a Boolean value.

Expression2 a derived value may be any expression; this value is used to generate more debugging information string message display.

Assertion is disabled by default, to enable assertions at compile time, you need to use source 1.4 mark:

javac -source 1.4 Test.java

To enable assertions at runtime, use -enableassertions or -ea mark.

To choose to disable assertions at runtime, use the -da or -disableassertions mark.

To enable assertions system classes, use -esa or -dsa mark. It can also enable or disable assertions on the basis of the package. Placed assertion can not reach any location in the normally expected. Assertions can be used to verify the parameters passed to the private methods. However, the assertion should not be used to validate the parameters passed to public methods, because regardless of whether the assertion is enabled, the public methods to check its parameters. However, both can also be utilized postcondition assertion test in non-public method in the public process. In addition, the assertion should not in any way alter the program state.

 

45, Error and Exception What is the difference?

Answer: Error indicates an abnormal level of system errors and does not have to deal with, is a serious problem in the case of recovery is not impossible, but very difficult; such as memory overflow, the program can not be expected to deal with such a situation; Exception expressed the need to capture abnormal or procedures required for processing, a design or implementation problems; that is, it means that if run properly, the situation never happened.

Added:.. Asked such a question "? If a process reports a stack overflow run-time error, what's the most possible cause", to the four options a lack of memory of the Motorola 2005 interview; b write on an invalid memory space; c recursive function calling;. d array index out of boundary Java program at runtime may also encounter StackOverflowError, this is an unrecoverable error, only to re-modify the code, this is the answer to face questions. c. If you can not write a recursive rapid convergence, it is likely to trigger a stack overflow error, as follows:

 

package com.lovo;  
  
public class StackOverflowErrorTest {  
  
    public static void main(String[] args) {  
        main(null);  
    }  
} //何问起 hovertree.com

 

Therefore, when writing a program using recursive must bear in mind two things: 1 recursive formula; 2 convergence condition (when it is no longer recursive but looking back on the)...
 

 

46, try {} there is a return statement, then the code will finally followed after the try {} Lane will not be executed, when executed, return before or after?

A: Yes execution, execution before returning to the caller method. Java allows you to change the return value is a bad practice in finally, as if finally block exist, try the return statement will not immediately return to the caller, but the return value to call again after finally block to be finished next record the return of its value, and then finally if you modify the return value, this program would cause great distress, C # on the provision can not do such a thing from the grammar.

 

47, how the Java language exception handling, keyword: throws, throw, try, catch, finally, respectively, how to use?

A: Java exception handling through object-oriented approach, to classify the various anomalies, and provides a good interface. In Java, each anomaly is an object that is an instance of the class Throwable or subclass. When an exception occurs after a method throws an exception object that contains exception information, call the object's method can capture this exception handling. Java exception handling is achieved through five keywords: try, catch, throw, throws, and finally. Under normal circumstances is try to execute a program, if an exception occurs, the system will throw (throw) an exception, this time you can catch it by type (catch) it, or finally (finally) by the default processor to deal; try to specify a prevention program for all "abnormal" is; catch clause immediately after the try block, used to specify the type you want to capture the "anomaly" of; throw statement to explicitly throw a " abnormal "; throws used to mark a member function may throw a variety of" abnormal "; finally a piece of code to ensure that no matter what happens" abnormal "are implementation of a code; you can write a statement to try out a member function call, within this member function to write another statement to try to protect other codes. Whenever there is a try statement, "abnormal" framework to put on top of the stack, until all try statements are completed. If the next level try statement is not some kind of "exception" for processing, the stack will expand until it encounters have to deal with this "anomaly" try statements.

 

48. What is the runtime exception with a test abnormalities similarities and differences?

A: The abnormality shows an abnormal state program is running that may arise abnormality shows runtime exceptions usually a virtual machine that may be encountered, is a common run-time error, as long as the programming was no problem usually does not occur. Subjects with abnormal context running about, even if the programming is correct, because the problem may still be used and thrown. Java compiler must request a declaration throws subjects abnormalities may occur, but is not required when running throw uncaught exceptions must be declared. Abnormalities and inherited, is what object-oriented programming is often abused, God as "Effective Java" in the use of the exception gives the following guidelines:

 

  • Do not use exception handling for normal control flow (well-designed API should not be forced to its caller to control the flow of normal and abnormal use)

  • Use subjects abnormal situation can be restored, an exception to the use of run-time programming errors

  • Subject to avoid unnecessary use of the exception (through some state detecting means to prevent the occurrence of abnormality)

  • Priority use of standard exceptions

  • Each method throws an exception must have documentation

  • Abnormality atomicity

  • Do not ignore caught exception in the catch

 

 

49, list some of your common runtime exception?

answer:

ArithmeticException (arithmetic exception)

A ClassCastException (class cast exception)

IllegalArgumentException (illegal argument exception)

IndexOutOfBoundsException (The following table bounds exception)

A NullPointerException (null pointer exception)

SecurityException (Security Exception)

 

50, final, finally, finalize the difference?

A: final: modifier (keyword) There are three usage: if a class is declared final, meaning that it can not send another unexpected new sub-class that can not be inherited, so it is abstract and antonyms. The variable declared as final, can guarantee that they are not in use change, it is declared as final variables must be given in the initial statement, and in subsequent references can only read can not be modified. Declared final approach also only use, can not be overridden in a subclass. finally: usually placed behind the try ... catch structure will always execute the code block, which means that the program regardless of normal or abnormal execution occurs, the code does not close here as long as the JVM can execute, you can release the code written in finally external resources block. finalize: Object methods defined in the class, Java allowed to use finalize () method to remove the object to do the necessary clean-up work before going out from memory in the garbage collector. This method is called by the garbage collector when the object is destroyed, you can tidy up system resources by overriding the finalize () method or perform other cleanup work.

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/93715976