java笔试题

请指出下面程序的运行结果(62)

public class Test {

    public static void main(String[] args) {
        System.out.println(test());
    }

    public static int test() {
        try {
            return 2;
        } catch (Exception e) {
            return 4;
        } finally {
            System.out.print("6");
        }

    }
}
// 62

java中,下面关于this()和super()说法正确的是? D

A、能够成功调用this(xxx,xxx),则类不能有无参数构造函数
B、在构造方法内super()必须放第一行,this()不需要
C、super()属于类级静态方法,而this()属于对象方法
D、父类没有无参数构造函数时,则在子类构造函数中必须显示调用super
E、 以上均无正确答案
解析:A:因为你就不可能成功调用this(xxx,xxx).B.super()和this()都必须放到第一行才行。两者的使用方式是一样的。C.我也不知道它想要表达啥呢。D,经过自己的实验,是对的。

下列代码输出为(D )

注意这道题目自己选错了,主要再考java是值传递不是引用传递。
A、aaa, bbb
B、aaa, ccc
C、ccc, aaa
D、aaa, aaa
E、 以上均无正确答案

public class ReferencesTest {
    static class Student {
        private String name;
    }

    public static void main(String[] args) {
        Student studentA = new Student();
        studentA.name = "aaa";
        Student studentB = new Student();
        studentB.name = "bbb";
        setName(studentA.name, "ccc");
        setName(studentB, "aaa");
        System.out.println(studentA.name + ", " + studentB.name);
    }

    private static void setName(String name, String newName) {
        name = newName;
    }

    private static void setName(Student student, String newName) {
        student.name = newName;
    }

}

image
image
image
image
image

静态代码块输出问题

public class Test {

int i = 0;

static {
    System.out.println("static code");
}

{
    System.out.println("code");
}

public void setValue(int i){
    this.i= i;
}

public static void main(String[] args) {
   Test test = new Test();
   test.setValue(2);
    System.out.println(test.i);
}

}
output:
static code
code
2

posted on   ~码铃薯~  阅读(34)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2019-11-16 还有很多问题
2019-11-16 idea启动项目连接mysql数据库后台报duplicate name异常
2019-11-16 讨论SQL语句中主副表之间的关系
2019-11-16 前端知识-控制div标签的显示与隐藏
2019-11-16 前端知识--控制input按钮的显示与隐藏
2019-11-16 前端知识--控制input按钮的可用和不可用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示