八、继承与多态
1)以下程序调试结果为:
class Base{
Base(){
inti = 100;
System.out.print (i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.print(i);
}
}
A.编译错误B.200C.100200D.100
(2)以下程序调试结果为:
public class Test {
int m=5;
publicvoid some(int x) {
m=x;
}
public static void main(String args []) {
new Demo().some(7);
}
}
class Demo extends Test {
int m=8;
publicvoid some(int x) {
super.some(x);
System.out.println(m);
}
}
A.5B.8C.7D.无任何输出E.编译错误
3)试完成下述程序片段:
public class Point()
{int x,y;
publicPoint(int x,int y)
{=x;=y;
}
......
}
A.Point.xPoint.yB.无解C.x1y1D.this.xthis.y
(4)考虑如下类:
1. class Test(int i) {
2.void test(int i) {
3.System.out.println("I am an int.");
4.}
5.void test(String s) {
6.System.out.println("I am a string.");
7.}
8.
9.public static void main(String args[]) {
10.Test t=new Test();
11.char ch="y";
12.t.test(ch);
13.}
14. }
以下哪条为真?
A.行5不能通过编译,方法不能被覆盖.
B.行12不能通过编译,因为没有一个test()方法含字符参数.
C.代码可以编译但在12行将出现异常.
D.代码可以编译且产生如下输出: I am an int.
E.代码可以编译且产生如下输出: I am a String.
(5)类Test1定义如下:
1.publicclassTest1{
2.publicfloataMethod(floata,floatb){}
3.
4.}
将以下哪种方法插入行3是不合法的。()
A.publicfloataMethod(floata,floatb,floatc){}
B.publicfloataMethod(floatc,float d){}
C.publicintaMethod(inta,int b){}
D.private floataMethod(int a,int b,int c){}
6)考虑如下代码:
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest {
public static void main( String[] args ) {
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println( "Pine" );
if( tree instanceof Tree )
System.out.println( "Tree" );
if( tree instanceof Oak )
System.out.println( "Oak" );
else
System.out.println( "Oops" );
}
}
则输出结果中有哪些?
A.PineB.TreeC.ForestD.OopsE.无输出
7)以下程序的编译和运行结果为?
abstract class Base{
abstract public void myfunc();
public void another(){
System.out.println("Another method");
}
}
public class Abs extends Base{
public static void main(String argv[]){
Abs a = new Abs();
a.amethod();
}
public void myfunc(){
System.out.println("My Func");
}
public void amethod(){
myfunc();
}
}
A.输出结果为My Func
B.编译指示Base类中无抽象方法
C.编译通过,但运行时指示Base类中无抽象方法
D.编译指示Base类中的myfunc方法无方法体,没谁会喜欢该方法。
8)以下程序的调试结果为?
class Base{
public final void amethod(){
System.out.println("amethod");
}
}
public class Fin extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}
}
A.编译指示带有final方法的类自己必须定义为final
B.编译指示不能继承含有final方法的类
C.运行错误,原因是Base类没有定义为final类
D.运行输出amethod
9)在同一目录编译和运行以下两文件结果如何?
//文件P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//文件P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
A.两个均通过编译,P2运行时输出What a fancy method
B.没一个通过编译
C.两个均通过编译,但P2运行时出错
D.P1通过编译,但P2出现编译错误
10)以下程序的调试结果为?
public class Outer{
public String name = "Outer";
public static void main(String argv[]){
Inner i = new Inner();
i.showName();
}
private class Inner{
String name =new String("Inner");
void showName(){
System.out.println(name);
}
}
}
A.输出结果Outer
B.输出结果Inner
C.编译错误,因Inner类定义为私有访问
D.在创建Inner类实例的行出现编译错误
11)设有如下代码:
class Base{}
public class MyCast extends Base{
static boolean b1=false;
static int i = -1;
static double d = 10.1;
public static void main(String argv[]){
MyCast m = new MyCast();
Base b = new Base();
//Here
}
}
则在//Here处插入哪个代码将不出现编译和运行错误。
A.b=m;B.m=b;C.d =i;D.b1 =i;
12)设有如下代码:
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef obj = new ObRef();
Base b = new Base();
Object obj1 = new Object();
IFace obj2 = new CFace();
//Here
}
}
则在//Here处插入哪个代码将不出现编译和运行错误。
A.obj1=obj2;B.b=obj;C.obj=b;D.obj1=b;
13)设有类定义如下:
class Base{
public Base(int i){}
}
public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s, int i){
this(i);
//Here
}
}
以下哪条语句可以安排在//Here处?
A.MyOver m = new MyOver();
B.super();
C.this("Hello",10);
D.Base b = new Base(10);
14)设有类定义如下:
class InOut{
String s= new String("Between");
public void amethod(final int iArgs){
int iam;
class Bicycle{
public void sayHello(){
//Here
}
}
}
public void another(){
int iOther;
}
}
以下哪些语句可以安排在//Here处?
A. System.out.println(s);
B.System.out.println(iOther);
C. System.out.println(iam);
D.System.out.println(iArgs);
九、常用系统类
1)关于以下程序段,正确的说法是
1.Strings1="Hello";
2.Strings2="Hello";
3.if(s1= =s2)
4.System.out.println("s1= =s2");
5.if (s1.equals(s2))
6.System.out.println("s1.equals(s2) ");
A.行4与行6都将执行
B.行4执行,行6不执行
C.行6执行,行4不执行
D.行4、行6都不执行
2)要产生[20,999]之间的随机整数使用哪个表达式?
A.(int)(20+Math.random()*979)
B. 20+(int)(Math.random()*980)
C. (int)Math.random()*999
D. 20+(int)Math.random()*980
3)下列程序运行的结果为:
public class Example{
String str=new String("good");
char[] ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
Sytem.out.print(ex.ch);
}
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
4)设有如下程序
public class test {
public static void main(String args[]) {
Integer intObj=Integer.valueOf(args[args.length-1]);
int i = intObj.intValue();
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
运行程序,输入如下命令:
java test 2
则输出为:
A. testB. test -1C. 0
D. 1E. 2
5)下列程序运行的结果为:
public class test {
public static void main(String args[]) {
int i;
floatf = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));
System.out.println(i);
}
}
A. 4B. 5C.6
D. 6.1E. 9
6)如果以下条件成立,则用到java.lang.Math类中哪个方法?
method( -4.4 ) == -4;
A. round()B. min()C. trunc()D. abs()
E. floor()F. ceil()
7) set集合如何处理重复元素
A.如果加入一个重复元素将抛出异常
B.如果加入一个重复元素add方法将返回false
C.集合通过调用equals方法可以返回包含重复值的元素。
D.重复值将导致编译出错。
8)以下哪个方法是Vector类中增加一个新元素的方法。
A.addElementB. insertC. appendD. addItem
9)以下哪些方法是Collection接口的方法?
A. iteratorB. isEmptyC. toArrayD. setText