Java interview questions, java questions
1. Can a ". java" source file contain multiple classes (not internal classes )? What are the restrictions?
A: Yes, but there can only be one public class. If there is a public class, the file name must be the same as the class name.
2. Does Java have a goto?
A: Reserved Words in java are not currently used in java.
3. What is the difference between "&" and?
A: Both & and & can be used as operators for logic and, indicating logic and (and). When the expressions on both sides of the operator return true, the entire operation returns true, if one side is false, the result is false.
& It also has the short circuit function, that is, if the first expression is false, the second expression is no longer calculated. For example, if (str! = Null &&! Str. equal ("") expression. If 'str' is null, the following expression is not executed, so NullPointerEeception will not occur. If & is changed to &, an NullPointerEeception exception will be thrown. If (x = 3 & ++ y> 0), y will grow. if yes
& Can also be used as an operator. When the expressions on both sides of the & operator are not of the boolean type, & indicates bitwise and operation. Generally, 0x0f is used to perform & operation with an integer, to obtain the minimum four bits of the integer. For example, the result of 0x31 & 0x0f is 0x01.
The two have both commonalities and special characteristics, as shown in the preceding example.
4. How can I jump out of the current multi-nested loop in JAVA?
A: in java, to jump out of multiple loops, you can define a label before an External Loop statement, and then use the break statement with a label in the code of the in-layer loop body, you can jump out of the outer loop. For example:
OK:
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
System. out. println ("I =" + I + ", j =" + j );
If (j = 5) break OK;
}
}
However, I personally think that the label method is not usually used. Instead, the result of the outer loop condition expression can be controlled by the code of the loop body in the layer. For example, you need to find a number in a two-dimensional array.
Int arr [] [] = {1, 2}, {2, 2}, {4 }};
Boolean found = false;
For (int I = 0; I <arr. length &&! Found; I ++)
{
For (int j = 0; j <arr [I]. length; j ++)
{
System. out. println ("I =" + I + ", j =" + j );
If (arr [I] [j] = 5)
{
Found = true;
Break;
}
}
}
5. Can the switch statement be applied to byte, long, or String?
A: In switch (expr1), expr1 can only be an Integer expression or enumeration constant (larger font). The Integer expression can be an int basic type or an Integer packaging type. Because, byte, short and char can be implicitly converted to int. Therefore, these types and their packaging types are also acceptable. Obviously, the long and String types do not comply with the switch syntax and cannot be implicitly converted to the int type. Therefore, they cannot be applied to swtich statements.
6. short s1 = 1; s1 = s1 + 1; what is the error? Short s1 = 1; s1 + = 1; what is the error?
A: For short s1 = 1; s1 = s1 + 1; because the s1 + 1 operation automatically increases the expression type, the result is int type, when a value is assigned to short type s1, the compiler reports errors that require forced conversion. For short s1 = 1; s1 + = 1; Because ++ = is an operator specified by the java language, the java compiler will perform special processing on it, so it can be compiled correctly.
7. Can a Chinese character be stored in a char variable? Why?
A: No.
Char occupies only one byte.
The Chinese character is usually expressed in two bytes and can be expressed in a char array.
For example
Char a [5] = "you"
8. How many equals 2x8 in the most efficient way?
A: Use the shift method 2 to shift three places left by 8.
9. Design a 10 billion Calculator
A:
10. When a variable is modified using the final keyword, can the reference not be changed, or can the referenced object not be changed?
A:
When you use the final keyword to modify a variable, it is suggested that the variable cannot be changed, and the content in the object to which the referenced variable points can still be changed. For example, for the following statement:
Final StringBuffer a = new StringBuffer ("immutable ");
Run the following statement to report compilation errors:
A = new StringBuffer ("");
However, the following statement can be compiled:
A. append ("broken! ");
When defining the parameters of a method, someone may want to use the following form to prevent the method from modifying the passed parameter object:
Public void method (final StringBuffer param)
{
}
In fact, this cannot be done. In this method, you can add the following code to modify the parameter object:
Param. append ("");
11. What are the differences between the "=" and equals methods?
A: = actually, the reference is compared. equals compares the content pointed to by the reference.
12. What are the differences between static variables and instance variables?
A: The difference between static variables is that class variables and instance variables are shared by all objects. One of them changes its value, other objects get the result after the change, and instance variables belong to the private object. A certain object changes its value without affecting other objects.
13. Can I call non-static methods from inside a static method?
No,
This is understandable.
The static modification method can be called directly using the class name.
Non-static modifier methods can be called only when the new object of the class is used
When we call the class name directly, the class object may not be new.
If a non-static method is called in the static method, a null pointer exception occurs,
Of course, this error will not pass during compilation.
14. Differences between Integer and int
A: Integer is an object. The defined variable references the address of the Integer object. ==, it compares the physical address of the object in the memory. So it does not.
The int variable is the reference of the value. (Range: Positive and Negative 2 ^ 31)
15. How much is Math. round (11.5? Math. round (-11.5) and so on?
A: Math. round () is a rounding function, So Math. round (11.5) is 12, Math. round (-11.5) is-11
16. What is wrong with the following code?
A:
17. indicate the scope of public, private, and protected, and the differences between them when they are not written.
A: The differences are as follows:
Scope: current class, same package, Child class, other package
Public √
Protected √ ×
Friendly √ ××
Private √ ×××
The default value is friendly when no data is written.
18. Differences between Overload and Override. Can the Overloaded method change the type of the returned value?
A: Overriding and Overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the 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, it calls the definition in the subclass. 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, it is called Overloading ). The Overloaded method can change the type of the returned value.
19. Can Constructor be overwritten?
A: Constructor cannot be inherited, so Overriding cannot be overwritten, but Overloading can be overloaded.
20. Can an interface inherit an interface? Can an abstract class implement the (implements) interface? Can an abstract class inherit a specific class (concreteclass )? Can there be static main methods in abstract classes?
Answer: 1. The interface can inherit the interface... but you need to use extends ~ Instead of using implements such as: interface a {} interface B extends {}
2. abstract classes can implement interfaces. For example, the AbstractCollection class in java. util is the implemented Collection interface.
3. abstract classes can inherit all the problems described in the following code of the entity class: interface MyInterface {} interface AnotherInterface extends MyInterface {} class EntityClass {} abstract class AbstractClass extends EntityClass implements MyInterface {}
The main method is static. The methods in the abstract class can be abstract methods or specific methods.
21. When writing the clone () method, there is usually a line of code. What is it?
A: clone has a default action. super. clone () is used because you must first copy the members in the parent class and then copy the members of the parent class.
22. What are the features of object orientation?
Answer: encapsulation, inheritance, and Polymorphism
23. What is the mechanism for implementing polymorphism in java?
A: The reference variables defined by the parent class or interface can point to the subclass or the Instance Object of the specific implementation class, and the method called by the program is dynamically bound at runtime, it refers to the method of referencing the specific instance object pointed to by the variable, that is, the method of the object that is running in the memory, rather than the method defined in the variable type.
25. can abstract methods be both static, native, and synchronized?
A: abstract methods cannot be static, because abstract methods must be implemented by the quilt class, but static and subclass cannot be connected!
The native method indicates that the method must be implemented in another programming language dependent on the platform. There is no problem with the implementation of the quilt class. Therefore, it cannot be abstract or mixed with abstract.
How can I obtain a java interview question?
In addition to Java, the interview process for IT technology is almost the same.
1. Test
During the first interview, most companies will provide you with a test question.
This pen test may be from the Internet or a question modified by the company's technical staff, but it was originally a question on the Internet.
2. machine testing (few small and medium enterprises)
Generally, the test is performed after the test. If you see someone else going to the test, but you are told to go home and wait for a notification, it will basically be okay. The content of the test usually requires you to set up a simple environment, and then perform a login, add, delete, modify, and query on any item.
You can bring your own USB flash drive to speed up the time, but if the U port of the computer is blocked, it will be a tragedy ~
3. Interview
Interview is the most critical step. It can be said that as long as you get this step, your previous interview score will not be helpful. You can choose to stay during the interview, even if your previous stage is not performing well. The interviewer will take your resume to ask you about your personal information and project experience. The most important thing is the project's story. If you are confused, you will be shot on the spot, but remember, you should not have too many on your resume. Otherwise, you may not know what you did before, and you will be refreshed.
Answers to a java interview question
A String constant that appears in the system. The system automatically creates a String object.
That is, you only need to create an object in the form of "aaaaa ".
Then, the new statement is equivalent to creating an object based on the existing object.
Therefore, to save space, you can write String s = "aaaaaaaa ";
In this way, there is only one object.