West Mail thoughtcoding Laboratory 2016 Breathing Test (Java section, for freshman sophomore, with reference answers)

Source: Internet
Author: User
Tags array length exception handling finally block sleep thread class throw exception stringbuffer

Thought coding laboratory 2016 breathing Pen test

Name: _ Class: ________ Contact: ________

Java section

Please take a look at the following procedure and answer the questions

public class Test1 {public
static void Main (string[] args) {
StringBuffer a = new StringBuffer ("thought");//Statement 1
  stringbuffer B = new StringBuffer ("coding"); Statement 2
operate (A, b);
System.out.println ("a =" + a);
System.out.println ("b =" + B);
}
static void Operate (StringBuffer x, StringBuffer y) {
x.append (y);//Statement 3
y = x;
}
}

1, the output of this program is what. Why.

Output:

A = thoughtcoding

b = Coding

Explanation: To investigate the use of Java parameter Transfer mechanism and StringBuffer.

The Operat method passes in a copy of the handle of the two StringBuffer type (or a copy of the pointer to the StringBuffer object in the heap memory), one is X, the other is Y, and executes "x.append (y);" After Y points to the StringBuffer object content "coding" is connected to the string content of the StringBuffer object that x refers to (the bottom is a value[) array, executes "y = x;" After, just modified the y point, and did not modify the Y point of the StringBuffer object string content, after the execution of the method, X, Y natural extinction.

2, the above procedures in the statements 1, 2, 3, respectively, modified to:

String a = "thought";
String b = new String ("coding");
x = x + y;

What is the output result. Why there is such a difference.

Output:

A = thought

b = Coding

Explanation: Examine the use of string and the difference from StringBuffer.

String uses private final char value[] to store the string, that is, after the string object is created, the string content stored in the object cannot be modified. In addition, Java has overloaded the "+" operator with string, and can connect two strings directly using "+".

In the operate method, the execution of "x = x + y" can be divided into two steps, first creating a new string object whose contents are concatenated with the string object content that x and Y point to, and the second to point X to the new string object. So the content of the original string that a points to does not change.

3. What is the difference between creating a string object by quotation marks and creating a string object with the New keyword?

Explanation: Examine the creation of string objects and the knowledge of the Java Run-time memory pool, heap memory.

Creating a String object in quotation marks is at compile time, and in a constant pool of the method area, a reference to the string object in the constant pool is returned directly if there is an object in the constant pool that has the same string content (as determined by equals). Using the New keyword creates a new object in heap memory, which returns a reference to an object in the heap in memory, regardless of whether there are objects of the same value in the constant pool.

4, "= =" and equals difference. To compare the string contents of two string objects, try overriding the Equals method of the object class.

Explanation: Examine the difference between "= =" and equals.

"= =" compares the address, and the Equals () method of the object class returns the result of the "= =" comparison.

public boolean equals (Object anobject)

{

If the same object

if (this = = AnObject)

{

return true;

}

If the argument passed in is an instance of the string class

if (anobject instanceof String)

{

String anotherstring = (string) anobject;

int n = count;//string length

if (n = = anotherstring.count)//If the length is equal to compare

{

Char v1[] = value;//to take characters from each position

Char v2[] = Anotherstring.value;

int i = offset;

int j = Anotherstring.offset;

while (n--! = 0)//For each location comparison

{

if (v1[i++]!= v2[j++])

return false;

}

return true;

}

}

return false;

}

Java collection classes are used very frequently in Java program Development, please answer related questions

1. Please briefly explain what is ArrayList and how to use it.

2, please briefly explain what is the hash algorithm. and hashmap how to realize data storage through hash algorithm.

3. Constructing Hashmap:hashmap map = new HashMap () by means of the parameterless construction method;

Now store 28 pairs of key-value data in the map, how to enlarge the map and how many times it needs to be enlarged.

4, please briefly explain the difference between HashMap and Hashtable.

5, (optional, plus) do you know the Java Concurrency Collection class.

Explanation: To investigate the use of common set ArrayList and the implementation of HashMap, the other collection classes such as HashSet (implemented by the HashMap of encapsulation value) can be extended.

See blog: Write a note series to JDK jdk1.6 container (1): ArrayList source Analysis



Please look at the following procedure and answer the questions

public class Test3 {public
static StringBuilder output= new StringBuilder ();
public static void foo (int i) {
try {
if (i = = 1)
{
output.append ("Try1");
throw new Exception ();
}
Output.append ("Try0");
}
catch (Exception e)
{
output.append ("catch");
return;
}
finally {
output.append ("finally");
}
Output.append ("End \ n");
}
public static void Main (String args[]) {
foo (0);  
Foo (1);
    SYSTEM.OUT.PRINTLN (output);
}


1. Do you understand the Java exception capture mechanism? Please write out the output of this code according to your understanding.

Output:

Try0 finally end

Try1 Catch finally

Explanation: To investigate the use of StringBuilder with Java exception capture mechanism, try-catch-finally execution order, return statement on the execution order, can extend the question, StringBuilder differs from StringBuffer and finally what circumstances do not execute.

A try is a segment of the program where an exception can occur;

Catch in order to write the corresponding exception handler method, when the exception is thrown, the runtime system in the stack from the current position in turn back to check the method, until the appropriate exception handling method is found, if not found, the execution

Finally or directly end the program run.

Finally: The statements in the finally block are executed whether or not the exception is captured or handled.

Note (important, interview frequently asked): When a return statement is encountered in a try block or catch block, the finally statement block is executed before the method returns.

Finally blocks are not executed in the following 4 special cases:

1 throws an exception in the finally statement block and is not processed.

2 System.exit () exits the program in the previous code.

3 the thread in which the program is located dies.

4 The CPU appears to be off the exception.

2. What run-time anomalies you have encountered during normal programming. Please provide an example.

Explanation: Investigate the operation of abnormal control and the accumulation of normal programming.

1 ' nullpointerexception: null pointer exception. When a nonexistent object is invoked or an object that is not instantiated or initialized is thrown, such as a property or method that attempts to manipulate an empty object (assigned to null).

(Instantiation: A popular understanding is to open up space for an object so that it can be invoked within a specified scope.) Note: User u; This is just an object declaration and is not instantiated or initialized.

Initialization: Assigning the base data Type field in the instantiated object to a default value or set value, assigning null to a non basic type, and initializing the static field only once. )

2 ' ArithmeticException: Arithmetic condition exception. The most common is that 0 is thrown when the divisor is made.

3 ' ClassNotFoundException: class did not find an exception. When a class is fetched by reflection Class.forName ("Class name"), an exception is thrown if it is not found.

4 ' arrayindexoutofboundsexception: array index out of bounds exception. Thrown when an attempt is made to manipulate an array with a negative index value or greater than or equal to the array size.

5 ' negativearraysizeexception: The array length is a negative value exception. is typically thrown when the initialization array is of a negative size.

6 ' arraystoreexception: Array type mismatch value exception. For example, when an object array is added to an integer and a string object, the type mismatch is thrown.

7 ' illegalargumentexception: illegal parameter exception. is thrown when the parameter value crosses over when the Java class library method is used.

The functions of single threaded routines are often very limited, and the development of multithreading and high concurrent environments has become a trend. Java provides very good multithreaded support, please answer the relevant questions

1. Java has several ways to create threads. What's the difference.

Explanation: Examine the mastery of the thread class.

The first is to inherit the thread class override method run () and not throw exceptions without return values
The second is to implement the Runnable interface implementation method run () as the target incoming thread class builder can not throw exception no return value for multithreading sharing the same resource situation
The third is to implement the Callable<t> interface, the method to be overridden in the interface is public <T> call () Note: This method can throw exceptions, and the first two cannot, and this method can have return value, used with Futuretask object wrapper, Incoming thread class builder as Target

Describe the life cycle of the thread, and which methods in Java can cause thread state changes.

Explanation: Investigate the mastery of thread state

2. The sleep () method of the thread class and the Wait () method of the object class can make the thread suspend execution, what's the difference?

Explanation: Examine the sleep () and wait () methods. Notice this problem Baidu is more easy to find, ask when see whether really understand, for example can let explain what is called not release object lock.

For the Sleep () method, we first need to know that the method belongs in the thread class. The wait () method, then, belongs to the object class.

The sleep () method causes the program to suspend execution of the specified time, giving up the CPU to the other thread, but his monitoring status is still maintained and will automatically resume running when the specified time is up.

During the call to the sleep () method, the thread does not release the object lock. (image is said to occupy the CPU to sleep)

When the Wait () method is invoked, the thread discards the object lock, enters the waiting lock pool for the object, and only after the Notify () method is invoked on this object does the thread enter the object lock pool preparation.

V. (optional, additional sub-item)

1. Do you know or use JSON?

2. What Java Open source framework do you know or use? (Oracle, Apache Open source framework also)

3, what you have developed Java or Android projects.


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.