加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA编程技术复习题

(2016-06-03 07:19:22)
标签:

教育

分类: 计算机

 

一、选择题

1JDK提供的编译器是(   )。

Ajava.exe                Bjavac.exe

Cjavap.exe                    Djavaw.exe

2.以下作为Java程序入口的main 方法声明正确的(   )。

Apublic void main(String args[])     

Bpublic int main(String args[])

Cpublic static void main(String args[])             

Dpublic static int main(String args[])

3.以下标识符错误的是(   )。

APublic B)张三  Cclass    Dmain

4.java中定义字符串String s=pzhu,下面操作可以取得字符串长度的是(   )。

As.length()  Bs.length  Cs.size()   Dlength(s)

5.如下定义数组,操作正确的是(   )。

int a[]={1,2,3};

Aa[3]=100      Ba[0].length   Ca++             Da.length

6.如下定义二维数组操作错误的是(   )。

int a[][]={{1,2},{3}};

Aa[0][1]=200    Ba[0].length Ca[1][1]=100      Da.length

7. 以下数据类型存储空间最大的是(   )。

Abyte Blong  Cfloat    Dchar

8. 面向对象的三大特性,不包括如下 (   )

A)异常 B)封装 C)继承 D)多态

9、关于类的定义以下说法错误(  )。

A)类定义使用class关键字 B)每个类中必须有一个main方法

C)一个包可以包含多个类 Djava中所有类都是Object类的子类

10. 关于构造方法以下说法错误的是 (      )

(A)构造方法名必须与类名一致  (B)构造方法可以重载

(C)构造方法是通过new来调用  (D)每个类都必须编写构造方法代码

11.关于继承如下说法错误的是(   

(A)Java是单继承的   (B)通过extends来定义继承

(C)所有父类方法都可以被override   (D)继承呈现的是is a的关系

12. 以下代码执行的结果是(   )

System.out.println("攀枝花学院pzhu".length());

(A)编译错误  (B)运行错误   (C)9 (D)14

13. 用来存储键值对的容器是(      )

AArrayList  (BLinkedList CHashSet   D HashMap

14java中用来抛出异常的关键字是(     )。

(A)try   (B)catch   (C)throw (D)throws

15.关于finally块中的代码,以下说法不正确的是(     )。

Atry块中的return语句会中断finally块中语句的执行

B)无论finally块前的语句运行是否产生异常,其中的语句都会执行

Cfinally块中的语句通常中用作资源的清理

(D)try块中的System.exit(1)语句会中断finally块中语句的执行

16.关于Java字符串说法错误的是(      )

(A)Java中的字符串是常量  (B)Java中的字符串不是对象

(C)Java中的字符串存储在常量池中  (D)一个字符串定义后的长度不可变

17.关于JDBC操作数据库,以下说法不正确的(     )。

(A)JDBC只能操作MySQL数据库

(B)JDBC中定义的Connection,Statement,ResultSet都是接口

(C)JDBC操作数据库必须要有相应的实现了JDBC接口的驱动

(D)JDBC可以通过将客户端的SQL传递给数据库服务器来实现数据库的操作

18.以下程序代码错误的是(      )。

abstract class P{}

class A extends P{}

abstract class B extends P{}

(A)P p=new A();(B)P p=new B();(C)A a=new A();(D)P p=new P(){void foo(){}};

19.以下Collection c创建有误的是(      )。

(A)Collection c=new ArrayList();(B)Collection c=new LinkedList();

(C)Collection c=new HashSet();(D)Collection c=new HashMap();

20. 以下程序代码错误的是(      )。

interface IA{

void f();

}

(A)abstract class A implements IA{} (B)class A implements IA{void f(){}}

(C)class A implements IA{void f(String s){}} (D)IA a=new IA(){void f(){}}

二、程序阅读


21.阅读程序,并写出程序运行结果

public class T21 {

static int init(){

System.out.println("A");

return 0;

}

static boolean test(int i){

System.out.println("B");

return i<1;

}

static int add(int i){

System.out.println("C");

return ++i;

}

public static void main(String[] args) {

for(int t=init();test(t);t=add(t)){

System.out.println("D");

}

}

}

22.阅读程序,并写出程序运行结果

class TObject{

TObject(){

System.out.println("A");

}

void m(String s){

System.out.println("B");

}

void m(int i){

System.out.println("C");

}

void m(){

System.out.println("D");

}

public String toString(){

return "E";

}

}

public class T22 {

public static void main(String[] args) {

TObject obj=new TObject();

System.out.println(obj);

obj.m();

obj.m(1);

obj.m("1");

}

}

23 阅读程序,并写出程序运行结果

abstract class P{

P(){

System.out.println("P");

}

abstract void goo();

}

class A extends P{

A(){

super();

}

void goo() {

System.out.println("A");

}

void foo(){

System.out.println("F");

}

}

class B extends P{

void goo() {

System.out.println("B");

}

void koo(){

System.out.println("K");

}

}

public class T23 {

public static void main(String[] args) {

A a=new A();

a.goo();

a.foo();

B b=new B();

b.koo();

}

}


24 阅读程序,并写出程序运行结果


interface IT{

void t1();

void t2();

}

abstract class TA implements IT{

public void t1() {

System.out.println("A");

}

public void t3() {

System.out.println("B");

}

}

class TB extends TA{

public void t1() {

System.out.println("C");

}

public void t2() {

System.out.println("D");

}

public void t2(int i) {

System.out.println("E");

}

}

public class T24 {

public static void main(String[] args) {

IT obj=new TB();

obj.t1();

obj.t2();

TA aObj=(TA)obj;

aObj.t1();

aObj.t3();

TB bObj=(TB)obj;

bObj.t2(100);

}

}


三、程序填空(每空2分,共 20分)

 

程序一:如下程序测试Math.random生成随机数的奇偶比率,仔细阅读程序和运行结果,补全空白处的代码。

 

public class T25 {

 

int[] createArray(int count){

int number[]=       (25)       ; //创建长度为countint数组

for(int i=0;i

int n=(int)(Math.random()*1000);

number[i]=      (26)      ;//number数组中写入生成的随机数

System.out.println("number["+i+"]="+number[i]);

}

return        (27)       ; //返回生成的数组

}

 

double calculateOddRate(int[] number){

int count=    (28)      ; //读取数组元素的个数,即要计算平均数的整数个数

double odd=0; //奇数计数

for(int n:number){

if(     (29)          ){//如果n是奇数,奇数计数加1

odd++;

}

}

return odd/count;

}

public static void main(String[] args) {

T25 t=new T25();

int[] number=t.createArray(100);

double oddRate=t.calculateOddRate(number);

System.out.println("奇数为:"+oddRate*100+"%");

System.out.println("偶数为:"+(1-oddRate)*100+"%");

}

}

运行结果:

number[0]=907

..//此处省略98

number[99]=598

奇数为:52.0%

偶数为:48.0%

 

程序二:以下程序是通过JDBC读取数据表Student的基本操作,认真阅读程序和运行结果,补全程序的空白处。

表:Students

ID

NAME

GENDER

2

name02

4

name04

部分程序如下

class Student{

private int id;

private String name;

private String gender;

public Student(int id, String name, String gender) {

super();

this.id = id;

this.name = name;

this.gender = gender;

}

…………//此处省略n

public String toString() {

return "Student [id=" + id + ", name=" + name + ", gender=" + gender+ "]";

}

}

public class T30 {

 

Connection getConnection(){

……//此处省略n

}

 

 

 

List queryAllStudent(){

List stuList=         (30)        ;//创建可以存储StudentList

Connection conn=null;

Statement st=null;

ResultSet rs=null;

try {

conn=getConnection();

st=       (31)      .createStatement(); //通过连接创建statement

rs=st.executeQuery("SELECT ID,NAME,GENDER FROM Students");

while(     (32)         ){ //结果是否有记录

Student  stu=new Student(

rs.getInt("ID"),

rs.getString("NAME"),

rs.getString("GENDER"));

        (33)             ; //stu对象加入到stuList

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

rs.close();

st.close();

conn.close();

} catch (SQLException e) {}

}

return stuList;

}

 

void showStudent(List stuList){

for(______(34)_______s:stuList){  //指明s的类型

System.out.println(s);

}

}

public static void main(String[] args) {

T30 demo=new T30();

List stuList=demo.queryAllStudent();

demo.showStudent(stuList);

}

}

运行结果

Student [id=2, name=Name02, gender=]

Student [id=4, name=Name04, gender=]

 

四、基本代码编写(共12分)

 

35(5)编写一个main方法,计算如下数组元素的平均值

double source[]={2,5,9,10,3};

36、(7分)文件名解析器,仔细阅读如下代码和运行结果,完成WindowsFileNameParse类的代码,执行后得到给定的运行结果。

interface FileNameParse{

 void showSourceFileName();

 String getDiskName();

 String getFullFileName();

 String getFileName();

 String getExtendName();

 String getDir();

}

class WindowsFileNameParse implements FileNameParse{

private String fileName;

WindowsFileNameParse(String fileName){

this.fileName=fileName;

}

public void showSourceFileName(){

System.out.println("解析文件名:"+this.fileName);

}

//////////////////////////////////////////////////////////////////////////////////////

//////////请完成此类的中其他方法的代码////////////////////////

}

public class T36 {

public static void main(String[] args) {

FileNameParse fp=new WindowsFileNameParse("d:/My Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java");

fp.showSourceFileName();

System.out.println("盘符:"+fp.getDiskName());

System.out.println("文件全名(带扩展名)"+fp.getFullFileName());

System.out.println("文件名(不带扩展名)"+fp.getFileName());

System.out.println("文件扩展名:"+fp.getExtendName());

System.out.println("路径(不带盘符):"+fp.getDir());

}

}

运行结果

解析文件名:d:/My Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java

盘符:d

文件全名(带扩展名)T37.java

文件名(不带扩展名)T37

文件扩展名:java

路径(不带盘符):/My Documents/MyJob/Pages/2012-2013-2/PageA/src

String类部分的api doc

public int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

Examples:  "abca".indexOf("a")  return 0

Parameters:

str - the substring to search for.

Returns:

the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.

public int lastIndexOf(String str)

Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().

Examples: "abca".lastIndexOf("a") return 3

Parameters:

str - the substring to search for.

Returns:

the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.

public String substring(int beginIndex)

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

"Harbison".substring(3) returns "bison"

"emptiness".substring(9) returns "" (an empty string)

Parameters:

beginIndex - the beginning index, inclusive.

Returns:

the specified substring.

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

 "hamburger".substring(4, 8) returns "urge"

 "smiles".substring(1, 5) returns "mile"

Parameters:

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

Returns:

the specified substring.

 

五、设计并编程(共8分)

37、仔细阅读给定的代码和程序运行结果,完方法size()del()代码编写。

MyList类是可以存储字符串对象的、基于链表的List的简单实现

class MyListNode {

String element;

MyListNode nextNode = null;

MyListNode(String element) {

this.element = element;

}

}

class MyList {

private MyListNode firstNode = null;

public void add(String element) { //加入字符串到MyList

MyListNode node = new MyListNode(element);

if (firstNode == null) {

firstNode = node;

} else {

MyListNode lastNode = firstNode;

while (lastNode.nextNode != null) {

lastNode = lastNode.nextNode;

}

lastNode.nextNode = node;

}

}

public int size() {//返回MyList中节点数

//////////////////完成此方法代码////////////////

}

public String[] toArray() {//MyList中存储的所有字符串转化成String[]

int count = size();

if (count == 0) {

return null;

}

String[] dest = new String[count];

MyListNode lastNode = firstNode;

int i = 0;

do {

dest[i++] = lastNode.element;

lastNode = lastNode.nextNode;

} while (lastNode != null);

return dest;

}

 

 

public void del(String element) {//删除节点元素值为element字符串的节点

 

///////////////完成此方法代码/////////////////

 

 

}

}

public class T37 {

public static void main(String[] args) {

MyList myList = new MyList();

myList.add("s001");

myList.add("s002");

myList.add("s003");

myList.add("s004");

myList.add("s005");

System.out.println("SIZE:" + myList.size());

String sa1[] = myList.toArray();

showArray(sa1);

myList.del("s001");

myList.del("s003");

myList.del("s005");

System.out.println("SIZE:" + myList.size());

String sa2[] = myList.toArray();

showArray(sa2);

}

static void showArray(String[] sa) {

System.out.print("[");

for (String s : sa) {

System.out.print(s + "  ");

}

System.out.println("]");

}

}

运行结果

SIZE:5    [s001  s002  s003  s004  s005  ]

SIZE:2

[s002  s004  ]

 

 

答案:

一、选择题

1B      2C    3C      4A       5D 6C     7B      8A       9、B 10、D11C 12C     13D 14、C 15、A 16、B   17、A 18、B  19、D 20、C

二、填空题


21ABDCB     22AEDCB     23PAFPK       24CDCBE

三、 程序填空

25new int[count]      26 n      27 number   28number.length    2 9 n%2==1    30new ArrayList()new LinkedList()    31 conn    32 rs.next()   33 stuList.add(stu)   34Student


四、基本代码编写

 

35、参考程序

public static void main(String[] args) {

int source[]={2,5,9,10,3};

double sum=0;

int count=source.length;

for(int i=0;i

sum+=source[i];

}

System.out.println(sum/count);

}

36、参考程序

public String getDiskName(){(1分)

return fileName.substring(0,fileName.indexOf(":"));

}

public String getFullFileName(){(1分)

return fileName.substring(fileName.lastIndexOf("/")+1);

}

public String getFileName(){2分)

return fileName.substring(fileName.lastIndexOf("/")+1, fileName.lastIndexOf("."));

}

public String getExtendName(){(1分)

return fileName.substring(fileName.lastIndexOf(".")+1);

}

public String getDir(){2分)

return fileName.substring(fileName.indexOf(":")+1, fileName.lastIndexOf("/"));

}

五、设计并编程(共8分)

37、参考程序

public int size() { (3分)

int len = 0;

if (firstNode != null) {

len = 1;

MyListNode lastNode = firstNode;

while (lastNode.nextNode != null) {

len++;

lastNode = lastNode.nextNode;

}

}

return len;

}

public void del(String element) {(5分)

if (firstNode != null) {

if (firstNode.element.equals(element)) {

firstNode = firstNode.nextNode;

} else {

MyListNode preNode = firstNode;

MyListNode lastNode=firstNode.nextNode;

while(lastNode!=null) {

if (lastNode.element.equals(element)) {

preNode.nextNode=lastNode.nextNode;

return;

}

preNode = lastNode;

lastNode = lastNode.nextNode;

}

}

 

}

}

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有