The basic javase of the Java Programmer's face test paper

Source: Internet
Author: User
Tags instance method modifier modifiers

First, the foundation of Javase 1, javase Basic grammar

###### (1), a brief description of your understanding of JVM,JRE,JDK?
Jvm:java virtual machines. is a simulated computer, equivalent to a real computer (software + hardware).
JDK: Is the core of the entire java. Programmer's product. Includes Java's runtime environment, Java tools, Java's base Class library.
JRE: The running environment.
Extend the relationship between----JVM, JDK, and JRE: JDK includes jre,jre including JVM
###### (2), a ". Java" source file can include more than one class (non-intrinsic Class)? What are the restrictions?
A ". Java" source file can have multiple classes, but only one public class, and the class name of public must match the file name.
A ". Java" source file can have only non-public classes, but if there is only one non-public class, this class can be different from the file name.
###### (3), Java have goto?
Goto exists as a reserved word in Java and is now unused in Java.
###### (4), talk about the difference between & and &&?
Both 1.& and && can be used as logic with the operator,&& for short circuits with,& not shorted with. In addition & can be used as an integer bitwise operator.
Example 1: For an if (str! = null&&!str.equals (")) expression, when STR is null, the subsequent expression does not execute, so it does not appear nullpointerexception if && is changed to &, the NullPointerException exception is thrown.
The 2.&& also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated.
Example 2:if (x==33 & ++y>0) y will grow, If (x==33 && ++y>0) will not grow.
Note: This topic first said the similarities between the two, and the && and & special, and cite some classic examples.
###### (5), how do I jump out of the current multiple nesting loops in Java?

  1. Break + Label
  2. Use break directly
  3. Return of the method of use

    例如下面代码片段:
  4. Break + Label
    ok:for (int i = 0; i <; i++) {
    for (int j = 0; J <; J + +) {
    System.out.println ("i=" + i + ", j=" + j);
    if (j = = 5)
    Break OK;
    }
    }
  5. Use break directly
    int arr[][] ={{1,2,3},{4,5,6,7},{9}};
    Boolean found = false;
    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
    }
    }
    }
  6. Return of the method of use
    private static int test () {
    int count = 0;
    for (int i = 0; i < i++) {
    for (int j = 0; J <; J + +) {
    count++;
    System.out.println ("i=" + i + ", j=" + j);
    if (j = = 5) {
    return count;
    }
    }
    }
    return 0;
    }
    ###### (6), switch statement can function on a byte, can function on a long, can function on a string?
    The switch statement can act on a byte, short, char, int, String, enum, wrapper class object.
    Other basic data types and reference data types are not eligible for case.
    ###### (7), short S1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + = 1; what's wrong?
    for short S1 = 1; S1 = s1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result is of type int, and when assigned to the short type S1, the compiler reports an error that requires a cast type.
    for short S1 = 1; S1 + = 1; since + = is a Java-language-defined operator, the Java compiler will treat it specially, so it compiles correctly.
    ###### (8), char type variable can not be stored in a Chinese character? Why?
    A char can be stored as a Chinese character, because Char is used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters.
    Supplemental Note: Unicode encoding consumes two bytes, so variables of type char are also two bytes.
    ###### (9), using the most efficient method to calculate 2 times 8 and so on?
    2 << 3 Since a number is shifted to the left n bit, it is equivalent to multiplying by 2 of the N-square, then a number multiplied by 8 to move it to the left 3 bits, and the bit operation CPU directly supported, the most efficient, so, 2 times 8 and several of the most efficient method is 2 << 3.
    ###### (10), when a variable is decorated with the final keyword, is the reference variable immutable or does the referenced object not change?
    The reference variable cannot be changed, the contents of the object to which the reference variable is pointing can be changed, but the new object cannot be used again.

    例1:如下面代码,编译就会出错    final StringBuffer a=new StringBuffer("immutable");    a=new StringBuffer("");例2:如下面代码,编译正确    final StringBuffer a=new StringBuffer("immutable");    a.append(“123”);

    What is the difference between ###### (11), "= =" and the Equals method?
    Their difference is primarily in the reference data type:
    1.== is compared with memory addresses to compare objects on both sides of the same object;
    2.equals is a method, the default is the memory address comparison, after overriding, is mainly used to compare the two sides of the value of the object is the same, and the implementation of the Equals method is related;
    Note: = = can be null on both sides, but the reference to the left of equals cannot be empty, otherwise a nullpointerexception exception will occur.
    What is the difference between ###### (12), static variables, and instance variables?
    1. The difference in syntax definitions: Static variables are added before the variable, and the instance variables are not added.
    2. Differences in program operation:
    An instance variable is a property of an object, and an instance object must be created where the instance variable is allocated space before the instance variable can be used.
    Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used.
    In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name.
    ###### (13), is it possible to make a call to a non-static method from within a static method?
    No.
    Because a non-static method (instance method) is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly.
    That is, when a static method is called, there may not be any instance objects created, and if a call to a non-static method is emitted from a static method, which object does that non-static method relate to? This logic cannot be set up, so a static method is issued inside a call to a non-static method.
    What is the difference between ###### (14), Integer and int?
    1.int is one of the 8 raw data types provided by Java, meaning integer, which takes up 4 bytes.
    2.Integer is the wrapper class provided by Java for Int and is the reference data type.
    The default value for 3.int is 0, and the default value for integer is null, that is, the integer can distinguish between unassigned and a value of 0, and int cannot express an unassigned condition. For example, if you want to express the difference between not taking an exam and 0 of your exam results, you can use only integer.
    4. In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int is 0, so when displayed in a text box with an El expression, the result is 0, so int does not work as a type of form data for the Web tier.
    5. In hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined for the int type, It is also necessary to set its Unsaved-value property to 0 in the HBM mapping file.
    6. In addition, the integer provides multiple integer-related operations, such as converting a string to an integer, and a constant that represents the maximum and minimum values for an integer is also defined in the integer.
    ###### (15), Math.Round (11.5) equals how much? How much does Math.Round (-11.5) equal?
    The result of Math.Round (11.5) is 12,math.round (-11.5) as a result of-11.
    Parsing: The Math class provides three methods related to rounding: Ceil, floor, and round, which correspond to the meanings of their English names.
    For example: The English meaning of the ceil is the ceiling, the method represents the upward rounding, the result of Math.ceil (11.3) is the result of 12,math.ceil (-11.3) is the -11;floor of the English meaning is the floor, the method is to be rounded down, math.ceil ( 11.6) The result of 11,math.ceil (-11.6) is-12.
    ###### (16), what's wrong with the code below?

  7. if (Username.equals ("Zxx") {...}
    Username may be null, the null pointer error is reported, and instead "Zxx". Equals (username)
  8. int x = 1;
    return x==1?true:false; This is changed to return x==1;
    ###### (17), tell me the scope public,private,protected, and the difference when I don't write?
    The visible range of these four scopes is shown in the following table. (Note: If no access modifiers are written above the decorated element, Friendly/default is represented.) )

    The difference between ###### (18), overload and override. Can the overloaded method change the type of the return value?
    1.Overload is the meaning of overloading, override is the meaning of rewriting.
    2.Overload and override have in common, the method names of the two methods must be the same, if different, neither constitutes overload, nor does it constitute an override.
    3.Override must occur between parent and child classes, and overload may not be between parent and child classes.
    Features of 4.Override (rewrite):
    A) The parameter list is exactly the same: the number is the same, the same type, the same order;
    b) The return value of the subclass cannot be greater than the range of return values of the parent class;
    c) The exception that the subclass method throws cannot be larger than the exception that is thrown by the parent class method;
    d) modifier can only be public, protected, friendly, cannot be private;
    e) The parent-child class method cannot use the static adornment.
    4. Overloading occurs between the same class or parent-child class, in which the parameter list satisfies at least one condition in which the number differs, the type differs, and the order is different, and does not contain the static method between the parent and child classes.
    ###### (19), constructor constructor can be overridden (that is, the constructor can be rewritten)?
    No.
    Because the constructor constructor cannot be inherited, it cannot be overridden. But the constructor constructor can be overload (overloaded).
    ###### (20), interface can inherit interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete Class)? Can I have a static main method in an abstract class? Can an abstract class have an inner class? Can an interface have an internal class?
    interfaces can inherit interfaces;
    Abstract classes can implement (implements) interfaces;
    Abstract classes can inherit concrete classes;
    There can be static Main method in abstract class;
    Abstract classes can have internal classes;
    An interface can have an inner class, but it must be a static inner class, but not necessarily final.
    (Note: The only difference between an abstract class and a normal class is that you cannot create an instance object and allow an abstract method.) )
    ###### (21), write Clone () method, usually have a line of code (not must have), what is it?
    Super.clone ();
    Because clone has a default behavior, the first thing to do is to copy the members of the class into place, and then copy their own members.
    What are the aspects of ###### (22), object-oriented features?
  9. encapsulation, hiding internal implementation, exposing only public behavior;
  10. inheritance, improve the reusability of code;
  11. Polymorphism, reflecting the difference of similar objects in real life;
  12. Abstraction, extracting the common denominator of similar objects in the real world.
    ###### (23), what are the necessary conditions for satisfying polymorphism?
  13. to have inheritance;
  14. to have a rewrite;
  15. The parent class reference points to the subclass object;
    ###### (24), what is the mechanism for polymorphism in Java?
    By inheriting the parent class or implementing the interface.
    1. Inheritance refers to subclasses inheriting all properties, methods, and inner classes of the parent class. For a property, if the property name of the child class is the same as the property name of the parent class, the child class hides the properties of the parent class. The property is called according to the reference, and the method is called according to the object; There is only single inheritance in Java, and a subclass can inherit only one parent class directly.
    2. Implementing an interface means that a class can implement some or all of the methods in an interface, and can inherit all the attributes and inner classes in the interface. The properties in the interface are public static final types, and the methods are public types, and inner classes are public static types. An interface can inherit multiple interfaces, and an implementation class can implement multiple interfaces.
    What is the difference between ###### (25), abstract class and interface?
    Class that contains the abstract modifier is an abstract class, an instance object that the abstract class cannot create. A class containing an abstract method must be defined as an abstract Class,abstract class in which the method does not have to be abstracted. Abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.
    An interface (interface) can be described as a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.
    Here's a comparison of the syntax differences between the two:
    1. Abstract classes can have construction methods, and interfaces cannot have constructors.
    2. There can be ordinary member variables in an abstract class, there are no ordinary member variables in the interface
    3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
    4. The access type of an abstract method in an abstract class can be public,protected and the default type, but the abstract method in the interface is only of the public type, and the public abstract type is the default.
    5. Abstract classes can contain static methods, and interfaces cannot contain static methods
    6. Both abstract classes and interfaces can contain static member variables, and the access types of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public static final type is the default.
    7. A class can implement multiple interfaces, but can inherit only one abstract class.
      Let's talk about the difference between the two applications:
      1. The interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules;
      2. While abstract classes play a role in code implementation, you can implement code reuse.
      ###### (26), 25, abstract (method) can be static at the same time (static)? Can it be localized (native) at the same time? Can it be synchronous (synchronized) at the same time? Can it be final at the same time?
      1. Abstract methods cannot be static, because abstract methods are implemented by the quilt class, and static is not related to the subclass!
      2. The localization (native) method means that the method is implemented using another platform-dependent programming language, and there is no problem with the implementation of the quilt class, so it cannot be abstract and cannot be mixed with abstract.
      3. The problem of synchronization (synchronized) and abstraction (abstract) can not be shared, abstract methods can only exist in the abstract class or interface, it can not directly produce objects, and the default synchronization (synchronized) method to lock the current object, No objects are unlocked. In addition, synchronized cannot be inherited, and when subclasses inherit, additional modifiers are required.
      4. The final (final) method and abstraction (abstract) cannot be shared, because final is not overridden by the quilt class, and the abstract method must be overridden by a class of subclasses.
      ###### (27), what is an inner class? What is the difference between a static inner class and an inner class?
      An inner class is a class that is defined inside a class.
      The difference between a static inner class and an inner class:
    8. Static inner classes need to use static adornments, while ordinary inner classes cannot use static adornments;
    9. A static inner class can be defined only in the same sibling as the attribute, and the ordinary inner class may be defined anywhere except the position of the parameter;
    10. A static inner class must have a name, whereas an ordinary inner class can be anonymous;
    11. The static inner class does not have this reference, only the static members of the outer class can be accessed, while the ordinary inner class accesses all members of the outer class;
    12. When a static inner class accesses a function with the same name as an external class, the external class name. Method name is used, and the ordinary inner class needs to use the external class name. this. External method;
    13. Static inner classes can define static methods, while ordinary inner classes cannot define static methods, but can define static properties of simple data types, and cannot define static properties of reference types.
      ###### (28), an inner class can reference a member of its containing class? Are there any restrictions?
  16. If the inner class is a static inner class, only static members of the outer class can be called, and if there are duplicate members, they need to be accessed with an "external class name. Member name" and cannot invoke an object member of an external class.
  17. If the inner class is a non-static inner class, you can invoke all members of the outer class, or, if there are duplicate members, you need to use the external class name. this. member name.
    ###### (29), Anonymous inner class can inherit other classes? Is it possible to implement an interface?
    You can inherit other classes or implement other interfaces. Not only is it possible, but it is necessary!
    is the ###### (30), Super.getclass () method, and the This.getclass () method returning the same object?
    The returned class object is the same object and is the object of the subclass.
    ###### (31), string is the most basic data type?
    String is a reference data type.
    The basic data types are byte, int, char, long, float, double, Boolean, and short.
    ###### (32), String s = "Hello"; s = s + "world!"; After the execution of these two lines of code, did the content in the original string object change?
    No.
    Because string is designed as an immutable (immutable) class, all its objects are immutable objects.
    ###### (33), can I inherit the String class?
    The string class is the final class and cannot be inherited.
    ###### (34), String s = new string ("xyz"), how many string Object have you created? What's the difference between the two?
    Two of objects. One is "XYZ", which is the buffer object. The other is the new string object. The values of the two objects are the same, but not the same object.
    ###### (35), what are the new objects in several ways?
  18. Use the New keyword
  19. Using reflection, call the Newinstance method
  20. Using the Clone method
  21. Using serialization and deserialization
  22. Dynamic proxies (proxy classes and Cglib)
    What is the difference between ###### (36), String and StringBuffer?
    The same: All two classes implement the Charsequence interface.
    The difference:

    1. Types are different because they are not a class or an inheritance relationship and cannot be shared when making arguments
    2. The string object is an immutable object, and the value cannot be modified. While StringBuffer is a mutable object, it can modify the value.
    3. When stitching strings, string produces new objects, and StringBuffer only adds new characters, does not produce new objects, and is therefore highly efficient.
    4. String overrides the Equals method and the Hashcode method, and StringBuffer does not overwrite the Equals method and the Hashcode method, so the problem occurs when the StringBuffer object is stored in the Java collection class.
      What is the difference between ###### (37), StringBuffer and StringBuilder?
      Similarities: Both classes are variable-length string storage classes that implement the Charsequence interface.
      The difference:
    5. Types are different because they are not a class or an inheritance relationship and cannot be shared when parameters are being used.
    6. StringBuffer is a thread-safe class, and StringBuilder is a thread-unsafe class.
    7. StringBuffer performance is low and StringBuilder performance is high, if StringBuilder is used in local preference.
    8. Before the JDK 1.5, the string was added using the StringBuffer object, followed by the StringBuilder object after 1.5.
      ###### (38), how to convert a comma-separated string into an array?
      1. Using regular expressions, the core code fragment is: String [] result = Orgstr.split (",",-1);
      2. Use Stingtokenizer
      3. The dumbest way to use String.IndexOf ();

      The code snippet is as follows:
      2. Using Stingtokenizer:
      StringTokenizer Tokener = new StringTokenizer (S, ",");
      String[] result = new String[tokener.counttokens ()];
      Integer i = 0;
      while (Tokener.hasmoretokens ()) {
      result[i++] = Tokener.nexttoken ();
      }
      3. The dumbest way to use String.IndexOf ()
      int index =-1;
      int oldindex = 0;
      list<string> ss = new arraylist<string> ();
      while (index = S.indexof (', ', index + 1))! =-1) {
      Ss.add (s.substring (Oldindex, index));
      Oldindex = index + 1;
      }
      if (S.charat (S.length ()-1) = = ', ') {
      Ss.add ("");
      }
      string[] Array = Ss.toarray (new String[ss.size ()));
      System.out.println (arrays.tostring (array));
      ###### (39), array have length () This method? Does string have the length () method? Does the JS array have the length () method? Does the string JS have the length () method?
      The array does not have the length () method, which has the length property. String has the length () method. JS has only the length property, there is no length method.
      ###### (40), String s= "a" + "B" + "C" + "D" How many objects does this statement create altogether?
      Produces an object, the "ABCD".
      The analysis is as follows:
      String S1 = "a";
      String s2 = s1 + "B";
      String s3 = "a" + "B";
      SYSTEM.OUT.PRINTLN (s2 = = "AB"); False
      System.out.println (s3 = = "AB"); True
      String s = "a" + "B" + "C" + "D";
      System.out.println (s = = "ABCD"); True S is optimized for "ABCD"

###### (41), can you write a class, also called java.lang.String?
OK.
But in the application, you need to use your own class loader to load,
Otherwise, the class loader of the system is always just going to load the java.lang.String in the Rt.jar package.
###### (42), there is a return statement in try {}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return?
Perhaps your answer is before return, but to be more granular, it should be done in the middle of return.
Take a look at the running results of the following program code:
public class Test {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (Test ());
}

        static int test() {           int x = 1;           try {               return x;           }           finally {               ++x;           }        }    }    ---------执行结果 ---------    1     运行结果是1,为什么呢?主函数调用子函数并得到结果的过程,好比主函数准备一个空罐子,当子函数要返回结果时,先把结果放在罐子里,然后再将程序逻辑返回到主函数。所谓返回,就是子函数说,我不运行了,你主函数继续运行吧,这没什么结果可言,而结果已经被放进罐子里了。

###### (43), final, finally, finalize the difference?
1.final is used to declare properties, methods, and classes, respectively, that the property is immutable, that methods are not overwritten, and that classes are not inheritable.
Note: To access local variables, a local variable must be defined as a final type.
As in the following code:

            final int[] number = { 20 };            new Thread() {                @Override                public void run() {                    for (int k = 0; k < 20; k++) {                        number[0]++;                    }                }            }.start();            Thread.sleep(10);            System.out.println(number[0]);        2.finally是异常处理语句结构的一部分,表示总是执行,用来释放资源。        3.finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。JVM不保证此方法总被调用。

The basic javase of the Java Programmer's face test paper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.