115-channel Java classic face test (highest-level, most complete) __java

Source: Internet
Author: User
Tags serialization thread class

115-Channel Java Classic face Test (with the highest face rate, the most complete)



Java is a computer programming language that supports concurrency, class, and object-oriented. The benefits of object-oriented software development are listed below:

Code development is modular and easier to maintain and modify.

Code reuse.

Enhance the reliability and flexibility of your code.

Increase the comprehensible nature of the code.

Object-oriented programming has many important features, such as encapsulation, inheritance, polymorphism, and abstraction. In the following sections we will analyze these features one by one.

Encapsulation

Encapsulation provides an object with the ability to hide internal attributes and behavior. Object provides methods that can be accessed by other objects to change its internal data. In Java, there are 3 modifiers: public,private and protected. Each modifier gives different access rights to the other objects located in the same package or under different packages.

Some of the benefits of using encapsulation are listed below:

Protects the state within an object by hiding its properties.

Increases the availability and maintainability of your code, because the behavior of an object can be changed individually or by extension.

Prevents undesirable interaction between objects to improve modularity.

Refer to this document for more details and examples of encapsulation.

polymorphic

Polymorphism is the ability of a programming language to display the same interface for different underlying data types. An operation on a polymorphic type can be applied to other types of values.

inherited

Inheritance provides an object with the ability to get fields and methods from the base class. Inheritance provides a reusable line of code, or you can add new attributes to an existing class without modifying the class.

Abstraction

Abstraction is the process of separating ideas from specific instances, so you create classes based on their capabilities rather than implementation details. Java supports the creation of an abstract class that has only a leaky interface and does not contain a method implementation. The main purpose of this abstract technique is to separate the behavior of the class from the implementation details.

different points of abstraction and encapsulation

Abstraction and encapsulation are complementary concepts. On the one hand, abstract attention to the behavior of the object. On the other hand, encapsulates the details of the object's behavior. Encapsulation is generally done by hiding the internal state information of the object, so the package can be viewed as a strategy for providing abstraction.

Common Java Problems

1. What is a Java virtual machine. Why Java is called "Platform-independent programming language".

A Java Virtual machine is a virtual machine process that can perform Java bytecode. Java source files are compiled into bytecode files that can be executed by the Java virtual machine.

Java is designed to allow applications to run on any platform without requiring programmers to rewrite or recompile individually for each platform. The Java Virtual machine makes this possible because it knows the instruction length and other features of the underlying hardware platform.

What is the difference between 2.JDK and JRE?

The Java Runtime Environment (JRE) is the Java virtual machine that will execute the Java program. It also contains the browser Plug-ins needed to execute the applet. The Java Development Kit (JDK) is a complete Java software development package that contains JRE, compilers, and other tools (such as the Javadoc,java debugger) that allow developers to develop, compile, and execute Java applications.

3. What does the "static" keyword mean? Whether a private or static method can be overridden in Java (override).

The "static" keyword indicates that a member variable or a member method can be accessed without an instance variable of the class to which it belongs.

The static method in Java cannot be overridden because method overrides are dynamically bound based on Run-time, and the static method is statically bound at compile time. The static method is not relevant to any instance of the class, so it is not conceptually applicable.

4. Whether a non-static variable can be accessed in a static environment.

A static variable is a class in Java and has the same value in all instances. The static variable is initialized when the class is entered by Java Virtual aircraft. If your code tries to access a non static variable without an instance, the compiler will complain because the variables have not yet been created and are not associated with any instances.

What data types are supported by 5.Java. What is an automatic disassembly box.

The basic data types in the Java language Support 8 are:

Byte

Short

Int

Long

Float

Double

Boolean

Char

Automatic boxing is a transformation of the Java compiler between the base data type and the corresponding object wrapper type. For example: Convert int into integer,double into double, and so on. The opposite is automatic unpacking.

What does the method override (overriding) and method overload (overloading) in 6.Java mean.

Method overloads in Java occur in the same class with two or more methods with the same method name but with different parameters. In contrast, method overrides are a way of saying that subclasses redefine the parent class. Method overrides must have the same method name, parameter list, and return type. The overridden person may not restrict access to the methods it overrides.

7.Java, what is a constructor. What is a constructor overload. What is a copy constructor.

When a new object is created, the constructor is invoked. Each class has a constructor. The Java compiler creates a default constructor for this class when the programmer does not provide a constructor for the class.

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

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

8.Java supports multiple inheritance.

Not supported, Java does not support multiple inheritance. Each class can inherit only one class, but can implement multiple interfaces.

9. What is the difference between an interface and an abstract class?

Java provides and supports the creation of abstract classes and interfaces. Their implementation has something in common, and the difference is:

All methods in an interface are implicitly abstract. Abstract classes, in turn, can contain both abstract and non-abstract methods.

A class can implement many interfaces, but can inherit only one abstract class

Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class may not implement all the methods declared by an abstract class, 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.

Interfaces are absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be invoked if it contains the main method.

You can also refer to the difference between an abstract class and an interface in JDK8

10. What is value passing and reference passing.

object is passed by value, meaning that a copy of the object is passed. Therefore, even changing the copy of the object does not affect the value of the source object.

The object is passed by reference, which means that it is not a real object, but a reference to the object. As a result, the external changes to the referenced object are reflected on all objects.

Java Threads

11. What is the difference between process and thread?

A process is an executing application, and a thread is a sequence of execution within a process. A process can have more than one thread. Threads are also called lightweight processes.

12. There are several different ways to create threads. Which one do you like? Why.

There are three ways to create threads:

Inherit Thread class

Implement Runnable interface

Applications can use the executor framework to create thread pools

Implementing the Runnable interface is a more popular approach because it does not need to inherit the thread class. In the case where other objects have been inherited in the application design, this requires multiple inheritance (and Java does not support multiple inheritance) and can only implement interfaces. At the same time, the thread pool is very efficient and easy to implement and use.

13. A general explanation of the several available states of the thread.

During execution, the thread can be in the following states:

Ready (Runnable): Threads are ready to run, not necessarily immediately.

In-run (Running): The process is executing the thread's code.

In Wait (waiting): The thread is in a blocked state, waiting for the external processing to end.

Sleep (sleeping): Thread is forced to sleep.

I/O blocking (Blocked on I/O): Waiting for I/O operation to complete.

Synchronous blocking (Blocked on synchronization): Waiting to acquire lock.

Death (Dead): Thread completes execution.

14. What is the difference between the sync method and the synchronized code block?

In the Java language, each object has a lock. Threads can use the Synchronized keyword to get a lock on an object. The Synchronized keyword can be applied at the method level (coarse-grained locks) or at the code block level (fine-grained locks).

15. Inside the monitor, how to do thread synchronization. What level of synchronization the program should take.

Monitors and locks are used together in a Java virtual machine. Monitor monitors a synchronized block of code, ensuring that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread does not allow synchronization code to execute until the lock is acquired.

16. What is a deadlock (deadlock).

A deadlock occurs when two processes are waiting for each other to execute before continuing. The result is that two processes are trapped in infinite waiting.

17. 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 to force threads to acquire locks in the order specified. Therefore, if all threads lock and release locks in the same order, there is no deadlock.

Java Collection Class

What are the basic interfaces of the 18.Java collection class framework?

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

The Java Collection class provides a well-designed set of interfaces and classes that support manipulating a set of objects. The most basic interfaces within the Java Collection class are:

Collection: Represents a set of objects, each of which is its child element.

Set: Collection that does not contain duplicate elements.

List: Sequential collection, and can contain duplicate elements.

Map: Keys can be mapped to values (value) of the object, the key can not be repeated.

19. Why the collection class does not implement the cloneable and serializable interfaces.

The semantics and meaning of cloning (cloning) or serialization (serialization) are related to specific implementations. Therefore, it is up to the concrete implementation of the collection class to decide how to be cloned or serialized.

20. What is an iterator (iterator).

The iterator interface provides many ways to iterate over the elements of a collection. Each collection class contains an instance of an iterator that can return instances of the

Iterative methods. Iterators can delete elements of the underlying collection during the iteration.

What is the difference between 21.Iterator and listiterator?

Here is a list of their differences:

Iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.

Iterator to the set can only be forward traversal, listiterator can be both forward and backwards.

Listiterator implements the iterator interface and includes other functions, such as adding elements, replacing elements, getting the index of the previous and last elements, and so on.

22. What is the difference between rapid failure (fail-fast) and security failure (fail-safe).

Iterator 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 the classes underneath the Java.util.concurrent package are security failures. A fast-fail iterator throws

Concurrentmodificationexception exception, and a security failure iterator never throws such an exception.

What is the working principle of the hashmap in 23.Java?

HashMap in Java store elements in the form of key-value pairs (key-value). HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/from the collection. When the put () method is invoked, HashMap calculates the hash value of the key and then stores the key value pairs on the appropriate index in the collection. If the key already exists, value is updated to the new value.

Some of the key features of HashMap are its capacity (capacity), load factor (loading factor), and expansion limit (threshold resizing).

The importance of the 24.hashCode () and Equals () methods is reflected in where.

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

What is the difference between 25.HashMap and Hashtable?

HashMap and Hashtable both implement the map interface, so many features are very similar. However, they have the following different points:

HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.

Hashtable is synchronized, and HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multi-threaded environments.

HashMap provides a collection of keys that are available for application iterations, so HashMap is fast failing. On the other hand, Hashtable provides an enumeration of keys (enumeration).

Hashtable is generally considered to be a legacy class.

26. What is the difference between array and list (ArrayList). When you should use array instead of ArrayList.

The different points of array and ArrayList are listed below:

An array can contain basic types and object types, and ArrayList can only contain object types.

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.