JAVA程序设计练习题(有答案) 下载本文

内容发布更新时间 : 2024/5/20 2:33:53星期一 下面是文章的全部内容请认真阅读。

A、 -1 B、2 C、1 D、3

12.有以下程序片段,下列哪个选项不能插入到行1。(D ) 1.

2 .public class Interesting{ 3. //do sth 4.}

A、import java.awt.*; B、package mypackage; C、class OtherClass{ } D、public class MyClass{ }

15.在编写Java Applet程序时,需在程序的开头写上( B )语句。 A、import java.awt.* ; B、import java.applet.Applet ; C、import java.io.*; D、import java.awt.Graphics ; 16.类Parent、Child定义如下: 1. publicclass Parent

2.{ public float aFun(float a, float b) throws 3. IOException { } 4.}

5.public class Child extends Parent{ 6. 7.}

将以下哪种方法插入行6是不合法的。( A) A、float aFun(float a, float b){ }

B、public int aFun(int a, int b)throws Exception{ } C、public float aFun(float p, float q){ }

D、public int aFun(int a, int b)throws IOException{ }

17.在使用interface声明一个接口时,只可以使用(D )修饰符修饰该接口。

A、private B、protected C、private protected D、public 18、有以下方法的定义,请选择该方法的返回类型(D )。 ReturnType method(byte x, double y) {

return (short) x/y*2; }

A、byte B、short C、int D、double 三.程序阅读题

1.以下程序的输出结果为 相等 。 class StringTest1 {

public static void main(String[] args) {

String s1=\

String s2=newString(\if(s1.equals(s2)){

System.out.println(\相等\}else{

System.out.println(\不相等\} } }

2.以下程序段的输出结果为 5 6 7 8 9 。 public class TestArray {

public static void main(String args[ ]){ int i , j ;

int a[ ] = { 5,9,6,8,7};

for (i = 0 ; i < a.length-1; i ++ ) { int k = i;

for ( j = i ; j < a.length ; j++ ) if ( a[j]

for (i =0 ; i

4.阅读以下程序,写出输出结果。 class Animal { Animal() {

System.out.print (\} public

class Dog extends Animal { Dog() {

System.out.print (\

public static void main(String[] args) { Dog snoppy=new Dog(); } }

Animal Dog

填空题

1、如果将类MyClass声明为public,它的文件名称必须是(MyClass.java)才能正常编译。

2、Java程序中的单行注释符是(//),多行注释符是(/* */ )。 3、Java中布尔类型的常量有两种,它们是(true)和(false)。

4、Java中用于定义小数的关键字有两个:(float)和(double),后者精度高于前者。 5、Java中用于两个数相等比较的运算符是:(==),用于不相等比较的运算符是(<>)。

6、在Java中定义一个字符串类型的变量str的语句是:(String str;),定义一个具有10个元素的整型数组a的语句是:(int [] arr = new int[10];)。 7、导入mypackage包中的所类的命令是( importmypackage.*;)。

8、当声明一个数组int arr[] = new int[5]; 时,这代表这个数组所保存的变量类型是(int),数组名是(arr),数组的大小为(5),数组元素下标的使用范围是(0到4)。

9、假设x=13,y=4,则表达式x%y != 0的值是(true),其数据类型是(boolean)。

10、异常处理是由(try)、(catch)和finally块三个关键所组成的程序块。 11、以下程序段的输出结果是(三角形) int x = 5, y = 6, z = 4;

if (x + y > z && x + z > y && z + y> x) System.out.println(\三角形\else

System.out.println(\不是三角形\

12、下面程序段的执行结果是(6 5 4 3 2) int a[] = { 2, 3, 4, 5, 6 };

for (int i = a.length - 1; i >= 0; i--) System.out.print(a[i] + \三、程序阅读题

1、以下程序的输出结果为_ Peter is 17 yearsold! __。 Public class Person { String name; int age;

public Person(String name, int age) { this.name = name; this.age = age; }

Public static void main(String[] args) { Person c = new Person(\

System.out.println(c.name + \} }

2、以下程序的输出结果为_课程号:101 课程名:ASP 学分: 3__。 Public class Course { private String cNumber; private String cName; private int cUnit;

public Course(String number, String name, int unit) { cNumber = number; cName = name; cUnit = unit; }

Public void printCourseInfo(){

System.out.println(\课程号:\课程名:\学分:\} }

class CourseTest {

public static void main(String[] args) {