Java Initial/intermediate questions and Answers "detailed"

Source: Internet
Author: User

More Java interview written paper series:Java interview written test station

How does the 1.Java hashmap work?
HashMap is a key value for the data structure, each key will have a corresponding value, the key is to identify such a value.

HashMap based on the hashing principle, we store and retrieve objects through the put () and get () methods. When we pass a key-value pair to the put () method, it calls the Hashcode () method of the Key object to calculate the hashcode, allowing the bucket position to be found to store the value object. When the object is fetched, the correct key-value pair is found by the Equals () method of the Key object, and then the value object is returned. HASHMAP uses LinkedList to solve the collision problem, and when a collision occurs, the object is stored in the next node of the LinkedList. HashMap stores key-value pairs of objects in each LinkedList node.

Reference:

On the Hashcode method in Java

Collection Series-hashmap Source analysis

2. What is a fail-safe iterator for fast failure?
A fast-failing Java iterator may cause concurrentmodifcationexception to be modified during the iteration of the underlying collection. Fail-Safe as an iteration of a replica that occurs in an instance, no exception is thrown. The fast-failing fail-safe paradigm defines how the system reacts when a fault is encountered. For example, a fast iterator ArrayList for failure and an iterator for fail-safe concurrenthashmap.

What is 3.Java blockingqueue?
Java Blockingqueue is part of a concurrent collection util package. The Blockingqueue queue is a support operation that waits for an element to become available to retrieve, and also to store elements when the space is available.

4. When do I use Concurrenthashmap?
In question 2 We see that Concurrenthashmap is used as an instance of a fail-safe iterator that allows full concurrent retrieval and update. When there is a large number of concurrent updates, Concurrenthashmap can be used at this time. This is very similar to Hashtable, but Concurrenthashmap does not lock the entire table to provide concurrency, so concurrenthashmap performance seems better from this point. So concurrenthashmap should be used when there is a lot of updates.

5. Which list implements the fastest insertion?
LinkedList and ArrayList are another implementation of a different list of variables. The advantage of ArrayList is that the dynamic growth array is well suited for use in situations where the total length is unknown at initial time. The advantage of LinkedList is that it is inserted and deleted in the middle position, and the speed is the fastest.

The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).

ArrayList implements a variable-size array. It allows all elements, including null. Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency.

The difference between 6.Iterator and Listiterator
Listiterator has the Add () method, you can add objects to the list, and iterator cannot.
Both Listiterator and iterator have the Hasnext () and Next () methods, which can be traversed sequentially, but Listiterator have the hasprevious () and previous () methods to enable reverse (sequential forward) traversal. Iterator is not allowed.
Listiterator can locate the current index position, Nextindex () and Previousindex () can be implemented. Iterator does not have this feature.
Can implement the Delete object, but Listiterator can implement the object modification, the set () method can be implemented. Iierator can only traverse and cannot be modified.

7. What is copyonwritearraylist, and how does it differ from ArrayList?
Copyonwritearraylist is a thread-safe variant of ArrayList, where all mutable operations (add, set, and so on) are implemented by a new copy of the underlying array. Compared to ArrayList it is slower to write because it requires a snapshot of the instance.

Copyonwritearraylist write operations require large-area copy arrays, so performance must be poor, but the read operation because the object and write operation is not the same object, read between the need to lock, the synchronization between read and write only after writing through a simple "=" To point a reference to a new array object, which hardly takes time, so that the read operation is very safe, suitable for use in multi-threading, never concurrentmodificationexception, So copyonwritearraylist is suitable for scenarios where read operations are much larger than write operations, such as caching.

8. The difference between iterators and enumerations
If the interviewer asks this question, his intentions must be to differentiate iterator from the two aspects of enumeration:
Iterator allows the removal of elements from the underlying collection.
The method name of the iterator is standardized.

How does 9.Hashmap sync?
When we need a synchronized hashmap, there are two options:
Use Collections.synchronizedmap (.. ) to synchronize the HashMap.
Using the Concurrenthashmap

The preferred choice between these two options is to use Concurrenthashmap, because we do not need to lock the entire object and get the lock through the Concurrenthashmap partition map.

The difference between 10.IdentityHashMap and HashMap
Identityhashmap is the implementation of the map interface. Different from HashMap, the reference equality is used here.
In HashMap if two elements are equal, then Key1.equals (Key2)
In Identityhashmap if two elements are equal, then key1 = = Key2

What are the advantages and disadvantages of 11.mybatis?
Pros: SQL written in XML for unified management and optimization
Provides mapping labels to support ORM field relationship mapping for objects and databases
SQL can be optimized
Cons: Large SQL workload
Mybagtis transplant last name is not good
Cascading is not supported

MyBatis Study:

MyBatis Learning Series (I.) INTRODUCTION

MyBatis Learning Series (ii) Mapper mapping file

MyBatis Learning Series (c) Dynamic SQL

MyBatis Learning Series (iv) Mapper interface dynamic Agent

MyBatis Learning Series (v) Related queries

MyBatis Learning Series (vi) Lazy loading

MyBatis Learning Series (vii) caching mechanism

12. Talk about SSH integration?
Struts (presentation layer) +spring (business layer) +hibernate (persistence layer)
Struts is a framework for representing layers that are primarily used to receive requests and distribute requests. Struts actually belongs to the VC level in MVC.
Hibernate is the framework of a persistent layer that is primarily responsible for working with relational databases
Spring is the framework of a business layer and is an integrated framework that is well-bonded to the presentation and persistence layers.

What is 13.maven? What's the effect?
is a project management, build tool
Role: Help download jar to find dependencies, help download dependent hot deployment, hot compile

14.WEB Front-end optimization?
Reduce the number of HTTP requests (merge CSS, JS, pictures)
Using the browser's caching mechanism
Using the gzip compressor system: Valid only for text-class resources
Put the CSS file at the beginning of the HTML
Put the JavaScript file at the end of HTML
Avoid CSS expressions (judging the browser)
Using JavaScript compression
Reduce DNS Lookups
Avoid redirects
Using Ajax

15. Security Testing
Use security testing techniques to identify potential vulnerabilities

16. Transaction Isolation Level (4 types)
Serializable (serialization): A transaction is completely invisible to the database when it is executed (the transaction does not allow other transactions to execute concurrently, and the transaction can be executed one after the other, not concurrently)
REPEATABLE READ (Repeatable Read): A transaction can see newly inserted records that other transactions have committed during execution, but cannot see updates to existing records from other transactions
Read commited (reading committed data): A transaction can see the newly inserted records that other transactions have committed during execution, and can see updates to existing records that have been committed by other transactions.
READ UNCOMMITTED (Reading uncommitted data): One transaction can see the update of newly inserted records that other transactions have not committed during execution, and can see that other transactions have not been committed to an update to an existing record

17.MYSQL Storage Engine (4 types)
MyISAM it does not support transactions, does not support foreign keys, especially fast access, there is no requirement for transactional integrity, or a SELECT, insert-based application can basically use this engine to create tables.
Each myisam is stored as 3 files on disk, with the same file name and table names, but with the following extensions:

. frm (save table definition)
MYD (MYData, storing data)
MYI (myindex, storage index)
InnoDB
The InnoDB storage Engine provides transactional security with commit, rollback, and crash resiliency. However, compared to the MyISAM storage engine, InnoDB writes are less efficient and consume more disk space to preserve data and indexes.
1) Auto-Grow column 2) foreign KEY constraint
MEMORY
Memory creates a table using the content that exists in it. Each memory table actually corresponds to a disk file in the format of. frm. The memory type of table access is very fast because it goes to the data in memory, and the hash index is used by default, but once the server shuts down, the data in the table is lost, but the table continues to exist.
MERGE
The merge storage engine is a combination of a set of MyISAM tables in which the MYISAM table structure must be identical, there is no data in the merge table, operations on tables of the merge type can be queried, updated, deleted, and these operations are actually operations on the internal MyISAM table.

18. Transaction propagation Characteristics
1. propagation_required: If there is a transaction, the current transaction is supported. If no transaction is turned on
2. Propagation_supports: If there is a transaction, the current transaction is supported. If there is no transaction, the execution of the non-transaction
3. Propagation_mandatory: If a transaction already exists, the current transaction is supported. Throws an exception if there is no active transaction.
4. Propagation_requires_new: Always open a new transaction. If a transaction already exists, the existing transaction is suspended.
5. Propagation_not_supported: Always executes in a non-transactional manner and suspends any existing transactions.
6. Propagation_never: Always executes in a non-transactional manner and throws an exception if there is an active transaction
7. propagation_nested: If an active transaction exists, it is run in a nested transaction. If there is no active transaction,

19. Describe the design patterns you know.

The so-called design pattern is a summary of the repeated use of code design experience (a proven solution to a problem in the context). Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. Design patterns make it easier and easier to reuse successful designs and architectures. The presentation of proven technologies into design patterns will also make it easier for new system developers to understand their design ideas.

In Gof's design patterns:elements of reusable object-oriented software, three classes (creation [abstraction of the instantiation process of a class], structural type [ Describes how to combine a class or object to form a larger structure], behavior [to divide responsibility between different objects and abstract the algorithm]) 23 design patterns, including: abstract Factory (Abstraction Factory mode), builder (Builders mode), Factory Method (Factory mode), Prototype (original model mode), Singleton (singleton mode), facade (façade mode), Adapter (adapter mode), Bridge (bridge mode), Composite (compositing mode), Decorator (decorative mode), Flyweight (enjoy meta mode), Proxy (Command mode), interpreter (interpreter mode), Visitor (visitor mode), Iterator (iterative sub-mode), Mediator (mediator mode), Memento (Memo mode), OBSERVER (Observer mode), State (status mode), strategy (policy mode), template method (templated methods mode), Chain of Responsibility (responsibility chain mode).
When asked about design patterns, the interview can be used to pick the most common answers, such as:

Factory mode: Factory classes can generate different instances of subclasses based on conditions, which have a common abstract parent class and implement the same method, but these methods operate differently for different data (polymorphic methods). When an instance of a subclass is obtained, the developer can call a method in the base class without having to consider which instance of the subclass is returned.

Proxy Mode: provides a proxy object for an object and controls the reference of the original object by the proxy object. In actual development, according to the different purposes, the agent can be divided into: Remote agent, virtual agent, protection agent, cache agent, firewall agent, synchronous agent, intelligent reference Agent.

Adapter Mode: transforms the interface of a class into another interface that the client expects, so that classes that are not used together because of an interface mismatch can work together.

Template Method Pattern: provides an abstract class that implements partial logic in the form of a concrete method or constructor, and then declares some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways (polymorphic implementations), thus implementing different business logic.

In addition, can also talk about the above mentioned façade mode, bridge mode, single case mode, decorating mode (collections tools and I/O system are used in decorating mode), anyway, the basic principle is to pick their most familiar, use the most answers, lest throat.

Design mode:

23 Design patterns and six major design principles

Java Initial/intermediate questions and Answers "detailed"

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.