日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/18 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 沈师 Java程序设计 PTA 填空题、程序填空题答案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

無答案版鏈接:https://blog.csdn.net/a2272062968/article/details/117787974

文章目錄

    • 填空題
    • 程序填空題

填空題

  • 請寫出以下程序運行結果:
  • 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

  • 閱讀以下程序:
  • 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

  • For code below:
  • Loop1: while ( true ) { // 1for ( ; 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

  • 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

  • 閱讀以下程序:
  • 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?

  • 在Java程序中,創建一個包即打包操作,應使用關鍵詞?????6.1?????
    ,該關鍵詞引導的語句必須放在程序的第?????6.2?????
    行,該行前只能有空格及注釋。
    在Java程序中,想要導入不同包下面的某個類時,可以使用關鍵詞?????6.3?????
    答案:
    6.1: package
    6.2:
    6.3: import

  • 閱讀下列程序:

  • 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

  • 給出以下代碼:
  • 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

  • Java的三大體系分別是封裝、( )、( )。?????9.1??????????9.2?????
    答案:
    9.1: 繼承
    9.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

  • 定義在類中的變量被稱為( ),定義在方法中的變量被稱為( )。?????11.1??????????11.2?????
    答案:
    11.1: 成員變量
    11.2: 局部變量

  • 訪問父類的成員可以使用?????12.1?????關鍵字。
    答案:
    12.1: super

  • 說出下列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

  • 閱讀以下程序,寫出輸出結果。

    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

  • 請說出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

  • 已知代碼:

    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

  • 閱讀下列程序:

    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: 喵喵

  • 閱讀以下程序,在空格內填上正確答案。

    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

  • 閱讀下列程序:

    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

  • 請寫出以下程序運行結果:

    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

  • 請寫出以下程序運行結果:

    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()

  • 接口中的方法的默認的訪問權限是?????22.1?????。
    答案:
    22.1: public

  • 已知代碼:

    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");elseSystem.out.println("md1!=md2");if(md1.equals(md2))System.out.println("md1 is equla to md2");elseSystem.out.println("md1 is not equal to md2");} }

    運行上述代碼,寫出其運行結果:?????23.1??????????23.2?????
    答案:
    23.1: md1!=md2
    23.2: md1 is not equal to md2

  • 用?????24.1?????關鍵字修飾的類不能被繼承。
    答案:
    24.1: final

  • 請寫出以下程序運行結果:

    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

  • 運行下列代碼,運行結果是什么?

    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

  • 一個類如果實現一個接口,那么它就需要實現接口中定義的全部( ),否則該類就必須定義成( )。?????27.1??????????27.2?????
    答案:
    27.1: 方法
    27.2: 抽象類

  • 給出以下代碼:

    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

  • 下列代碼的運行結果是什么?

    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

  • 請寫出以下程序運行結果:

    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

  • 請寫出以下程序運行結果:

    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

  • 請寫出以下程序運行結果:

    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!

  • 在Java中,用戶自定義異常類必須繼承?????33.1?????
    類, 用戶人工拋出自定義異常使用關鍵詞?????33.2?????。
    答案:
    33.1: Exception
    33.2: throw

  • 在Java中,?????34.1?????類是所有異常和錯誤的頂級父類。
    答案:
    34.1: Throwable

  • 當編譯并運行下列代碼時,其運行結果是什么?

    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

  • 當編譯并運行下列代碼時,其運行結果是什么?

    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

  • 給出以下代碼:

    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

  • 當編譯并運行下列代碼時,其運行結果時什么?

    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

  • 當編譯并運行下列代碼時,其運行結果是什么?

    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));elseSystem.out.print(str.charAt(0));} }

    上述代碼的運行結果為:?????39.1?????
    答案:
    39.1: rtt

  • ?????40.1?????類實現了緩存功能的InputStream。
    答案:
    40.1: BufferedInputStream

  • 在java.io包內包含了處理各種流的基本類,所有的字節輸出流都繼承于?????41.1?????
    類,所有的字符輸入流都繼承于?????41.2?????類。
    答案:
    41.1: OutputStream
    41.2: Reader

  • InputStreamReader類是用于將( )轉換為( )。?????42.1??????????42.2?????
    答案:
    42.1: 字節流
    42.2: 字符流

  • 對于java.io包中的所有I/O類,根據數據流所關聯的是數據源還是其他數據流,可分為節點流和?????43.1?????。
    答案:
    43.1: 處理流

  • 調用線程對象的?????44.1?????方法可以啟動線程,使線程處于可運行狀態。
    答案:
    44.1: start()

  • Java程序中的線程被設計為一個對象,該對象具有自己的生命周期,可以利用接口Runnable和類?????45.1?????創建一個線程。
    答案:
    45.1: Thread

  • 無論采用何種方式定義線程類,線程類中均需重新定義?????46.1?????方法,該方法負責完成線程所需執行的任務。
    答案:
    46.1: run()

  • 在實現多線程的程序時有兩種方式,一是通過繼承( )類,二是通過實現( )接口。?????47.1??????????47.2?????
    答案:
    47.1: Thread
    47.2: Runnable

  • 進程已結束,退出代碼0

    程序填空題

  • 以下程序的功能是求一個二維數組中每行的最大值和每行的和。
    輸入樣例
    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]

  • 題目要求:
    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;

  • 輸入一行字符,請分別統計出英文字母、數字、空格和其他字符個數。

    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___*/;elsed++;}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++

  • 本題目要求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

    總結

    以上是生活随笔為你收集整理的沈师 Java程序设计 PTA 填空题、程序填空题答案的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。