Notes for several questions in Java

Source: Internet
Author: User

1. in Java, evaluate the length of the array char ch [] (number of elements): ch. length; Evaluate the length of string s: s. length ();

2. The arraycopy function is used to copy an array and is defined as follows:

Static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length)

Copy an array from the specified source array. Copy starts from the specified position and ends at the specified position of the target array.

Src: source array to be copied

SrcPos: the starting position of the array to be copied. The value starts from 0.

Dest: the destination array to be copied.

DestPos: the position of the target array to be placed. The value starts from 0.

Length: the length of the source array to be copied.

NB: note that the elements at the target location will be replaced.

Eg: int a = new int [] {, 5}; int B = new int [] {6, 7, 8, 9}

System. arraycopy (a, 2, B, 1, 3 );

After this sentence, B = {6, 3, 4, 5}

3. byte b1 = 10 + 1; // pass

Byte b2 = 2; byte b3 = 3;

Byte b4 = b2 + b3; // error, type incompatible

Byte b5 = (byte) (b2 + b3); // pass

Q: The right side of b1 is addition, and the right side of b4 is addition. Why is the first sentence passed and the second sentence reported an error?

A: during compilation, the compiler will calculate the data that can be directly computed first to improve efficiency. Therefore, the right side of b1 is regarded by the compiler as 11 rather than 10 + 1. When the compiler knows that int Type 11 is converted to byte type, it does not exceed the byte expression range, so no error is reported; for b4, because b2 and b3 are not a direct quantity, the compiler cannot directly identify the value of b2 + b3 during compilation, but can be determined at runtime, therefore, the compiler cannot determine whether the int type value (b2 + b3) will exceed the valid representation range of the byte type when being converted to the byte type. Because Java is a secure language, therefore, the compiler reports an error saying that the data cannot be converted to the byte type. After forcibly converting the right side to the byte type, the compiler error disappears, just like b5. The compiler knows that it is the programmer who asks the compiler to do this. Even if it exceeds the byte expression range, the compiler will ignore this problem because the compiler knows that this problem has been taken into consideration by the programmer.

4. while (...) {

If (...) {

Break; // This break does not exit if, but exits the while loop.

}

}

5. analyze the cause of the following code error:

Father. java:

Public class Father {

Private String name;

Public Father (String name ){

This. name = name;

}

}

Son. java

Public class Son extends Father {} // Error

A: Although the subclass does not write anything, the system will add a default constructor to the subclass. If this constructor does not have this keyword, the first sentence calls the non-argument constructor of the parent class. The parent class does not have a constructor. Therefore, an error is returned. The implicit super () default constructor is not defined. An implicit constructor must be defined.

Remember:The constructor of any subclass must call the constructor of the parent class.

6. char [] c = new char [] {'A', 'B', 'C', 'D', 'E '};

System. out. println (c); // outputs the element in c: ABCED

System. out. println ("char [] c:" + c); // output char [] c: [C + address (hashCode)

When printing a boolean array, if the parameter is directly set to this array, the output is [Z @ + address

Any value or string that you want to add is a string.

7. The String object in Java will never change and can only be copied

String s1 = "abc ";

String s2 = s1;

S1 + = "def ";

System. out. println (s1); // abcdef >>> s1 opens up a new storage space.

System. out. println (s2); // abc

8. String str1 = "abc"; String str2 = "abc"; str1 = str2 --> true

String str3 = "a" + "bc"; str1 = tr3 --> true

String str4 = str1 + "; str1 = str4 --> false

Integer i1 = 12; Integer i2 = 12; i1 = i2 --> true

Integer i3 = 128; Integer i4 = 128; i3 = i4 --> false

Note: The value of str1 "abc" is placed in the constant pool in the heap. When str2 is defined, the system finds that the literal "abc" already exists in the constant pool. Therefore, this value is directly returned to str2, therefore, str2 and str2 share the same literal. When str3 is created, the compiler will calculate the value that can be directly calculated. Therefore, during compilation, it is found that the value of str3 is "abc ", then return "abc" to str3. In this case, str1, str2, and str3 share the same variable and point to the same address. For str4, the value of str4 cannot be directly calculated during compilation, so you need to re-create an object, which is different from the address of str1, str2, str3.

For i3, it is an Integer object, but when its value is <= 127, there is only one object with the same Integer value. However, when the value is greater than or equal to 128, Integer i3 = 128 is equivalent to Integer i3 = new Integer (); that is, a new object is generated, the new Integer object will certainly be different from the i4.

9. Comparison of thread security and insecurity:

StringBuffer was provided in the early stage of Java (JDK1.0), with a slow speed and threadsSecurity

StringBuilder is provided after Java 5, with fast speed and threadInsecure

ArrayList and secure list, thread unsafe ----- Vector, thread safe

HashMap, insecure --- HashTable, secure

Use the following code to package a non-thread-safe list as a thread-safe

List list = new ArrayLsit ();

List = Collections. synchronizedList (list );

The following code encapsulates a non-thread-safe map as a thread-safe

Map map = new HashMap ();

Map = Collections. synchronizedMap (map );

10. The output result of the following code is:

String regex = "\\\\"; // four "\"

String str2 = "\\\\\\\"; // eight "\"

String str3 = str2.replaceAll (regex ,"*");

System. out. println (str3 );

Answer :****

Explanation: There are transfer characters in the string, so \\\\ represents two diagonal lines, that is, "\\". In a regular expression, one of the two diagonal lines is an escape character, the regular expression is regarded as a character "\". Str2 is actually four diagonal lines. Therefore, use a diagonal line as the replacement rule to replace the four diagonal lines and get the four heart signs.

11. public class EJB {

Class {

Private static int= 12;

Public void print (){

System.Out. Println (B);

}

}

}

The field B cannot be declared static; static fields can only be declared in static or top level types.

Static fields can only be declared as static or top-level fields.

12. BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("file. dat "));

Bos. write (1, 100 );

Assume that file. dat does not exist,After running this program segment, The number of bytes of file. dat is

A.0 B .1 C.2 D.3

Select ]. If the buffer zone is not refreshed, the written bytes are written in the buffer zone (that is, the memory ). If the displayed calling bos. flush (), or display the call bos. close () [bos. when you close the buffer, the flush method is automatically called to clear the content of the buffer zone.] The data in the buffer zone is written to the file, and the file has content.

13.Interface java. lang. Comparable <T>

This interface forcibly sorts the objects of each class. This sort is called the natural sorting of classes, and the compareTo method of classes is called its natural comparison method.

Objects (and Arrays) that implement this interface can be automatically sorted by Collections. sort (and Arrays. sort. The object implementing this interface can be used as a key or element in an ordered set in an ordered ing without specifying a comparator.

Interface java. util. Comparator <T>

A comparison function that forcibly sorts the collection objects. Comparator can be passed to the sort method (such as Collections. sort or Arrays. sort) to allow precise control over the sorting order. You can also use Comparator to control the order of certain data structures (such as ordered set or ordered ing), or provide sorting for object collections without natural order.

14.InputStream, OutputStream, Reader, and Writer are all abstract classes. Java. util. List extends Collection is an interface

15. The default character set for XML is the UTF-8.

16. In JavaScript: typof (undefined) = undefined; typeof (null) = object;

17. The DOM tree is parsed from top to bottom. If a script (not a function, but a script other than function) is encountered, it is also executed in sequence.

18. JS variables do not have block-level scopes. If a block in a function statement (for example, in a for loop) defines a variable, in this function, even if it is out of the, you can also use this variable.

19. When you use select to query rownum, only the value smaller than is used. If the condition is greater than, no result is returned.

20. What is output by the following statement?

Public class T15 {

Public static void main (String [] args ){

Int s = 99;

Switch (s ){

Case 0:

System. out. println ();

Default:

System. out. println ("error ");

Case 9:

System. out. println ("good ");

Case 2:

System. out. println ("best ");

}

}

}

Answer: error, good, best.

Cause: When a switch is encountered, the optional values are compared in sequence. This comparison compares all values, whether in the middle or the end. If all values except the default value can be matched, the code of the matched value is executed. If you do not exit (without a break statement), the code is executed in sequence. If no matching value is found, run the default statement. If it does not exit afterwards, the case statement after default will be executed in sequence until break or curly braces are encountered.

21. If a class implements two different interfaces and these two interfaces define the same method, the following principle is incorrect: (C)

A. If the two methods of each interface have the same method declaration, implement A method in the class and the method meets both interfaces;

B. If the two method parameters are different, two methods must be implemented to satisfy the interface definitions respectively;

C. If the two methods have the same parameters but different return types, two different methods must be defined to return different types respectively;

D. If the two methods have the same parameters but different return types, you cannot create a method that meets the two interfaces.

A: In C, two methods with the same return value cannot be defined in a class.

22. Which of the following statements about class inheritance are true? (C)

A. private method A is defined in the base class. methods with the same name cannot be defined in the derived class)

B. The protected method A is defined in the base class. You can overload method A in the derived class and modify the access level to private. (subclass cannot reduce the visibility of the parent class method, that is to say, it cannot be changed from protected to private)

C. The base class defines private variable a. The derived class can define the same variable a and change the access level to public;

D. The base class defines the protected static variable A. The same variable cannot be defined in the derived class. (Customizable)

23. Add a drop-down list item in two ways:

12.16.doc ument. getElementById ('s1'). options [I] =

New Option ('shanghai', 'sh ');

2). var op = document. createElement ('option ');

Op. value = 'sh ';

Op. innerHTML = 'shanghai ';

Document. getElementById ('s1'). appendChild (op );

24.One interface can inherit from multiple interfaces

25.An abstract class can inherit from a common class.

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.