Java interview pen questions (Java basic interview questions)

Source: Internet
Author: User
Tags finally block

1. What is the difference between abstract class and interface?
A: The class that declares the existence of a method without implementing it is called abstract class. It is used to create a class that reflects some basic behaviors and declare a method for this class, however, this class cannot be implemented in this class. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes. Instead, implement this method in the subclass. Other classes that know their behavior can implement these methods in the class.
An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. All methods in the interface are abstract, and none of them have a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines all the methods of this interface. Then, you can call the interface method on any object of the class that implements this interface. Because there is an abstract class, it allows the interface name as the type of the referenced variable. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.

2. Simple Principle and Application of the Exception Handling Mechanism in Java.

A: When a Java program violates the Java Semantic Rules, the Java Virtual Machine will indicate an error as an exception. There are two types of violation of Semantic Rules: one is the built-in semantic check of the Java class library. For example, if the array subscript is out of bounds, indexoutofboundsexception is triggered. If the null object is accessed, nullpointerexception is thrown. Another scenario is that Java allows programmers to extend this semantic check. programmers can create their own exceptions and choose when to use the throw keyword to cause exceptions. All exceptions are subclasses of Java. Lang. thowable.

3. What is the difference between sleep () and wait?
A: Sleep () is a thread-like thread method. As a result, this thread suspends the execution for a specified time and gives the execution opportunity to other threads. However, the monitoring status remains unchanged and will be automatically restored, calling sleep does not release the object lock.
Wait () is an object method. Calling the wait method for this object causes this thread to discard the object lock and enter the waiting lock pool for this object, this thread enters the object lock pool only after the notify method (or notifyall) is issued for this object to prepare for obtaining the object lock to enter the running state.

4. Is string the most basic data type?
A: string is not a basic data type. The basic data types include byte, Int, Char, long, float, double, Boolean, and short. Java. Lang. string class is of the final type. Therefore, it cannot be inherited or modified.

5. Differences between string and stringbuffer.
A: The Java platform provides two classes: string and stringbuffer, which can store and operate strings, that is, character data containing multiple characters. This string class provides a string whose values cannot be changed. The stringbuffer class provides strings for modification. You can use stringbuffer when you know that the character data is to be changed. Typically, stringbuffers can be used to dynamically construct character data.

6. Can an interface inherit an interface? Can an abstract class implement interfaces? Can an abstract class inherit an object class?
A: The interface can inherit the interface. Abstract classes can implement interfaces. Whether the abstract class can inherit the object class, but the premise is that the object class must have a clear constructor.


7. Does Java have a goto?
A: goto is a reserved keyword in Java, but it is not currently used in Java.

8. Do I use run () or start () to start a thread ()?
A: To start a thread is to call the START () method so that the virtual processor represented by the thread is runable, which means it can be scheduled and executed by JVM. This does not mean that the thread will run immediately. The run () method can generate the exit sign to stop a thread.

9. Differences between class variables and instance variables.
A: class variables are shared by all objects, and all instance objects share one class variable. There is only one space in the memory to store the class variable value. If one of the objects changes the value of the class variable, other objects get the changed result. When the class is loaded into the memory, the class variable will allocate the corresponding memory space.

10. What are the similarities and differences between runtime exceptions and general exceptions?
A: An exception indicates an abnormal state that may occur during the running of the program. An exception indicates an exception that may occur during common operations on the virtual machine. It is a common running error. The Java compiler requires that methods must declare and throw possible non-runtime exceptions, but do not require that they throw uncaptured runtime exceptions.

11. What are the differences between instance and class methods? 
A: The static keyword in front of the method is used to modify the class method; otherwise, the method is used as the instance method. Instance methods can call other methods in this class. Class methods can only call other class methods, but cannot call instance methods. When class files are loaded into memory, instance methods are not allocated memory space, objects are allocated only after they are created. The class method allocates the corresponding memory space when the class is loaded into the memory.

12. What is a hash table?
A: A hash table, also called a hash table, is a record storage technology that maps keywords into storage addresses. To store data, first design an algorithm (hash function) and calculate the hash code of each record based on the key words of the data record, this hash code is used as an index for record data related to keywords. To retrieve a record, you only need to recalculate it based on the hash function. After obtaining the hash code, you can directly access the record at the corresponding location.

13. Can I call non-static methods from inside a static method?
A: No. If it contains the method () of the object, object initialization cannot be guaranteed.

14. What does "\ uxxxx" represent in Java escape characters?
A: \ uxxxx is the character encoding method in Java. The prefix \ U indicates that the character is a Unicode character, and XXXX indicates the hexadecimal number of 1 to 4 characters, this escape character form can be used to represent any character in the Unicode Character Set.

15. What type of data can the expression in the switch statement be?
A: The expression value can be byte, short, Int, or char data, but cannot be float or double data.

16. During data insertion, who are the faster arraylist, sorted list, and vector?
A: arraylist and vector store data in arrays. Element movement is required during data insertion. Therefore, data insertion is slow. A sort list is a linked list structure. when inserting data, you only need to modify the forward and backward directions of the linked list. Therefore, the insertion speed is faster.


17. Differences between final, finally, and finalize.
A: Final-modifier (keyword) If a class is declared as final, it means that it cannot generate a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final. Declare variables or methods as final to ensure that they are not changed during use. Variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be overloaded.
Finally is a Finally block for exception handling to perform any cleanup operation. If an exception is thrown, the matched catch clause is executed, and the control enters the Finally block (if any ).
Finalize is the method name. Java technology allows you to use the finalize () method to clear objects from the memory before the Garbage Collector clears them. This method is called by the garbage collector when it determines that this object is not referenced. It is defined in the object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object.

18. Give up to five common classes, packages, and interfaces. 
A: common classes: bufferedreader, bufferedwriter, filereader, filewirter, and string.
Common packages: Java. Lang, java. AWT, java. Io, java. util, and Java. SQL.
Common interfaces: Remote, list, MAP, document, and nodelist.

19. What are the implementation methods of multithreading? What are the implementation methods of synchronization?
A: multithreading can be implemented in two ways: Inheriting the Thread class and implementing runnable.
Interface synchronization can be implemented in two ways: synchronized, wait, and notify.

20. When system. In. Read (buffer) is used to input a row of n characters from the keyboard, what are the bytes stored in the buffer? 
A: When using system. in. after a row of n characters is input from the keyboard, the number of bytes stored in the buffer is n + 2, that is, after n characters are entered, the carriage return and line feed characters are also stored.

21. How to split strings?
A: The first method is to use the split () method to save the split content in the specified string array. The second method is to use stringtokenizer and use stringtokenizer's nexttoken () and hasmoretokens.

22. How does JVM load class files?
A: In JVM, class loading is implemented by classloader and its subclass. Java classloader is an important Java runtime system component. It is responsible for finding and loading classes of class files at runtime.

23. Under what circumstances will finally statements not be executed?
A: If you execute a system. Exit (0) statement within try to terminate the application execution, the statements in finally will not be executed.

24. Scope: public, private, and protected.
A: The specific differences are as follows:

Scope Same class Same package Child class All classes
Public
Protected ×
Private × × ×
Default × ×

25. Tell the difference between rewriting and overloading. Can the rewrite method change the type of the returned value?
A: overriding and overloading are different manifestations of Java polymorphism. Rewriting is a manifestation of polymorphism between the parent class and the Child class, and overloading is a manifestation of polymorphism in a class.
If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten. When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, they are called method overloading.
The overload method can change the type of the returned value.

26. What is the difference between throw and throws?
A: The throw keyword is generally used inside a method to throw an exception class object. Once an exception is thrown, the program code after the throw statement will not be executed.
The throws keyword usually appears in the method declaration to specify the exceptions that the method may throw. If multiple exceptions may be thrown, use commas to separate them.

27. What is the result of the compareto method of a string?
A: The compareto () method can be used to compare the size relationship between a string and a string. If the current string is less than the specified string, a value smaller than 0 is returned; otherwise, a value greater than 0 is returned; if the two strings are equal, 0 is returned.

28. How do I implement string connection operations? 
A: You can use the "+" operator and the Concat () method to connect strings. In addition, the "+" operator can connect strings to other types of data. Data types that can be connected to strings include int, long, float, double, Boolean, and char.

29. when an object is passed as a parameter to a method, this method can change the attributes of this object and return the changed result, so is it a value transfer or a reference transfer?
A: It is value transfer, because in Java programming language, only value transfer parameters are allowed. When an object instance is passed as a parameter to a method, the parameter value is a reference to the object. The object content can be changed in the called method, but the object reference will never change.

30. Is the value of class myclass equivalent to class myclass extends object correct?
A: Correct. Because the object class is the highest-level class in Java, It is the superclass of all classes. All classes can be said to be inherited from objects, but we do not need to explicitly specify extends objects.

31. Is there a length () method for the array?
Does string have the length () method?

A: The array does not have the length () method. It only has the Length attribute. String has the length () method.

32. What are the features of object orientation?
A: (1) Abstraction: Abstraction ignores aspects irrelevant to the current goal in a topic, so that you can pay more attention to the aspects related to the current goal. Abstraction is not intended to understand all the problems, but to select a part of the problem. Abstract involves two aspects: Process abstraction and data abstraction.
(2) Inheritance: inheritance is a hierarchical model that connects classes and allows class reuse. It provides a clear way to express commonalities. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the features of the original class. The new class is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class ). A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make it more suitable for special needs.
(3) Encapsulation: encapsulate processes and data, and access to data can only be performed through the defined interface. The real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface.
(4) polymorphism: allows different types of objects to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. The polymorphism language has the advantages of flexibility, abstraction, behavior sharing, and code sharing, and solves the same name problem of application functions.

33. What is the mechanism for implementing polymorphism in Java?
A: Method rewriting and overloading are different manifestations of Java polymorphism. Rewriting is a manifestation of polymorphism between the parent class and the Child class, and overloading is a manifestation of polymorphism in a class.


34. How can I connect to a database through JDBC?
A: JDBC-ODBC bridge driver can access an ODBC data source, but each client computer needs to install and configure ODBC drivers.
Native-API partly Java driver converts JDBC calls into calls to specific database APIs. This method requires that drivers of specific databases be installed on each client computer.
JDBC-net pure Java driver converts JDBC calls to a proprietary database access protocol independent from the database Middleware vendor, and is then responsible for connection activities with the database.
Native-Protocol pure Java driver converts JDBC calls to standard network protocols (such as HTTP) directly used by databases ). In this way, you do not need to install the client software. The client computer can directly communicate with the database server.

35. What are the features of the interfaces list, map, and set when accessing elements?
A: The list holds elements in a specific order. duplicate elements are allowed. Set cannot have repeated elements and can be sorted internally. Map stores the key-value, which can be multiple values.

36. What is the difference between the "=" operator and the equals operator when comparing objects?
A: The "=" operator indicates whether the addresses referenced by the two objects are equal, and the equals operator indicates whether the referenced content of the two objects is equal.

37. What is circular nesting? What are their characteristics?
A: nested loop refers to a loop body that contains another complete loop statement. The characteristics of multiple loop statements are that the outer loop variables are relatively stable, and the inner loop Variables change one by one, that is, "multi-layer loop, internal (layer) external (layer) exclusive, external (layer) change once, and change the internal layer once."

38. Can a Chinese character be stored in a char variable? Why?
A: It can be defined as a Chinese character. Because Java uses unicode encoding and a char occupies 16 bytes, it is okay to put a Chinese character.

39. What are the differences between error and exception?
A: Error indicates that the restoration is not an impossible but difficult case. For example, memory overflow.
Exception indicates a design or implementation problem. That is to say, it indicates that if the program runs normally, it will never happen.

40. How to Implement Java serialization?
A: implement the serializable interface for the class to be serialized. This interface does not need to be implemented. It is only used to mark that the object is serializable, then you need to use an output stream to construct an objectoutputstream object, and then use objectoutput
The writeobject () method of the stream object can write the object whose parameter is obj. To restore the object, use the input stream.

41. How many types of streams are there in Java? JDK provides some abstract classes for each type of stream for inheritance. which classes are they?
A: byte streams and byte streams. Byte streams are inherited from inputstream outputstream, and bytes streams are inherited from inputstreamreader outputstreamwriter. There are many other streams in the Java. Io package, mainly to improve performance and ease of use.

42. String S = new string ("A"); how many string objects are created together?
A: Two. It includes a character object and a character object reference object.

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.