Java face question

Source: Internet
Author: User

1. Can I include more than one class (not an inner class) in a ". Java" source file? What are the restrictions?
A: Yes, but there can only be one public class and if there is a public class, the name of the file should be the same as the name of the class.
2. Does Java have goto?
A: Reserved words in Java are not currently used in Java
3. Tell me the difference between & and &&.
Both:& and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire operation result is true, as long as the one is false and the result is false
&& also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for an if (Str!=null&&!str.equal (")) expression, when STR is NULL, The following expression does not execute, so the nullpointereeception will not appear if you change && to & Nullpointereeception exception will be thrown. if (x==3&++y>0) Y will grow if it is && does not grow
& can also be used as an operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and typically uses 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, such as,0x31& The result of 0x0f is 0x01
Both have common ground, and each has its own special place, such as the above example
4. How do I jump out of the current multiple nesting loops in Java?
A: In Java, to jump out of multiple loops, you can define a label in front of the outer loop statement, and then use a labeled break statement in the code of the inner loop body to 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;
}
}
But I personally think that this is usually not the way to use the label, but let the result of the outer loop condition expression can receive the control of the inner Loop body code, for example, to find a number in a two-dimensional array.
int arr[][]={{1,2,2},{2,2,5},{4,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 function on a byte, can it function on a long string?
A: in switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant (larger font), the integer expression can be an int primitive type or an integer wrapper type, because, Byte,short,char can be implicitly converted to int, so, These types, as well as these types of wrapper types, are also available. Obviously, long and string types do not conform to the syntax of switch, and cannot be implicitly converted to int types, so they cannot be used in Swtich statements.
6, short S1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + = 1; what's wrong?
Answer: for short S1 = 1; S1 = s1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result is of type int, and when assigned to the short type S1, the compiler reports an error that requires a cast type. for short S1 = 1; S1 + = 1; since + = is a Java-language-defined operator, the Java compiler will treat it specially, so it compiles correctly.
7. Can I store a Chinese character in char type?
Answer: No
Char occupies only one byte.
Chinese characters are typically two bytes, and can be represented by using a char array.
For example
Char a[5]= "You"
8. Use the most efficient method to calculate 2 times 8.
A: Shift method 2 to the left 3 is multiplied by 8
9, please design a 10 billion of the calculator
For:
10. When a variable is decorated with the final keyword, is the reference immutable or does the referenced object not change?
For:
When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and the contents of the object to which the reference variable is pointing can be changed. For example, for the following statement:
Final StringBuffer a=new StringBuffer ("immutable");
Executing the following statement will report a compile-time error:
A=new StringBuffer ("");
However, the following statements can be executed by compiling:
A.append ("broken!");
When someone defines a method's parameters, you might want to block the method from modifying the passed-in Parameter object in the following form:
public void method (final stringbuffer param)
{
}
In fact, this cannot be done, and the following code can still be added inside the method to modify the Parameter object:
Param.append ("a");
11. What is the difference between "= =" and Equals method?
A: = = does compare the reference, equals compares the reference point to the content
12. What is the difference between a static variable and an instance variable?
A: Static variables have a static difference between the class variable and the instance variable is that the class variable is common to all objects, where one object changes his value, the other object gets the changed result, and the instance variable belongs to the object's private, one object changes its value and does not affect other objects.
13. Is it possible to make a call to a non-static method from within a static method?
No way
You can understand that.
Static modified methods can be called directly with the class name
A non-static decorated method must use the class new out object to invoke the
When we call directly with the class name, the object of this class may not be new.
A null pointer exception occurs if a non-static method is called in the static method.
Of course, this error is not compiled by
14, the difference between integer and int
A: An integer is an object that defines a reference to an integer object address, = = compares the physical address of an object in memory. So not to wait.
The variable defined by int is a reference to the value. (range is positive or negative 2^31)
15, Math.Round (11.5) how much? Math.Round (-11.5) how much?
Answer: Math.Round () is a rounded function, so math.round (11.5) is a, math.round (-11.5) is-11
16. What's wrong with the code below?
For:
17, please say the scope public,private,protected, and do not write when the difference
A: The difference is as follows:


Scope Current class same package descendant class other package


Public√√√√


Protected√√√x


Friendly√√xx


Private√xxx


Default to friendly when not written
18, the difference between overload and override. Can the overloaded method change the type of the return value?
A: The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.
19. Can the constructor constructor be override?
A: The constructor constructor cannot be inherited, so overriding cannot be overridden, but can be overloaded overloading
20. Can interfaces inherit interfaces? Can an abstract class implement (implements) interfaces? Can an abstract class inherit a concrete Class (Concreteclass)? Is there a static main method in the abstract class?
A: 1. Interfaces can inherit interfaces. But to use extends~ instead of implements such as: interface A{}interface B extends a{}
2. An abstract class can implement an interface. For example, the Abstractcollection class in Java.util is the collection interface implemented.
3. Abstract classes can inherit all the problems of the following code description of the entity class without error: interface MyInterface {}interface Anotherinterface extends MyInterface {}class Entityclass {}abstract Class AbstractClass extends Entityclass implements MyInterface {}
The main methods are static, and the methods in the abstract class can be abstract or concrete.
21, when writing the Clone () method, usually have a line of code, what is it?
A: Clone has a default behavior, Super.clone (), because the members of the parent class are first copied into place, and then the members are copied.
22. What are the aspects of object-oriented features
Answer: encapsulation, inheritance, polymorphism
23. What is the mechanism for polymorphism in Java?
A: A reference variable that is defined by a parent class or interface can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound at run time, that is, the method that refers to the specific instance object that the variable points to, that is, the method of the object that is running in memory, not the method defined in
25, the abstract method can be static at the same time, whether it can be native at the same time, whether it can be synchronized at the same time?
A: Abstract method can not be static, because the abstraction is to be a quilt class implementation, and static and sub-class is not related!


The native method means that the method is implemented with another platform-dependent programming language, and there is no problem with the implementation of the quilt class, so it cannot be abstract and cannot be mixed with abstract.

Java face question

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.