Some Java questions for interviews

Source: Internet
Author: User

1. What is a Java virtual machine? Why is Java called a "platform-agnostic programming language"?

A virtual machine is a virtual machine process that can execute Java bytecode, compiling the source file into an executable bytecode file. The Java Virtual machine knows the instruction length and the corresponding features of the hardware platform's underlying

2,JDK and JRE

A, the JRE is called Java Run Environment, is the virtual machine that executes the program, and the JDK is Java Develop kit refers to the Java Development Kit, contains the JRE, compiler, and other tools. Can be developed, cheap, executed

In 3,java, what is a constructor function? What is a constructor overload? What is a copy constructor?

A constructor is called when a new object is created. Each class has a constructor function. The Java compiler creates a default constructor for this class in cases where the programmer does not provide a constructor for the class.

Constructor overloading and method overloading are similar in Java. You can create multiple constructors for a class. Each constructor must have its own unique argument list.

Java does not support copy constructors like in C + + because Java does not create a default copy constructor if you do not write the constructor yourself.

4, what is the difference between an interface and an abstract class?

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:

    • All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
    • A class can implement many interfaces, but only one abstract class is inherited
    • Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, and of course, in this case, the class must also be declared abstract.
    • An abstract class can implement an interface without providing an interface method implementation.
    • The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
    • The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
    • An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.

5, what is the difference between a process and a thread?

A process is an application that executes, and a thread is an execution sequence within a process. A process can have multiple threads. Threads are also called lightweight processes.

6, how to ensure that n threads can access n resources without causing deadlocks?

When using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and force threads to acquire locks in the order specified. Therefore, if all the threads lock and release the lock in the same order, there will be no deadlock.

7, why does the collection class not implement the cloneable and serializable interfaces?

The collection class interface specifies a set of objects called elements. Each specific implementation class of the collection class interface can optionally save and sort the elements in its own way. Some collection classes allow duplicate keys, some are not allowed.

8,

What is the difference between iterator and listiterator?

Their differences are listed below:

    • The iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
    • Iterator to a collection can only be forward traversal, listiterator can be either forward or back.
    • Listiterator implements the iterator interface and includes other functions such as adding elements, replacing elements, getting the index of the previous and subsequent elements, etc.

9, what is the difference between fast failure (fail-fast) and security failure (fail-safe)?

Iterator's security failure is based on a copy of the underlying collection, so it is not affected by modifications on the source collection. All of the collection classes below the Java.util package are fast failures, and all classes under the Java.util.concurrent package fail safely. A fast-failing iterator throws an concurrentmodificationexception exception, and a security-failed iterator never throws such an exception.

Where does the importance of the 10,hashcode () and Equals () methods manifest?

HashMap in Java uses the hashcode () and Equals () methods to determine the index of key-value pairs, which are used when the values are obtained by key. If these two methods are not implemented correctly, two different keys may have the same hash value, and therefore may be considered equal by the collection. Moreover, these two methods are also used to discover duplicate elements. So the realization of these two methods is very important to the accuracy and correctness of hashmap.

11, what is the difference between an array and a list (ArrayList)? When should I use array instead of ArrayList?

The different points of the array and ArrayList are listed below:

    • An array can contain primitive types and object types, and ArrayList can only contain object types.
    • The array size is fixed, and the size of the ArrayList is dynamically changing.
    • ArrayList provides more methods and features, such as AddAll (), RemoveAll (), iterator (), and so on.
    • For basic type data, the collection uses automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with fixed-size base data types.

12, how do you trade out an unordered array or an ordered array?

The greatest benefit of an ordered array is that the time complexity of the lookup is O (log n), and the unordered array is O (n). The disadvantage of an ordered array is that the time complexity of the insert operation is O (n), because elements with large values need to be moved backward to make the new element space. Instead, the insertion time complexity of an unordered array is constant O (1).

What are the differences between the 13,enumeration interface and the iterator interface?

The enumeration is twice times faster than iterator and consumes less memory at the same time. However, iterator is far more secure than enumeration because other threads are not able to modify objects in the collection that is being traversed by iterator. At the same time, iterator allows the caller to delete elements from the underlying collection, which is not possible for enumeration.

What's the difference between 14,hashset and TreeSet?

HashSet is implemented by a hash table, so its elements are unordered. The time complexity of the add (), remove (), contains () method is O (1).

On the other hand, TreeSet is implemented by a tree-shaped structure in which the elements are ordered. Therefore, the time complexity of the add (), remove (), contains () method is O (Logn).

What will 15,system.gc () and RUNTIME.GC () do?

These two methods are used to prompt the JVM for garbage collection. However, starting or delaying a garbage collection immediately depends on the JVM's

Some Java questions for interviews

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.