LZ名約山炮

博客园 首页 新随笔 联系 订阅 管理

1. 用最有效的的方法算出2称以8等于几

答案:2<<3

2. Math.round(11.5)Math.round(-11.5)的值是多少?

Math.round(11.5)12

Math.round(-11.5)-11

3. 两个对象ab,请问a==ba.equals(b)有什么区别?

a==b:比较对象地址

a.equals(b):如果a对象没有重写过equals方法,效果和==相同,如果重写了就按照重写的规则比较。

4. switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?

答案一:switch可以作用在byte上,不能作用在long上,JDK1.7之后可以作用在String上。

答案二:switch支持的类型byte,short,int,charJDK1.5之后支持枚举,JDK1.7之后支持String类型。

5. char型变量中是否可以存储一个汉字?

能,因为Java一个字符是2个字节,每一个字符使用Unicode编码表示

6. float f=3.4;是否正确,表达式15/2*2的值是多少

答案:不正确,float f = 3.4F;    14

7. 编写代码实现两个变量值交换,int m = 3, n =5;

答案一:

int temp = m;

m = n;

n = temp;

答案二:

m = m + n;

n = m - n;

m = m - n;

答案三:

m = m ^ n;

n = m ^ n;

m = m ^ n;

8. Java的基本数据类型有哪些?String是基本数据类型吗?

基本数据类型有:byte,short,int,long,float,double,char,boolean

String是引用数据类型,不是基本数据类型

9. 数组有没有length()方法?String有没有length()方法?File有没有length()方法?ArrayList有没有length()方法?

数组没有length()方法,但是有length属性。

StringFilelength()方法。

ArrayList没有length()方法,有size()方法获取有效元素个数。

10. String str = new String(hello);创建了哪些对象?

字符串常量池中有一个对象,堆中有一个字符串对象。

11. 如何将String类型转化Number类型?举例说明String str = 123;

答任意一个都对:

Integer num1 = new Integer(str);

int num2 = Integer.parseInt(str);

Integer num3 = Integer.valueOf(str);

12. 以下代码的运行结果:

public static void main(String[] args) {

  char x = 'x';

  int i = 10;

  System.out.println(true? x : i);

  System.out.println(true? 'x' : 10);

}

答案:

120

x

/*

 * 如果其中有一个是变量,按照自动类型转换规则处理成一致的类型;

 * 如果都是常量,如果一个是char,如果另一个是[0~65535]之间的整数按char处理;

 * 如果一个是char,另一个是其他,按照自动类型转换规则处理成一致的类型;

 */

13. 以下代码的执行结果

public static void main(String[] args) {

  int a = 8, = 3;

  System.out.println(a>>>b);

  System.out.println(a>>>b | 2);

}

答案:

1

3

14. 下面程序片段的输出结果是?

public static void main(String[] args) {

  int a = 3;

  int b = 1;

  if(a = b){

    System.out.println("Equal");

  }else{

    System.out.println("Not Equal");

  }

}

答案:编译不通过

15. 执行如下代码后,c的值是多少?

public static void main(String[] args) {

  int a = 0;

  int c = 0;

  do {

    --c;

    a = - 1;

  } while (>= 0);

  System.out.println("c = " + c);

}

答案:c = -1

16. 以下代码的运行结果?

public static void main(String[] args) {

int i=10;

while(i>0){

  i = +1;

  if(i==10){

    break;

  }

}

  System.out.println("i=" + i);

}

答案一:是一个负数,因为i一直累加会超过int的存储范围

答案二:死循环

17. 修正如下代码

下面是一段程序,目的是输出10=,但是不小心代码写错了,现在需要修改代码,使得程序完成功能,但是只能“增加”或“修改”其中“一个”字符,很明显,将i--改为i++,可以完成功能,但是需要修改“两个”字符,所以并不是一个正确的答案?

public static void main(String[] args) {

  int n=10;

  for (int i = 0; < n; i--) {

    System.out.println("=");

  }

}

i<n修改为-i<n

18. 以下代码的运行结果是什么?

public class Test {

  public static boolean foo(char c) {

    System.out.print(c);

    return true;

  }

 

  public static void main(String[] args) {

    int i = 0;

    for (foo('A'); foo('B') && (i < 2); foo('C')) {

      i++;// 1 2

      foo('D');

    }

  }

}

答案:ABDCBDCB

19. 以下代码的执行结果是什么

public static void main(String[] args) {

  int i = 0;

  change(i);

  i = i++;

  System.out.println("i = " + i);

}

public static void change(int i){

  i++;

}

答案:i = 0

20. 以下程序的运行结果:

public static void main(String[] args) {

  String str = new String("world");

  char[] ch = new char[]{'h','e','l','l','o'};

  change(str,ch);

  System.out.println(str);

  System.out.println(String.valueOf(ch));

}

public static void change(String str, char[] arr){

  str = "change";

  arr[0] = 'a';

  arr[1] = 'b';

  arr[2] = 'c';

  arr[3] = 'd';

  arr[4] = 'e';

}

答案:

world

abcde

21. 以下代码的运行结果是:

public static void main(String[] args) {

  Integer i1 = 128;

  Integer i2 = 128;

  int i3 = 128;

  int i4 = 128;

  System.out.println(i1 == i2);

  System.out.println(i3 == i4);

  System.out.println(i1 == i3);

}

答案:

false

true

true

Integeri1i2是对象,他们==比较的是地址。如果-128~127范围,那么使用缓存的常量对象,如果超过这个范围,是新new的对象,不是常量对象

22. 以下代码的运行结果

public static void main(String[] args) {

  double a = 2.0;

  double b = 2.0;

  Double c = 2.0;

  Double d = 2.0;

  System.out.println(a == b);

  System.out.println(c == d);

  System.out.println(a == d);

}

答案:

true

false

true

23. 以下代码的运行结果是?

public class Test {

  int a;

  int b;

  public void f(){

    a = 0;

    b = 0;

    int[] c = {0};

    g(b,c);

    System.out.println(a + " " + + " " + c[0]);

  }

  public void g(int b, int[] c){

    a = 1;

    b = 1;

    c[0] = 1;

  }

  public static void main(String[] args) {

    Test t = new Test();

    t.f();

  }

}

答案:1 0 1

24. 以下代码的运行结果是?

public class Test {

  static int x, y, z;

 

  static {

    int x = 5;

    x--;

  }

 

  static {

    x--;

  }

 

  public static void main(String[] args) {

    System.out.println("x=" + x);

    z--; 

    method();

    System.out.println("result:" + (z + y + ++z));

  }

 

  public static void method() {

    y = z++ + ++z;

  }

}

答案:

x=-1

result:3

25. 以下程序的运行结果是:

public class Test {

  public static void main(String[] args) {

    new A(new B());

  }

}

class A{

  public A(){

    System.out.println("A");

  }

  public A(B b){

    this();

    System.out.println("AB");

  }

}

class B{

  public B(){

    System.out.println("B");

  }

}

答案:

B

A

AB

26. 如下代码是否可以编译通过,如果可以,运行结果是什么?

interface A{

  int x = 0;

}

class B{

  int x = 1;

}

class C extends B implements A{

  public void printX(){

    System.out.println(x);

  }

  public static void main(String[] args) {

    new C().printX();

  }

}

答案:编译错误

System.out.println(x);报错,x有歧义

27. 以下代码的运行结果:

public class Test {

  public static void main(String[] args) {

    Base b1 = new Base();

    Base b2 = new Sub();

  }

}

class Base{

  Base(){

    method(100); //1.base : 100 //2.sub : 100 //3.base : 70

  }

  public void method(int i){

    System.out.println("base : " + i); 

  }

}

class Sub extends Base{

  Sub(){

    super.method(70);

  }

  public void method(int j){

    System.out.println("sub : " + j);

  }

}

答案:

base : 100

sub : 100

base : 70

28. 以下代码的执行过程?

public static void main(String[] args) {

  int test = test(3,5);

  System.out.println(test);

}

 

public static int test(int x, int y){

  int result = x;

  try{

    if(x<0 || y<0){

      return 0;

    }

    result = + y;

    return result;

  }finally{

    result = - y;

  }

}

答案:8

29. 以下代码的运行结果?

public static void main(String[] args) {

  Integer[] datas = {1,2,3,4,5};

  List<Integer> list = Arrays.asList(datas);

  list.add(5);

  System.out.println(list.size());

}

运行异常,不允许添加元素

30. {1}添加什么代码,可以保证如下代码输出100

提示:t.wait()  或  t.jion()  t.yield() 或  t.interrupt()

public class Test {

  public static void main(String[] args) {

    MyThread m = new MyThread();

    Thread t = new Thread(m);

    t.start();

                  {1}               

    int j = m.i;

    System.out.println(j);

  }

}

class MyThread implements Runnable{

  int i;

  public void run(){

    try {

      Thread.sleep(1000);

    } catch (InterruptedException e) {

      e.printStackTrace();

    }

    i=100;

  }

}

答案:t.join()

31.以下代码如何优化

if(username.equals(admin){

  ....

}

答案:

if(admin.equals(username)){

}

posted on 2021-03-25 15:26  LZ名約山炮  阅读(278)  评论(0编辑  收藏  举报