Article 3 Summary of common java interview pen questions

Source: Internet
Author: User
40. Can Constructor be overwritten?
Constructor cannot be inherited, so Overriding cannot be overwritten, but Overloading can be overloaded.

41. Can I inherit the String class?
The String class is a final class, so it cannot be inherited.

42. Does swtich work on byte, long, and String?
In switch (expr1), expr1 is an integer expression. Therefore, the parameters passed to the switch and case statements should be int, short, char, or byte. Long and string cannot apply to swtich.

43. If there is a return statement in try {}, will the code in finally {} following this try be executed? When will it be executed, before or after return?
Will be executed, before return.

44. Programming question: how many equals 2x8 in the most efficient way?
2 <3

45. The values of the two objects are the same (x. equals (y) = true), but different hash codes are available, right?
No. It has the same hash code.

46. After an object is passed as a parameter to a method, this method can change the attributes of this object and return the changed result, so is it a value transfer or a reference transfer?
Is the value transfer. Java programming language only supports value passing parameters. When an object instance is passed as a parameter to a method, the parameter value is a reference to the object. The object content can be changed in the called method, but the object reference will never change.

47. After a thread enters a synchronized method of an object, can other threads access other methods of this object?
No. One synchronized Method of an object can only be accessed by one thread.

48. Programming question: Write a Singleton.
The Singleton mode ensures that only one instance of a Class exists in a Java application.
The Singleton mode generally has several forms:
The first form: defines a class. Its constructor is private. It has a static private class variable. When the class is initialized, use a public getInstance method to obtain its reference, and then call the method.

Public class Singleton {
Private Singleton (){}
// Define your own instance internally. Isn't it strange?
// Note that this is private for internal calls only
Private static Singleton instance = new Singleton ();
// Here is a static method for external access to this class, which can be accessed directly.
Public static Singleton getInstance (){
Return instance;
}
}

Second form:

Public class Singleton {
Private static Singleton instance = null;
Public static synchronized Singleton getInstance (){
// This method is better than above. You don't need to generate objects every time. It's just the first time.
// Generate instances during use, improving efficiency!
If (instance = null)
Instance = new Singleton ();
Return instance ;}
}

Other forms:
Defines a class. Its constructor is private and all methods are static.
It is generally considered that the first form is more secure.

49. The similarities and differences between Java interfaces and C ++ virtual classes.
Because Java does not support multi-inheritance, it is possible that a class or object must use methods or attributes in several classes or objects respectively. The existing single Inheritance Mechanism cannot meet the requirements. Compared with inheritance, interfaces are more flexible because they do not have any implementation code. After a class implements an interface, this class implements all the methods and attributes in the interface, and the attributes in the interface are under the default state are public static, and all methods are public by default. A class can implement multiple interfaces.

50. Simple Principle and Application of the Exception Handling Mechanism in Java.
When a JAVA program violates the JAVA Semantic Rules, the JAVA Virtual Machine will indicate an error as an exception. There are two types of violation of Semantic Rules. One is the built-in semantic check of the JAVA class library. For example, if the array subscript is out of the range, IndexOutOfBoundsException is triggered. When a null object is accessed, NullPointerException is thrown. Another scenario is that JAVA allows programmers to extend this semantic check. programmers can create their own exceptions and choose when to use the throw keyword to cause exceptions. All exceptions are subclasses of java. lang. Thowable.

51. Advantages and principles of garbage collection. Two recovery mechanisms are also considered.
A notable feature of Java is the introduction of the garbage collection mechanism, which helps c ++ programmers solve the most troublesome memory management problems, it eliminates the need for Java programmers to consider memory management when writing programs. Because of the garbage collection mechanism, objects in Java do not have the "Scope" concept, and only objects can be referenced with "Scope ". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector is usually used as a separate low-level thread to clear and recycle objects that have been killed in the memory heap or are not used for a long time, programmers cannot call the Garbage Collector to recycle an object or all objects in real time. The collection mechanism involves generational replication, garbage collection, marking, and incremental garbage collection.

52. Tell us the thread synchronization method you know.
Wait (): puts a thread in the waiting state and releases the lock of the object it holds.
Sleep (): It is a static method that calls a running thread to capture InterruptedException exceptions.
Y (): Wake up a thread in the waiting state. Note that when this method is called, it cannot actually wake up a thread in the waiting state, instead, the JVM determines which thread to wake up, not by priority.
Allnotity (): Wake up all threads in the wait state. Note that it is not a lock for all wake-up threads but a lock for them to compete.

53. What are the collection classes you know? What are the main methods?
The most common collection classes are List and Map. The specific implementation of List includes ArrayList and Vector, which are variable-size lists and are suitable for building, storing, and operating the List of elements of any type of object. List is applicable to accessing elements by Numerical index.
Map provides a more general method for storing elements. The Map Collection class is used to store element pairs (called "keys" and "values"). Each key maps to a value.

54. How does JVM load class files?
In JVM, class loading is implemented by ClassLoader and its subclass. Java ClassLoader is an important Java runtime system component. It is responsible for finding and loading classes of class files at runtime.

55. Can a Chinese character be stored in a char variable? Why?
It can be defined as a Chinese character. Because java uses unicode encoding and a char occupies 16 bytes, it is okay to put a Chinese character.

56. What are several implementation methods for multithreading? What are the implementation methods of synchronization?
Multithreading can be implemented in two ways: Inheriting the Thread class and implementing the Runnable interface.
There are two types of synchronization implementation: synchronized, wait, and Y.

57. built-in JSP objects and methods.
Request indicates the HttpServletRequest object. It contains information about browser requests and provides several useful methods for obtaining cookie, header, and session data.
Response indicates the HttpServletResponse object, and provides several methods (such as cookies and header information) for setting the response to the browser)
The out object is an instance of javax. jsp. JspWriter. It provides several methods for sending output results to the browser.
PageContext indicates a javax. servlet. jsp. PageContext object. It is used to facilitate access to various namespaces and servlet-Related Object APIs, and encapsulates common servlet-related functions.
Session indicates the javax. servlet. http. HttpSession object of a request. Session can store user status information
Applicaton indicates a javax. servle. ServletContext object. This helps you find information about the servlet engine and servlet environment.
Config indicates a javax. servlet. ServletConfig object. This object is used to access the initialization parameters of the servlet instance.
Page indicates a servlet instance generated from the page.

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.