Java Basics Quiz

Source: Internet
Author: User
Tags arithmetic serialization try catch xml parser

Doing repetitive work every day, while being familiar with existing skills, will also weaken your impressions of some of the basics, and always come back to see, after all

                         ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                                            ,         &N Bsp      -----A Journey

1. What are the main aspects of object-oriented features?
The object-oriented features are mainly in the following aspects
-Abstract abstraction is the process of constructing a class by summarizing the common features of a certain class of objects, including the two aspects of behavioral abstraction and attribute abstraction, which focus on the properties and behaviors of the object, and do not pay attention to its specific details.
-Encapsulation encapsulation is the way to bind data and manipulate the data, and to access the data only through defined interfaces. The encapsulation of the method enhances the reusability of the Code, and the encapsulation of the attributes increases the security of the Code, and in general, the core concept of encapsulation is to hide everything that can be hidden, providing only the simplest programming interface.
-Inheritance inheritance is the process of obtaining information from existing classes to create a new class, the class that provides the inheriting information is called the parent class (superclass, base class), the class that obtains the inheriting information is called the subclass, the inheritance also improves the reusability of the code, and also can be used as an extension to the original class.
-Polymorphic polymorphism is the same type of different objects in the face of the same thing to make different reactions, divided into the compile-time polymorphism and run-time polymorphism, compile-time polymorphism refers to the parent class type to point to the subclass type object, Run-time polymorphism refers to the way a subclass is represented when a polymorphic object created by a parent class calls a method overridden by a quilt class.

2. Access modifiers
Public visible to all objects
Protect not visible to other package objects
Default is not visible to other package objects and subclass objects
Private only visible to own class

Is 3.String a basic data type?
No! There are only 8 basic data types, Boolean (Boolean) char (Character) two byte (byte) a short (short) two int (Integer) four long (long) eight float (float) Four Double (double) eight, and string is a special reference type, except that the base data type is the reference data type.

4.float f= 3.4, right?
Wrong! The decimal type defaults to the double type, the double type is a eight-byte unit, and the conversion to a four-byte float requires a strong turn. or write float f = 3.4f;

5.short S1=1;s1=1+s1, right, short s1= 1;s1+=1, right?
The first is not correct, S1 is a short type object, and the int type object does the addition operation when it will naturally become an int type of variable, so the result is type int, and the short type of S1 is unacceptable. The second one is right, because when you do the + = operation, the system will help you convert the value to the short type, which is equivalent to s1= (short) (s1+1), so the result of the calculation is the short type.

6.java No Goto
Goto is a reserved word in Java and is not currently used, but it is not possible to use Goto when the identifier is named.

What is the relationship between 7.int and integer
Java is a nearly complete object-oriented language, but for programming convenience or the introduction of eight basic data types, in order to be able to use these eight data type classes as Object class operations, Java also provides eight types of packaging. After jdk1.5 is the support for automatic boxing and unpacking between the base data type and the corresponding wrapper type.
Integer also has a constant pool, assuming that the creation of an integer object between 128 and 127 will be the same as the creation of a string, first go to the constant pool, if there is a direct point to the constant pool.

The difference between 8.& and &&
& can do bitwise and arithmetic, also can do logic and operation. && is called short-circuit & arithmetic, assuming that the expression in the && front is judged to be false, the expression after,&& will not be executed.

9. Explain the usage of the in-memory stack, heap, method area.
Usually we define a variable of a basic data type, a reference to a data, and a function call that is called by the field to save the stack space inside the JVM. Objects created through the new keyword and the constructor are heap spaces, which are part of the method area.

10.math.round (11.5) is how much, math.round (-11.5) is how much
The first result is 12, and the second result is-11. Because the rounding mechanism in Java is to add 0.5 to the parameter and then take the whole down.

Whether the 11.switch can function on a byte, whether it can function on a long, whether it can function on a string
Before JAVA5, switch could use a value of Char,byte,short,int, added an enumeration type after 5, and added a string after 7, but no matter when, long is not allowed.

12. How to calculate the most efficient 2*8
2<<3, the shift operation is always the computer's best at it.

13. The array has no length () method, and string does not have the length () method
There is no length () method in the array, but the array has the length property, and string is the length () method

How to jump out of multiple nested loops in 14.java
Before the outermost loop, add a tag such as a, then break A; You can jump out of the current multi-loop, Java is support tagged break and continue statements, but try to avoid using them, because they will cause some confusion to your code.

15. Whether the constructor can be overridden
Constructors cannot be inherited, so there is no way to rewrite this, you have to say you can write a name as an overload.

16. Two objects satisfy equals, but have a different hash code, is that right
No, if two objects satisfy equals, then their hash code must be the same, but in other words, their hash code is the same, their equals may not be the same

17. Can I inherit the String class
No, there's a big final on the string head.

The difference between 18.string,stringbuilder,stringbufferr
The Java platform provides two types of strings, string and Stringbuilder/stringbuffer, where they can store and manipulate strings, where string is a read-only string, meaning that the contents of a string cannot be changed. In StringBuilder and StringBuffer, the value inside can be directly modified, and the relationship between the two is Stringbffer is thread-safe, but the inevitable efficiency is not as StringBuilder, Because he was synchronized locked part of the performance.

19. When using the + operator for string connections better performance than stringbuilder/stringbuffer connection strings
+ The essence of string concatenation is to create a StringBuilder object to put two strings in the stitching .... In the amount of less than 100, in fact, the performance gap is not very large, so there is no big relationship, but if the number of splicing is huge, then you must use to Stringbuilder/stringbuffer to improve efficiency

20. Overrides and overloads, whether overloaded methods can be differentiated according to return values

21. Describe the principle mechanism of the JVM loading class file?
The loading of classes in the JVM is implemented by the ClassLoader and its subclasses, which are responsible for finding and loading classes in class files at run time.
Summarize the class loading process
1. Default initialization of static member variables
2. Static code block
3. Allocate memory and initialize the variables of ordinary members
4. Display initialization for all variables
5. Executing common code blocks
6. Execute the constructor
7. Return memory address

22.char type is able to store a Chinese character, why
The char type can store a Chinese character because the default encoding used in Java is Unicode, one Chinese in Unicode is two bytes and one char type just stores two bytes

23. What are the similarities and differences between abstract classes and interfaces?
Interfaces can be said to be abstract class abstraction, neither of them can be instantiated, abstract classes may have an abstract method can also have no abstract method, but a class there is an abstract method that he must be an abstract class, abstract class in addition to not instantiate and ordinary class no big difference, abstract class can have a constructor, interface is not the same, Constructors and code blocks cannot be defined in an interface, interfaces can be inherited more than interfaces, interfaces can be implemented more, members in interfaces must be public decorated, and methods must be abstract, and the attributes inside the interface must be constants.

24. Brief description of the difference between static and instance variables
A static variable is a variable modified by static, also known as a class variable, which belongs to a class and not to an object within the class, and the static variable has only one variable in the class, and regardless of how many objects are created, the object will share the static variable. The initialization of a static variable starts before all loads.

25. Can I make a call to a non-static method from a static method?
No, because the static method is loaded when the class is called, so at that time the object is not created, and the method of non-static method call must be done by the object, there is a problem, so the static method can not invoke the non-static method, For this reason, however, non-static methods can arbitrarily invoke static or static properties.

26. Can abstract methods be modified by static fields?
Obviously not, the meaning of the abstract method of life is to be rewritten to be implemented, while static field is a way to eliminate other methods to rewrite the field, if you can use it at the same time there will be an abstract method that could never be implemented.

27. Are there several ways to clone an object?
The first is to implement the Cloneable interface and rewrite the Clone method inside the object, which is called a shallow clone, because if a reference type is maintained within him, the point will not change.
Another is to implement the Serializable interface, through serialization and deserialization to achieve deep cloning, such a clone reference type data will also be cloned together, we call it deep cloning.

What is 28.GC and why should there be GC
GC is the meaning of garbage collection, memory is the programmer is prone to problems, forget or wrong memory recycling can cause system instability or even crash, so Java provides automatic detection of whether the object exceeds the scope of the GC function, the Java language does not provide a way to release the allocated memory. Java programmers do not have to worry about memory management because the garbage collection mechanism is automatically recycled, and programmers can only use the System.GC () method and the Runtime.getruntime (). GC () method to make recommendations for garbage collection. However, the GC does not guarantee that it will do so (not reluctantly).
Garbage collection is significant, this mechanism can effectively prevent the leakage of memory, efficient use of memory, garbage collection mechanism is generally as a low-priority background thread to run, unpredictable to have been dead or long time unused objects to clean and reclaim, garbage collection has two sides, One of the highlights in Java was that he freed programmers from the intricacies of memory management, but it was because of the uncontrolled memory that led to some bad user experiences.

29.String s =new stirng ("XYZ"), several objects created
Two objects, one created in a constant pool, and the other created in the heap area.

30. Whether an interface can inherit an interface, whether an abstract class implements an interface, whether an abstract class can inherit a non-abstract class/
Interfaces can inherit interfaces, and even interfaces can inherit multiple interfaces, and abstract classes can, of course, implement interfaces, he simply cannot create objects, and of course can inherit non-abstract classes.

31. Can a. java file contain more than one class that is not an inner class?
Sure, but there is only one class that can be modified by public, and the class name being decorated needs to be equal to the file name.

32. Can anonymous inner classes inherit other classes? Is it possible to implement an interface?
Is possible, in fact, it is one of the methods used to implement interface callbacks or to implement monitoring.

33. Inner classes can refer to members of external classes, and there are no restrictions.
An inner class can access all members that create his external class, including private members.

What are the uses of the final keyword in 34.java
Used to modify the method, Yuck yuck, this method can still be inherited, but can not be rewritten, to modify the class, the class will not be inherited, to modify the variable, the variable will no longer be assigned by the virtual machine default initial value and once the assignment will no longer be changed.

35. How do I convert a string to a basic data type? How to convert a base data type to a string.
The valueof (string) or Parsexxx (string) method in the wrapper type that invokes the base data type can change the corresponding string to the base data type.
The basic data type + "" can be programmed into strings, or the valueof () method of the string class can be used to convert the base data type to the appropriate string.

36. How to implement the reversal and substitution of strings.
Lying trough this question is good to go back to write a reversal method.
After writing the discovery is so, only found in JS in the array has a reversal method is convenient called array.reverse (char[]), he can be a character array inversion.
The replacement is also very simple, directly with the regular can be.

37. Date and Month
1. How to obtain the day of the month
2. How to obtain the number of milliseconds from 1970 to present
3. How to get the last day of each month
4. How to format a date
1. Call the corresponding get method in date can be obtained, but these methods are basically obsolete, now generally is to create a Java.util.Calendar instance, call its get method to pass the corresponding parameter (more trouble)
2. There is a gettime () method to get this thing straight.
3. is also the creation of Java.util.Calendar this mighty object, and then pass the corresponding parameters can be.
4. Call the Format method in the subclass inside the Java.text.DataFormat to initialize the date.

38.java and Javasciprt
Java and JS are different products developed by two companies. Java is a near-real object-oriented language, no matter how simple the development of the program must design objects, JS is a scripting language, can be used to make network-independent, user interaction with the complex software, is an object-based and event-driven programming language, Java code must be compiled before executing , but JS is an explanatory language, the source code does not need to be compiled, interpreted directly by the browser, Java as a strongly typed language, all variables must be declared before compiling, and JS can even not declare variables before the use of variables.

39. If there is a return statement inside the try statement, will the code immediately following the finally statement be executed? is the order performed before or after the return?
are executed, and the order of execution is performed before return.
It is bad practice to change the return value in Finally, because the return in the try statement in Java is tentatively executed after the finally statement block is executed. In this case, the return value changes if the value of return is changed in the finally statement block (despicable)

Keywords for handling Exceptions in 40.java: How to use throws,throw,try,catch,finally respectively.
Java uses an object-oriented approach to exception handling, classifies various exceptions in Java, and provides a good interface, each of which is an object, an instance of Throwable or one of his subclasses, and throws an exception object when the method has an exception. This exception object contains the exception information, we can call the exception object method to obtain the corresponding exception information. Exception handling is implemented by these five keywords, we use a try to execute a program, if the system throws (throw) An exception object, we will use a corresponding exception object to capture him and in the catch statement after the exception is defined in the processing operation, Throw is used to explicitly throw an exception, and throws is often used to declare its own possible problems in the method (allowing some degree of moan), and finally to make sure that a piece of code can be executed normally regardless of the declaration exception, and that the try statement can be nested with each other. The exception to the nested statement is placed in the exception stack until the Try statement is executed, then the exception is passed to the outer try statement, and if an exception is not handled, the exception is thrown out of the box until there is a statement that handles the exception or until the exception is thrown to the virtual machine.

41. Describe the differences between final,finally,finalize
There are three usages of the Final:finai modifier, which are used to modify the class to ensure that the class cannot be inherited, and that the method cannot be overridden to modify the variable to ensure that the variable becomes constant ....
Finally: The code that is usually placed after the try Catch method indicates that no matter what exception is occurring, it must be executed.
Finalize: Is a method inside the object class that calls this method when the GC determines that there are no more references to the object, but the object does not say that it must be recycled, and that the object's recycling is only related to memory. As long as the JVM is not ready to run out of memory, he will not waste time on garbage collection.

42.list,set,map whether all three inherit from collection interface
List,set Yes, but map is not, because map is a container of key-value pairs mappings, and list and set have essential differences, list of the linear structure of the container

43. Describe the storage performance and characteristics of arraylist,vector,linkedlist
Both ArrayList and vectors store data in arrays, adding and inserting elements when the number of elements is greater than the number of stores, allowing the element to be indexed directly by ordinal, but inserting elements involves memory operations such as element movement, so queries are fast and insert slowly, where vectors use the thread lock mechanism, It ensures that threads are safe while reducing their own performance, so they are eliminated gradually. While Linklist uses the two-way linked list implementation of the storage, indexed by the number of forward or backward traversal, but when inserting data as long as the record item before and after the item can be, so the retrieval efficiency is low but the insertion efficiency is very high, when the need for a large number of data inserted in the time we should use it. Similarly, LinkedList is not guaranteed to be thread-safe, and we want to ensure thread safety by using some special methods to convert it into a thread-safe container.

The difference between 44.collection and collections
Collection is a collection of interfaces, and collections is a tool class that provides a range of methods to assist with container operations, including retrieval of containers, ordering, threading, and so on.

What are the characteristics of the 45.list,set,map when the elements are stored between them.
The list accesses elements using a particular index, can have duplicate elements, and set cannot be used to store duplicate elements (which are distinguished by the Equals method), which is a pair of pairs of key-value pairs mapped, and the mappings can be one-to-many.

How 46.TreeMap and TreeSet compare element sizes when sorting.
The two themselves are required to store the object must implement the comparable interface, this interface provides a CompareTo () method, when inserting the element will be called back to the method to compare the size of the element.
We've also talked about another way of creating a collection object, the TreeSet object, by passing in a comparator () object and implementing his compare () method in the form of a static inner class that specifies the order within the method.

What is the actual difference between the sleep () method inside the 47.Tread class and the Wait () method inside the object to allow the thread to pause execution?
Sleep is a static method inside the thread, and invoking this method causes the current thread to pause for a period of time, giving the CPU to another thread, but the lock of the object remains and executes after the end of the sleep time, but the wait method is the method of object. The wait method of the calling object causes the thread to discard the current lock and let the thread temporarily stop executing, entering the object's waiting pool, and only the Notify method of the calling object wakes the thread in the pool and gets the lock back into the operational state. That means one has a time limit and the other will sleep forever if you don't wake him up.

48. What is the difference between the sleep () method of a thread and the yield () method?
When the sleep method is called, it goes directly to the sleeping state and throws the thread into a blocking state, throwing an exception. Yield (), also known as thread concessions, will only give the opportunity to run for a thread of the same rank or higher than its own, and is not insured, and this method does not throw any exceptions.

How to use 49.synchronized keywords?
The synchronized keyword represents synchronization, and you can mark objects or methods as synchronous to achieve mutually exclusive access to objects and methods, and to some extent avoid thread-safety issues.

50. How do I start a thread?
The call thread's Start method can start a thread, and invoking the thread object's Run method is definitely not a startup thread, but a normal method of invoking a class.

51. How is serialization implemented in Java? What's the point?
Serialization is a mechanism used to deal with object flow, so-called object flow is the content of the object is streamed, can be streamed after the object read and write operations, can also be the flow of the object transfer between the network, Serialization is to solve the various problems that occur after the object stream reads and writes (if the data is not serialized, the problem of chaos), want to let a class to implement serialization needs to implement the Serializable interface, this interface is an identity interface, that is, this interface is an empty interface, Just to identify that the object can be serialized, it can be persisted for data persistence, and can be used for deep cloning of objects.

There are several types of streams in 52.java
Byte stream and character stream, all streams are transformed by these two streams to be packaged. The streaming unit of a byte stream is a single character, and the stream unit of the character stream is a single char.

How many types of 53.xml documents are there? What are the essential differences between them? What are the different ways to parse an XML document?
The definition of XML document is divided into DTD constraint definition and schema constraint definition. Both are constraint definitions, the essential difference is that the schema itself is also an XML file, can be parsed by the XML parser, the constraint ability is better than the DTD constraints. The parsing of XML is generally divided into two kinds of Dom parsing and sax parsing, sax parsing is a lightweight parsing, reading mechanism is read by line, so it can ignore the impact of memory, and the reading efficiency is very high, Dom parsing in processing large files, performance degradation is very much, This is because the read mechanism requires the XML file to be read in memory first, but his advantage is that it can be randomly read and can change the content at read time, which cannot be done by sax parsing.

54. Explain the JDBC Operational database steps
1. Load Driver
2. Get the linked object
3. Obtain the corresponding statement object according to the link
4. Using the statement object to execute SQL statements
5. If the finished statement has a result set, the corresponding result set of the processor
6. Close Resources

What's the difference between 55.statement and preparestatement, which performance is better
The 1.preparestatement interface represents this precompiled statement, and his advantage is the ability to reduce SQL compilation errors and increase the security of SQL (reducing the likelihood of SQL injection attacks)
SQL statements in 2.preparestatement are available with parameters, avoiding concatenation of strings
3. Preparestatement has a natural performance advantage in batch SQL statements or frequent execution of the same query, eliminating the ability to compile and generate execution time again.

56. What does a connection pool do when compiling a database?
Because of the huge resources that are required to create the connection and release the connection, especially if the database server is not local, we can create several connections in the connection pool in advance to improve the performance of the system when accessing the database, get it directly from the connection pool when the link is needed, and return the connection pool instead of closing at the end. Avoids the overhead of frequent creation and release of connections, which is typically a space-for-time strategy that wastes space storage but greatly reduces the time it takes to create and release connections.

57. What does the acid of a transaction mean?
Refers to the four characteristics of a transaction
Atomic atomicity: All operations in a transaction either do it all or do nothing, and any failure of one operation will cause the failure of all transactions
Consistent consistency: The state of the system is consistent after the end of the transaction
Isolated isolation: Things that are executed concurrently cannot see each other's middle state
Durable persistence: All changes made after the transaction is completed will be persisted, even if a catastrophic failure has occurred.

58. What is data dirty read, Phantom Read, non-repeatable read?
Dirty read: The rollback of a transaction is performed after reading to the modified but uncommitted transaction content. In other words, a dog forced to modify the data when the change is not correct and then perform a rollback, you read the changes he made before the rollback of dirty data.
Non-repeatable reads: The contents of the two reads are inconsistent. In other words, the content you read is submitted and modified, and you read it again and it is inconsistent.
Phantom reading: refers to two reads when the content read increased, that is, read the first time after the other people inserted new content, as if they read the illusion.

59. What is the relationship between threads and processes
Thread: is the smallest unit that the program runs.
Whenever a Java program runs, the JVM creates a main thread for the application, and the main thread's task is to complete the code in the Main method.
Process: is the smallest unit of memory allocation
The process of the JVM, the main method is a thread, so we can only manipulate threads and not manipulate processes, because the JVM is just a process, and a process can have multiple threads.

Java Basics Quiz

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.