Datang Telecom JAVA Written Exam Questions Interview Questions - Answers

Reprint: http://my.oschina.net/chape/blog/164762

 

content[-]

1. Describe the usage of public, protected, private, final, native, synchronized , volatile, transient keywords in Java?  

?
1
2
3
4
5
类内部       package内        子类          其他
public            允许         允许             允许         允许
protected         允许         允许             允许         不允许
default           允许         允许             不允许       不允许
private           允许         不允许           不允许       不允许

Classes defined as final are not allowed to have subclasses, cannot be overridden, and field values ​​are not allowed to be modified

The native is related to the operating platform, its method is not implemented when it is defined, and the implementation of the method is implemented by an external library

For a static method, the JVM locks its class before executing it; for a non-static class method, it locks a specific object instance before executing it.

When a volatile asynchronous thread can access a field, it cannot save its private copy for . Volatile shields the necessary code optimization in the VM . Therefore, the efficiency is relatively low, so this keyword must be used only when necessary. 

transient, the field is not part of the object's persistent state ( serialization ), and the field should not be strung with the object

2. What is the difference between Abstract class and Interface?

1. An abstract class can have a constructor, but an interface cannot have a constructor.

2. There can be ordinary member variables in the abstract class , but there are no ordinary member variables in the interface

3. An abstract class can contain non-abstract ordinary methods, all methods in the interface must be abstract, and there cannot be non-abstract ordinary methods.

4.  The access type of the abstract method in the abstract class can be public, protected and default type , but the abstract method in the interface can only be of the public type, and the default is the public abstract type.

5. An abstract class can contain static methods, but an interface cannot contain static methods 

6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but variables defined in interfaces can only be of public static final type, and the default is public static final type. However , data members are generally not defined in the interface . 

7. A class can implement multiple interfaces , but can only inherit from one abstract class . 

3. What is the difference between Vector and ArrayList? What is the difference between HashTable and HashMap?

Vector and ArrayList are implemented by arrays. The difference between the two is that Vector is thread-safe, so its performance is not as good as ArrayList.

1. The method of HashTable is synchronized, and HashMap is not synchronized, so in multi-threaded situations, the difference between manually synchronizing HashMap is like Vector and ArrayList.
2.HashTable does not allow null values ​​(neither key nor value), HashMap allows null values ​​(both key and value).
3. HashTable has a contains(Object value), which has the same function as containsValue(Object value).
4.HashTable uses Enumeration , HashMap uses Iterator .
5. The default size of the hash array in HashTable is 11, and the way to increase is old*2+1. The default size of the hash array in HashMap is 16, and it must be an index of 2.
6. The use of hash values ​​is different. HashTable directly uses the hashCode of the object. The code is as follows:

?
1
2
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF ) % tab.length;

And HashMap recalculates the hash value, and uses and instead of modulo: 

4. What is the role of Iterator in Java collections?

     Iterator iterator, traversing the collection, there are two commonly used methods: .hasNext(); and .next(); the former returns boolean and the latter returns the result. The advantage of using Iterator is that you can use the same way to traverse the elements in the collection (List is an ordered collection), regardless of the internal implementation of the collection class (as long as it implements the java.lang.Iterable interface). If you use Iterator to traverse the elements in the collection, once you no longer use List to use TreeSet to organize data, the code for traversing elements does not need to be modified. If you use for to traverse, all algorithms for traversing this collection must be adjusted accordingly. .

5. What are the ways to create an object in Java and what are the differences?

1. Create an object with the new statement, which is the most common way to create an object.
2. Using reflection , call the newInstance() instance method of the java.lang.Class or java.lang.reflect.Constructor class.
3. Call the clone() method of the object.
4. Use the deserialization method to call the readObject() method of the java.io.ObjectInputStream object (object serialization and deserialization).

6. There are several representation methods for multithreading, what are they? There are several implementation methods for synchronization, what are they?

Thread implementation method:
(1) Inherit the Thread class, rewrite the run function, and
 start the thread: object.start() //Start the thread, and the run function runs

(2) Implement the Runnable interface and rewrite the run function

(3) Implement the Callable interface and rewrite the call function

        Callable is an interface similar to Runnable. The class that implements the Callable interface and the class that implements Runnable are tasks that can be executed by other threads.   There are several differences between Callable and Runnable:

        1. The method specified by Callable is call(), and the method specified by Runnable is run(). 
        2. The task of Callable can return a value after execution, and the task of Runnable is that it cannot return a value. 3. The
        method of call() can throw An exception is thrown, and the run() method cannot throw an exception. 

        4. Run the Callable task to get a Future object, Future represents the result of asynchronous calculation. It provides a method to check whether the calculation is completed, to wait for the completion of the calculation, and retrieve the result of the calculation. Through the Future object, you can understand the execution of the task, cancel the execution of the task, and obtain the result of the task execution.

Synchronization method:

1 wait method:
        This method belongs to the method of Object. The function of the wait method is to stop the execution of the thread that currently calls the part (code block) where the wait method is located, and releases the currently obtained lock of the code block where the wait method is called, and executes it in other threads. Reverts to a contended lock state when calling the notify or notifyAll methods (resumes execution once the lock is acquired).
        There are a few points to note when calling the wait method:
        first point: when wait is called, it must be in the code block that owns the lock (that is, synchronized ).         The second point: After the execution is resumed, the execution starts from the next statement of wait, so the wait method should always be called in the while loop, so as to avoid the situation where the conditions for continuing execution after the execution are resumed are not met but the execution continues.         The third point: If there is time in the wait method parameter, in addition to notify and notifyAll being called to activate the thread in the wait state (waiting state) to enter the lock competition, after interrupting it or the parameter time in other threads, the thread also will be activated into a race condition.         The fourth point: the thread on which the wait method is called must obtain the lock that was released when the wait method was executed before it can resume execution. 2 notify method and notifyAll method:         The notify method notifies that the wait method is called, but a thread that has not been activated enters the thread scheduling queue (that is, enters the lock competition). Note that it is not executed immediately. And which thread specifically is not guaranteed.





另外一点就是被唤醒的这个线程一定是在等待wait所释放的锁。
        notifyAll方法则唤醒所有调用了wait方法,尚未激活的进程进入竞争队列。

3 synchronized关键字:
        第一点:synchronized用来标识一个普通方法时,表示一个线程要执行该方法,必须取得该方法所在的对象的锁,this
        第二点:synchronized用来标识一个静态方法时,表示一个线程要执行该方法,必须获得该方法所在的类的类锁,class
        第三点:synchronized修饰一个代码块。类似这样:synchronized(obj) { //code.... }。表示一个线程要执行该代码块,必须获得obj的锁。这样做的目的是减小锁的粒度,保证当不同块所需的锁不冲突时不用对整个对象加锁。利用零长度的byte数组对象做obj非常经济。

7.    描述一下Java中的异常机制,什么是Checked Exception, Unchecked Exception?

     

      当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就会将发生的错误表示为一个异常。违反语义规则包括2种情况。一种是JAVA类库内置的语义检查。例如数组下标越界,会引发IndexOutOfBoundsException;访问null的对象时会引发NullPointerException。另一种情况就是JAVA允许程序员扩展这种语义检查,程序员可以创建自己的异常,并自由选择在何时用throw关键字引发异常。所有的异常都是java.lang.Thowable的子类。

1、任何的异常都是Throwable类,并且在它之下包含两个字类Error和Exception。RuntimeException是Exception的子类。
2、 除了Error与RuntimeException,其他剩下的异常都是你需要关心的,而这些异常类统称为Checked Exception,至于Error与RuntimeException则被统称为Unchecked Exception
3、Error:Error仅在当在Java虚拟机中发生动态连接失败或其它的定位失败的时候,Java虚拟机抛出一个Error对象。
4、Checked exception:这类异常都是Exception的子类 。异常的向上抛出机制进行处理,如果子类可能产生A异常,那么在父类中也必须throws A异常。可能导致的问题:代码效率低,耦合度过高。(SQLException)
5、Unchecked exception:  这类异常都是RuntimeException的子类,虽然RuntimeException同样也是Exception的子类,但是它们是特殊的,它 们不能通过client code来试图解决,所以称为Unchecked exception。(NullPointerException)
6、当程序执行过程中,遇到uncheck exception,则程序中止,不再执行之后的代码。

8.    描述一下Java ClassLoader 或者 J2EE ClassLoader的工作原理?

ClassLoader的工作原理 

      每个运行中的线程都有一个成员contextClassLoader,用来在运行时动态地载入其它类系统默认的contextClassLoader是 systemClassLoader,所以一般而言java程序在执行时可以使用JVM自带的类、$JAVA_HOME/jre/lib/ext/中的类 和$CLASSPATH/中的类可以使用Thread.currentThread().setContextClassLoader(...);更改当前线程的contextClassLoader,来改变其载入类的行为 

ClassLoader被组织成树形,一般的工作原理是: 
1) 线程需要用到某个类,于是contextClassLoader被请求来载入该类 
2) contextClassLoader请求它的父ClassLoader来完成该载入请求 
3) 如果父ClassLoader无法载入类,则contextClassLoader试图自己来载入 

Java中一共有四个类加载器,之所以叫类加载器,是程序要用到某个类的时候,要用类加载器载入内存。 
    这四个类加载器分别为:Bootstrap ClassLoader、Extension ClassLoader、AppClassLoader 
和URLClassLoader,他们的作用其实从名字就可以大概推测出来了。其中AppClassLoader在很多地方被叫做System ClassLoader 

9.    MVC的各个部分都有那些技术来实现?如何实现?

10. Describe how Struts works?

11. Which built-in objects are included in JSP?

12. What are the two jumping methods in JSP? What is the difference?

13. Describe the usage of taglib in JSP?

14. How to check numbers in Javascript?

15. What is included in EJB2.0? What are the functions?

16. Which components does a SessionBean contain? What is the function of each component?

17. Describe the role of the EJB deployment file in an EJB application and the deployment file of the App Server that you are familiar with?

18. List the design patterns you know (including EJB, J2EE design patterns), and the occasions where they are applied?

19. What are the main methods of parsing XML? What is the difference between them?

20. Do you know about Open Source projects? If so, please describe a few and their functions?

21. Which JAVA books have you read, can you list them?

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327034510&siteId=291194637