
1、d 面向对象的三大特征是 封装、继承、多态
2、d 不能以数字开头 只能一下划线,$,字母 作为开头
3、a 不做解释
4、b
5、c 子类只能有一个父类,父类可以有多个子类,子类可以实现多个接口
6、b 因为case2没加break;
shape接口
圆类
矩形类
三角形类1
三角形类2
测试类
你自己啥也不参考,自己写个学生信息管理系统。就差不多了。然后再写个生产者和消费者的关系,(线程)。
神啊 ,,原来是你问的啊。。呵呵。我去找找。
简答、论述、程序设计
1、请问 ”2” 、’2’、2之间有什么不同?并回答下面程序的输出结果是什么 (提示:‘2’的ASCII码值是50 )(8分)
class test
{
public static void main(String[] args)
{
int a=2;
int b='2';
Systemoutprintln (a+a);
Systemoutprintln (a+b);
}
}
2、你认为java、C、C++他们之间有没有联系和区别?和C、C++相比,java有哪些优点?(10分)
3、下面两段代码具有多处错误,请找出你认为错误的地方,作上标记,并说明为何出错。如果你认为该行没有错误,请打上√ (12分)
i)public int search (int [10] number) 1、 ______________________
{
number = new int[10]; 2、 ______________________
for (int i=0;i<numberlength;i++) 3、_______________________
{
number[i]=number[i-1]+number[i+1]; 4、_______________________
return number; 5、_______________________
}
}
ii)
class MyclassOne
{
public final int A=365;
public int b;
private float c;
private void myMethodOne(int a)
{
b=a;
}
public float myMethodTwo()
{
return 36;
}
}
class MyClassMain
{
public static void main(String[] args)
{
MyClassOne w1=new MyClassOne();
w1A=12; 6、______________________
w1b=12; 7、_______________________
w1c=12; 8、_______________________
w1myMethodOne(12); 9、_______________________
w1myMethodOne(); 10、_____________________
Systemoutprintln(w1myMethodTwo(12)); 11、___________________
w1c=w1myMethodTwo(); 12、____________________
}
}
请简要说明下面程序的功能
1) public class Sum ( 5分 )
{ public static void main( String args[ ])
{ double sum = 00 ;
for ( int i = 1 ; i <= 100 ; i + + )
sum += 10/(double) i ;
Systemoutprintln( "sum="+sum );
}
}
程序设计:(10分)
编写一个java程序。要求该程序能够具有以下功能:
定义一个坐标类coord。坐标类coord必须满足如下要求:
a)coord类含有两部分数据:横坐标x和纵坐标y。x和y的类型都是int类型。
b)coord类的方法有:
coord( ) : 构造函数,将横坐标和纵坐标的值都赋值为0
coord( int x , int y ) : 构造函数,形参 x 为横坐标的初值,y为纵坐标的初值。
coord coordAdd(int x, int y) : 将当前坐标对象与形参的值相加,所得的结果仍是一个坐标,返回给此方法的调用者。
(提示:可以将两个坐标相加定义为横坐标和横坐标相加,纵坐标和纵坐标相加。例如(1,2)+(3,4)=((1+3),(2+4))=(4,6))
程序设计:(10分)
请编写一个java程序,利用该程序计算并输出 1+2+3+……+100的值
答案
1、请问 ”2” 、’2’、2之间有什么不同?并回答下面程序的输出结果是什么 (提示:‘2’的ASCII码值是50 )(8分)
答:"2"是字符串,'2'算字符。2是数字。
class test
{
public static void main(String[] args)
{
int a=2;
int b='2';
Systemoutprintln (a+a);
Systemoutprintln (a+b);
}
} 输出结果为:4
52
2、你认为java、C、C++他们之间有没有联系和区别?和C、C++相比,java有哪些优点?(10分)
答:java是以c及c++为基础的。许多地方沿用了它们的思想。但最主要的,java是完全面向对象的编程,而c是面向过程,c+则不完全是面向对象。java相对说来,编程更方便,安全,结构,模块化强,易于移植,跨平台性好等。
3、下面两段代码具有多处错误,请找出你认为错误的地方,作上标记,并说明为何出错。如果你认为该行没有错误,请打上√ (12分)
i)public int search (int 错[10] number) 1、 引用时只能是类型不能带值{
number错 = new int[10]; 2、 数组没有下标
for (int i=0;i<numberlength;i++) 3、对
{
number[i]=number[i-1]+number[i+1]错; 4、数组在i+1在i=numberlength-1是超界
return number; 5、对
}
}
ii)
class MyclassOne
{
public final int A=365;
public int b;
private float c;
private void myMethodOne(int a)
{
b=a;
}
public float myMethodTwo()
{
return 36;
}
}
class MyClassMain
{
public static void main(String[] args)
{
MyClassOne w1=new MyClassOne();
w1A=12; 6、错误,试图给final型再次赋值
w1b=12; 7、对Myclassone中b赋值
w1c=12; 8、对myclassone float c赋值
w1myMethodOne(12); 9、调用myclassone的mymethodone形参为int的方法,
w1myMethodOne(); 10、调用myclassone的mymethodone无形参的方法Systemoutprintln(w1myMethodTwo(12)); 11、输出myclassone的mymethodtwo(12)值
w1c=w1myMethodTwo(); 12让c引用mymethodtwo方法
}
}
请简要说明下面程序的功能
1) public class Sum ( 5分 )
{ public static void main( String args[ ])
{ double sum = 00 ;
for ( int i = 1 ; i <= 100 ; i + + )
sum += 10/(double) i ;
Systemoutprintln( "sum="+sum );
}
} 功能为 求出1/1+1/2+1/3+1/4…+1/100的和
程序设计:(10分)
编写一个java程序。要求该程序能够具有以下功能:
定义一个坐标类coord。坐标类coord必须满足如下要求:
a)coord类含有两部分数据:横坐标x和纵坐标y。x和y的类型都是int类型。
b)coord类的方法有:
coord( ) : 构造函数,将横坐标和纵坐标的值都赋值为0
coord( int x , int y ) : 构造函数,形参 x 为横坐标的初值,y为纵坐标的初值。
coord coordAdd(int x, int y) : 将当前坐标对象与形参的值相加,所得的结果仍是一个坐标,返回给此方法的调用者。
(提示:可以将两个坐标相加定义为横坐标和横坐标相加,纵坐标和纵坐标相加。例如(1,2)+(3,4)=((1+3),(2+4))=(4,6))
public class Coord {
int x=100;
int y=200;
public Coord(){
thisx=0;thisy=0;
}
public Coord(int x,int y) {
x=thisx;y=thisy;
}
void coordAdd(int x,int y){
thisx+=x;
thisy+=y;
}
public static void main (String[] args) {
}
}
程序设计:(10分)
请编写一个java程序,利用该程序计算并输出 1+2+3+……+100的值
Sumjava
public class Sum {
public Sum() {
int sum=0;
for(int i=1;i<=100;i++)
sum+=i;
Systemoutprintln("1+2+3+100="+sum);
}
public static void main (String[] args) {
new Sum();
}
}
Java试题(一)
一、 选择
1欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的 ?
A ArrayList myList=new Object();
B List myList=new ArrayList();
C ArrayList myList=new List();
D List myList=new List();
2paint()方法使用哪种类型的参数
A Graphics
B Graphics2D
C String
D Color
3指出正确的表达式
A byte=128;
B Boolean=null;
C long l=0xfffL;
D double=09239d;
4指出下列程序运行的结果
public class Example{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
exchange(exstr,exch);
Systemoutprint(exstr+" and ");
Sytemoutprint(exch);
}
public void change(String str,char ch[]){
str="test ok";
ch[0]='g';
}
}
A good and abc
B good and gbc
C test ok and abc
D test ok and gbc
5运行下列程序, 会产生什么结果
public class X extends Thread implements Runable{
public void run(){
Systemoutprintln("this is run()");
}
public static void main(String args[])
{
Thread t=new Thread(new X());
tstart();
}
}
A 第一行会产生编译错误
B 第六行会产生编译错误
C 第六行会产生运行错误
D 程序会运行和启动
6要从文件" filedat"文件中读出第10个字节到变量C中,下列哪个方法适合
A FileInputStream in=new FileInputStream("filedat"); inskip(9); int c=inread();
B FileInputStream in=new FileInputStream("filedat"); inskip(10); int c=inread();
C FileInputStream in=new FileInputStream("filedat"); int c=inread();
D RandomAccessFile in=new RandomAccessFile("filedat"); inskip(9); int c=inreadByte();
7容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变?
A CardLayout
B FlowLayout
C BorderLayout
D GridLayout
8给出下面代码:
public class Person{
static int arr[] = new int[10];
public static void main(String a[])
{
Systemoutprintln(arr[1]);
}
}
那个语句是正确的?
A 编译时将产生错误;
B 编译时正确,运行时将产生错误;
C 输出零;
D 输出空。
9哪个关键字可以对对象加互斥锁?
A transient
B synchronized
C serialize
D static
10下列哪些语句关于内存回收的说明是正确的
A 程序员必须创建一个线程来释放内存;
B 内存回收程序负责释放无用内存
C 内存回收程序允许程序员直接释放内存
D 内存回收程序可以在指定的时间释放内存对象
11下列代码哪几行会出错:
1) public void modify() {
2) int I, j, k;
3) I = 100;
4) while ( I > 0 ) {
5) j = I 2;
6) Systemoutprintln (" The value of j is " + j );
7) k = k + 1;
8) I--;
9) }
10) }
A line 4
B line 6
C line 7
D line 8
二、多项选择
1执行下列代码后,哪个结论是正确的 String[] s=new String[10];
A s[10] 为 "";
B s[9] 为 null;
C s[0] 为 未定义
D slength 为10
2下面的表达式哪个是正确的
A String s="你好";int i=3; s+=i;
B String s="你好";int i=3; if(i==s){ s+=i};
C String s="你好";int i=3; s=i+s;
D String s="你好";int i=3; s=i+;
E String s=null; int i=(s!=null)&&(slength>0)slength():0;
3选出合理的标识符
A _sys1_lll
B 2mail
C $change
D class
4哪个布局管理器使用的是组件的最佳尺寸( preferred size)
A FlowLayout
B BorderLayout
C GridLayout
D CardLayout
EGridBagLayout
5下列哪个方法可用于创建一个可运行的类
A public class X implements Runable{ public void run(){ } }
B public class X implements Thread{ public void run(){ } }
C public class X implements Thread{ public int run(){ } }
D public class X implements Runable{ protected void run(){ } }
Epublic class X implements Thread{ public void run(){ } }
6下面哪个方法可以在任何时候被任何线程调用
A notify()
B wait()
C notifyAll()
D sleep()
Eyield()
Fsynchronized(this)
7构造BufferedInputStream的合适参数是哪个
A BufferedInputStream
B BufferedOutputStream
C FileInputStream
D FileOuterStream
E File
8下列说法正确的是
A javalangClonable是类
B javalangRunnable是接口
C Double对象在javalang包中
D Double a=10是正确的java语句
9指出正确的表达式
A double a=10;
B Double a=new Double(10);
C byte a = 340;
D Byte a = 120;
10定义一个类名为"MyClassjava"的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为:
A private class MyClass extends Object
B class MyClass extends Object
C public class MyClass
D public class MyClass extends Object
11指出下列哪个方法与方法public void add(int a){}为合理的重载方法。
A public int add(int a)
B public void add(long a)
C public void add(int a,int b)
D public void add(float a)
12如果下列的方法能够正常运行,在控制台上将显示什么?
public void example(){
try{
unsafe();
Systemoutprintln("Test1");
}
catch(SafeException e)
{Systemoutprintln("Test 2");}
finally{Systemoutprintln("Test 3");}
Systemoutprintln("Test 4");
}
A Test 1
B Test 2
C Test 3
D Test 4
13下列哪些情况可以终止当前线程的运行?
A 抛出一个例外时。
B 当该线程调用sleep()方法时。
C 当创建一个新线程时。
D 当一个优先级高的线程进入就绪状态时。
三、 填空题
1执行下列代码后的结果是什么 int x,a=2,b=3,c=4; x=++a+b+++c++;
2 包包含了Collection的接口和类的API
3main方法的声明格式包括
4下列程序中构造了一个SET并且调用其方法add(),输出结果是
public class A{
public int hashCode(){return 1;}
public Boolean equals(Object b){return true}
public static void main(String args[]){ Set set=new HashSet();
setadd(new A());
setadd(new A());
setadd(new A());
Systemoutprintln(setsize());
}
}
5下列程序的运行结果是
class A{
class Dog{
private String name;
private int age;
public int step;
Dog(String s,int a)
{
name=s;
age=a;
step=0;
}
public void run(Dog fast)
{
faststep++;
}
}
public static void main (String args[])
{
A a=new A();
Dog d=anew Dog("Tom",3);
dstep=25;
drun(d);
Systemoutprintln(dstep);
}
}
四、 编程题
1编写一个输出"Hello World!"的程序,用两种方式实现(Application、Applet)。
2打印输出10行杨晖三角形
3有下面一段Server段程序,目的是能够同时服务多个客户,客户的请求是一句话(一个 String)。如果这个请求的内容是字符串"plain"的话,服务器仅将"hello"字符串返回给用户。否则将用户的话追加到当前目录的文本文件 Memotxt中(路径为"Memotxt"),并向用户返回"OK"。注意Server并发的处理多用户,Memotxt被共享,要求不能出现数据不一致。Server的程序如下文件Serverjava:
public class Server{
public static void main(String args[]){
MemoController memoController = new MemoController();
try{
ServerSocket ss = new ServerSocket(1999);
while (true){
Socket s = ssaccept();
try{
UserThread t = new UserThread(s, memoController);
tstart();
}catch(Exception e){
eprintStackTrace();
}
}
}catch(Exception e){
eprintStackTrace();
}finally{
memoControllerclose();
}
}
}
类UserThread程序如下:
文件UserThreadjava:
public class UserThread extends Thread{
Socket s;
MemoController memo;
public UserThread (Socket s, MemoController memo){
thiss = s;
thismemo = memo;
}
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(sgetInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(sgetOutputStream()));
String req = brreadLine();
if (reqequals("plain")){
pwprintln("hello");
}else{
memoappend(req);
pwprintln("OK");
}
pwflush();
pwclose();
brclose();
sclose();
}catch(Exception e){
eprintStackTrace();
}
}
}
请根据题目的要求和现有的Serverjava, UserThreadjava的程序完成类MemoControllerjava的程序。
4用输入/输出写一个程序,让用户输入一些姓名和电话号码。每一个姓名和号码将加在文件里。用户通过点"Done"按钮来告诉系统整个列表已输入完毕。 如果用户输入完整个列表,程序将创建一个输出文件并显示或打印出来。 格式如:555-1212,Tom 123-456-7890,Peggy L 234-5678,Marc 234-5678,Ron 876-4321,Beth&Brian 331424570,Jean-Marc
四、 编程题答案
1
public class HelloWorld
{
public static void main(String args[])
{
Systemoutprintln("Hello,World!");
}
}
import javaawtGraphics;
import javaappletApplet;
public class HelloWorld extends Applet{
String s;
public void init(){
s="Hello World!";
}
public void paint(Graphics g){
gdrawString(s,25,25);
}
}
2
class yanghui
{
public static void main (String args[])
{
int i,j;
int yhlevel=10;
int yanghui[][];
Systemoutprintln("杨晖三角形:");
yanghui=new int[yhlevel][];
for(i=0;i<yanghuilength;i++)
yanghui[i]=new int[i+1];
yanghui[0][0]=1;
for (i=1; i<yanghuilength;i++)
{
yanghui[i][0]=1;
for(j=1;j<yanghui[i]length-1;j++)
yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];
yanghui[i][yanghui[i]length-1]=1;
}
for (i=0; i<yanghuilength;i++)
{
for(j=0;j<yanghui[i]length;j++)
Systemoutprint(yanghui[i][j]+" ");
Systemoutprintln();
}
}
}
输出结果是:
杨晖三角形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
3
import javaio;
public class MemoController{
FileOutputStream fos;
OutputStreamWriter osw;
BufferedWriter bw;
public MemoController(){
try{
fos=new FileOutputStream("memotxt",true);
osw=new OutputStreamWriter(fos);
bw=new BufferedWriter(osw);
}catch(FileNotFoundException e){};
}
public synchronized void append(String s){
try{
bwwrite(s,0,slength());
bwflush();
bwclose();
oswclose();
fosclose();
}catch(IOException e){}
}
public static void main(String args[]){
MemoController mmc=new MemoController();
mmcappend("I am xubin ");
}
}
4
import javaio;
class Phones
{
static FileOutputStream fos;
public static final int lineLength = 81;
public static void main(String args[]) throws IOException
{
byte[] phone = new byte[lineLength];
byte[] name = new byte[lineLength];
int I;
try
{
fos = new FileOutputStream("phonenumbers");
}
catch(FileNotFoundException e)
{ }
while (true)
{
Systemerrprintln("Enter a name (enter 'done' to quit)");
readLine(name);
if ("done"equalsIgnoreCase(new String(name,0,0,4)))
{
break;
}
Systemerrprintln("Enter the phone number");
readLine(phone);
for (int i=0;phone[i]!= 0;i++)
{
foswrite(phone[i]);
}
foswrite(',');
for (int i=0;name[i]!= 0;i++)
{
foswrite(name[i]);
}
foswrite('\n');
}
fosclose();
}
private static void readLine(byte line[]) throws IOException
{
int i=0,b=0;
while ((i<lineLength-1)&&((b=Systeminread())!='\n'))
{
line[i++] = (byte)b;
}
line[i]=(byte) 0;
}
}
一、 选择题答案
选择第1题
B
选择第2题
A
选择第3题
C
选择第4题
B
选择第5题
A
选择第6题
A
选择第7题
B
选择第8题
C
选择第9题
B
选择第10题
B
选择第11题
C
二、多项选择题答案
多项选择第1题
BD
多项选择第2题
AE
多项选择第3题
AC
多项选择第4题
AE
多项选择第5题
AE
多项选择第6题
DEF
多项选择第7题
AC
多项选择第8题
BC
多项选择第9题
AB
多项选择第10题
CD
多项选择第11题
CD
多项选择第12题
ACD
多项选择第13题
ABD
三、 填空题答案
填空第1题
x=10,a=3,b=4,c=5
填空第2题
javautil
填空第3题
(public )(static )(void)(main)(String args[])
填空第4题
1
填空第5题
26
15,parent,child,child
160,8888,9999
17:redblueredblueredblue
18:j=1
j=2
j=0
j=2
j=0
j=1
i=3
答案如下:1-5 C C D C D
6-10 A B C A A
11-15 C B B D 无
16-19 C D A B
1、 Unicode是一种____c_________
A) 数据类型 B)java包 C)字符编码 D)java类
2、 下面的逻辑表达式中合法的是__c________
A)(7+8)&&(9-5) B)(95)||(97) C)9>6&&8<10 D)(9%4)&&(83)
3、 假设int a=3,b=2,c=1,以下语句正确的是____d_______。
A) c=c/float(a//b) b) c=c/((float a)/b)
c) c=(float)c/(a/b) d) c= c/(int)(a/(float)b)
4、 2|3&5的值是_____c___________。
A)1 B)2 C)3 D)5
5、指出下列正确的语句: d
A) byte i = 389;
B) long lv = i3+45;
C) int x = 87L;
D) long l = 10;
6、指出下列类型转换中正确的是________a______。
A)int i=’A’ B)long L=84f C)int i=(boolean)89 D)int i=83
7、以下的选项中能正确表示Java语言中的一个整型常量的是(b )。
A) 12 B) -20 C) 1,000 D) 4 5 6
8、若所用变量都已正确定义,以下选项中,非法的表达式是(c )。
A) a != 4||b==1 B) 'a' % 3
C) 'a' = 1/2 D) 'A' + 32
9、对于一个三位的正整数 n,取出它的十位数字k(k为整型)的表达式是(a )。
A) k = n / 10 % 10 B) k = ( n - n / 100 100 )%10
C) k = n % 10 D) k = n / 10
10、现有一变量声明为boolean aa;下面赋值语句中正确的是(a )。
A)aa=false; B)aa=False; C)aa="true"; D)aa=0;
11、设有定义 int i = 6 ;,则执行以下语句后,i 的值为(c )。
i += i - 1;
A) 10 B) 121 C) 11 D) 100
12、下列语句序列执行后,ch1 的值是( b)。
char ch1=’A’,ch2=’W’;
if(ch1 + 2 < ch2 ) ++ch1;
A) ‘A’ B) ‘B’ C) ‘C’ D) B
13、设有定义 float x=35f, y=46f, z=57f;则以下的表达式中,值为true的是(b )。
A) x > y || x > z B) x != y
C) z > ( y + x ) D) x < y & ! ( x < z )
14、设有定义 int i=123; long j=456; 下面赋值不正确的语句是( d)。
A) j=i; B) j=(long)i; C) i=(int)j; D) i=j;
16、以下的变量定义语句中,合法的是(c )。
A) float $_5= 34F; B) byte b1= 15678;
C) double a =10000;
D) int _abc_ = 3721L;
17、以下字符常量中不合法的是(d )。
A) '|' B) '\' C) "\n" D) '我'
18、若以下变量均已正确定义并赋值,下面符合Java语言语法的语句是(a )。
A) b = a!=7 ; B) a = 7 + b + c=9;
C) i=123 % 4; D) a = a + 7 = c + b;
19、执行下列程序段后,b, x, y的值分别是( b)。
int x=6,y=8;
boolean b;
b=x>y&&++x==--y;
A) true , 6, 8 B) false , 7, 7
C) true, 7, 7 D) false, 6, 8
才子_辉祝您愉快
1要激活一个java小应用程序applet需具有 class 文件。
2一个java源程序文件必须以 java为其扩展名。
3java编译器产生的文件的扩展名为 class。
4每个java程序语句以 分号 结束。
5所有程序均可以用 顺序结构,分支结构 和 循环结构 三种控制结构编写。
6一个只有在定义它的方法中可知的变量称为 局部变量
7被调用方法中的 return 语句可以将表达式的值回送给调用它方法。
8方法头中的关键字void 用来表示方法不还返回值。
9当applet开始执行时,调用一次init 方法。
10在java中可以使用许多方法同名,但各方法使用的参数的类型和数量不同,这称为方法 重载。
11限定词final 用来声明只读变量。
12指定为private 的类成员只能被此类中的成员访问。
13类的方法通常设为public,而类的实例变量一般设为private指定为 static的类成员可被此类作用域中的任何对象访问。
15关键字final表明一个对象或变量在初始化后不能被修改。
16声明为static的方法不能访问 非静态 类成员。
17如Alpha类继承Beta类,Alpha类被称为 子 类,Beta类被称为 父 类。
18四种成员访问限制是 public ,protected,private,friendly。
19如果一个类包含了一个或多个abstract方法,它是 抽象 类。
20线程通过调用sleep() 方法可以休眠一段时间,然后恢复运行。
21Applet类的 getImage()方法将图像装入applet。
22FileOutputStream、FileInputStream、RandomAccessFile文件流类的 close() 方法,可用于关闭文件。
23鼠标被移动时会发生 MouseEvent 事件。
24使用setLayout() 方法为构件容器设置布局管理器。
25Java的网络类在javanet包中
以上就是关于java练习题需要答案全部的内容,包括:java练习题需要答案、java程序设计题,求解答、2008年专升本考试就要来了,有关JAVA程序设计那位帮我~!!~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)