沈师 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.11.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.15.25.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.17.27.37.47.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.19.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.111.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:
class16.1
has name16.2
class16.3
has name16.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 is18.1
and numbers[0] is18.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.119.219.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.120.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.121.221.321.421.521.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.123.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.126.226.326.426.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.127.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.130.230.330.430.530.630.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.132.232.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: rtt40.1类实现了缓存功能的InputStream。
答案:
40.1: BufferedInputStream在java.io包内包含了处理各种流的基本类,所有的字节输出流都继承于41.1
类,所有的字符输入流都继承于41.2类。
答案:
41.1: OutputStream
41.2: ReaderInputStreamReader类是用于将( )转换为( )。42.142.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.147.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 填空题、程序填空题答案相关推荐
- 沈师 Java程序设计 PTA 填空题、程序填空题 无答案版
答案链接:https://blog.csdn.net/a2272062968/article/details/117787042 请写出以下程序运行结果: public class MyFor{pub ...
- 大专java考试试题_专科—程序设计基础题库-java.doc
专科-程序设计基础题库-java 专科15级<程序设计基础>题库100道 总共抽8道题,每小题12.5分,共100分. 按题型:顺序(1道).分支(1道).单循环(2道).多循环(1道). ...
- 沈师 数据库原理 PTA 选择题答案
无答案版链接:https://blog.csdn.net/a2272062968/article/details/117712085 下面对数据模型的不正确叙述是( ). A. 逻辑层次上的数据模型有 ...
- 吉大java考试题_吉大20春学期《JAVA程序设计》在线作业二-1(答案)
答案来源:奥学网(www.aoxuewang.net)-[吉林大学]吉大20春学期<JAVA程序设计>在线作业二 试卷总分:100 得分:100 第1题,在 Java 中,所有类的根 ...
- 【传智播客】Javaweb程序设计任务教程 黑马程序员 课后答案【合集】
[传智播客]Javaweb程序设计任务教程 黑马程序员 第一章 课后答案 [传智播客]Javaweb程序设计任务教程 黑马程序员 第二章 课后答案 [传智播客]Javaweb程序设计任务教程 黑马程序 ...
- 沈师 数据库原理 PTA 填空题 无答案版
答案链接:https://blog.csdn.net/a2272062968/article/details/117713227 1.1是长期存储在计算机内有组织.可共享的大量数据的集合. 数据模型的 ...
- 尚学堂 实战java程序设计 第1,2章课后题答案
第1章 一.选择题 1.C 2.AD 3.D 4.B 5.A 二.简答题 1.答:计算机语言总的来说分为机器语言,汇编语言,高级语言三大类.这三种语言是计算机语言发展历史的三个阶段. 2.答:java ...
- java程序设计教程视频_Java程序设计标准教程:DVD视频教学版
第1章 搭建Java开发环境. 001 1.1 Java语言的产生与发展 002 1.2 Java语言的特点 002 1.3 搭建Java开发环境 003 1.3.1 下载JDK 003 1.3.2 ...
- 201671010128 2017-11-10《Java程序设计》之应用程序部署(2)
一.Applet类中常用的几种常用方法 public class appletName extends Applet { public void init( ) { - - } public void ...
- java程序设计 郑莉_Java程序设计基础-清华大学-计算机科学与技术-郑莉教授-学堂在线-第一章...
旁听课程记录 一.Java语言基础知识 1.机器语言 汇编语言 高级语言 { 面向过程的高级语言(C).面向对象的高级语言(Java_1995)} 2.面向对象语言的基本特征: 抽象和封装.继 ...
最新文章
- Oracle分页查询语句(六)
- 前端学习(3079):vue+element今日头条管理-数据筛选处理
- 华为服务器怎么格式化系统,如何格式化服务器
- python你的人生_人生苦短:运行你的第一个 Python 脚本
- Android基础控件之Button的基本使用
- BigDecimal你遇见过哪些坑?
- Latex appendix 生成附录A和B
- #QCon#北京2011大会“更有效地做测试”专题Slides资料
- 【预测模型】基于BP神经网络预测股票matlab代码
- (休息几天)读曼昆之微观经济学——公共物品和资源
- php电子面单接口,可一次性接入全国45家主流快递~顺丰、京东、邮政、EMS、德邦、四通一达
- 剧情插件Cutscene Creator uSequencer 1.3.7.1使用说明一
- 激光雷达在自动驾驶中的应用
- 计算机找不到ac97前面板怎么办,Win7前面板没有声音的解决方法(声卡设置+前面板插线)...
- Android涂鸦画板原理详解——从初级到高级(一)
- hadoop的map和reduce
- Linux用户态与内核态通信的几种方式(待完善)
- 欲登千层楼,又何惧寒风
- 循环语句介绍(笔记学习)
- 周末之个人杂想(十五)