zoukankan      html  css  js  c++  java
  • 【笔试题】Overriding in Java

    笔试题 Overriding in Java

    Question 1 以下程序的输出结果为( )。

    class Derived {
        protected final void getDetails() {
            System.out.println("Derived class");
        }
    }
    
    public class Test extends Derived {
        protected final void getDetails() {
            System.out.println("Test class");
        }
    
        public static void main(String[] args) {
            Derived obj = new Derived();
            obj.getDetails();
        }
    }
    

    a) Derived class
    b) Test class
    c) Runtime error
    d) Compilation error

    参考答案

    ``` d ```

    Question 2 以下程序的输出结果为( )。

    class Derived {
        public void getDetails(String temp) {
            System.out.println("Derived class " + temp);
        }
    }
    
    public class Test extends Derived {
        public int getDetails(String temp) {
            System.out.println("Test class " + temp);
            return 0;
        }
    
        public static void main(String[] args) {
            Test obj = new Test();
            obj.getDetails("GFG");
        }
    }
    

    a) Derived class GFG
    b) Test class GFG
    c) Compilation error
    d) Runtime error

    参考答案

    ``` c ```

    Question 3 以下程序的输出结果为( )。

    class Derived {
        public void getDetails() {
            System.out.println("Derived class");
        }
    }
    
    public class Test extends Derived {
        protected void getDetails() {
            System.out.println("Test class");
        }
    
        public static void main(String[] args) {
            Derived obj = new Test(); // line xyz
            obj.getDetails();
        }
    }
    

    a) Test class
    b) Compilation error due to line xyz
    c) Derived class
    d) Compilation error due to access modifier

    参考答案

    ``` d ```

    Question 4 以下程序的输出结果为( )。

    import java.io.IOException;
    
    class Derived {
        public void getDetails() throws IOException { // line 23
            System.out.println("Derived class");
        }
    }
    
    public class Test extends Derived {
        public void getDetails() throws Exception { // line 24
            System.out.println("Test class");
        }
    
        public static void main(String[] args) throws IOException { // line 25
            Derived obj = new Test();
            obj.getDetails();
        }
    }
    

    a) Compilation error due to line 23
    b) Compilation error due to line 24
    c) Compilation error due to line 25
    d) All the above

    参考答案

    ``` b ```

    Question 5 以下程序的输出结果为( )。

    class Derived {
        public void getDetails() {
            System.out.printf("Derived class ");
        }
    }
    
    public class Test extends Derived {
        public void getDetails() {
            System.out.printf("Test class ");
            super.getDetails();
        }
    
        public static void main(String[] args) {
            Derived obj = new Test();
            obj.getDetails();
        }
    }
    

    a) Test class Derived class
    b) Derived class Test class
    c) Compilation error
    d) Runtime error

    参考答案

    ``` a ```

    参考链接

  • 相关阅读:
    bzoj 1030: [JSOI2007]文本生成器
    hdu 2222 Keywords Search
    poj 2406 Power Strings
    poj 2752 Seek the Name, Seek the Fame
    ASP.NET中WebForms简介与搭建HelloWorld项目
    VisualStudio2017中新建项目没有Asp.Net项目
    C#中导出百万级Excel只需几秒除了NPOI还可以这样
    Winform中实现双击Dev的TreeList在ZedGraph中生成对应颜色的曲线
    Winform中设置ZedGraph在生成多条曲线时随机采用不同的颜色
    DevExpress的TreeList实现自定义节点NodeCell的背景颜色和前景色
  • 原文地址:https://www.cnblogs.com/hgnulb/p/11343046.html
Copyright © 2011-2022 走看看