JAVA编程技术复习题
(2016-06-03 07:19:22)
标签:
教育 |
分类: 计算机 |
一、选择题
1.JDK提供的编译器是(
(A)java.exe
(C)javap.exe
2.以下作为Java程序入口的main
方法声明正确的(
(A)public void main(String
args[])
(B)public int main(String args[])
(C)public static void
main(String args[])
(D)public static int main(String args[])
3.以下标识符错误的是(
(A)Public
(B)张三
(C)class
4.java中定义字符串String s=”pzhu”,下面操作可以取得字符串长度的是(
(A)s.length()
5.如下定义数组,操作正确的是(
int a[]={1,2,3};
(A)a[3]=100
6.如下定义二维数组操作错误的是(
int a[][]={{1,2},{3}};
(A)a[0][1]=200
7. 以下数据类型存储空间最大的是(
(A)byte
(B)long
(C)float
8.
面向对象的三大特性,不包括如下 (
(A)异常 (B)封装 (C)继承 (D)多态
9、关于类的定义以下说法错误( )。
(A)类定义使用class关键字
(C)一个包可以包含多个类
10. 关于构造方法以下说法错误的是 (
(A)构造方法名必须与类名一致
(C)构造方法是通过new来调用
11.关于继承如下说法错误的是(
(A)Java是单继承的
(C)所有父类方法都可以被override的
12. 以下代码执行的结果是(
System.out.println("攀枝花学院pzhu".length());
(A)编译错误 (B)运行错误 (C)9 (D)14
13. 用来存储键值对的容器是(
(A)ArrayList
(B)LinkedList
(C)HashSet
14、java中用来抛出异常的关键字是(
(A)try
15.关于finally块中的代码,以下说法不正确的是(
(A)try块中的return语句会中断finally块中语句的执行
(B)无论finally块前的语句运行是否产生异常,其中的语句都会执行
(C)finally块中的语句通常中用作资源的清理
(D)try块中的System.exit(1)语句会中断finally块中语句的执行
16.关于Java字符串说法错误的是(
(A)Java中的字符串是常量
(C)Java中的字符串存储在常量池中
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{}
(C)class A implements IA{void f(String
s){}}
二、程序阅读
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[]=
for(int i=0;i
int n=(int)(Math.random()*1000);
number[i]=
System.out.println("number["+i+"]="+number[i]);
}
return
}
double calculateOddRate(int[] number){
int count= (28)
double odd=0; //奇数计数
for(int n:number){
if(
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=
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=getConnection();
st=
rs=st.executeQuery("SELECT ID,NAME,GENDER FROM Students");
while(
Student
rs.getInt("ID"),
rs.getString("NAME"),
rs.getString("GENDER"));
}
} 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){
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
36、(7分)文件名解析器,仔细阅读如下代码和运行结果,完成WindowsFileNameParse类的代码,执行后得到给定的运行结果。
interface FileNameParse{
}
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
Returns the index within this string of the first occurrence of the specified substring.
Examples:
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
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")
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
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
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:
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
SIZE:2
[s002
答案:
一、选择题
1、B
二、填空题
21、ABDCB
三、
25、new
int[count]
四、基本代码编写
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;
}
}
}
}