当前位置:文档之家› Java程序设计试题A

Java程序设计试题A

哈工大 2011 年 秋 季学期

Java 程序设计 试题A

一.单项选择题(每题3分,共30分)

1.下列选项中,哪项是Java 语言的关键字?( D ) A .Object

B .object

C .Class

D .class

2.下列选项中,哪项是不合法的Java 标识符?( B ) A .变量A

B .2Student

C .Hello_World

D .$100

3.下列选项中,哪项赋值语句不能通过编译?( D ) A .int a = 123; B .int a = 'A';

C .char c = 123;

D .char c = '123';

4.JDK 中的Color 类在下列哪个包中?( A ) A .java.awt

B .java.io

C .https://www.doczj.com/doc/339721921.html,ng

D .https://www.doczj.com/doc/339721921.html,

5.JPanel 容器的默认布局是?(

A ) A .FlowLayout

B .BorderLayout

C .GridLayout

D .CardLayout

6.下列选项中,哪项是正确的赋值语句?( D ) A .int a = 0.2; B .int a = 0.2f;

C .int a = null;

D .String s = null;

7.下面关于Java 语言的描述错误的是( B )。 A .一个类可以继承自一个父类 B .一个类可以继承自多个父类 C .一个类可以实现多个接口

D .一个接口可以继承自多个接口

8.下面程序的运行结果是( C )。

public class Test {

public static void main(String[] args) {

int a = 1;

int b = 2;

int c = 3;

int d = 4;

int result = (a > b) && (c++ < d) ? b:c;

System.out.println(result);

}

}

A.1 B.2 C.3 D.4

9.对下面程序的描述正确的是( D )。

public class Test {

static String[] arr = new String[10];

public static void main(String[] args) {

System.out.println(arr[1]);

}

}

A.编译错误

B.编译正确,运行产生异常

C.编译正确,运行输出0

D.编译正确,运行输出null

10.现有一个Java源文件,其中包含一个名为Student的public类,为了成功编译该文件需要满足以下哪个条件?( C )

A.源文件必须导入https://www.doczj.com/doc/339721921.html,ng包

B.Student类中必须定义一个main方法

C.源文件名必须为Student.java

D.源文件中必须有package语句

二.程序分析题(每题5分,共50分)

以下各题中的程序:如果你认为程序不能通过编译,请回答:【编译错误】;如果你认为程序能够通过编译,但运行时会出现异常,请回答:【编译正确,运行时产生异常】;如果你认为程序能够正确编译并正常运行,请写出程序的运行结果。

1.

class Test {

public static void main(String[] args) {

int i = 0;

while(true) {

i++;

if(i > 10) {

break;

}

}

System.out.println("i=" + i);

}

}

输出:i=11

1.

class Test {

public static void main(String[] args) {

String s = "我爱Java";

System.out.println(s.length());

}

}

输出:6

2.

class Test {

public static void add3(Integer i) {

int val = i.intValue();

val += 3;

i=new Integer(val);

}

public static void main(String args[]) {

Integer i = new Integer(2);

add3(i);

System.out.println(i.intValue());

}

}

输出:2

3.

class Test {

public static void main(String[] args) { String s = "Java";

hello(s);

System.out.println(s);

}

public static void hello(String s) {

s = "Hello:" + s;

}

}

输出:Java

4.

class Student {

private String name;

public Student(String name) {

https://www.doczj.com/doc/339721921.html, = name;

}

public void setName(String name) {

https://www.doczj.com/doc/339721921.html, = name;

}

public String getName() {

return name;

}

}

class Test {

public static void main(String[] args) { Student s = new Student("小明");

rename(s);

System.out.println(s.getName());

}

public static void rename(Student s) { s.setName("小强");

}

}

输出:小强

5.

class Test {

public static void main(String[] args) { int s = sum(1,2,3,4);

System.out.println(s);

}

public static int sum(int ... a) {

int s = 0;

for(int i=0;i

s += a[i];

}

return s;

}

}

输出:10

6.

class Fruit {

public void hello() {

System.out.println("我是水果");

}

}

class Grape extends Fruit {

public void hello() {

System.out.println("我是葡萄");

}

}

class Test {

public static void main(String[] args) { Fruit f = new Grape();

f.hello();

}

}

输出:我是葡萄

7.

class Fruit {

private String name;

https://www.doczj.com/doc/339721921.html, = name;

}

public void setName(String name) {

https://www.doczj.com/doc/339721921.html, = name;

}

public String getName() {

return name;

}

}

class Grape extends Fruit {

public Grape(String name) {

super(name);

}

}

class Test {

public static void main(String[] args) {

Fruit f = new Fruit("水果");

hello((Grape)f);

System.out.println(f.getName());

}

public static void hello(Grape g) {

g.setName("葡萄");

}

}

【编译正确,运行时产生异常】,运行异常如下:

Exception in thread "main" https://www.doczj.com/doc/339721921.html,ng.ClassCastException: Fruit cannot be cast to Grape at Test.main(Test.java:21)

8.

import java.util.Arrays;

class Student implements Comparable {

private String name;

private int age;

public Student(String name,int age) {

https://www.doczj.com/doc/339721921.html, = name;

this.age = age;

}

return name;

}

public int getAge() {

return age;

}

public int compareTo(Student other) {

if(age < other.age) return 1;

else if(age > other.age) return -1;

else return 0;

}

}

class Test {

public static void main(String[] args) {

Student[] ss = new Student[3];

ss[0] = new Student("小明",19);

ss[1] = new Student("小亮",20);

ss[2] = new Student("小丽",18);

Arrays.sort(ss);

for(Student s : ss)

System.out.println(s.getName() + "," + s.getAge() + "岁");

}

}

输出:小亮,20岁

小明,19岁

小丽,18岁

8.

class Fruit {

private String name;

public Fruit(String name) {

https://www.doczj.com/doc/339721921.html, = name;

}

public String getName() {

return name;

}

}

class Test {

public static void main(String[] args) {

Fruit f1 = new Fruit("苹果");

Fruit f2 = new Fruit("葡萄");

swap(f1,f2);

System.out.println(f1.getName());

}

public static void swap(Fruit f1,Fruit f2) { Fruit temp = f1;

f1 = f2;

f2 = temp;

}

}

输出:苹果

9.

class Test {

public static void main(String[] args) {

String s1 = "Hello";

String s2 = new String("Hello");

String s3 = "Hello";

System.out.println(s1.equals(s2));

System.out.println(s1 == s2);

System.out.println(s1 == s3);

}

}

输出:true

false

true

10.

class Fruit {

public static int count = 0;

public Fruit() {

count++;

}

static {

count++;

}

{

count++;

}

}

class Grape extends Fruit {

public Grape() {

count++;

}

static {

count++;

}

{

count++;

}

}

class Test {

public static void main(String[] args) {

Fruit f1 = new Grape();

Fruit f2 = new Grape();

System.out.println(Fruit.count);

}

}

输出:10

三.已知下面程序的输出结果是true,请完成Student类的代码。(10分)class Student {

public static Student getInstance(){

return null;

}

}

public class Test {

public static void main(String[] args) {

Student s1 = Student.getInstance();

Student s2 = Student.getInstance();

System.out.println(s1 == s2);

}

}

四.下图是程序运行后的界面,请完成程序中未给出的代码。(10分)

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Test {

public static void main(String[] args) {

JFrame f = new MyFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

class MyFrame extends JFrame {

public MyFrame() {

add( new MyPanel() );

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300,300);

setResizable(false);

}

}

class MyPanel extends JPanel {

private JButton bNorth = new JButton("North");

private JButton bSouth = new JButton("South");

private JButton bWest = new JButton("West");

private JButton bEast = new JButton("East");

private JButton bCenter = new JButton("Center");

public MyPanel() {

setFocusable(true);

add(bNorth,"North");

add(bSouth,"South");

add(bWest,"West");

add(bEast,"East");

add(bCenter,"Center");

}

}

相关主题
文本预览
相关文档 最新文档