您的位置:首页 > 职场人生

Java面试题汇编

2012-11-20 16:19 239 查看
1.设计模式,

2.智能指针,

3.静态函数,

The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

    ClassName.methodName(args)

    Note: You can also refer to static methods with an object reference like

            instanceName.methodName(args)

    but this is discouraged because it does not make it clear that they are class methods.

A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:

    public static int getNumberOfBicycles() {

        return numberOfBicycles;

    }

Not all combinations of instance and class variables and methods are allowed:

    * Instance methods can access instance variables and instance methods directly.

    * Instance methods can access class variables and class methods directly.

    * Class methods can access class variables and class methods directly.

    * Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

4.常见的异常,

1. java.lang.nullpointerexception

异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对象,

2. java.lang.classnotfoundexception

4. java.lang.arrayindexoutofboundsexception

异常的解释是"数组下标越界",

算术异常类:ArithmeticExecption

空指针异常类:NullPointerException

类型强制转换异常:ClassCastException

数组负下标异常:NegativeArrayException

数组下标越界异常:ArrayIndexOutOfBoundsException

文件已结束异常:EOFException

文件未找到异常:FileNotFoundException

操作数据库异常:SQLException

输入输出异常:IOException

方法未找到异常:NoSuchMethodException

java.lang.OutOfMemoryError

内存不足错误。当可用内存不足以让Java虚拟机分配给一个对象时抛出该错误。

java.lang.StackOverflowError

堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出时抛出该错误。

java.lang.UnknownError

未知错误。用于指示Java虚拟机发生了未知严重错误的情况。

java.lang.ArithmeticException

算术条件异常。譬如:整数除零等。

java.lang.ArrayIndexOutOfBoundsException

数组索引越界异常。当对数组的索引值为负数或大于等于数组大小时抛出。

java.lang.ClassCastException

类造型异常。假设有类A和B(A不是B的父类或子类),O是A的实例,那么当强制将O构造为类B的实例时抛出该异常。该异常经常被称为强制类型转换异常。

java.lang.ClassNotFoundException

找不到类异常。当应用试图根据字符串形式的类名构造类,而在遍历CLASSPAH之后找不到对应名称的class文件时,抛出该异常。

java.lang.CloneNotSupportedException

不支持克隆异常。当没有实现Cloneable接口或者不支持克隆方法时,调用其clone()方法则抛出该异常。

java.lang.IllegalStateException

违法的状态异常。当在Java环境和应用尚未处于某个方法的合法调用状态,而调用了该方法时,抛出该异常。

java.lang.IndexOutOfBoundsException

索引越界异常。当访问某个序列的索引值小于0或大于等于序列大小时,抛出该异常。

java.lang.NullPointerException

空指针异常。当应用试图在要求使用对象的地方使用了null时,抛出该异常。譬如:调用null对象的实例方法、访问null对象的属性、计算null对象的长度、使用throw语句抛出null等等。

java.lang.RuntimeException

运行时异常。是所有Java虚拟机正常操作期间可以被抛出的异常的父类。

java.lang.StringIndexOutOfBoundsException

字符串索引越界异常。当使用索引值访问某个字符串中的字符,而该索引值小于0或大于等于序列大小时,抛出该异常。

5.字符串编程,

6.线程,

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread
that computes primes larger than a stated value could be written as follows:

         class PrimeThread extends Thread {

             long minPrime;

             PrimeThread(long minPrime) {

                 this.minPrime = minPrime;

             }

     

             public void run() {

                 // compute primes larger than minPrime

                  . . .

             }

         }

     

The following code would then create a thread and start it running:

         PrimeThread p = new PrimeThread(143);

         p.start();

     

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example
in this other style looks like the following:

         class PrimeRun implements Runnable {

             long minPrime;

             PrimeRun(long minPrime) {

                 this.minPrime = minPrime;

             }

     

             public void run() {

                 // compute primes larger than minPrime

                  . . .

             }

         }

     

The following code would then create a thread and start it running:

         PrimeRun p = new PrimeRun(143);

         new Thread(p).start();

     

7.android几个基本组件。

Q:     

What is the difference between an Interface and an Abstract class?

A:     An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members
and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

.

      

TOP

Q:     

What is the purpose of garbage collection in Java, and when is it used?

A:     The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program
in which it is used.

      

TOP

Q:     

Describe synchronization in respect to multithreading.

A:     With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process
of using or updating same shared variable. This usually leads to significant errors.

      

TOP

Q:     

Explain different way of using thread?

A:     The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help.

      

TOP

Q:     

What are pass by reference and passby value?

A:     Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.

      

TOP

Q:     

What is HashMap and Map?

A:     Map is Interface and Hashmap is class that implements that.

      

TOP

Q:     

Difference between HashMap and HashTable?

A:     The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant
over time. HashMap is unsynchronized and Hashtable is synchronized.

      

TOP

Q:     

Difference between Vector and ArrayList?

A:     Vector is synchronized whereas arraylist is not.

      

TOP

Q:     

Difference between Swing and Awt?

A:     AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT.

      

TOP

Q:     

What is the difference between a constructor and a method?

A:     A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

      

TOP

Q:     

What is an Iterator?

A:     Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain
a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.

      

TOP

Q:     

State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

A:     public : Public class is visible in other packages, field is visible everywhere (class must be public too)

private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.

protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected
feature.

default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

      

TOP

Q:     

What is an abstract class?

A:     Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is
automatically abstract itself, and must be declared as such.

A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.

      

TOP

Q:     

What is static in java?

A:     Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on
the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a
static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.

      

TOP

Q:     

What is final?

A:     A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: