Java Written test questions (iv)

Source: Internet
Author: User
Tags garbage collection thread class

a daily sentence: I am neither pessimistic, nor optimistic, just open my eyes every morning to greet a new day, a person trying to live

1. How many objects does this statement create altogether: String s = "a" + "B" + "C" + "D";

For the following code:

String s1 = "a";String s2 = s1 + "b";String s3 = "a" + "b";System.out.println(s2 == "ab");System.out.println(s3 == "ab");

The first statement prints the result of false, the second statement prints a true result, which means that the Javac compiler can optimize the expression that adds the string constants directly, without having to wait until the run time to do the addition operation, but rather to remove the plus sign at compile time. Directly compile it into one of these constants connected results.
The first line of code in the title is optimized by the compiler at compile time, which is equivalent to a string that defines a "ABCD" directly, so the above code should only create a string object. Write the following two lines of code:

String s = "a"+"b"+"c"+"d";System.out.println(s == "abcd");

The final print result is true.

2. Final, finally, finalize the difference.
Final is used to declare properties, methods, and classes, respectively, to indicate that the property is immutable, that methods are not overridden, and that classes are not inheritable. Internal classes to access local variables, local variables must be defined as final types.
Finally is part of the exception-handling statement structure, which indicates that it is always executed.
Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the object being retracted, and can override this method to provide garbage collection of other resource recycles, such as closing the file. The JVM does not guarantee that this method is always called.
3. What are the similarities and differences between runtime exceptions and general exceptions?
An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error. The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.
4. What is the difference between error and exception?
Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations. Exception represents a design or implementation issue. In other words, it means that if the program can run normally, it never happens.
5. How does the Java language perform exception handling, and what does the keyword throws,throw,try,catch,finally mean? Can I throw an exception in a try block?
Java exception handling is implemented by the above 5 keywords, generally use try to execute a program, if an exception occurs, the system throws (throws) an exception, this time you can catch it by its type (catch) it, or finally (finally) is handled by the default processor. Use try to specify a block that prevents all "exceptions", and immediately following the try should include a catch clause to specify the type of exception you want to capture.
The throw statement is used to explicitly throw an exception.
Throws is used to indicate various exceptions that a member function might throw.
Finally, a piece of code that ensures that a piece of code is executed regardless of what exception occurs.
6. Are there several ways to implement threads in Java?
Two kinds: Inheriting the thread class and implementing the Runnable interface, respectively.
Modifies the synchronization method with the Synchronize keyword.
Oppose the use of Stop () because it is unsafe. The suspend () method is used to take place in the deadlock.
7. What is the difference between sleep () and wait ()?
Network answer: Sleep () is a thread-class method that causes this thread to pause execution for a specified time and take the execution opportunity to another thread, but the monitoring state remains and is automatically restored. Calling sleep () does not release the object lock. Wait () is a method of the object class, which calls the wait method on this subject to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.
8. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately?
If the data will be shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then the data is shared and must be accessed synchronously.
Asynchronous programming should be used when an application calls a method that takes a long time to execute on an object, and does not want the method to wait for the program to return, and in many cases it is often more efficient to adopt an asynchronous approach.
9. Are there several ways to implement multithreading? How are there several implementations of synchronization?
Multithreading is the implementation of two methods: inheriting the thread class and implementing the Runnable interface.
There are two ways to implement synchronization: Synchronize,wait and notify, respectively.
Wait (): causes a thread to wait and releases the lock of the object it holds.
Sleep (): causes a running thread to sleep and is a static method that calls this method to catch the interrupedexception exception.
Notify (): Wakes up a waiting thread, noting that when this method is called, it does not actually wake up a waiting state thread, but is determined by the JVM to wake up which thread, and not by priority.
Notifyall (): Wakes all waiting threads, noting that it is not a lock for all wake-up threads, but rather that they compete.
10. Start a thread using Run () or start ()?
Starting a thread is calling the start () method, making the thread-ready state, which can then be scheduled to run, and a thread must associate some specific execution code with the run () method that is the execution code associated with the thread.
11. When a thread enters an synchronized method of an object, can other threads enter another method of this object?
(1) Whether the Synchronized keyword was added before other methods, if not added, it can.
(2) If Wait () is called internally by this method, then other synchronized methods can be entered.
(3) If the Synchronized keyword is added to other methods and no Wait () is called internally, it cannot.
(4) If the other method is static, it uses the synchronization lock is the current class of bytecode, and non-static methods can not be synchronized, because the non-static method with this.
What is the difference between list and map?
List stores a collection of single-column data, map is a data set of two columns, such as storing keys and values, and the data stored in the list is sequential and repeatable; the data stored in the map is not sequential, its keys are not repeatable, but the values can be duplicated.
does list,set,map inherit from the collection interface?
List,set Yes, map is not.
What is the difference between HashMap and Hashtable?
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), Hashtable is based on the old dictionary class, HashMap is Java1.2 introduced the implementation of the map interface;
HASHMAP allows null (NULL) key value (key), because of thread safety, in the case of only one thread access, efficiency is higher than Hashtable;
HashMap allows NULL as a entry key or value, and Hashtable is not allowed;
HashMap is Hashtable contains method removed, replaced by Containsvalue and ContainsKey.
The method of Hashtable is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization.
What is the difference between collection and collections?
Collection is the ancestor interface of the collection class, and the interface that inherits from it has set and list;
Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, threading, and so on for various collections.
16. Two object values are the same (a.equals (b) = = true), but there are different hash code, this sentence is not?
17. Describe briefly how the JVM loads the class file?
The loading of classes in the JVM is implemented by ClassLoader and its subclasses, and Java ClassLoader is an important Java Runtime system component. It is responsible for locating and loading classes of class files at run time.
What is the difference between heap and stack?
Java's memory is divided into two categories, one is stack memory, and the other is heap memory. Stack memory is when a program enters a method, it allocates a private storage space for the method, which stores the local variables inside the method, and when the method ends, the stack allocated to the method is freed, and the variables in the stack are freed.
A heap is a stack of different memory that is typically used to store data that is not in the current method stack, for example: Creating an object with new is placed in the heap, so he will not disappear with the end of the method. The local variables in the method are placed in the heap instead of the stack, using the final decoration.
What is a GC? Why do you have a GC?
GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems, forgetting or wrong memory recycling can cause program or system instability or even crashes, The GC mechanism provided by Java can automatically monitor whether an object exceeds the scope to achieve the purpose of automatically reclaiming memory, and the Java language does not provide an explicit way to dispose of the allocated memory.
20. Write a singleton mode (Singleton)

//第一种 :饿汉模式public class SingleTon{    private SingleTon(){}    //实例化放在静态代码块里可提高程序的执行效率,但也可能占用更大的空间    private final static SingleTon instance = new SingleTon();    public static SingleTon getInstance(){        return instance;    }}//第二种:懒汉模式publi class SingleTon{    private SingleTon(){}    private static instance = null;    public static synchronized SingleTon getInstance(){        if(instance == null){            instance = new SingleTon();        }        return instance;    }}

Java Written test questions (iv)

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: 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.