程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

沈师 Java程序设计 PTA 填空题、程序填空题答案

发布于2021-06-12 13:47     阅读(940)     评论(0)     点赞(5)     收藏(0)


无答案版链接:https://blog.csdn.net/a2272062968/article/details/117787974

填空题

  1. 请写出以下程序运行结果:
public class MyFor{
 public static void main(String argv[]){
     int i, j;
outer:    for (i=1;i <3;i++)
inner:    for (j=1; j<3; j++) {
     if (j==2)continue outer;
     System.out.println("Value for i=" + i + " Value for j=" +j);
     }
 }
}

     1.1          1.2     
答案:
1.1: Value for i=1 Value for j=1
1.2: Value for i=2 Value for j=1

  1. 阅读以下程序:
import java.util.Scanner;
public class Test {
  public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     int number, max;
     number = input.nextInt();
     max = number;
     while (number != 0) {
         number = input.nextInt();
         if (number > max)
              max = number;
     }
     System.out.println("max is " + max);
     System.out.println("number " + number);
  }
}

假设输入是 2 3 4 5 0,程序输出是:
max is      2.1     

number      2.2     
答案:
2.1: 5
2.2: 0

  1. For code below:
Loop1:
while ( true ) {            //    1
 for ( ; true; ) {
     if ( i ==2 )
         break Loop1;    //    2
 }
 i=4;                //    3
}
i=5;                    //    4

After executing line 2, where will the program jump to?     3.1     
答案:
3.1: 4

  1. For a class Class1 in Class1.java as below:
package Testpackage; 
public class Class1{ 
… … 
}

The main() is in MainPro.java as below:

import Testpackage.*;
… …

The CLASSPATH is "c:\java\lib\classes.zip;.; ". The MainPro.java is in c:\Testdir. The current directory is c:\Testdir. Where should we put the Class1.class?     4.1     
答案:
4.1: c:\Testdir\Testpackage

  1. 阅读以下程序:
public class Test extends TT{ 
 public static void main(String args[]){ 
     Test t=new Test("Tom"); 
 } 
 public Test(String s){ 
     super(s); 
     System.out.println("How do you do?"); 
 } 
 public Test(){ 
     this("I am Tom"); 
 } 
} 
class TT { 
 public TT(){ 
     System.out.println("What a pleasure!"); 
 } 
 public TT(String s){ 
     this(); 
     System.out.println("I am "+s); 
 } 
} 

程序运行结果为:     5.1          5.2          5.3     
答案:
5.1: What a pleasure!
5.2: I am Tom
5.3: How do you do?

  1. 在Java程序中,创建一个包即打包操作,应使用关键词     6.1     
    ,该关键词引导的语句必须放在程序的第     6.2     
    行,该行前只能有空格及注释。
    在Java程序中,想要导入不同包下面的某个类时,可以使用关键词     6.3     
    答案:
    6.1: package
    6.2:
    6.3: import

  2. 阅读下列程序:

1. public class Main{ 
    public static void main(String[]args){ 
        System.out.println("创建一个Faculty对象"); 
        new Faculty(); 
    } 
   } 
   class Faculty extends Employee { 
    public Faculty(){ 
        System.out.println("完成Faculty的任务"); 
    } 
   } 
   class Employee extends Person{ 
    public Employee(){ 
        this("调用Employee的重载构造方法"); 
        System.out.println("完成Employee的任务"); 
    } 
    public Employee(String s){ 
        System.out.println(s); 
    } 
   } 

class Person { 
    public Person() { 
        System.out.println("完成Person的任务"); 
    } 
}

从下面选项中进行选择,将相应序号(1、2、3、4、5中选其一)填入空格中:
(1)完成Person的任务
(2)完成Employee的任务
(3)完成Faculty的任务
(4)调用Employee的重载构造方法
(5)创建一个Faculty对象
按程序执行顺序输出结果:     7.1          7.2          7.3          7.4          7.5     
答案:
7.1: 5
7.2: 1
7.3: 4
7.4: 2
7.5: 3

  1. 给出以下代码:
class Number {
 int i;
 public Number(int ii) { i=ii; }
}
public class Main {
 public static void main(String[] args) {
     Number[] a = new Number[5];
     for ( int i=0; i<a.length; i++ ) a[i] = new Number(i);
     for ( int i=0; i<2; i++ )
         for ( Number n : a ) {
             System.out.print(n.i);
             n.i = 5-n.i;
         }
 }
}

程序运行结果是:     8.1     
答案:
8.1: 0123454321

  1. Java的三大体系分别是封装、( )、( )。     9.1          9.2     
    答案:
    9.1: 继承
    9.2: 多态

  2. 给出以下代码:

    public class Test {
    public int t=4;
    public static void main(String[] args) {
        new Test().NumberPlay();
    }
    public void NumberPlay() {
        int t=2;
        t = t+5;
        this.t = this.t-2;
        t = t-this.t;
        System.out.println(t+this.t+"ok");
    }
    }
    

    程序运行后输出结果为:     10.1     
    答案:
    10.1: 7ok

  3. 定义在类中的变量被称为( ),定义在方法中的变量被称为( )。     11.1          11.2     
    答案:
    11.1: 成员变量
    11.2: 局部变量

  4. 访问父类的成员可以使用     12.1     关键字。
    答案:
    12.1: super

  5. 说出下列A类中【代码1】~【代码3】的输出结果

    class  Fish { int weight=1; } 
    class  Lake{ 
       Fish  fish; 
       void  setFish(Fish s) { fish=s; } 
       void  foodFish(int m){  fish.weight=fish.weight+m; } 
     }
     public  class  Main {
    public static void main(String args[]){ 
         Fish  redFish=new Fish();
         System.out.println(redFish.weight);//【代码1】
         Lake lake=new Lake();
         lake.setFish(redFish); 
         lake.foodFish(120); 
         System.out.println(redFish.weight);//【代码2】 
         System.out.println(lake.fish.weight);//【代码3】
    }
    } 
    

    【代码1】     13.1     
    【代码2】     13.2     
    【代码3】     13.3     
    答案:
    13.1: 1
    13.2: 121
    13.3: 121

  6. 阅读以下程序,写出输出结果。

    public  class   AppleMobilePhone   extends MobilePhone{
     public   static   void   main(String[] args){  
           new  AppleMobilePhone(); 
     }
     public   AppleMobilePhone(){ 
           System.out.println("This is a Applemobilephone"); 
     }
    }
    class    MobilePhone   extends   Phone{
     public   MobilePhone(){ 
          System.out.println("This is a mobilephone"); 
     }
    }
    class   Phone{
     public   Phone(){  
          System.out.println("This is a phone"); 
     }
    }
    

    程序运行后输出: 从下列选项中选,在空格处填上相应的序号数字(1、2或3)
    (1)This is a phone
    (2)This is a Apple mobile phone
    (3)This is a mobile phone
    第一行输出:     14.1     
    第二行输出:     14.2     
    第三行输出:     14.3     
    答案:
    14.1: 1
    14.2: 3
    14.3: 2

  7. 请说出A类中System.out.println的输出结果。

     class B{
    int x=100,y=200;
    public void setX(int x){ x=x; }
    public void setY(int y){ this.y=y; } 
    public int getXYSum(){ return x+y; } 
     } 
     public class A { 
    public static void main(String args[]){
        B b=newB(); 
        b.setX(-100); 
        b.setY(-200); 
        System.out.println("sum="+b.getXYSum()); 
    } 
     } 
    

    程序输出结果为:sum=     15.1     
    答案:
    15.1: -100

  8. 已知代码:

    class A{
      String name="A";
      String getName(){
     return name;
      }
      String greeting(){
     return "class A"; 
      }
    }
    class B extends A{
      String name="B";
      String greeting(){
     return "class B";
      }
    }
    public class Main{
      public static void main(String args[]){
     A a=new A();
     A b=new B();
     System.out.println(a.greeting()+" has name "+a.getName());
     System.out.println(b.greeting()+" has name "+b.getName());
      }
    }
    

    在下列运行结果的空格中选择填写A或B:
    class     16.1     
    has name     16.2     
    class     16.3     
    has name     16.4     
    答案:
    16.1: A
    16.2: A
    16.3: B
    16.4: A

  9. 阅读下列程序:

    class Animal{
       Animal(){ System.out.println("我是动物"); }
       void  shout(){ System.out.println("动物叫"); }
    }
    class  Dog   extends   Animal{
         void   shout(){
                super.shout();
                System.out.println("汪汪");
          }
    }
    class  Cat   extends   Animal{
         void  shout(){ System.out.println("喵喵"; }
    }
    public   class   Test{
          public  static   void   main(String[]args){
                   Animal   dog=newDog();
                   Animal   cat=newCat();
                   dog.shout();
                   cat.shout();
           }
    }
    

    按程序执行顺序输出结果:
    (1)     17.1     
    (2)     17.2     
    (3)     17.3     
    (4)     17.4     
    (5)     17.5     
    答案:
    17.1: 我是动物
    17.2: 我是动物
    17.3: 动物叫
    17.4: 汪汪
    17.5: 喵喵

  10. 阅读以下程序,在空格内填上正确答案。

    public class Test {
       public static void main(String[] args) {
       int number = 0;
       int[] numbers = new int[1];
    
       m(number, numbers);
    
       System.out.println("number is " + number+ " and numbers[0] is " + numbers[0]);
       }
      public static void m(int x, int[] y) {
        x = 3;
       y[0] = 3;
       }
    }
    

    程序运行后,输出:
    number is     18.1     
    and numbers[0] is     18.2     
    答案:
    18.1: 0
    18.2: 3

  11. 阅读下列程序:

    class A{ 
    int i=7; 
    public A(){ 
        setI(20); 
        System.out.println("i="+i); 
    } 
    public void setI(int i){
        this.i=2*i;
    } 
    }
    class B extends A{ 
    public B(){ 
        System.out.println("i="+i); 
    } 
    public void setI(int i){
        this.i=3*i;
    } 
    } 
    public class Main{ 
    public static void main(String[] args){ 
        new A(); 
        new B(); 
    } 
    } 
    

    程序运行后依次输出:     19.1          19.2          19.3     
    答案:
    19.1: i=40
    19.2: i=60
    19.3: i=60

  12. 请写出以下程序运行结果:

    class A
    {
    int    x;
    String s;
    };
    class HelloWorld
    {
    public static void main(String[] args) 
    {
        A a= new A();
        System.out.println(a.s);
        System.out.println(a.x);
    }}
    

         20.1          20.2     
    答案:
    20.1: null
    20.2: 0

  13. 请写出以下程序运行结果:

    class Window {
       Window(int marker) { System.out.println("Window(" + marker + ")"); }
       }
       class House {
       Window w1 = new Window(1); 
       House() {
           System.out.println("House()");
           w3 = new Window(33); 
       }
       Window w2 = new Window(2); 
       void f() {
           System.out.println("f()");
       }
       static Window w3 = new Window(3); 
       }
    
    public class Est {
        public static void main(String[] args) {
            House h = new House();
            h.f(); 
        }
    }
    

         21.1          21.2          21.3          21.4          21.5          21.6     
    答案:
    21.1: Window(3)
    21.2: Window(1)
    21.3: Window(2)
    21.4: House()
    21.5: Window(33)
    21.6: f()

  14. 接口中的方法的默认的访问权限是     22.1     
    答案:
    22.1: public

  15. 已知代码:

    class MyDate{
      int year;
      int month;
      int day;
      MyDate(int year,int month,int day){
     this.year=year;
     this.month=month;
     this.day=day;
      }
    }
    public class Main{
      public static void main(String args[]){
     MyDate md1=new MyDate(2009,2,10);
     MyDate md2=new MyDate(2009,2,10);
     if(md1==md2)
       System.out.println("md1==md2");
     else
       System.out.println("md1!=md2");
     if(md1.equals(md2))
       System.out.println("md1 is equla to md2");
     else
       System.out.println("md1 is not equal to md2");
      }
    }
    

    运行上述代码,写出其运行结果:     23.1          23.2     
    答案:
    23.1: md1!=md2
    23.2: md1 is not equal to md2

  16.      24.1     关键字修饰的类不能被继承。
    答案:
    24.1: final

  17. 请写出以下程序运行结果:

    class Letter {
    char c;
    }
    public class Main {
    static void f(Letter y) {
        y.c = 'z';
    }
    
    public static void main(String[] args) {
        Letter x = new Letter();
        x.c = 'a';
        f(x);
        System.out.println(x.c);
    }
    }
    

         25.1     
    答案:
    25.1: z

  18. 运行下列代码,运行结果是什么?

    public class Main{
      int i=2;
      static int is;
      static{
    System.out.println("in static block");
    is=5;
    System.out.println("static variable is="+is);
      }
      {
    System.out.println("in non-static block");
    i=8;
      }
      Main(){
    i=10;
      }
      public static void main(String args[]){
    System.out.println("in main()");
    Main m1=new Main();
    System.out.println(m1.i);
      }
    }
    

    运行上述代码,则运行结果为:     26.1          26.2          26.3          26.4          26.5     
    答案:
    26.1: in static block
    26.2: static variable is=5
    26.3: in main()
    26.4: in non-static block
    26.5: 10

  19. 一个类如果实现一个接口,那么它就需要实现接口中定义的全部( ),否则该类就必须定义成( )。     27.1          27.2     
    答案:
    27.1: 方法
    27.2: 抽象类

  20. 给出以下代码:

    class Number {
    public int i;
    public Number(int ii) {i=ii;};
    }
    public class Main {
    static void f(Number n) {
        n.i = 9;
    }
    static void g(Integer n) {
        n=9;
    }
    public static void main(String[] args) {
        Number k = new Number(10);
        f(k);
        Integer m = new Integer(10);
        g(m);
        System.out.println(k.i+":"+m);
    }
    }
    

    程序运行后输出结果是:     28.1     
    答案:
    28.1: 9:10

  21. 下列代码的运行结果是什么?

    public class Main{
      static{
     System.out.print("Hi here,");
      }
      public void print(){
     System.out.print("Hello");
      }
      public static void main(String args[]){
     Main m1=new Main();
     m1.print();
     Main m2=new Main();
     m2.print();
      }
    }
    

    上述代码的运行结果为:     29.1     
    答案:
    29.1: Hi here,HelloHello

  22. 请写出以下程序运行结果:

    class NoWater extends Exception {}
        class NoDrinkableWater extends NoWater {}
    
    public class FinallyWorks {
        static int count = 0;
        public static void main(String[] args) throws NoWater {
            while ( true ) {
               try {
               count++;
               if ( count == 1 ) { 
                 System.out.println("OK");
               } else if ( count == 2 ) {
                 System.out.println("Exception raised: NoDrinkableWater");
                 throw new NoDrinkableWater();
               } else if ( count == 3 ) {
                 System.out.println("Exception raised: NoWater");
                 throw new NoWater();
               }
                } catch (NoDrinkableWater e) {
                    System.out.println(e);
                } finally {
                    System.out.println("finally");
                    if ( count == 3 )
                        break;
                }
            }
        }
    }
    

         30.1          30.2          30.3          30.4          30.5          30.6          30.7     
    答案:
    30.1: OK
    30.2: finally
    30.3: Exception raised: NoDrinkableWater
    30.4: NoDrinkableWater
    30.5: finally
    30.6: Exception raised: NoWater
    30.7: finally

  23. 请写出以下程序运行结果:

    public class X {  
    public static void main(String [] args) {
        try {
            badMethod();  
            System.out.print("A");  
        } catch (RuntimeException ex) { 
            System.out.print("B"); 
        } catch (Exception ex1) { 
            System.out.print("C"); 
        } finally {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() { 
        throw new RuntimeException(); 
    }}
    

         31.1     
    答案:
    31.1: BDE

  24. 请写出以下程序运行结果:

    class Exception1  extends Exception {}
    class Exception2  extends Exception1  {}
    public class Test {
       public static void main(String[] args) 
      throws Exception {
      try {
         try {
             throw new Exception2();
         } catch ( Exception1  a ) {
             System.out.println("Caught Exception1");
             throw a;
         }
      } catch ( Exception2 s ) {
         System.out.println("Caught Exception2");
         return ;
      } finally {
         System.out.println("Hello World!");
    }}}
    

         32.1          32.2          32.3     
    答案:
    32.1: Caught Exception1
    32.2: Caught Exception2
    32.3: Hello World!

  25. 在Java中,用户自定义异常类必须继承     33.1     
    类, 用户人工抛出自定义异常使用关键词     33.2     
    答案:
    33.1: Exception
    33.2: throw

  26. 在Java中,     34.1     类是所有异常和错误的顶级父类。
    答案:
    34.1: Throwable

  27. 当编译并运行下列代码时,其运行结果是什么?

    public class Main{
       public static void main(String args[]){
     String s="Hello";
     methodA(s);
     s=s.replace('e', 'a');
     System.out.println(s);
       }
       public static void methodA(String str){
     str+="World";
       }
    }
    

    上述代码的运行结果为:     35.1     
    答案:
    35.1: Hallo

  28. 当编译并运行下列代码时,其运行结果是什么?

    public class Main{
       public static void main(String args[]){
     String s="Java";
     StringBuffer sb=new StringBuffer("Java");
     change(s);
     change(sb);
     System.out.println(s+sb);
       }
       public static void change(String s){
     s=s.concat("Hello");
       }
       public static void change(StringBuffer sb){
     sb.append("Hello");
       }
    }
    

    上述代码的运行结果为:     36.1     
    答案:
    36.1: JavaJavaHello

  29. 给出以下代码:

    public class Main {
    int i=0;
    Main(int ii) { i = ii; }
    Main(String s) { this(s.length()); }
    public String toString() { return String.format("%02X",i); }
    public static void main(String[] args) {
        Main m = new Main("hello world");
        System.out.println(m);
    }
    }
    

    程序运行输出结果是:     37.1     
    答案:
    37.1: 0B

  30. 当编译并运行下列代码时,其运行结果时什么?

    public class Main{
       public static void main(String args[]){
     String s="Java";
     StringBuffer sb=new StringBuffer("Java");
     hello(sb,s);
     System.out.println(sb+s);
       }
       public static void hello(StringBuffer sb, String s){
     sb.append("A");
     s=sb.toString();
       }
    }
    

    上述代码的运行结果为:     38.1     
    答案:
    38.1: JavaAJava

  31. 当编译并运行下列代码时,其运行结果是什么?

    public class Main{
       public static void main(String args[]){
     certkiller("four");
     certkiller("tee");
     certkiller("to");
       }
       public static void certkiller(String str){
     int check=4;
     if(check==str.length())
       System.out.print(str.charAt(check-=1));
     else
       System.out.print(str.charAt(0));
       }
    }
    

    上述代码的运行结果为:     39.1     
    答案:
    39.1: rtt

  32.      40.1     类实现了缓存功能的InputStream。
    答案:
    40.1: BufferedInputStream

  33. 在java.io包内包含了处理各种流的基本类,所有的字节输出流都继承于     41.1     
    类,所有的字符输入流都继承于     41.2     类。
    答案:
    41.1: OutputStream
    41.2: Reader

  34. InputStreamReader类是用于将( )转换为( )。     42.1          42.2     
    答案:
    42.1: 字节流
    42.2: 字符流

  35. 对于java.io包中的所有I/O类,根据数据流所关联的是数据源还是其他数据流,可分为节点流和     43.1     
    答案:
    43.1: 处理流

  36. 调用线程对象的     44.1     方法可以启动线程,使线程处于可运行状态。
    答案:
    44.1: start()

  37. Java程序中的线程被设计为一个对象,该对象具有自己的生命周期,可以利用接口Runnable和类     45.1     创建一个线程。
    答案:
    45.1: Thread

  38. 无论采用何种方式定义线程类,线程类中均需重新定义     46.1     方法,该方法负责完成线程所需执行的任务。
    答案:
    46.1: run()

  39. 在实现多线程的程序时有两种方式,一是通过继承( )类,二是通过实现( )接口。     47.1          47.2     
    答案:
    47.1: Thread
    47.2: Runnable

进程已结束,退出代码0

程序填空题

  1. 以下程序的功能是求一个二维数组中每行的最大值和每行的和。
    输入样例
    3
    1 2 3
    6 5 4
    7 9 8
    输出样例
    1 2 3 3 6
    6 5 4 6 15
    7 9 8 9 24

    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
        Scanner sc=new Scanner(/*___1.1___*/);
        int n=sc.nextInt();
        int a[][]=new int[n][n];
        int b[]=new int[n];
        int c[]=new int[n];
        for(int i=0;i<a.length;i++){
            for(int j=0;j</*___1.2___*/;j++){
                a[i][j]=sc.nextInt();
            }
        }        
        int max,s;
        for(int i=0;i<a.length;i++){
        	max=a[i][0];
        	/*___1.3___*/;
            for(int j=0;j<a[i].length;j++){
                if(a[i][j]>max){
                /*___1.4___*/;
                }
                s+=a[i][j];
            }
            b[i]=max;
            c[i]=s;
        }
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length;j++){
                System.out.printf("%3d",/*___1.5___*/);
            }
            System.out.printf("%3d%3d",b[i],c[i]);
            System.out.println();
            }
        }
    }
    

    答案:
    1.1 System.in
    1.2 a[i].length
    1.3 s=0
    1.4 max=a[i][j]
    1.5 a[i][j]

  2. 题目要求:
    1.使用this调用已有的有参构造函数,width与length分别为5和6。
    2.为Rectangle类覆盖toString。按照width=实际宽度值,length=实际长度值的格式输出

    public Rectangle(){ 
        /*___2.1___*/
    }
    public Rectangle(int width, int length) {
        this.width = width;
        this.length = length;
    }
    public /*___2.2___*/{
    	/*___2.3___*/
    }    
    
    

    答案:
    2.1 this(5,6); 或 this.width=5;this.length=6;

    2.2 String toString()

    2.3 return “width=”+width+",length="+length;

  3. 输入一行字符,请分别统计出英文字母、数字、空格和其他字符个数。

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String str=sc.nextLine();
            char x[]=/*___3.1___*/;
            int a=0;
            int b=0;
            int c=0;
            int d=0;
            for(int i=0;/*___3.2___*/;i++){
                char ch=x[i];
                if(/*___3.3___*/)
                	a++;
                else if(/*___3.4___*/)
                	b++;
                else if(ch==' ')
                    /*___3.5___*/;
                else
                    d++;
            }
            System.out.println("letters="+a);//输出英文字母个数
            System.out.println("digits="+b);//输出数字个数
            System.out.println("spaces="+c);//输出空格个数
            System.out.println("others="+d);//输出其他字符个数
        }
    }
    

    答案:
    3.1 str.toCharArray();
    3.2 i<x.length
    3.3 ch>=‘a’&&ch<=‘z’ ||ch>=‘A’&&ch<=‘Z’
    3.4 ch>=‘0’&&ch<=‘9’
    3.5 c++

  4. 本题目要求t1线程打印完后,才执行主线程main方法的最后一句System.out.println(Thread.currentThread().getName()+" end");

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(new PrintTask());
            /*___4.1___*/
            /*___4.1___*/
            System.out.println(Thread.currentThread().getName()+" end");
        }
    }
    

    答案:
    4.1 t1.start();
    4.2 t1.join();

进程已结束,退出代码0

原文链接:https://blog.csdn.net/a2272062968/article/details/117787042



所属网站分类: 技术文章 > 博客

作者:你不要惹我

链接:http://www.javaheidong.com/blog/article/221875/ac6b54f80ca1a2c17f93/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)