A Java interview questions Basics

1. a ".java" if the source file can contain multiple class (not an internal class), are there any restrictions?

  There can be multiple classes, but only one public class, and the public class name must be consistent with the file name.

In 2.Java there goto?

  java goto is a reserved word, but now there is no use in java.

3. talk about the difference and & & &?

  & && and can be used as the logical AND operator, and represents a logical (and), when the result of the expression operators on both sides are true, the result was the entire operation is true, otherwise, as long as one of them is false, then the result is false.

  && also has a function of short circuit, i.e. if the first expression is false, the second expression no longer.

  & Bitwise operators may also be used, when both sides of the expression is not a boolean operators &, & represents a bitwise AND operation, we typically use computing and & OxOf to an integer, to obtain the lowest bit of the integer 4 bits, e.g., as a result Ox31 & OxOf Ox01.

4. How out of the current multiple nested loops in Java?

  In Java, to multi-loop out, a label can be defined before the outer loop, with the break statement label and then use the code in the loop body layer, to the outer loop out. E.g,

  ok:

  for(int i=0;i<10;i++)    {

           for(int j=0;j<10;j++)              {

                    System.out.println(“i=” + i + “,j=” + j);

                    if(j == 5) break ok;

           }

}

 

 

  We usually do not use numbers in this way, but to make the results of the outer loop conditional expression may be controlled inner layer of the loop code, for example, to find a number in a two-dimensional array.

  

int arr[][] = {{1,2,3},{4,5,6,7},{9}};

  for(int i=0;i<arr.length && !found;i++)   {

           for(int j=0;j<arr[i].length;j++){

                    System.out.println(“i=” + i + “,j=” + j);

                    if(arr[i][j]  == 5) {

                             found = true;

                             break;

                    }
           }
}

 

 

 

5.switch statement can act on byte, whether acting on the long, whether acting on String?

  In the switch (condition), condition can only be an integer expression or enumeration constant (larger font), integer expression can be a primitive type int or Integer type of packaging because, byte, short, char can be implicitly converted int, and so these types, and these types of packaging types are also possible. Obviously, long and String types do not meet the syntax requirements of the switch, and can not be implicitly converted to int type, but a new feature in Java1.7, so the switch statement can act on String, provided that the Java version 1.7 1.7 and above.

6.short s1 = 1; s1 = s1 + 1; there anything wrong? short s1 = 1; s1 + = 1; there anything wrong?

  For the former, s1 + 1 will be automatically converted to int type, improved type of the expression, then re-assigned to the short type, the compiler will report requires mandatory installation of the wrong change.

  For the latter, a predetermined java + = operator is, the compiler will java his special processing, it is possible to correct translation.

7.char type variable can store a Chinese characters? why?

  char type variable is used to store Unicode character encoding, Unicode-encoded character set contains characters, so, of course, char type variable can store characters. However, some special characters not included in the Unicode coded character set, then, at this time char type variable can not store the special characters. It is noteworthy that, Unicode character encoding is two bytes, so a variable of type char also occupies two bytes.

8.2 * 8 equal to a few of the most efficient method of calculating?

  2 << 3, shift operator, a number of several left, multiplied by the equivalent of 2 times square.

9. Please design a ten billion calculators.

  We know the positive range int is 31 th power, the size of the representation of 20 million that is not an ordinary addition and subtraction to calculate 10 billion, we can design a class similar to a class java.math.BigInteger , two inner members of this class variables, a symbol representing the other binary value represented by a byte array, a constructor, a character string that contains the value of multi-bit symbols and converted to internal byte array provided arithmetic functions.

10. Use the keyword final modification of a variable, a reference can not be changed, or variable references can not be changed?

  When modifying a variable with the final keyword, refer to reference variables can not be changed, the content object reference variables referred to in can still change.

  For example, the following statement:

   final StringBuffer a = new StringBuffer ( " immutable");
  execute the following statement reports compile-time error:

  a = new StringBuffer ( "") ;
  however, the following statement may be performed by the compiler:

  a.append(" broken!");

11. "==" and equals method is what is the difference?

  "==" operator designed to compare two values ​​are equal, i.e. for comparing the variables corresponding to the values ​​stored in the memory are the same, to compare two basic types of data or two reference variables are equal, only "==" operator.

  equals method is used to compare two separate objects have the same contents.

  Generally the comparison string equals.

  It is worth noting that if a class does not own definition of the equals method, which by default equals method (inherited from the Object class) is to use the == operator, also comparing two variables point to whether the object is the same object, this time using equals and use == will get the same results, if the comparison of two separate objects are always returns false. If you want to write the kind of content can compare two instances of objects created from that class is the same, then you must override the equals method to you to decide what to write code that can be considered in the content of the two objects are the same.

12. The difference between static variables and instance variables?

  Differences in syntax definition: before the static keyword to add the static variable, while the former instance variables are not added.

  Differences in program runtime: instance variables belonging to the properties of an object, you must create an instance of an object, in which instance variables are allocated space in order to use this instance variable. Static variables do not belong to an instance of an object, but belong to the class, it is also known as class variables, as long as the program is loaded bytecode class, do not create any instance of an object, static variables will be assigned space, static variables can be used. In short, after the instance variables must create an object can be used by this object, you can use static variables directly reference the class name.

13. Can I issue a call to a non-static method from within a static method?

  Not. Because the non-static method is associated with the object to be together, because the non-static method is associated with the object to be together, after you must create an object, it can invoke a method on the object, and when the need to create a static method call object, you can directly call other words, when a static method is called, may not create any instance of an object, if you issue a call to a non-static method from a static method, the non-static method is associated with that object on it? This logic can not be established, the internal static method makes a call to a non-static methods.

The difference 14.Integer and int?

  int is one of eight types of raw data provided by the java. Providing Java class for each primitive type package, Integer int is to provide a package java classes. Int The default value is 0, and Integer default value is null, that is, Integer can distinguish the difference between an assignment and not a value of 0, int can not express without assignment, for example, to express not take the exam and exam 0 results for the difference, you can only use Integer. In the JSP development, the default Integer is null, it is used when displaying el expression text box is empty string, int default and default value 0, the display in the text box with the expression el time, the result is 0, it is, int type is not suitable as a web form data layer.

  In Hibernate, if the OID defined type Integer, Hibernate can have a null value if it is judged in accordance with whether an object is temporary, if the OID defined for int type, which also need to set hbm mapping file unsaved- attribute value is 0.

  Further, Integer provided a method of operating a plurality of integer-related, for example, a string to an integer, Integer also defines the maximum and minimum values ​​of the constants represent integers.

15.Math.round (11.5) is equal to how much? Math.round (-11.5) equals how much? 

  Math class provides three methods of rounding related: ceil, floor, round, meaning the role of these methods and their corresponding names in English, for example, the English meaning ceil is the ceiling, which it represents rounding up the results Math.ceil (11.3) is 12, the result Math.ceil (-11.3) is -11; English meaning floor of the floor, then the method indicates rounding down results Math.ceil (11.6) for 11 results Math.ceil (-11.6) -12; most difficult to master is round method, which means "rounding" algorithm Math.floor (x + 0.5), soon after the original figures to plus 0.5 under rounding, so that the result Math.round (11.5) is 12, the result Math.round (-11.5) -11.

16. Please tell scope public, private, protected, and when the difference is not written.

  

The four visible range of the scope shown in the following table.

 

Note: If you do not write any access modifier in the modified elements appear, the friendly (all classes in the same package can access).

 

 

The difference 17.Overload and Override. Overloaded methods can change the type of return value?

  Overload Overload represents the name of the same method for a plurality of the same class, but the parameter list of these methods vary (i.e., different number or type of parameters).

 

  重写Override表示子类中的方法可以与父类中的某个方法的名称和参数完全相同,通过子类创建的实例对象调用这个方法时,将调用子类中的定义方法,这相当于把父类中定义的那个完全相同的方法给覆盖了,这也是面向对象编程的多态性的一种表现。子类覆盖父类的方法时,只能比父类抛出更少的异常,或者是抛出父类抛出的异常的子异常,因为子类可以解决父类的一些问题,不能比父类有更多的问题。子类方法的访问权限只能比父类的更大,不能更小。如果父类的方法是private类型,那么,子类则不存在覆盖的限制,相当于子类中增加了一个全新的方法。

 

18.构造器Constructor是否可被Override?

  构造器Constructor不能被继承,因此不能重写Override,但可以被重载Overload。

 

19.接口是否可继承接口?抽象类是否可以实现(implement)接口?抽象类是否可继承具体类?抽象类中是否可以有静态的main方法?

  接口可以继承接口。抽象类可以实现接口,抽象类可以继承具体类,抽象类中可以有静态的main方法。

20.写clone()方法时,通常都有一行代码,是什么?

  clone 有缺省行为,super.clone();因为首先要把父类中的成员复制到位,然后才是复制自己的成员。

 

21.面向对象的特征有哪些方面?

  封装、继承、抽象、多态

22.java中实现多态的机制是什么?

  靠的是父类或接口定义的引用变量可以指向子类或具体实现类的实例对象,而程序调用的方法在运行期才动态绑定,就是引用所指向的具体实例对象的方法,也就是内存里正在运行的那个对象的方法,而不是引用变量的类型中定义的方法。

24.abstract class和interface有什么区别?

 

  1.抽象类可以有构造方法,接口中不能有构造方法。

  2.抽象类中可以有普通成员变量,接口中没有普通成员变量

  3.抽象类中可以包含非抽象的普通方法,接口中的所有方法必须都是抽象的,不能有非抽象的普通方法。

  4. 抽象类中的抽象方法的访问类型可以是public,protected和(默认类型,虽然

  eclipse下不报错,但应该也不行),但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。

  5. 抽象类中可以包含静态方法,接口中不能包含静态方法

  6. 抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。

  7. 一个类可以实现多个接口,但只能继承一个抽象类。

 

 

25.abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronize?

26.什么是内部类?Static Nested Class 和 Inner Class 的不同。

  内部类就是在一个类的内部定义的类,内部类中不能定义静态成员(静态成员不是对象的特性,只是为了找一个容身之处,所以需要放到一个类中而已。

27.内部类可以引用它的包含类的成员吗?有没有什么限制?

  

  完全可以。如果不是静态内部类,那没有什么限制!如果你把静态嵌套类当作内部类的一种特例,那在这种情况下不可以访问外部类的普通成员变量,而只能访问外部类中的静态成员,

28.Anonymous Inner Class(匿名内部类)是否可以extends(继承)其他类,是否可以implement(实现)interface(接口)?

  

  可以继承其他类或实现其他接口。不仅是可以,而是必须!

 

29.下面程序的输出结果是多少?

 

  

import java.util.Date;
public  class Test extends Date{
	public static void main(String[] args) {
		new Test().test();
	}
	public void test(){
		System.out.println(super.getClass().getName());
	}
}

 

 

  在test方法中,直接调用getClass().getName()方法,返回的是Test类名由于getClass()在Object类中定义成了final,子类不能覆盖该方法,所以,在test方法中调用getClass().getName()方法,其实就是在调用从父类继承的getClass()方法,等效于调用super.getClass().getName()方法,所以,super.getClass().getName()方法返回的也应该是Test。如果想得到父类的名称,应该用如下代码:getClass().getSuperClass().getName();

 

30.String是最基本的数据类型吗?

  不是,他是final类型的。

31.String s='Hello';s=s+"world!";这两行代码执行后,原始的String对象中的内容到底变了没?

  没有变,只是s所指的对象发生了改变。

32.是否可以继承String类?

  String类是final类故不可以继承。

  

 

Guess you like

Origin www.cnblogs.com/gory/p/12131576.html