本篇续接《2023年11月15号期中测验判断题(Java)》->传送门
以下程序运行结果是
- public class Test extends Father{
- private String name="test";
- public static void main(String[] args){
- Test test = new Test();
- System.out.println(test.getName());
- }
- }
- class Father{
- private String name="father";
- public String getName() {
- return name;
- }
- }
A. father
B. 编译出错
C. test
D. 运行出错,无输出
正确答案:A
解释:
本题主要考查继承的重写(override)知识点,详见此篇标题五->传送门
其中,Father是父类,Test继承Father,也就继承了其中的getName方法,但是继承后并未重写该方法,因此用Test类示例化的对象调用getName方法时本质上调用的是未重写的Father类中的方法,也就获取到Father类中的name变量的值,即"father"。
If a method defined in a derived class has the same name, return type and parameters with the base one, we call this situation as: (____)
A. overload
B. override
C. inheritance
D. construction
正确答案:B
解释:
如下代码的输出是( )。
- public class Test {
- public static void main(String[] args) {
- new Person().printPerson();
- new Student().printPerson();
- }
- }
-
- class Student extends Person {
- private String getInfo() {
- return "Student";
- }
- }
-
- class Person {
- private String getInfo() {
- return "Person";
- }
-
- public void printPerson() {
- System.out.println(getInfo());
- }
- }
A. Person Person
B. Person Student
C. Stduent Student
D. Student Person
正确答案:A
解释:
设类B是类C的父类,下列声明对象x1的语句中不正确的是?
A. B x1 = new B( );
B. B x1=new C( );
C. C x1 = new C( );
D. C x1=new B( );
正确答案:D
解释:
如下在一个Java抽象类中对抽象方法的声明哪个是正确的?
A. public abstract method();
B. public abstract void method();
C. public void abstract method();
D. public abstract void method() {}
正确答案:B
解释:
有如下程序代码,程序执行的结果是:( )。
- class Base {
- static void test() {
- System.out.println("Base.test()");
- }
- }
-
- class Child extends Base {
- static void test() {
- System.out.println("Child.test()");
- Base.test(); // Call the parent method
- }
- }
-
- public class Main {
- public static void main(String[] a) {
- Child.test();
- }
- }
A.
Child.test() Base.test()B.
Base.test() Base.test()C.
Base.test() Child.test()D.
Runtime error. Cannot override a static method by an instance method
正确答案:A
解释:
有如下程序代码,执行的结果是( )。
- class Father {
- int a = 100;
- int b = 200;
-
- public void print() {
- System.out.println(a + " " + b);
- }
- }
-
- class Child extends Father {
- int b = 300;
- int c = 400;
-
- public void print() {
- System.out.println(a + " " + b + " " + c);
- }
-
- public void printExtend() {
- System.out.println(c);
- }
-
- }
-
- public class Main {
- public static void main(String[] a) {
- Father obj=new Child();
- System.out.println(obj.a+" "+obj.b);
- obj.print();
- }
- }
A.
100 200 100 200 400B.
100 300 100 300 400C.
100 300 100 200 400D.
100 200 100 300 400
正确答案:D
解释:
有如下代码,程序执行结果是:( )。
- abstract class Person {
- public abstract void eat();
- }
- public class Main {
- public static void main(String[] a) {
-
- Person p = new Person() {
- public void eat() {
- System.out.println("eat something");
- }
- };
- p.eat();
- }
- }
A. 空字符串
B. 编译错误
C. eat something
D. 运行错误
正确答案:C
解释:
父类的引用指向自己子类的对象是()的一种体现形式。
A. 封装
B. 继承
C. 多态
正确答案:C
解释:
请问以下哪个程序代码体现了对象之间的 IS-A关系?
A.
public interface Color { } pulic class Shape { private Color color; }B.
interface Component { } public class Container implments Component { private Component[] children; }C.
public class Species { } public class Animal { private Species species; }
正确答案:B
解释:
给出以下Java程序代码,请问运行结果是()?
interface Base { int k = 0; } public class Test implements Base { public staic void main(Strin[] args){ int i; Test obj = new Test(); i = obj.k; i = Test.k; i = Base.k; System.out.println(i); } }
A. 无内容输出。
B. 代码编译失败。
C. 代码输出0
D. 代码输出1
正确答案:C
解释:
可以用于在子类中调用被重写父类方法的关键字是()
A. this
B. implements
C. extends
D. super
正确答案:D
解释:
Java中( ) 。
A. 一个子类可以有多个父类,一个父类也可以有多个子类
B. 一个子类可以有多个父类,但一个父类只可以有一个子类
C. 一个子类可以有一个父类,但一个父类可以有多个子类
D. 上述说法都不对
正确答案:C
解释:
将以下哪种方法插入行3是不合法的。
- public class Test1{
- public float aMethod(float a,float b){ }
- }
A. public float aMethod(float a, float b,float c){ }
B. public float aMethod(float c,float d){ }
C. public int aMethod(int a, int b){ }
D. private float aMethod(int a,int b,int c){ }
正确答案:B
解释:
若A1、A2为已定义的接口 ,以下接口定义中没有语法错误的是( ) 。
A. interface B { void print() { } }
B. abstract interface B { void print() }
C. abstract interface B extends A1,A2 { abstract void print(){ };}
D. interface B { void print();}
正确答案:D
解释:
以下关于继承的叙述正确的是( )。
A. 在Java中类只允许单一继承
B. 在Java中一个类只能实现一个接口
C. 在Java中一个类不能同时继承一个类和实现一个接口
D. 在Java中接口只允许单一继承
正确答案:A
解释:
下面说法不正确的是( )
A. 一个子类的对象可以接收父类对象能接收的消息;
B. 当子类对象和父类对象能接收同样的消息时,它们针对消息产生的行为可能不同;
C. 父类比它的子类的方法更多;
D. 子类在构造函数中可以使用super( )来调用父类的构造函数;
正确答案:C
解释:
下面是People和Child类的定义和构造方法,每个构造方法都输出编号。在执行new Child("mike")的时候都有哪些构造方法被顺序调用?请选择输出结果 ( )
class People {
String name;
public People() {
System.out.print(1);
}
public People(String name) {
System.out.print(2);
this.name = name;
}
}
class Child extends People {
People father;
public Child(String name) {
System.out.print(3);
this.name = name;
father = new People(name + ":F");
}
public Child(){
System.out.print(4);
}
}
A. 312
B. 32
C. 432
D. 132
正确答案:D
解释:
下面哪个对类的声明是错误的?
A. class MyClass extends MySuperClass1, MySupperClass2 {}
B. public class MyClass{}
C. abstract class MyClass implements YourInterface1, Youriterface2 {}
D. private class MyClass {}
E. class MyClass extends MySuperClass implements YourInterface {}
正确答案:A
解释:
在Java中,能实现多重继承效果的方式是( )。
A. 接口
B. 继承
C. 内部类
D. 适配器
正确答案:A
解释:
下列选项中,用于定义接口的关键字是( )。
A. interface
B. implements
C. abstract
D. class
正确答案:A
解释:
对于下列代码:
- class Parent {
- public int addValue( int a, int b) {
- int s;
- s = a+b;
- return s;
- }
- }
- class Child extends Parent { }
下述哪个方法可以加入类Child?
A. int addValue( int a,int b ){// do something...}
B. public void addValue (int a,int b ){// do something...}
C. public int addValue( int a ){// do something...}
D. public int addValue( int a,int b )throws MyException {//do something...}
正确答案:C
解释: