好文档 - 专业文书写作范文服务资料分享网站

《JAVA程序设计》期末考试试题五

天下 分享 时间: 加入收藏 我要投稿 点赞

答案: true false

equals方法比较两个字符串的内容是否相等;运算符“==”判断两个对象是否指向同一个引用,即是否为同一个对象。

2、阅读下面的程序,并回答问题(问3分,问3分,共6分)。

import java.io.*;

public class Test {

public static void main(String args[]) throws IOException { BufferedReader buf=new BufferedReader(

new InputStreamReader(System.in)); while(true) {

String str = buf.readLine(); if(str.equals(\ break;

int x=Integer.parseInt(str); System.out.println(x*x); } } }

编译运行上面的程序:

从键盘输入10,回车后输出的结果如何?

从键盘输入exit,回车后程序能正确执行吗?为什么?

答案: 100

不能;因为方法Integer.parseInt(str)不能将字符串“exit”转化为整数,抛出异常。

3、写出下面的程序编译、运行后的结果(6分)。

public class Test{

public static void main(String args[]) { new Student(\ new Student(\ new Student(\

System.out.println(\ Student.print();

第 6 页 共 8 页

} }

class Student {

protected String name; protected char sex;

protected int chinese; protected int english; protected Student next; static Student list;

Student (String name, char sex, int chinese, int english) { this.name=name; this.sex=sex;

this.chinese=chinese; this.english=english; this.next=list; list=this; }

static void print() {

Student friend=list; if (friend==null)

System.out.println(\ else {

do{

System.out.println(friend.toString()); friend=friend.next; }while(friend!=null); } }

public String toString() {

return new String(name+\+sex+\ } }

第 7 页 共 8 页

答案:

name sex chinese englishMary f 76 86Jack m 66 89Tom m 90 88

第 8 页 共 8 页

《JAVA程序设计》期末考试试题五

答案:truefalseequals方法比较两个字符串的内容是否相等;运算符“==”判断两个对象是否指向同一个引用,即是否为同一个对象。2、阅读下面的程序,并回答问题(问3分,问3分,共6分)。importjava.io.*;publicclassTest{
推荐度:
点击下载文档文档为doc格式
1ky8194q3m0zdc523xwm17c19373ml00gmw
领取福利

微信扫码领取福利

微信扫码分享