Questions on Java basic plane

Source: Internet
Author: User

Problem:
1. Does list,set,map inherit from collection interface?
Answer: List,set Yes, map is not

2, short s1=1;s1 = s1+1; what's wrong? Short S1 = 1; S1 +=1; what's wrong?

Answer: short S1 = 1; S1 = s1 + 1; (S1+1 operation result is int type, need cast type)
Short S1 = 1; S1 + = 1; (can be compiled correctly)

3. What are the similarities and differences between runtime anomalies?

A: An exception indicates an unhealthy state that may occur during a program's operation, and a run-time exception that represents an exception that may be encountered in a virtual machine's usual operation 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. Can I store a Chinese character in char type variable? Why?

A: can be defined as a Chinese, because in Java encoded in Unicode, a char accounted for 2 bytes, so put a Chinese is no problem

5. Start a thread with run () or start ()?

A: Starting a thread is calling the start () method so that the virtual processor represented by the thread is in a running state, which means it can be invoked and executed by the JVM. This does not mean that

The thread will run immediately. The run () method can produce a flag that must be exited to stop a thread.

6. What is the difference between heap and stack?
A: The stack is a linear collection, and its addition and deletion of elements should be done in the same paragraph. The stack is processed in a last-in, first-out manner.
A heap is a constituent element of a stack

7. What are the collection classes you know?

A: The most common collection classes are List and Map. The specific implementations of the list include ArrayList and LinkedList, which are variable-sized lists that are more appropriate for building, storing, and manipulating any type of object. MAP provides a more general method of storing elements. The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value.

8. A class declaration implements an interface, so what does this class need to do?

A: Implement all the methods in the interface, and the access rights of these methods must be publi

9. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described.

Answer: If the data is 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.
When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches.

10. Which of the following conditions can terminate the running of the current thread? A. When an exception is thrown.

B. When the thread calls the sleep () method.
C. When a new thread is created.
D. When a high-priority thread enters the ready state.

Answer: ABD

11, say Arraylist,vector, LinkedList storage performance and characteristics

A: Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly. Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster.

12. Can I inherit the String class?

A: The string class is the final class and cannot be inherited.

13. How to determine if a is an instance of B, and which operator to use?

Answer: You can use the instanceof operator

14, how to go to the decimal two digits, and rounded.

Answer: Double d=1256.22d;
d=d/100;
System.out.println (Math.Round (d) *100);

15. Describe the life cycle of a thread?

A: New---running---blocking------death

16. Basic concepts of threads, the basic state of threads, and the relationship between states

A: A thread is an execution unit that executes a program's code during the execution of a program, and each program has at least one thread, the program itself.
There are four types of threads in Java: Run, ready, suspend, end.

17. Basic concepts of threads, the basic state of threads, and the relationship between states

A: A thread is an execution unit that executes a program's code during the execution of a program, and each program has at least one thread, the program itself.
There are four types of threads in Java: Run, ready, suspend, end.

18. What is GC? Why do you have a GC?

A: GC is garbage collection meaning (Gabage Collection), memory processing is a problem for programmers, forget or wrong memory recycling can cause the program or system instability or even crash, The GC functionality 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 a way to release the displayed operation of the allocated memory

19. What is the difference between heap and stack?

A: The stack is a linear collection, and its addition and deletion of elements should be done in the same paragraph. The stack is processed in a last-in, first-out manner. A heap is a constituent element of a stack

20. What are the packages and core classes in the homing mechanism of the class?

Answer: Java.lang.Class

? Java.lang.refrection.Method
? Java.lang.refrection.Field
? Java.lang.refrection.Constructor
? Java.lang.refrection.Modifier
? Java.lang.refrection.Interface

21. Please say the thread-related methods you know

A: Wait (): causes a thread to be in a wait state and releases the lock of the object it holds.

Sleep (): Makes a running thread sleep, is a static method that calls this method to catch the interruptedexception 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.
Allnotity (): Wakes all the threads that are in the waiting state, noting that they do not give all the wake-up threads an object lock, but instead let them compete.

22. In the Java language, overrides (overriding) and overloads (overloading) of methods are different manifestations of polymorphism. Which of the below statements is true?

A Overrides are a representation of the polymorphism between a parent class and a subclass.
B Rewriting is a representation of polymorphism in a class.
C Overloading is a representation of polymorphism in a class.
D Overloading is a representation of the polymorphism between a parent class and a subclass.

Answer: AC

23, the following about the interface of the narrative which is wrong

A. The data in the interface must set the initial value
B. General methods cannot be declared in an interface
C. Interfaces can declare references
D. The above are correct

Answer: BC

24. What is the difference between int and Integer

A: Java offers two different types: reference types and primitive types (or built-in types). int is the raw data type of Java, and integer is the wrapper class provided by Java for Int. Java provides a wrapper class for each primitive type.

Raw type Encapsulation class
Boolean---Boolean
Char---Character
BYTE---Byte
Short---short
int---Integer
Long---long
Float---float
Double---double
The behavior of reference types and primitive types is completely different, and they have different semantics. Reference types and primitive types have different characteristics and usages, including: size and speed issues, which types of data structures are stored as the default values that are specified when reference types and primitive types are used as instance data for a class. The default value of an object reference instance variable is NULL, and the default value of the original type instance variable is related to their type.

25. Abstract class Something {

Private abstract String dosomething ();
}
What's wrong with this?

Answer: wrong. Abstract methods cannot be modified with private. Abstract methods is to let sub-class implement (Implementation) specific details, how can use private to abstract

26. What is the difference between super () and this ()?

Answer: This (): the object of the current class, Super Parent class object.

Super (): Access to the members and behavior of the parent class in the subclass must be constrained by the class inheritance rule
And this he represents the current object, of course all resources can be accessed.
In the constructor, if the first row is not written super (), the compiler inserts it automatically. But if the parent does not have a constructor with no arguments, or if the function is privatized (with private adornments). You must now add an instantiation construct to the parent class. And this does not have this requirement, Because of its own instantiation of the construction.
The method used by super and this is pretty much the same. It's just super. Consider whether you can access the resources of its parent class.

27, say your most common to the runtime exception.

Answer: ArithmeticException, ClassCastException, IllegalArgumentException,

Indexoutofboundsexception, Nosuchelementexception, NullPointerException,

SystemException,

28. Can the constructor constructor be override?

A: The constructor constructor cannot be inherited, so overriding cannot be overridden, but it can be overloaded with overloading.

29, Scope public,protected,private, and do not write when the difference?

A: Public: Available in different packages, same package, class

? Private: In-Class
? Protected: Subclasses of different packages, same package, within class are available
? When not written: within the same package, within the class

30. There is a return statement in try {}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return?

A: It executes and executes before return.

31. What is the flyback mechanism of a class?

A: Through classes (class object), you can draw the current class of fields, methods, Construtor, interface, superclass, modified, and so on, you can instantiate an instance through the class, set properties, Wake method. Everything in spring is a flyback, struts, and hibernate are all developed through the class's flyback.

32, how to obtain the number of milliseconds from 1970 to present

Answer: Java.util.Date dat=new Date ();

Long Now=dat.gettime ();

33. What happens if I don't define a construction method?

A: A parameterless construction method is automatically obtained.

34. When a thread enters an synchronized method of an object, does the other thread have access to other methods of this object?

A: No, an synchronized method of an object can only be accessed by one thread.

35, Math.Round (11.5) how much? Math.Round (-11.5) how much?

Answer: Math.Round (11.5) ==12

Math.Round (-11.5) ==-11

36. public class Something {

public int AddOne (final int x) {
return ++x;
}
}
What's wrong with this?

Answer: wrong. int x is modified to final, meaning that x cannot be modified in AddOne method.

37, final, finally, finalize the difference.

A: Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.

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 reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file.

38, C/S and b/s difference:

A: CS is client/server (client/server) structure, the main features are strong interactivity, with secure access mode, low network traffic, fast response. The structure of the program is targeted development, the change is not flexible, maintenance and management is more difficult. Usually confined to small local area networks, which is not conducive to expansion. BS is the Browser/server (browser/server) architecture, which is to install and maintain only one server, while the client runs the software using the browser (BROWSE). The main features of B/s structure are strong distribution, convenient maintenance, simple development and strong sharing, low total cost of ownership.

39, the difference between & and &&.

A:& is a bitwise operator that indicates that bitwise AND operation,&& are logical operators, representing logic and (and).

40, multi-threaded there are several ways to achieve, what is it? How are there several implementations of synchronization?

A: There are two ways to implement multithreading, namely inheriting the thread class and implementing the Runnable interface

There are two implementations of synchronization, namely Synchronized,wait and notify

41, encoding conversion, how to implement the GB2312 encoded string into a iso-8859-1 encoded string.

A: String A=new string ("Medium". GetBytes ("gb2312"), "iso-8859-1");

String A=new string ("Medium". GetBytes ("iso-8859-1"));

42. Are there several types of streams in Java? The JDK provides some abstract classes for inheritance for each type of stream, tell me what classes they are.

A: byte stream, character stream. The byte stream inherits from the InputStream OutputStream, and the character stream inherits from the InputStreamReader OutputStreamWriter. There are many other streams in the java.io package, mainly to improve performance and ease of use.

43. What are the characteristics of the final category?

Answer: Property Constants
Method can not overridding
Class can not inherit

44. How to convert numeric characters to numbers (integer,double)?

Answer: Integer.parseint ("1234")

Double.parsedouble ("123.2")

45. What is the difference between error and exception?

A: 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. That is, it means that if the program runs normally, it never happens.

46. In the Java language, overrides (overriding) and overloads (overloading) of methods are different manifestations of polymorphism. Which of the below statements is true?

A Overrides are a representation of the polymorphism between a parent class and a subclass.
B Rewriting is a representation of polymorphism in a class.
C Overloading is a representation of polymorphism in a class.
D Overloading is a representation of the polymorphism between a parent class and a subclass.

Answer: AC

47. Describe the principle mechanism of the JVM loading class file?

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

48. The difference between HashMap and Hashtable.

A: HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.

HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.
HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.
Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.
The biggest difference is that the Hashtable method 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.
The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

49. The simple principle and application of exception handling mechanism in Java.

For:

Principle
Errors go directly to the exception handling section or throw up.
Application:
Java exceptions are errors, and there are two types of runtime that can be encoded without capturing. One is a generic exception, and if throws is declared, it must be processed.

50. There is a memory leak in Java, please describe it briefly.

Answer: Yes.  such as: int i,i2;   return (I-I2); When I is a positive number large enough, the i2 is a large enough negative number. The result is an overflow that causes an error.

Continuous Update

Questions on Java basic plane

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.