Java interview questions compulsory 50 (with answers) 4

31, String s = new String ( "xyz"); created several String Object?

A: The two objects, a static memory area of ​​"xyz", with a new one is created in the object on the heap.

 

32, the interface is inheritable (extends) interfaces? Are abstract class can implement (implements) the interface? Abstract class is inheritable specific class (concrete class)?

Answer: The interface can be inherited interfaces. Abstract class can implement (implements) interface, a concrete class inherit the abstract class, but only if specific class must have explicit constructor.

 

33, a ".java" whether the source file can contain multiple classes (not internal)? What are the limits?

A: Yes, but a source file can only have a public class (public class) and the file name and class name must be fully consistent with the public class.

 

34, Anonymous Inner Class (anonymous inner classes) can inherit other classes? Can I implement an interface?

A: You can inherit from other classes or implement other interfaces, commonly used in this way Swing programming to implement event listeners and callbacks.

 

35, the inner class can refer to its members containing class (outside of class) do? Are there any restrictions?

A: An inner class object can access the founding members of its outer class object, including private members.

 

36, Java's final keyword in what usage?

 

A: (1) modifying classes: the class can not be inherited representation; (2) modification methods: Method representation can not be rewritten; (3) modification of variables: variables can only represent an assignment can not be modified after the (constant).

 

37, pointed out that the results of running the following programs:

 

1 class A{  
 2   
 3     static{  
 4         System.out.print("1");  
 5     }  
 6   
 7     public A(){  
 8         System.out.print("2");  
 9     }  
10 }  
11   
12 class B extends A{  
13   
14     static{  
15         System.out.print("a");  
16     }  
17   
18     public B(){  
19         System.out.print("b");  
20     }  
21 }  
22   //何问起 hovertree.com
23 public class Hello{  
24   
25     public static void main(String[] args){  
26         A ab = new B();  
27         ab = new B();  
28     }  
29   
30 }

 

A: Execution result: 1a2b2b. When you create an object constructor is calling sequence: initialize static member, and then call the parent class constructor, then initialize non-static member, and finally call itself constructor.

 

 

38, the data type conversion between:

1) How to convert the string primitive data types?

2) How to convert basic data types to strings?

answer:

1) call type data corresponding to the basic method parseXXX (String) or valueOf (String) to return a respective type of package base class;

2) a method in which basic data types and the empty string ( "") connected to (+) to obtain its corresponding string; Another method is to call the class String valueOf (...) method returns the corresponding character string

 

39, and how to reverse the replacement string?

A: Many methods can write your own implementations can also use methods of the String or StringBuffer / StringBuilder in. There are a common plane is achieved questions reversing recursive string, code as follows:

public static String reverse(String originStr) {  
        if(originStr == null || originStr.length() <= 1)   
            return originStr;  
        return reverse(originStr.substring(1)) + originStr.charAt(0);  
    }  //何问起 hovertree.com

40, how to GB2312 encoded string into ISO-8859-1 encoded string?

A: The code is as follows:

String s1 = "hello";

String s2 = newString(s1.getBytes("GB2312"), "ISO-8859-1");

 

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/93715844