Java Foundation Interview (i)

Source: Internet
Author: User
Tags bitwise

1. Can I include more than one class (not an inner class) in a ". Java" source file? What are the restrictions?

There can be multiple classes, but only one public class, and the class name of public must match the file name.

2. Does Java have goto?

Reserved words in Java are not currently used in Java. L

3, talk about 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 result is true, otherwise the result is false if one of the parties 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.equals (s)) expression, when STR is NULL, The following expression does not execute, so the nullpointerexception will not appear if you change && to & NullPointerException exception will be thrown. if (x==33 &++y>0) y grows, if (x==33 && ++y>0) does not grow

& can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 & The result of 0x0f is 0x01.

4. How do I jump out of the current multiple nesting loops in Java?

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:

for (int i=0;i<10;i++) {
for (intj=0;j<10;j++) {
System.out.println ("i=" + i + ", j=" + j);
if (j = = 5) break OK;
}
}

In addition, I personally do not usually use the label this way, but let the outer loop condition expression results can be controlled by the inner Loop body code, for example, to find a number in a two-dimensional array.

int arr[][] ={{1,2,3},{4,5,6,7},{9}};

Boolean found = false;

for (int i=0;i<arr.length&&!found;i++) {

for (intj=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?

In switch (e), E can only be an integer expression or enumeration constant (larger font), an integer expression can be an int primitive type or an integer wrapper type, since Byte,short,char can be implicitly converted to int, 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 is the int type, and the left side of the equals sign is the short type, so a strong turn is required) 1 + 1; what's wrong? Short S1 = 1; S1 + = 1; what's wrong? (No mistake)

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?

Char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Supplemental Note: Unicode encoding consumes two bytes, so variables of type char are also two bytes.

8. Use the most efficient method to calculate 2 times 8.

2<< 3, (three-bit left) because a number is shifted to the left N-bit, which is equal to 2 of the N-square, then a number multiplied by 8 to move it to the left 3 bits, and the bit operation CPU directly supported, the most efficient, so, 2 times 8 is the most efficient method is 2<< 3.

9. When a variable is decorated with the final keyword, is the reference immutable or does the referenced object not change?

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:

Finalstringbuffer 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");

10, what is the difference between a static variable and an instance variable?

The difference between the syntax definitions: Static variables are added with the static keyword, and the instance variable is not added before.

The difference when the program runs: instance variables belong to an object's properties, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used. In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name.

For example, for the following program, no matter how many instance objects are created, only one Staticvar variable is always assigned, and each instance object is created, the Staticvar adds 1; However, each instance object is created, a Instancevar is assigned. That is, multiple instancevar may be assigned, and each Instancevar value is only added 1 times.

public class varianttest{

publicstatic int staticvar = 0;

Publicint Instancevar = 0;

Publicvarianttest () {

staticvar++;

instancevar++;

System.out.println (Staticvar +instancevar);

}

}

Online sorting out some of the major companies commonly used to interview pen questions, for everyone in the daily spare time to learn a few of the topics, the cumulative, wait until the interview, all the time, the interview will be the natural ease!

Java Foundation Interview (i)

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.