【java】pta18周练习题

来源:互联网 发布:买家淘宝退货率 编辑:程序博客网 时间:2024/05/16 04:55

判断题

1、java语言中不用区分字母的大写小写。 F

2、如果线程死亡,它便不能运行。 T

3、静态变量是被同一个类的所有实例所共享的。 T

4、在Java中,高优先级的可运行线程会抢占低优先级线程。 F

5、ArrayList类是线程安全的。F

6、Java中所有的I/O都是通过流来实现的。 T

7、Java中数组的元素只能是简单数据类型。 F

8、当GUI应用程序使用边界布局管理器BorderLayout时,GUI组件可以按任何顺序添加到面板上。( ) T

9、类也是一种数据类型(type)。 T

10、匿名类的类体中不可以声明static成员变量和static方法。 T

11、Almost anything can be considered an object. 分值为3分。T

12、对象是类的实例(instance)。 T

13、容器是用来组织其他GUI组件的单元,它不能嵌套其他容器。( ) F

14、StringBuilder类是线程安全的,StringBuffer类是线程不安全的。 F

15、如果一个类的声明中没有使用extends关键字,这个类被系统默认为是继承Object类。T

16、在AWT的事件处理机制中,每个事件类对应一个事件监听器接口,每一个监听器接口都有相对应的适配器。( ) F


选择题

1、在复选框中移动鼠标,然后单击一选项,要捕获所选项必需实现哪个接口?() (2分)

MouseListener
MouseMotionListern
ItemListener
ActionListener

2、下列哪一项不属于布局管理器? ( ) (2分)

GridLayout
CardLayout
BorderLayout
BagLayout

3、list是一个ArrayList的对象,哪个选项的代码填写到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象?( ) (2分)

    Iterator it = list.iterator();    int index = 0;    while (it.hasNext()){           Object obj = it.next();           if (needDelete(obj)) { //needDelete返回boolean,决定是否要删除               //todo delete           }           index ++;    }

list.remove(obj);
list.remove(it.next());
list.remove(index);
it.remove();

4、下面说法不正确的是( ) (2分)

当子类对象和父类对象能接收同样的消息时,它们针对消息产生的行为可能不同;
子类在构造函数中可以使用super( )来调用父类的构造函数;
一个子类的对象可以接收父类对象能接收的消息;
父类比它的子类的方法更多;

5、JFrame的缺省布局管理器是( )。 (2分)

GridLayout
FlowLayout
CardLayout
BorderLayout

6、编译Java源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为( )。(2分)

.exe
.byte
.class
.html

7、下列方法头中哪一个不与其他方法形成重载(overload)关系?( ) (2分)

void mmm()
void mmm(String s)
int mm()
void mmm(int i)

8、下列哪些语句关于Java内存回收的说明是正确的? ( ) (2分)

内存回收程序负责释放无用内存
内存回收程序允许程序员直接释放内存
内存回收程序可以在指定的时间释放内存对象
程序员必须创建一个线程来释放内存

9、以下关于Java的局部内部类的说法错误的是( ) (2分)

局部内部类不能包含静态成员
局部内部类只能在当前类中使用
在局部内部类中定义的内部类不能被private修饰符修饰
局部内部类可以访问外部类的所有成员

10、Swing组件必须添加到Swing顶层容器相关的( )。(2分)

选项卡上
复选框内
内容面板上
分隔板上

11、以下关于构造函数的描述错误的是( )。 (2分)

构造函数的返回类型只能是void型。
构造函数是类的一种特殊函数,它的方法名必须与类名相同。
构造函数的主要作用是完成对类的对象的初始化工作。
一般在创建新对象时,系统会自动调用构造函数。

12、如果需要从文件中读取数据,则可以在程序中创建哪一个类的对象()。 (2分)

DataOutputStream
FileWriter
FileInputStream
FileOutputStream

13、以下关于继承的叙述正确的是( )。 (2分)

在Java中类只允许单一继承
在Java中接口只允许单一继承
在Java中一个类不能同时继承一个类和实现一个接口
在Java中一个类只能实现一个接口

14、声明并创建一个按钮对象b,应该使用的语句是( ) (2分)

button b=new button( );
Button b=new Button( );
b.setLabel(“确定”);
Button b=new b( );

15、paint( )方法使用哪种类型的参数? ( ) (2分)

Graphics
String
Color
Graphics2D

16、要产生[20,999]之间的随机整数使用哪个表达式? ( ) (2分)

(int)Math.random()*999
20+(int)(Math.random()*980)
(int)(20+Math.random()*97)
20+(int)Math.random()*980

17、JPanel组件的默认布局管理器是( )。 (2分)

BorderLayout
GridLayout
FlowLayout
CardLayout


函数题

从抽象类shape类扩展出一个正n边形
在一个正n边形(Regular Polygon)中,所有边的边长都相等,且所有角的度数相同(即这个多边形是等边、等角的)。请从下列的抽象类shape类扩展出一个正n边形类RegularPolygon,这个类将正n边形的边数n和边长a作为私有成员,类中包含初始化边数n和边长a的构造方法。

计算正n边形的面积公式为: Area=n×a×a/(tan((180度/n))×4);

类名:RegularPolygon
裁判测试程序样例:

abstract class shape {// 抽象类    /* 抽象方法 求面积 */    public abstract double getArea();    /* 抽象方法 求周长 */    public abstract double getPerimeter();}/* 你提交的代码将嵌入到这里 */ public class Main {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数        int n=input.nextInt();        double side = input.nextDouble();        shape rp = new  RegularPolygon(n,side);        System.out.println(d.format(rp.getArea()));        System.out.println(d.format(rp.getPerimeter()));        input.close();    }}

输入样例:
5
7
输出样例:
84.3034
35
提交代码:

class RegularPolygon extends shape {    private int n;    private double side;    public RegularPolygon(int n, double side) {        this.n = n;        this.side = side;    }    public double getArea() {        return n*side*side/(Math.tan(Math.toRadians(180/n))*4);    }    public double getPerimeter() {        return n*side;    }}

编程题

1、字符串替换
将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。

输入样例:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
输出样例:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

提交代码:

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in=new Scanner(System.in);        String strings=new String();        String s=in.nextLine();        while(!s.equals("end")){            strings=strings.concat(s);            s=in.nextLine();        }        String re1=in.next();        String re2=in.next();        String string2=strings.replaceAll(re1,re2);        System.out.print(string2);        in.close();    }}

2、找素数
请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。

输入样例:
9223372036854775839 2

输出样例:
9223372036854775907
9223372036854775931

提交代码:

import java.math.BigInteger;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in=new Scanner(System.in);        BigInteger x =in.nextBigInteger();        int count=in.nextInt();        while(count>0){            if (x.isProbablePrime(100)) {                System.out.println(x);                x=x.nextProbablePrime();                count--;            }else                x=x.nextProbablePrime();        }        in.close();    }}

3、查找电话号码
文件phonebook1.txt中有若干联系人的姓名和电话号码。

高富帅 13312342222

白富美 13412343333

孙悟空 13512345555

唐三藏 13612346666

猪悟能 13712347777

沙悟净 13812348888

请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found. 由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。

输入样例:

白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精
输出样例:
Not found.

提交代码:

import java.text.DecimalFormat;import java.util.HashMap;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        HashMap<String, String> map = new HashMap<String, String>();        String str = null;        while (in.hasNext()){            if (!(str = in.next()).equals("noname")) {                map.put(str, in.next());            } else {                break;            }        }        String s = in.next();        if (map.containsKey(s)) {            System.out.println((map.get(s)));        } else            System.out.println("Not found.");        in.close();    }}

4、日期加减
请编程从键盘输入一个长整型的值,该值表示从1970年1月1日算起的一个特定时间(毫秒数),以此时间构造一个日期对象。再输入一个普通整型值,该值表示天数,加上该天数后,然后输出对应的年、月、日。

输入样例:

1234567898765
158
输出样例:

2009-02-14
2009-07-22

提交代码:

import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        Calendar date = Calendar.getInstance();        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");        long time = in.nextLong();        date.setTimeInMillis(time);        System.out.println(format.format(date.getTime()));        int day = in.nextInt();        date.add(Calendar.DAY_OF_MONTH, day);        System.out.println(format.format(date.getTime()));        in.close();    }}
0 0