2019 125 common pen java interview questions summary (b)

26, when to use assert. 
assertion (assertion) is a common debug mode in software development languages support this mechanism for many developers. In the implementation, assertion is a statement in the program, it checks a boolean expression, a program must ensure that the correct boolean expression evaluates to true; if the value is false, explain procedures already in a bad state , the system will give a warning or quit. 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.

27, GC What is? Why should there be GC? 
GC is a garbage collection means (Gabage Collection), memory handling is where programmers prone to problems, and forget or false memory recovery program will lead to instability or even system crashes, Java GC function provides automatic monitoring of the object exceeds the scope so as to achieve the purpose of automatic recovery of memory, Java language does not provide a method of operating the display release allocated memory.

28, short s1 = 1; s1 = s1 + 1; there is nothing wrong with short s1 = 1;? S1 + = 1; there anything wrong? 
Short s1 = 1; s1 = s1 + 1; (s1 + 1 operation result is int type, you need to type cast)
Short s1 = 1; s1 + = 1; (compile correctly)

29, Math.round (11.5) is equal to the number of Math.round (-11.5) equals how much?? 
 Math.round (11.5) = 12 is =
Math.round (-11.5) == -. 11
method returns the nearest round long integer parameter, the parameter is increased by seeking the floor 1/2.

30, S = String new new String ( "XYZ"); created several String Object? 
two

31, EJB comprising (the SessionBean, EntityBean) say their life cycle, and how to manage the affairs?
SessionBean: Stateless Session Bean's life cycle is determined by the container when the client makes a request to create an instance of a Bean, EJB containers do not have to create a new instance of the Bean for client calls, but now easily find a some examples are provided to the client. When a client first call a Stateful Session Bean, the container must be created immediately in the server a new Bean instance, and related to the client, the client calls after this when Stateful Session Bean method will call dispatch to the container Bean instance associated with this client.
EntityBean: Entity Beans can survive for a relatively long time, and the state is continuing. As long as the database data exist, Entity beans have been alive. Not the application process for the program or service. Even if the EJB container crashes, Entity beans also survived. Lifecycle Entity Beans Beans can be container or manage their own.
EJB technology through the following management practices: Object Management Group (OMG) object Practice Services (OTS), Sun  Microsystems's Transaction Service (JTS), Java Transaction API (JTA), the development group (X / Open) XA Interface.

32, application servers have those?
BEA WebLogic Server,IBM WebSphere Application Server,Oracle9i Application Server,jBoss,Tomcat

33、给我一个你最常见到的runtime exception。
ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IndexOutOfBoundsException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

34, the interface is inheritable interfaces? Are abstract class can implement (implements) the interface? Abstract class is inheritable entity class (concrete class)?
Interfaces can be inherited interfaces. Abstract class can implement (implements) interface abstract class is inheritable entity class, but only if the entity class must have explicit constructor.

35, List, Set, Map whether inherited from the Collection interface?
List, the Set Shi, Map not

36, what data connection pooling mechanism to say that?
The J2EE  server startup will establish a pool certain number of connections, the number has been maintained and number of pool connections thereto. Needs to connect client program, the pool driver returns a pool of unused connections and marked as busy. If no connection is idle, the driver on the new pool certain number of connections, the number of new connections have configuration parameters decision. When the call is completed using a connection pool, pool table driver this connection referred to as idle, other calls can use this connection.

37, abstract whether the method is also static, it also is a native, it is also synchronized?
Can not be

38, the array has no length () this method? String has no length () this method?
Arrays are not length () this method, length attributes. String with a length () this method.

)? How do they 39, Set in the element is not repeated, then the method used to distinguish whether or not repeat it? Is == or equals (the difference?
The Set, the elements are not repeated, then use the iterator () A method to distinguish whether or not repeated.

equals () is the interpretation of whether the two Set for equality. equals () == method and value at the decision point to the same object equals () is covered in the class, and the type of the content when two separate objects match it returns a true value. 

40, whether Constructor constructor can be override?
Constructor constructor can not be inherited and can not rewrite Overriding , but can be overridden Overloading.

41, can inherit String class?
String class is final class can not inherit it.

42, swtich on whether the role of the byte, whether acting on the long, whether acting on String?
Switch (expr1) in, expr1 is an integer expression. Therefore, the parameters passed to the switch and case statements should be int, short, char or byte. long, string can not act on the swtich.

43, try {} there is a return statement, then followed by code in finally after this try {} there will not be executed, when executed, the return before or after?
Is executed, executed before return.

44, the programming problem: 2 by multiplying 8 the most efficient method of calculating equal to a few? 
2 <<. 3   

45, the values of two identical objects (x.equals (y) == true) , but it may have different hash code , this sentence right?
No, have the same hash code.

46, when an object is passed as a parameter to a method, this method can change the properties of this object, and returns the result of changes in, then in the end is passed by reference or value transfer here? 
Is passed by value.

Java programming language only pass parameters value . When a method is passed to the object instance as a parameter, the value of the parameter is a reference to the object. Contents of the object may change in the method is called, but the object reference will never change. 47, when a thread enters a synchronized method of an object, other threads can access this object other methods? No, an object of a synchronized method can only be accessed by a thread. 48, the programming problem: to write out a Singleton. 





Singleton pattern major role is to ensure that a Java application, a Class of only one instance of existence.
General Singleton pattern usually has several in various forms:


The first form: (starving formula)

Define a class, its constructor is private, it has a private static class variables, examples, then, reference to it by obtaining a public getInstance method in the case of initialization, which in turn calls the method.


public class Singleton {
private Singleton(){}
    //在自己内部定义自己一个实例,是不是很奇怪?
    //注意这是private 只供内部调用
    private static Singleton instance = new Singleton();
    //这里提供了一个供外部访问本class的静态方法,可以直接访问 
    public static Singleton getInstance() {
    return instance; 
    } 
    }  
/*
 * 枚举类型:表示该类型的对象是有限的几个
 * 我们可以限定为一个,就成了单例
 */
public enum Singleton{
	INSTANCE
}
public class Singleton3 {
	public static final Singleton3 INSTANCE;
	private String info;
	
	static{
		try {
			Properties pro = new Properties();
			
			pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
			
			INSTANCE = new Singleton3(pro.getProperty("info"));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	private Singleton3(String info){
		this.info = info;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	@Override
	public String toString() {
		return "Singleton3 [info=" + info + "]";
	}
	
}

The second form :( lazy man)

public class Singleton { 
private static Singleton instance = null;
public static synchronized Singleton getInstance() {
//这个方法比上面有所改进,不用每次都进行生成对象,只是第一次 
//使用时生成实例,提高了效率!
if (instance==null)
instance=new Singleton();
return instance;   } 

上面的其实还是会有线程问题的产生,如果线程产生了睡眠的情况,那么的会产生两种的不同的实例对象。
/*
 * 在内部类被加载和初始化时,才创建INSTANCE实例对象
 * 静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始化的。
 * 因为是在内部类加载和初始化时,创建的,因此是线程安全的
 */
public class Singleton {
	private Singleton(){
		
	}
	private static class Inner{
		private static final Singleton INSTANCE = new Singleton();
	}
	
	public static Singleton getInstance(){
		return Inner.INSTANCE;
	}
}

 Other forms:
define a class, its constructor is private, all of the static method.
The first form is generally believed to be more secure some, in fact, the most secure form using the static inner classes

summary:

• If you are hungry man type, the simplest form of enumeration

• If you are the lazy type, static inner classes simplest form


49, Java interfaces and C ++ the same and different from the virtual class.
Because Java does not support multiple inheritance, there is a possibility of a class or object to use each method or property in a few classes or objects inside the existing single inheritance mechanism can not meet the requirements. Compared with the inheritance, interfaces have greater flexibility, because the interface does not contain any implementation code. When a class implements the interface after the implementation of the interface class to which all of the methods and properties, and the properties of the interface which is the default state following public static, default, all methods is public. A class can implement multiple interfaces.

50, simple principle and application of exception handling mechanisms in Java.
When the JAVA program violates the semantic rules of JAVA, JAVA virtual machine error will occur is expressed as an exception. Violation of semantic rules include two cases. One is the JAVA class libraries built semantic checks. For example, cross-border array subscript, will trigger IndexOutOfBoundsException ; access null will cause the object NullPointerException . In other cases, JAVA allows programmers to extend this semantic checks, programmers can create their own unusual and freedom to choose when and with throw an exception is thrown keywords. All exceptions are java.lang.Thowable subclass.

Welcome to add a personal micro-channel to explore.

Guess you like

Origin blog.csdn.net/weixin_41865602/article/details/92380312