Java Written test questions (Iii.)

Source: Internet
Author: User
Tags stringbuffer

One sentence a day: think, is the problem, do, is the answer.

    1. Can the constructor constructor be override?
      The constructor constructor cannot be inherited and therefore cannot override override, but can be overloaded with overload.
    2. Interface can inherit interfaces? is an abstract class achievable (implement) interface? Can abstract classes inherit concrete classes (concrete Class)? Can there be static class methods in an abstract class?
      Interfaces can inherit interfaces. An abstract class can implement an interface. Abstract classes can inherit concrete classes. There can be static class methods in an abstract class.

    3. There is usually a line of code when writing the Clone method, what is it?
      Clone has default behavior, Super.clone (); Because the first thing to do is to copy the members of the parent class into place, then copy their own members.

    4. What are the aspects of object-oriented features?
      (1) Package

Encapsulation is to ensure that the software components have a good modular foundation, the goal of the package is to achieve the software components of the "high cohesion, low coupling" to prevent program interdependence and the impact of the changes.

(2) Abstract

Abstraction is the identification of similarities and commonalities of things, and then the attribution of these things to a class that considers only the similarities and commonalities of these things, and ignores those aspects that are irrelevant to the current subject and goal, focusing on aspects related to the current goal.

(3) Inheritance

When a class is defined and implemented, it can be done on the basis of an existing class, with the content defined by the existing class as its own content, and by adding a number of new content, or modifying the original method to make it more suitable for a particular need, which is inheritance. Inheritance is the mechanism by which subclasses automatically share the parent data and methods, which is a relationship between classes, which improves the reusability and extensibility of the software.

(4) polymorphic

Polymorphism refers to the specific type of variables defined in the program and the method calls made by the reference variable are not deterministic during programming, but are determined during the program's run, that is, the instance object in which the reference variable will point to which class, and the method call that the reference variable emits is the method implemented in the class. Must be determined by the time the program is running.

    1. What is the mechanism for polymorphism in Java?
      A reference variable that is either a parent class or an interface definition can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound at run time, that is, the method that refers to the specific case object to which the variable is pointing, that is, the method of the object being executed in memory, not the one defined in the

    2. What is the difference between an abstract (abstract) class and a interface (interface)?
      A class that contains an abstract modifier is an abstract class, and an abstract class cannot create an instance object. A class that contains an abstract method must be positioned as an abstract class,abstract class in a method that is not necessarily an abstraction. Abstract methods defined in abstract classes must be implemented in specific (concrete) classes, so there can be no abstract construction methods or abstract static methods. If the subclass does not implement all of the abstract methods in the parent class, then the subclass must also be defined as an abstract class.

An interface (interface) can be said to be a special case of an abstract class, and all methods in an interface must be abstract. The method in the interface is defined by default to the public abstract type, and the member variable in the interface defaults to public static final.

The specific syntax differences are:

(1) Abstract classes can have a construction method, the interface cannot have a construction method.

(2) There can be ordinary member variables in an abstract class, and there are no ordinary member variables in the interface.

(3) Abstract classes can contain non-abstract ordinary methods, and all methods in an interface must be abstract.

(4) Abstract methods in an abstract class can have access types of public, protected, and default, but the abstract methods in an interface are only public types, and the default is public abstract type.

(5) A static method can be included in an abstract class, not in an interface.

(6) Abstract classes and interfaces can contain static member variables, the access type of static member variables in the abstract class can be arbitrary, but the variables in the interface can only be public static final type, default to this type.

(7) A class may implement multiple interfaces, but only one abstract class can be inherited.

Differences in Application:

Abstract classes play a role in code implementation and can be reused in code, and interfaces are more useful in system architecture design methods, primarily for defining communication contracts between modules.

    1. Whether the abstract method can be static at the same time, whether it is native at the same time, can it be synchronized?
      Abstract methods can not be static at the same time, because abstractions are implemented by subclasses, and static and subclasses do not matter.

The native method means that the method is implemented with 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 used with abstract. For example: FileOutputStream class implementation to deal with the hardware, the underlying implementation using operating system-related API implementation, such as in Windows with the C language implementation, so, view the JDK source code can be found FileOutputStream the definition of the Open method is as follows:

Private native void open (String name) throws FileNotFoundException;
With regard to the problem of synchronized and abstract, it has never been seen that synchronized should be useful in a specific way, and that the synchronization lock object used by synchronized synchronization on the method is this, The abstract method cannot determine what this refers to.

    1. What is an internal class? What is the difference between Static Nested class and Inner class?
      An inner class is a class defined inside a class that cannot define a static member, an inner class can directly access a member variable in an external class, and an inner class can be defined in the method of an external class, as follows:
public class Outer{    int out_x = 0;    public void method(){        Inner1 inner1 = new Inner1();        public class Inner2{//在方法体内定义的内部类            public method(){            out_x = 3;            }        }        Inner2 inner2 = new Inner2();    }    public Class Inner1{//在方法体外定义的内部类    }}

The access type of an inner class defined outside the method body can be 4 types such as public,protected, default, private, and so on, as if the type of access that defines member variables in the class, they determine whether the definition of this inner class is visible to other classes.

    1. Can an inner class reference a member of its containing class? Are there any restrictions?
      It's perfectly possible. If it is not a static inner class, there is no limit.

If you treat a static nested class as a special case of an inner class, you cannot access the normal member variables of the outer class, but only the static members in the outer class, such as the following code:

class Outer{    static int x;    static class Inner{        void test(){            System.out.println(x);        }    }}
    1. Can an anonymous inner class inherit another class, or can it implement an interface?
      You can inherit other classes and implement interfaces. Not only can, but must!

    2. The Super.getclass () method call.
      What are the output results of the following program?

Import Java.util.Date;
public class Test Extend date{
public static void Main (string[] SRGs) {
New test (). Test ();
}
public void Test () {
System.out.println (Super.getclass (). GetName ());
}
}

Answer: Test32. Those classes in the JDK cannot be inherited? Classes that cannot inherit are those that are decorated with the final keyword. The types that generally compare the basic type or prevent the extension class from inadvertently destroying the original method should be final, and System,string,stringbuffer in the JDK are the basic types. is a. String the most basic type of data? The basic data types are byte, short, int, long, float, double, Boolean, Char. The Java.lang.String class is final type, so it is not possible to inherit this class, cannot modify the class, in order to improve efficiency and save time, we should use the StringBuffer class. 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 the string class is designed as an immutable class, all its objects are immutable objects. In this code, s originally pointed to a string object, the content is ' Hello ', and then A + operation on S, then, S does not point to the original object, but pointed to another string object, the content is "Helloworld", the original object also exists in memory, Just s This reference variable no longer points to it. 35. Can I inherit the String class? No, the string class is the final class. string s = new String ("abc"); How many string Object have you created? What is the difference between the two? Two of objects. "ABC" corresponds to an object, which is placed in a string constant buffer, and the constant "ABC" is the one in the buffer, no matter how many times it occurs. A new string is created every time it is written. The difference between the PNS string and the StringBuffer. The Java platform provides two classes of string and stringbuffer that can store and manipulate strings, that is, character data that contains multiple characters. The String class represents the immutable strings of content. Instead, the StringBuffer class represents a variable-content string. In addition, string implements the Equals method, new string ("abc"). Equals (New String ("abc")) results in true, while StringBuffer does not implement the Equals method new StringBuffer ("abc"). Equals (New StringBuffer ("abc")) results in false. . StringThe difference between buffer and StringBuilder. StringBuffer and StringBuilder classes both represent content-modifiable string classes, StringBuilder are thread insecure, run efficiently, and if a string variable is defined within a method, this situation may only have one thread accessing it, There is no unsafe factor, then use StringBuilder. If you want to define member variables within a class, and the instance objects of this class are used in a multithreaded environment, it is best to use StringBuffer. 39. How do I convert a comma-delimited string into an array? Reference: (1) with regular expressions, the code is roughly: ' string[] result = Orgstr.split (","); ' (2) with StringTokenizer, code:

StringTokenizer Tokener = StringTokenizer (Orgstr, ",");

String[] result = new String[tokener.counttokens ()];

int i = 0;

while (Tokener.hasnext ()) {result[i++] = Tokener.nexttoken ();}

40. 数组有没有length()方法?String有没有length()方法?数组没有此方法,有length属性;String有length()方法。

Java Written test questions (Iii.)

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.