Java Interview Questions - Basics 1

Java Interview Questions - Basics 1

2017-09-02 Amuxia   Java   Companionship

Foreword: There are more or less errors or clerical errors in the interview questions released before. Many students have left me a message to correct me. I think I will declare the mistakes under each message after the message is opened. The message function the day before yesterday finally arrived. It turned out that the message can only be posted after the article is opened, which is still too young! Thinking not to mislead the children, I should reorganize the article and correct the wrong points! !


1. Can a ".java" source file include multiple classes (not inner classes)? What are the restrictions?

        There can be multiple classes, but there can only be one public class, and the public class name must be the same as the file name.


2. Does Java have goto?

        Reserved word in java, not used in java now.


3. Talk about the difference between & and &&.

        Both & and && can be used as logical AND operators, representing logical AND (and). When the results of the expressions on both sides of the operator are true, the entire operation result is true. Otherwise, as long as one of the expressions is false, then The result is false.

        && also has the ability to short-circuit, that is, if the first expression is false, the second expression is no longer evaluated, for example, for if(str != null&& !str.equals(s)) expression, when str is When null, the following expression will not be executed, so there will be no NullPointerException. If you change && to &, a NullPointerException will be thrown. If(x==33 &++y>0) y will grow, If(x==33 && ++y>0) will not grow

        & can also be used as a bitwise operator. When the expressions on both sides of the & operator are not of boolean type, & represents a bitwise AND operation. We usually use 0x0f to perform & operation with an integer to get the lowest 4 bits of the integer. Bits, for example, 0x31 & 0x0f result in 0x01.


4. How to jump out of the current multiple nested loop in JAVA?

        In Java, if you want to jump out of multiple loops, you can define a label before the outer loop statement, and then use the break statement with the label in the code of the inner loop body to jump out of the outer loop.

E.g:

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 usually don't use labels, but let the result of the outer loop conditional expression be controlled by the inner loop body code, for example, to find a certain 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、switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

        在switch(e)中,e只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也是可以的。显然,long和String类型都不符合switch的语法规定,并且不能被隐式转换成int类型,所以,它们不能作用于swtich语句中。

switch语句能否作用在String上说错了,Java1.7之后已经支持这种写法了!


6、short s1= 1; s1 = (s1+1是int类型,而等号左边的是short类型,所以需要强转)1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?(没有错)

        对于short s1= 1; s1 = s1 + 1;由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类型s1时,编译器将报告需要强制转换类型的错误。

        对于short s1= 1; s1 += 1;由于 +=是java语言规定的运算符,java编译器会对它进行特殊处理,因此可以正确编译。


7、char型变量中能不能存贮一个中文汉字?为什么?

        char型变量是用来存储Unicode编码的字符的,unicode编码字符集中包含了汉字,所以,char型变量中当然可以存储汉字啦。不过,如果某个特殊的汉字没有被包含在unicode编码字符集中,那么,这个char型变量中就不能存储这个特殊汉字。补充说明:unicode编码占用两个字节,所以,char类型的变量也是占用两个字节。


8、用最有效率的方法算出2乘以8等於几?

        2<< 3,(左移三位)因为将一个数左移n位,就相当于乘以了2的n次方,那么,一个数乘以8只要将其左移3位即可,而位运算cpu直接支持的,效率最高,所以,2乘以8等於几的最效率的方法是2<< 3。


9、使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?

        使用final关键字修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的。例如,对于如下语句:

 finalStringBuffer a=new StringBuffer("immutable");
执行如下语句将报告编译期错误:

a=new StringBuffer("");
但是,执行如下语句则可以通过编译:

a.append(" broken!");

有人在定义方法的参数时,可能想采用如下形式来阻止方法内部修改传进来的参数对象:

public void method(final  StringBuffer param){

}

实际上,这是办不到的,在该方法内部仍然可以增加如下代码来修改参数对象:

        param.append("a");


10,静态变量和实例变量的区别?

        在语法定义上的区别:静态变量前要加static关键字,而实例变量前则不加。

        在程序运行时的区别:实例变量属于某个对象的属性,必须创建了实例对象,其中的实例变量才会被分配空间,才能使用这个实例变量。静态变量不属于某个实例对象,而是属于类,所以也称为类变量,只要程序加载了类的字节码,不用创建任何实例对象,静态变量就会被分配空间,静态变量就可以被使用了。总之,实例变量必须创建对象后才可以通过这个对象来使用,静态变量则可以直接使用类名来引用。

        例如,对于下面的程序,无论创建多少个实例对象,永远都只分配了一个staticVar变量,并且每创建一个实例对象,这个staticVar就会加1;但是,每创建一个实例对象,就会分配一个instanceVar,即可能分配多个instanceVar,并且每个instanceVar的值都只自加了1次。

public class VariantTest{

        publicstatic int staticVar = 0;

        publicint instanceVar = 0;

        publicVariantTest(){

              staticVar++;

              instanceVar++;

              System.out.println(staticVar +instanceVar);

        }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612438&siteId=291194637