Complete Works of Java Interview Questions (4)

Complete Works of Java Interview Questions (4)

Baiyu IT haha

31. String s = new String("xyz"); How many string objects have been created?

Answer: Two objects, one is "xyz" in the static area, and the other is an object created on the heap with new.

32. Can the interface extend? Can abstract classes implement interfaces? Can abstract classes inherit concrete classes?

Answer: Interfaces can inherit interfaces and support multiple inheritance. Abstract classes can implement interfaces, and abstract classes can inherit concrete classes or abstract classes.

33. Can a ".java" source file contain multiple classes (not internal classes)? What are the restrictions?

Answer: Yes, but there can be at most one public class in a source file and the file name must be exactly the same as the class name of the public class.

34. Can Anonymous Inner Class inherit from other classes? Can the interface be implemented?

Answer: You can inherit other classes or implement other interfaces. This method is commonly used in Swing programming and Android development to implement event monitoring and callbacks.

35. Can an inner class refer to the members of its containing class (outer class)? Are there any restrictions?

Answer: An inner class object can access the members of the outer class object that created it, including private members.

36. What are the uses of the final keyword in Java?

Answer: (1) Modified class: indicates that the class cannot be inherited; (2) Modified method: indicates that the method cannot be rewritten; (3) Modified variable: indicates that the variable can only be assigned once and the value cannot be modified (constant).

37. Point out the results of the following program.


class A {    static {
        System.out.print("1");
    }
    public A() {
        System.out.print("2");
    }
}
class B extends A{
    static {
        System.out.print("a");
    }
    public B() {
        System.out.print("b");
    }
}
public class Hello {
    public static void main(String[] args) {
        A ab = new B();
        ab = new B();
    }
}

Answer: Implementation result: 1a2b2b. When creating an object, the calling sequence of the constructor is: first initialize the static members, then call the parent class constructor, then initialize the non-static members, and finally call the own constructor.

Hint: If you can't give the correct answer to this question, it means that the Java class loading mechanism of question 21 has not been fully understood, so hurry up and look again.

38. Conversion between data types:

-How to convert a string to a basic data type?
-How to convert basic data types to strings?
answer:

  • Call the method parseXXX(String) or valueOf(String) in the packaging class corresponding to the basic data type to return the corresponding basic type;
  • One method is to connect the basic data type with an empty string ("") (+) to get the corresponding string; the other method is to call the valueOf() method in the String class to return the corresponding string

    39. How to reverse and replace strings?

    Answer: There are many methods, you can write your own implementation or use the methods in String or StringBuffer/StringBuilder. A very common interview question is to use recursion to achieve string reversal. The code is as follows:


public static String reverse(String originStr) {
        if(originStr == null || originStr.length() <= 1) 
            return originStr;
        return reverse(originStr.substring(1)) + originStr.charAt(0);
    }

40. How to convert GB2312 encoded string to ISO-8859-1 encoded string?

Answer: The code is as follows:
String s1 = "Hello";
String s2 = new String(s1.getBytes("GB2312"), "ISO-8859-1");

Guess you like

Origin blog.51cto.com/15061944/2593692