笔试基础
1.下面中哪两个可以在A的子类中使用:( )
class A {
protected int method1 (int a, int b) {
return 0;
}
}
A. public int method 1 (int a, int b) { return 0; }
B. private int method1 (int a, int b) { return 0; }
C. private int method1 (int a, long b) { return 0; }
D. public short method1 (int a, int b) { return 0; }
解答:AC
主要考查子类重写父类的方法的原则
B,子类重写父类的方法,访问权限不能降低
C,属于重载
D,子类重写父类的方法 返回值类型要相同或是父类方法返回值类型的子类
2.Abstract method cannot be static. True or False ?
A True
B False
解答:A
抽象方法可以在子类中被重写,但是静态方法不能在子类中被重写,静态方法和静态属性与对象是无关的,只与类有关,这与abstract是矛盾的,所以abstract是不能被修饰为static,否则就失去了abstract的意义了
3.What will be the output when you compile and execute the following program.
class Base
{
void test() {
System.out.println(“Base.test()”);
}
}
public class Child extends Base {
void test() {
System.out.println(“Child.test()”);
}
static public void main(String[] a) {
Child anObj = new Child();
Base baseObj = (Base)anObj;
baseObj.test();
}
}
Select most appropriate answer.
A Child.test()
Base.test()
B Base.test()
Child.test()
C Base.test()
D Child.test()
解答:D
测试代码相当于:Base baseObj = new Child();父类的引用指向子类的实例,子类又重写了父类
的test方法,因此调用子类的test方法。
4.What will be the output when you compile and execute the following program.
class Base
{
static void test() {
System.out.println(“Base.test()”);
}
}
public class Child extends Base {
void test() {
System.out.println(“Child.test()”);
Base.test(); //Call the parent method
}
static public void main(String[] a) {
new Child().test();
}
}
Select most appropriate answer.
A Child.test()
Base.test()
B Child.test()
Child.test()
C Compilation error. Cannot override a static method by an instance method
D Runtime error. Cannot override a static method by an instance method
解答:C
静态方法不能在子类中被重写
下列说法正确的有()
A. class中的constructor不可省略
B. constructor必须与class同名,但方法不能与class同名
C. constructor在一个对象被new时执行
D. 一个class只能定义一个constructor
答案:C
下面程序运行得结果是什么
class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println("I'm A class"); } static { System.out.println("static A"); } } public class HelloB extends HelloA { public HelloB() { System.out.println("HelloB"); } { System.out.println("I'm B class"); } static { System.out.println("static B"); } public static void main(String[] args) { new HelloB(); } }
答案:
static A static B I'm A class HelloA I'm B class HelloB
解析:说实话我觉得这题很好,考查静态语句块、构造语句块(就是只有大括号的那块)以及构造函数的执行顺序。
对象的初始化顺序:(1)类加载之后,按从上到下(从父类到子类)执行被static修饰的语句;(2)当static语句执行完之后,再执行main方法;(3)如果有语句new了自身的对象,将从上到下执行构造代码块、构造器(两者可以说绑定在一起)。
下面稍微修改下上面的代码,以便更清晰的说明情况。
class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println("I'm A class"); } static { System.out.println("static A"); } } public class HelloB extends HelloA { public HelloB() { System.out.println("HelloB"); } { System.out.println("I'm B class"); } static { System.out.println("static B"); } public static void main(String[] args) { System.out.println("-------main start-------"); new HelloB(); new HelloB(); System.out.println("-------main end-------"); } }
结果
static A static B -------main start------- I'm A class HelloA I'm B class HelloB I'm A class HelloA I'm B class HelloB -------main end-------
抽象方法可以被static修饰嘛?
A True
B False
解答:B
抽象方法可以在子类中被重写,但是静态方法不能在子类中被重写,静态方法和静态属性与对象是无关的,只与类有关,这与abstract是矛盾的,所以abstract是不能被修饰为static,否则就失去了abstract的意义了
下列哪个叙述是正确的()
A.final类可以有子类。
B.abstract类中只可以有abstract方法。
C.abstract类中可以有非abstract方法,但该方法不可以用final修饰。
D.不可以同时用final和abstract修饰同一个方法。
E.允许使用static修饰abstract方法。
答案:D
用final修饰的类是不允许被继承的,A错误
abstract类中也可以有普通成员方法,B错误
abstract类中的非abstract方法是可以用final修饰的,而abstract方法是不可以被final修饰的,C错误
D同上,正确
不允许使用static修饰abstract方法,E错误。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)