java题目汇总(1)

java题目汇总(1)

1.** 阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()**

**package NowCoder;**

`class Test {

public static void hello() {

System.out.println(“hello”);

}

}

public class MyApplication {

public static void main(String[] args) {

Test test=null;

test.hello();

}

}

A 能编译通过,并正确运行

B 因为使用了未初始化的变量,所以不能编译通过

C 以错误的方式访问了静态方法

D 能编译通过,但因变量为null,不能正常运行

正确答案:A

知识: static所修饰的方法或者属性不属于当前类,直接可由类本身调用

  1. 在使用super和this关键字时,以下描述正确的是()

A 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一

行,否则编译不通过

B super()和this()不一定要放在构造方法内第一行

C this()和super()可以同时出现在一个构造函数中

D this()和super()可以在static环境中使用,包括static方法和static语句块

正确答案:A

知识: super在子类构造方法中必须载第一行,否则编译报错

this()调用也必须在构造方法中的第一行,否则编译报错

所以this() 和 super() 不能出现在同一隔构造函数中

  1. 如下代码的输出结果是什么?

public class Test {

public int aMethod(){

static int i = 0;

i++;

return i;

}

public static void main(String args[]){

Test test = new Test();

test.aMethod();

int j = test.aMethod();

System.out.println(j);

}

}

A 0

B 1

C 2

D 编译失败

正确答案:D

知识: 方法内部不能定义static变量。因为static所修饰的变量为静态变量,与类一起加载,先与对象的创建,而非static修饰的方法虽对象的创建而加载。

  1. 有以下代码片段:

String str1=“hello”;

String str2=“he”+ new String(“llo”);

System.out.println(str1==str2);

请问输出的结果是:

A true

B 都不对

C null

D false

正确答案:D

知识: 字符串拼接的相关结论:

1.常量与常量的拼接结果在常量池,且常量池中不会存在相同的常量

2.只要其中有一个是变量,结果就在堆中

3.如果拼接的结果调用intern()方法,返回值就在常量池中
对 intern() 的理解

如:

        String s1 = "abc";
        String s2 = "def";

        String s3 = "abc" + s2;
        String s4 = s1 + "def";
        String s5 = s1 + s2;
        String s6 = "abcdef";
        String s7 = "abc" + "def";

        System.out.println(s3 == s4);//false
        System.out.println(s3 == s5);//false
        System.out.println(s6 == s7);//true
        System.out.println(s6 == s4);//false

        String s8 = s5.intern();//返回值得到的s8使用的常量池中已经存在的数据地址
        System.out.println(s8 == s6);//true
  1. java语言的下面几种数组复制方法中,哪个效率最高?

A for 循环逐一复制

B System.arraycopy

C Array.copyOf

D 使用clone方法

正确答案:B

知识

System.arraycopy 方法是native所修饰的本地方法,底层是直接调用c语言,速度相对较快

如果我们看源码发现Array.copyOf底层调用的也是 System.arraycopy

clone方法比Array.copyOf稍慢

下列代码执行结果为()

public static void main(String args[])throws InterruptedException{

Thread t=new Thread(new Runnable() {

public void run() {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

throw new RuntimeException(e);

}

System.out.print(“2”);

}

});

t.start();

t.join();

System.out.print(“1”);

}

A 21

B 12

C 可能为12,也可能为21

D 以上答案都不对

正确答案:A

知识: thread 类的t.join 方法会结束当前的线程对cpu的控制,然后将t占据cpu控制,直到t线程执行完

  1. 下列Java代码中的变量a、b、c分别在内存的存储区存放。

class A {

private String a = “aa”;

public boolean methodB() {

String b = “bb”;

final String c = “cc”;

}

}

A 堆区、堆区、堆区

B 堆区、栈区、堆区

C 堆区、栈区、栈区

D 堆区、堆区、栈区

E 静态区、栈区、堆区

F 静态区、栈区、栈区

正确答案:C

知识: 记住一个原则即可:方法体中的引用变量和基本类型的变量都在栈上,其他都在堆上。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值