Java interview questions collection (2)

Source: Internet
Author: User
Tags websphere application server
11.
& Is a bitwise operator. & Is a Boolean logical operator.
For example:
1 & 0 = 1;
I> 0 & I! = 3;

12. Differences between hashmap and hashtable.
All belong to the map interface class, which maps the unique key to a specific value.
The hashmap class is not classified or sorted. It allows a null key and multiple null values.
Hashtable is similar to hashmap, but does not allow null keys and null values. It is also slower than hashmap because it is synchronized.

13. Differences between collection and collections.
Collection is an interface under java. util. It is the parent interface of various collection structures.
Collections is a Java. util class that contains various static methods related to set operations.

14. When to use assert.
An assertion is a statement that contains a Boolean expression. When executing this statement, it is assumed that the expression is true.
If the expression is calculated as false, the system reports an assertionerror. It is used for debugging purposes:
Assert (a> 0); // throws an assertionerror if a <= 0
There are two types of assertions:
Assert expression1;
Assert expression1: expression2;
Expression1 should always generate a Boolean value.
Expression2 can be any expression that generates a value. This value is used to generate and display more debugging
The string message of the information.
Assertions are disabled by default. To enable assertions during compilation, use the source 1.4 flag:
Javac-source 1.4 test. Java
To enable assertions at run time, you can use-enableassertions or-ea flag.
To disable assertions during running, use the-da or-disableassertions flag.
To enable assertions in the system class, you can use the-ESA or-DSA tag. You can also enable or disable Assertion Based on the package.
You can place assertions on any location that is not expected to arrive normally. Assertions can be used to verify parameters passed to private methods. However, assertions should not be used to verify the parameters passed to the public method, because public methods must check their parameters whether or not assertions are enabled. However, you can use assertions to test the post-condition either in a public method or in a non-public method. In addition, assertions should not change the state of the program in any way.

15. What is GC? Why does GC exist? (Basic ).
GC is the garbage collector. Java Programmers don't have to worry about memory management, because the Garbage Collector automatically manages. To request garbage collection, you can call one of the following methods:
System. GC ()
Runtime. getruntime (). GC ()

16 string S = new string ("XYZ"); how many string objects are created?
Two objects, one being "XYZ" and the other being the referenced object s pointing to "XYZ.

17 what is math. Round (11.5? Math. Round (-11.5) and so on?
Math. Round (11.5) returns (long) 12, math. Round (-11.5) returns (long)-11;

18 short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?
Short S1 = 1; S1 = S1 + 1; error: S1 is short type, S1 + 1 is int type, cannot be converted to short type explicitly. It can be changed to S1 = (short) (S1 + 1 ). Short S1 = 1; S1 + = 1 is correct.

19 what is the difference between sleep () and wait? Thread favorites
The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads may be running and not scheduled to give up execution, unless (a) the thread "wakes up" has a higher priority (B) the running thread is blocked for other reasons.
When wait () is a thread interaction, if the thread sends a wait () call to a synchronization object X, the thread will pause the execution and the called object will enter the waiting state, wait until the wake-up or wait time is reached.

20 does Java have a goto?
Reserved Words in GOTO-Java are not currently used in Java.

21. Differences between static nested class and inner class.
Static nested class is an internal class declared as static. It can be instantiated without relying on external class instances. In general, internal classes must be instantiated before they can be instantiated.

22. What is the difference between dynamic include and static include in JSP?
Dynamic include is implemented using JSP: include action <JSP: Include page = "embedded ded. JSP "Flush =" true "/> always checks for changes in the contained files. It is suitable for inclusion of dynamic pages and can contain parameters.
Static include is implemented using the include pseudo code, and changes to the included files are not checked. This applies to static pages containing <% @ include file = "included.htm" %>

23. When to use assert.
Assertion is a common debugging method in software development. Many development languages support this mechanism. In implementation, assertion is a statement in the program. It checks a Boolean expression. A correct program must ensure that the value of this Boolean expression is true. If this value is false, if the program is in an incorrect state, the system will give a warning or exit. Generally, assertion is used to ensure the most basic and critical correctness of the program. The assertion check is usually enabled during development and testing. To improve performance, the assertion check is usually disabled after the software is released.

24. What is GC? Why does GC exist?
GC is the meaning of garbage collection (gabage collection). Memory Processing is a place where programmers are prone to problems. Forgetting or wrong memory collection can lead to instability or even crash of programs or systems, the GC function provided by Java can automatically monitor whether the object exceeded the scope to automatically recycle the memory. the Java language does not provide a display operation to release the allocated memory.

25. Short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?
Short S1 = 1; S1 = S1 + 1; (the S1 + 1 operation results in int type, which requires forced conversion)
Short S1 = 1; S1 + = 1; (it can be compiled correctly)

26. How much is math. Round (11.5? Math. Round (-11.5) and so on?
Math. Round (11.5) = 12
Math. Round (-11.5) =-11
The round method returns a long integer closest to the parameter. After the parameter is added to 1/2, the floor is obtained.

27. String S = new string ("XYZ"); how many string objects are created?
Two

28. Design four threads, two of which increase 1 to J each time, and the other two reduce 1 to J each time. Write the program.
The following programs use internal classes to implement threads, and do not consider the sequence when J is added or subtracted.
Public class threadtest1 {
Private Int J;
Public static void main (string ARGs []) {
Threadtest1 TT = new threadtest1 ();
INC Inc = TT. New Inc ();
Dec dec = TT. New Dec ();
For (INT I = 0; I <2; I ++ ){
Thread t = new thread (INC );
T. Start ();
T = new thread (DEC );
T. Start ();
}
}
Private synchronized void Inc (){
J ++;
System. Out. println (thread. currentthread (). getname () + "-Inc:" + J );
}
Private synchronized void Dec (){
J --;
System. Out. println (thread. currentthread (). getname () + "-Dec:" + J );
}
Class Inc implements runnable {
Public void run (){
For (INT I = 0; I <100; I ++ ){
INC ();
}
}
}
Class dec implements runnable {
Public void run (){
For (INT I = 0; I <100; I ++ ){
Dec ();
}
}
}
}

29. Is there a goto in Java?
Reserved Words in Java, which are not currently used in Java.

30. Do I use run () or start () to start a thread ()?
When a thread is started, the START () method is called to make the virtual processor represented by the thread in a running state, which means that it can be scheduled and executed by JVM. This does not mean that the thread will run immediately. The run () method can generate the exit sign to stop a thread.

31. EJB includes (sessionbean, entitybean) indicating their lifecycle and how to manage transactions?
Sessionbean: the lifecycle of stateless Session Bean is determined by the container. When the client sends a request to create a bean instance, the EJB container does not have to create a new bean instance for the client to call. Instead, it finds an existing instance and provides it to the client. When the client calls a stateful Session Bean for the first time, the container must immediately create a new bean instance on the server and associate it with the client, when this client calls the stateful Session Bean method later, the container will dispatch the call to the bean instance associated with this client.
Entitybean: entity beans can survive for a relatively long time, And the status is continuous. As long as the data in the database exists, entity beans will survive. It is not based on the application or service process. Even if the EJB container crashes, entity beans survive. Entity beans can be managed by containers or beans themselves.
EJB uses the following technical management practices: ots of Object Management Organization (OMG), Transaction Service (JTs) of Sun Microsystems, and Java transaction API (JTA ), the Xa interface of the Development Group (x/open.

32. What are the application servers?
BEA WebLogic Server,
IBM WebSphere Application Server,
Oracle9i Application Server,
JBoss, Tomcat

33. Give me the most common runtime exception.
Except, arraystoreexception, except, failed, cannotredoexception, cannotundoexception, classcastexception, cmcmexception, except, domexception, emptystackexception, except, failed, imagingopexception, failed, missingresourceexception, failed, failed, nullpointerexception, profiledataexception, providerexception, rasterformatexception, securityexception, systemexception, undeclaredthrowableexception, unmodifiablesetexception, unsupportedoperationexception

34. CAN interfaces be inherited? Can an abstract class implement the (implements) interface? Can an abstract class inherit a concrete class )?
Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces, and whether abstract classes can inherit object classes, provided that object classes must have clear constructors.

35. Does list, set, and map inherit from the collection interface?
List, set is, map is not

36. What is the working mechanism of the data connection pool?
When the J2EE server is started, a certain number of pool connections will be established, and a certain number of pool connections will be maintained. When the client program needs to connect, the pool driver returns an unused pool connection and logs its table as busy. If no idle connection exists, the pool driver creates a certain number of connections. The number of new connections is determined by the configuration parameters. When the pool connection call is completed, the pool driver records the connection table as idle, and other calls can use the connection.

37. can abstract methods be both static, native, and synchronized?
None

38. Does the array have the length () method? Does string have the length () method?
The array does not have the length () method. It has the Length attribute. String has the length () method.

39. The elements in the set cannot be repeated. How can we identify whether the elements are repeated or not? Is = or equals () used ()? What are their differences?
The elements in the set cannot be repeated, so the iterator () method is used to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal.
The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, in order to return the true value when the content and type of the two separated objects match.

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 makes memory management unnecessary for Java programmers 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.

58. Basic concepts of threads, basic states of threads, and relations between States
A thread refers to an execution unit that can execute program code during program execution. Each program has at least one thread, that is, the program itself.
Java threads have four states: running, ready, suspended, and ended.

59. Common JSP commands
<% @ Page Language = "Java" contentype = "text/html; charset = gb2312 "session =" true "buffer =" 64kb "autoflush =" true "isthreadsafe =" true "info =" text "errorpage =" error. JSP "iserrorpage =" true "iselignored =" true "pageencoding =" gb2312 "Import =" Java. SQL. * "%>
Iserrorpage (whether the exception object can be used), iselignored (whether to ignore the expression)
<% @ Include file = "FILENAME" %>
<% @ Taglib prefix = "C" uri = "http: // ......" %>

60. Under what circumstances do I call doget () and dopost ()?
The method attribute in the form label in the JSP page is doget () when get is called, and dopost () is called when post is called ().

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.