甲骨文大赛校内测试题(含答案)
![甲骨文大赛校内测试题(含答案)](https://img.360docs.net/imgdf/06pke36j7t5zzhakcl7x-f1.webp)
![甲骨文大赛校内测试题(含答案)](https://img.360docs.net/imgdf/06pke36j7t5zzhakcl7x-a2.webp)
甲骨文程序设计大赛试题
姓名:专业班级:
一、单项选择题:
1. Map
请问以下哪个赋值语句会出错?(D)
A、map=new HashMap<>( );
B、map=new HashMap
C、map=new HashMap
D、map=new LinkedHash Map
2.代码片段:
public class Certkiller3 implements Runnable {
public void run( ) {
System.out.print("running");
}
public static void main(String[ ] args) {
Thread t = new Thread(new Certkiller3( ));
t.run( );
t.run( );
t.start( );
}
}
运行结果是(E)。
A、编译出错
B、运行时抛出异常
C、代码正常执行并且输出:running
D、代码正常执行并且输出:runningrunning
E、代码正常执行并且输出:runningrunningrunnng
3.代码片段:
public class Test {
private int a;
public int b;
protected int c;
int d;
public static void main(String[ ] args) {
Test test = new Test( );
int a = test.a++;
int b = test.b--;
int c = test.c++;
int d = test.d--;
System.out.println(a + " - " + b + " - " + c + " - " + d);
}
}
下列哪个说法是正确的?(C)
A、编译错误,因为变量a,b,c和d没有被初始化
B、编译错误,因为变量a无法被访问
C、编译成功并输出0–0–0–0
D、编译成功并输出1 - - 1 –1 - - 1
4. 代码片段1:
public class ComplexCalc {
public int value;
public void calc( ) { value += 5; }
}
代码片段2:
public class MoreComplexCalc extends ComplexCalc {
public void calc( ) { value -= 2; }
public void calc(int multi) {
calc( );
super.calc( );
value *= multi;
}
public static void main(String[ ] args){
MoreComplexCalc calc = new MoreComplexCalc( );
calc.calc(3);
System.out.println("Oh it is: " + calc.value);
}
}
请问编译运行结果是?(A)
A、Oh it is: 9
B、编译出错
C、Oh it is: 15
D、Oh it is: -6
E、代码正常执行但没有输出
F、运行时抛出异常
G.、Oh it is: 6 H、Oh it is: -15
5. 代码片段:
void waitForSignal( ){
Object obj=new Object( );
synchronized(Thread.currentThread( )){
obj.wait( );
obj.notify( );
}
}
以下哪个描述是正确的?(A)
A、需要处理InterruptedException
B、代码能编译但可能运行时抛出IllegalStateException
C、运行10分钟后代码抛出TimeOutException
D、需要把obj.wait( )替换为((Thread)obj).wait( )后代码才能通过编译。
E、把obj.wait( )和obj.notify( )这两句调换一下位置,能使代码执行。
6.代码片段:
contestKiller=new ReallyBigObject( );
//这里省略部分代码
contestKiller=null;
/*在这里补充代码*/
以下哪一项的代码是告诉虚拟机尽最大的能力去回收contestKiller这个对象所占用的内存?(B)
A、Runtime.getRuntime( ).freeMemory( )
B、Runtime.gc( )
C、System.freeMemory( )
D、Runtime.getRuntime( ).growHeap( )
7.代码片段:
public void aSafeMethod(Object value){
//在这里检查方法的参数
//这里省去其他的代码
Sytem.out.println(value.toString( ));
}
代码中的方法要求传入的参数是非空的,请问有什么比较好的方式去处理一个空值?(B)
A、assert value=null;
B、if(value==null){throw new Illeg alArgumentException(“value can not be null”);}
C、if(value==null){throw new AssertionException(“value can not be null”);}
D、assert value !=null :”value can not be null”;
8.代码片段:
package certkiller;
class Target{
p ublic String name=”hello”;
}
哪些类能够直接访问并且改变name这个变量的值?(A)
A、任意类
B、只有Target这个类
C、certkiller包下的类
D、Target的子类
9. 代码:String text="Welcome to Java contset";
String[ ] words=text.split("\s");
System.out.println(words.length);
请问编译运行的结果是什么?( D )
A、0
B、1
C、4
D、编译出错
E、运行时抛出一个异常
10.代码: String[ ] elements={ "for","tea","too" };
String first=( elements.length > 0 ) ? elements[0] : null ;
以下哪个是正确的结果?(D)
A、编译失败
B、运行时抛出异常
C、first的值被设为null
D、first的值被设为“for”
11.代码片段:System.out.format("Pi is approximately %d.", Math.PI);
请问执行的结果是什么?(D)
A、编译出错
B、Pi is approximately 3.
C、Pi is approximately 3.141593.
D、运行时抛出异常
12.代码:public class Test {
public Test( ){
System.out.print("test ");
}
public Test(String val){
this( );
System.out.print("test with "+val);
}
public static void main(String[ ] args){
Test test=new Test("wow");
}
}
请问编译运行的结果是什么?(B)
A、test
B、test test with wow
C、test with wow
D、编译失败
13.代码片段:
public class JavaContest {
public static void fun(short n){ System.out.print("short "); }
public static void fun(Short n){ System.out.print("SHORT "); }
public static void fun(Long n){ System.out.print("LONG ");}
public static void main(String[ ] args){
Short y=0;
int z=y;
fun(y);
fun(z);
}
}
请问编译运行的结果是什么?(C)
A、short Long
B、SHORT LONG
C、编译出错
D、运行时抛出异常
14.如下代码:
public static void main(String[ ] args){
method1(1,2);
System.out.print(" java"); }
public static void method1(int x1,int x2){
System.out.print("hello"); }
public static void method1(int x1,int x2,int x3){
System.out.print("hi");
}
}
请问编译运行的结果是什么?(A)
A、hello java
B、编译失败
C、hi java
D、hellohi java
E、hihello java
15.代码:public class Person {
private String name;
public Person(String name) { https://www.360docs.net/doc/d118714089.html,=name; }
public boolean equals(Person p){ return https://www.360docs.net/doc/d118714089.html,.equals(https://www.360docs.net/doc/d118714089.html,); } }
哪个选项的描述是正确的?(C)
A、equals方法没有正确覆盖Object类中的equals方法
B、编译这段代码会出错,因为第五行的私有属性https://www.360docs.net/doc/d118714089.html,访问不到。
C、如果要与基于哈希的数据结构一起正常地工作,只需要在这个类中再实现hashCode方法即可。
D、当添加一组Person对象到类型为Java.util.Set的集合时,第四行中的equals方法能避免重复。
二、多项选择题:
16.给出一个尚未使用泛型的方法:
11. public static int getSum(List list){
12. int sum=0;
13. for(Iterator iter=list.iterator( ) ; iter.hasNext( ) ; ) {
14. int i=((Integer)iter.next( )).intValue( );
15. sum+=i;
16. }
17. return sum;
18.}
为了使用泛型,需要对代码做一下那三项改动?(ACF)
A、删除第14行
B、第14行替换成int i=iter.next( );
C、将第13行替换成for(int i:intList){
D、将第13行替换成for(Iterator iter: intList)
E、方法的参数声明改为getSum(List
F、方法的参数声明改为getSum(List
17.代码片段:
public abstract interface Sudo { public void crazy(String s); }
请问以下哪些选项中的类定义是正确的?(BC)
A、public abstract class MySudo implements Sudo{
public abstract void crazy(String s){ }
}
B、public abstract class YourSudo implements Sudo{}
C、public abstract class HerSudo implements Sudo{
public void crazy(String i){ }
public void crazy(Integer s){ }
}
D、public class HisSudo implements Sudo{
public void crazy(Integer i){ }
}
E、public class ItsSudo extends Sudo{
public void crazy(Integer i){}
}
18.public class Test {
public static void main(String[ ] args){
int i=3,j;
outer: while(i>0){
j=3;
inner: while (j>0){
if(j<2) break outer;
System.out.println(j+" and "+i);
j--;
}
i--;
}
}}
请问哪些选项的内容会出现在输出中?(AE)
A、3 and 3
B、3 and 2
C、3 and 1
D、3 and 0
E、2 and 3
19.如下代码:
import java.io.*;
class Directories {
static String[ ] films={"sora","shu"};
public static void main(String[ ] args){
for(String fp : films){
//在这里插入第一句代码
File file=new File(path,args[0]);
//在这里插入第二句代码
}
}
}
有一个文件夹,他有2个子文件夹,分别是“sora”和“shu”,“sora”里面有只名为“aoi.txt”的文件,“shu”里面只有名为“qi.txt”的文件。
在此文件夹下以执行以下命令:
java Directories qi.txt
输出结果是:“false true”.
请问把以下哪些选项的代码分别插入到上面代码中能达到此效果?(AC)
A、//第一句代码
String path=fp;
//第二句代码
System.out.print(file.exists( )+””);
B、//第一句代码
String path=fp;
//第二句代码
System.out.print(file.isFile( )+””);
C、//第一句代码
String path=File.separator+fp;
//第二句代码
System.out.print(file.exists( )+””);
D、//第一句代码
String path=File.separator+fp;
//第二句代码
System.out.print(file.isFile( )+””);
20.
A、long n1=12_3_45___789;
B、long n2=__123_45_678_9;
C、int n3=0xFc_aB_C3_353;
D、double n4=0b11001_001_0_0_11;
E、float n5=1.4_142_13;
F、float n6=0_1_2_3;
三、编程题:
21.写一个名叫Square的类用于求一个数的平方。类里面提供两个静态方法,名字叫square。其中一个方法的参数和返回值都是long类型,另一个方法的参数和返回值是double型。
22.给出以下接口HelloWorld,请编写一个类MyHelloWorld实现该接口,并满足接口中所要求的功能。
给定如下的代码:
public interface HelloWorld {
/**
* 返回name+" say:hello world!".
*/
String sayHelloWorld(String name);
}
请您写出一个类名为MyHelloWorld的类,要求满足题意。
23.给出如下shape类,请实现一个共有类Rectangle,满足以下要求:
1. 继承于Shape,实现Shape所规定的功能
2. 有int类型的width和height属性(宽和高)及相应的getter和setter
3. 有一个带两个int参数的共有构造方法,第一个参数用于设置宽,第二个参数用于设置高给定如下代码:
public abstract class Shape {
/*
* 计算形状的面积
*/
abstract public int getArea( );
}
请写出一个类名为Rectangle的类,要求能满足题意。
24.在某软件公司里,小蔡接到上头的一个任务:某位离职的员工留下了一个接口IList,但是该接口的实现类的源码却已丢失,现在需要为该接口重新开发一个实现类MyList。
下面提供了IList接口的代码。
要实现的MyList类是一个公有类,里面需要提供一个公有无参构造方法MyList( ),用于创建一个空的(不含任何元素的)IList。
请你帮小蔡写出这个实现类的代码。
(注意:若要使用https://www.360docs.net/doc/d118714089.html,ng包之外的类,请别忘了import,竞赛时将不会有此提醒)
/**
* 精简的列表(List),用于存储一系列的元素(对象)。
* IList里面存储的元素会按插入的顺序排放,也能根据下标获取这些元素。下标从0开始。 */
public interface IList {
/**
* 往列表的尾部增加一个元素
*/
void add(Object elem);
/**
* 获取下标所指定的元素。当下标越界时抛出https://www.360docs.net/doc/d118714089.html,ng.IndexOutOfBoundsException */
Object get(int index);
/**
* 获取列表里当前元素的个数
*/
int size( );
/**
* 清空列表,移除列表里的所有元素
*/
void clear( );
}
请你写出MyList类,要求能满足题意。