單項選擇題的第9題,請大神賜教,我認爲是隻有它的子類纔可以直接訪問。
一、單項選擇題
1 、執行的結果是:
System.out.format("Pi is approximately %d.", Math.PI);
請問執行結果: 運行時異常
解釋: d% 用來輸出 十進制整數,format的第2個參數必須是十進制整數。
Math.PI 是double類型的數據(3.14159...),可以用%f輸出。
圖1:Math.PI需要用%f來輸出
圖2:%d只能輸出十進制整數
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();
}
}
輸出的結果爲: runningrunningrunning
解釋: Thread.run()與Thread.start()的區別,Thread.run()相當於只是在主線程中,調用了Thread裏的一個普通方法run().未開啓多線程。Thread.start()實現了多線程運行程序,run()方法執行完畢,子線程結束。
3、代碼片段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) { //調用這個方法 multi = 3
calc(); //調用自己的方法,value = 1;
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); // 3
}
}
輸出的結果是:Oh it is:9
解釋:請看圖解(從main方法開始執行)
4、下列說法正確的是:
public class Person {
private Stringname;
public Person(String name) {
this.name = name;
}
public boolean equals(Person p) {
return p.name.equals(this.name);
}
}
A:equals方法沒有正確覆蓋Object類中的equals方法。
B:編譯這段代碼會出錯,因爲第5行的私有屬性p.name訪問不到。
C:如果要與基於哈希的數據結構一起正常地工作,只需要在這個類中在實現hashCode方法即可。
D:當添加一組Person對象到類型爲java.util.Set的集合時,第4行中的equals方法能夠避免重複。
答案:A
解釋:如果正確覆蓋,需要形參爲Object。
5、 打印的結果是:
public class JavaContest {
public static void main(String[] args) throws Exception {
Thread.sleep(3000);
System.out.println("alive");
}
}
結果:會在3秒後輸出alive。
解釋:延遲的效果需要自己去試。
6、請問有什麼較好的方法來過濾value是空值的情況。
public void aSafeMethod(Object value) {
//在這裏檢查方法的參數
//這裏省略其他代碼
System.out.println(value.toString());
}
答案:
if(value == null) {
throw new IllegalArgumentException("value can not be null.");
}
解釋:IllegalArgumentException 指傳入了一個不合法的參數異常。
7、打印的結果:
public static void main(String[] args) {
method1(1,2); // 執行 method1(int x1, int x2),hello java
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");
}
結果是:hello java
解釋:從main方法開始執行,第5行只有2個形參,調用有2個參數的method1方法,執行完後才接着執行第6行。
8、選出正確說法:
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
A: 需要處理InterruptedException
B:
代碼能編譯單可能運行時拋出IllegalStateException.
C: 運行10分鐘後代碼拋出TimeOutException.
D: 需要把obj.wait()替換爲((Thread) obj).wait()後代碼才能通過編譯。
E: 把obj.wait()和obj.notify()這兩句調換一下位置,能使代碼執行。
答案:A
9、請問什麼類可以直接訪問並且改變name的值?
package certkiller;
class Target {
public String name = "hello";
}
答案:Target的子類(以下3個類的圖片),但參考答案給的是包certkiller下的類,請大神給解釋。
圖1:Test類
圖2:Person類
圖3:t1類
10、 打印的結果:
int i = 1;
while(i != 5) {
switch(i++%3) {
case 0:
System.out.print("A");
break;
case 1:
System.out.print("B");
break;
case 2:
System.out.print("C");
break;
}
}
結果:BCAB
解釋: i++%3 表達式是先計算,後對i自加1。
11、執行的結果:
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");
}
}
結果: test test with wow
解釋:此題考查構造方法的調用
12、編譯運行的結果是: 編譯出錯
String text = "Welcome to Java contest";
String[] words = text.split("\s");
System.out.println(words.length);
解釋:鼠標移至錯誤地方:Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
13、以下選項說法正確的是:
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);
}
}
A編譯錯誤,因爲變量a、b、c和d沒有被初始化
B編譯錯誤,因爲變量a無法被訪問
C編譯成功並輸出0 - 0 - 0 - 0
D編譯成功並輸出1 - -1 - 1 - -1
解釋: j=i++ 與j=++i的區別
j=i++, 先將i賦值給j,再對i自身加1;
j=++i, 先對i自身加1,再賦值給j;
14、Map<String, ? extends Collection<Integer>> map; 哪個賦值語句會出錯
A、map = new HashMap<>();
B、map = new HashMap<String, List<Integer>>();
C、map = new HashMap<String, LinkedList<Integer>>();
D、map = new LinkedHashMap<Object, List<Integer>>();
答案:D
解釋:Map的Key指題目中已經確定爲String, A 答案,泛型可以只有一邊寫明
15、
contestKiller = new ReallyBigObject();
//這裏省略部分代碼
contestKiller = null;
/*在這裏補充代碼*/
哪個是告訴虛擬機盡最大可能回收contestKiller 佔用的內存空間:
A、Runtime.getRuntime().freeMemory()
B、Runtime.gc()
C、System.freeMemory()
D、Runtime.getRuntime().growHeap()
E、System.gc()
答案:E
解釋:A和B是正在運行過程中的空閒內存,c是系統中的空閒內存,D是運行中的堆棧釋放,E是程序運行時所有的空閒內存空間。
二、多項選擇題:
1、給出一個尚未使用泛型的方法
11. public static int getSum(List list) {
12. int sum = 0;
13. for(Iterator iter = list.iterator(); iter.hashNext();) {
14. int i = ((Integer) iter.next()).intValue();
15. sun += i;
16. }
17. return sum;
18. }
爲了適應泛型,需要對代碼做以下那三項改動?
A、刪除第14行
B、將第14行替換成int i = iter.next();
C、將第13行替換成for(int i : intList) {
D、將第13行替換成for(Iterator iter : intList)
E、方法的參數聲明改爲getSum(List<int> intList)
F、方法的參數聲明改爲getSum(List<Integer> intList)
答案: ACF
2、 哪些類的定義是正確的:
public abstract interface Sudo {
public void crazy(String s);
}
A、public abstract class MySudo implements Sudo {
public abstract void crazy(String s) {} }
B、public abstract class YourSudo implements Sudo {}
C、public 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){} }
答案:BC
解釋:接口就是抽象的,因此Sudo是否用abstract修飾都沒有關係。實現接口就必須要實現接口裏的方法。
3、 請問會輸出哪些情況:
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; //跳出外部outer循環
System.out.println(j + " and " + i);
j--;
}
i--;
}
}
}
答案: 3 and 3 2 and 3
解釋:本問題考察的是2個while循環,使用inner 和 outer標間。
外層循環, i=3 j = 3,進入內層循環, 執行一次輸出, j自減後成2
再內層循環,執行輸出,j自減後成1,執行那個break outer退出外層循環。
4、
有一個文件夾,它有2個子文件夾,分別是“sora”和“shu”, "sora”裏面只有名爲“aoi.txt”的文件,“shu”裏面只有名爲“qi.txt”的文件。
在此文件夾下執行以下命令:
java Directories qi.txt
輸出的結果是:“false true”
請問以下哪些選項的代碼分別插到上面的代碼中能達到此效果?
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]);
//在這裏插入第二句代碼
}
}
} <span style="font-size:18px;"></span>
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() + " ");
答案: AB
解釋:file.isFile()指文件存在,且爲標準文件。
5、以下哪些選項的代碼存在錯誤
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;
答案:BCE
解釋: n2不能不以數字開頭
n3 提示超出了int類型的最大值
n5 右側數據爲double類型
三、編程題
1、 寫一個名叫Square的類用於求一個數的平方。
類裏面提供兩個靜態方法,名字都叫square。
其中一個方法的參數和返回值都是long型,另一個方法的參數和返回值都是double型
答:
public class Square{
public static long square(long l){
return l*l;
}
public staic double squre(double d){
return d*d;
}
}
2、給出以下接口HelloWorld,請編寫一個類MyHelloWorld實現該接口,並滿足接口中所要求的功能。
答:
public interface HelloWorld{
public void sayHello();
}
Public class MyHelloWorld implements HelloWord{
@overide
public void sayHello(){
System.out.println(“中國你好!”);
}
}
3、給出如下Shape類,請事先一個公有類Rectangle,滿足以下要求:
繼承於Shape,實現Shape的所規定的功能
有int類型的width和height屬性(寬和高)及相應的getter和setter
有一個帶兩個int參數的共有構造方法,第一個參數設置寬,第二個參數設置高
給定如下的代碼:
public abstract class Shape {
/**計算形狀的面積 */
abstract public int getArea();
}
請您寫出一個類名爲Rectangle的類,要求能滿足題意。[代碼編輯區]
public class Rectangle extends Shape{
private int width;
private int height;
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public void setHeight(int height){
this.height = height;
}
public int getHeight(){
return height;
}
public Rectangle(int width, int height){
this.width = width;
this.height = height;
}
public int getArea(){
return this.width*this.height;
}
}
4、在某間軟件公司裏,小蔡接到上頭的一個任務:某位高職的員工留下了一個接口IList,但是該接口的實現類的源碼卻已丟失,現在需要爲該接口重新開發一個實現類MyList.
下面提供了IList接口的代碼。
要實現的MyList是一個公有類,裏面需要提供一個公有的無參構造方法MyList(),用於創建一個空的(不含任何元素的)IList。
請你幫小蔡寫出這個實現類的代碼。
(注意。若要使用java.lang包以外的類,請別忘了import。競賽時將不會有此提醒)
給定如下的代碼:
/** 精簡的列表(List),用於儲存一系列的元素(對象)。
IList裏面儲存的元素會按插入的順序排放,也能根據下標獲取這些元素。下標從0開*始。 */
public interface IList {
/**往列表的尾部增加一個元素 */
void add(Object o);
/**獲取下標所指定的元素。當下標越界時拋出異常 java.lang.IndexOutBoundsException */
object get(int i);
/**獲取列表裏當前元素的個數*/
int size();
/**清空列表,移除列表裏所有的元素*/
void clear();
}
請您寫出一個類名爲MyList的類,要求能滿足題意。[代碼編輯區]
import java.util.ArrayList;
import java.util.List;
public class MyList implements IList {
List list = null;
public MyList() {
list = new ArrayList();
}
void add(Object o) {
list.add(o);
}
object get(int i) {
if(i>list.size()) {
throw new IndexOutBoundsException (“超出大小”);
}
return list.get(i);
}
int size() {
return list.size();
}
void clear() {
list.clear();
}
}