摩羯面試題總結
阿新 • • 發佈:2018-12-01
面試題總結 —— 摩羯
選擇題
1.下列說法正確的有() A.class中的constructor不可省略 B.constructor必須與class同名,但方法不能與class同名 C.constructor在一個物件被new時執行 D.一個class只能定義一個constructor 解析: A:無參構造器是可以省略的 B:constructor不一定與class同名,因為存在內部類的原因 D:方法的過載 2.下列程式碼的輸出結果是() public class Test{ public static void main(){ People ab = new Child(); } } class People{ static{ System.out.println("1"); } { System.out.println("2"); } public People(){ System.out.println("3") } } class Child extends People{ static{ System.out.println("a"); } { System.out.println("b"); } public People(){ System.out.println("c") } } A:cab312 B:312cab C:a123bc D:1a23bc 解析:首先入口是main()方法,運用了多型,既然涉及到了多型那麼就涉及到了 例項化物件時候順序的問題,先執行父類的方法,在執行子類的方法,其中靜態代 碼塊是最先初始化的,它是在類載入到記憶體的時候就可以執行的,其次是程式碼塊 所以,通過前面的分析我們可以知道前面兩步的執行順序為:1a,之後再次進行 分析靜態程式碼塊之後是程式碼塊 3.下面程式碼輸出結果() public static void main(){ Integer a = new Integer(3); Integer b = 3; int c = 3; System.out.println(a == b); System.out.println(a == c); } A:true true B:true false C:false true D:false false 分析:Integer是一個物件,int是8個基本資料型別裡面的也就是常量 第一條語句:生成了一個物件 第二條語句:Integer b = 3 其實在底層呼叫了Integer.valueOf()方法 其中Integer.valueOf()返回一個Integer物件 且==就是比較的是地址 所以第一條為 false 第三條語句:int c = 3;在與Integer a = 3;相比較的時候會自動拆箱 比較的是數值,所以返回true 所以選擇C 4.在第三行中生成的object在第幾行執行後會成為garbage collection的物件 1.public class MyClass{ 2. public StringBuffer aMethod(){ 3. StringBuffer sf = new StringBuffer("Hello"); 4. StringBuffer[] sf_arr = new StringBuffer[1]; 5. sf_arr[0] = sf; 6. sf = null; 7. sf_arr[0] = null; 8. return sf; 9. } } A: 第五行 B:第六行 C:第七行 D:第八行 分析:被垃圾回收機制回收的物件存在以下的特點: 1.沒有被其他物件所引用 2.當前物件為null 所以正確答案:C 5.下面程式的輸出結果是() public static void main(String[] args){ Thread t = new Thread(){ public void run(){ pong(); } }; t.run(); System.out.println("ping"); } Static void pong(){ System.out.println("pong"); } A:pingpang B:pongping C:pingpang和pangping都用可能 D:d都不輸出 分析:順序執行沒有什麼好說的B
解答題
1.整型數的反轉.例如輸入123,返回321,輸入 -1234返回-4321超過最大值返回-1 public class InverseString { public static String InverseMethod(String strValue){ char[] charArrayofString = strValue.toCharArray(); String strInverse; String charBuffer = new String(); for(int i = charArrayofString.length - 1; i >= 0; i--){ strInverse = String.valueOf(charArrayofString[i]); charBuffer +=strInverse; } return charBuffer; } public static void main(String[] args){ Scanner cin = new Scanner(System.in); int integerValue = cin.nextInt(); String inverseStringValue = new String(); if(integerValue > 0){ String stringValue = integerValue + ""; inverseStringValue = InverseMethod(stringValue); }else if(integerValue < 0){ integerValue = Math.abs(integerValue); String stringValue = integerValue + ""; inverseStringValue = InverseMethod(stringValue); inverseStringValue = "-"+inverseStringValue; } System.out.println("結束:" + inverseStringValue); } } 2.有兩張表 學生基本資訊表student_info(student_no,name,age,sex) 學生成績表score_info(student_no,subject,score) 問題:請用sql查詢出沒有掛科的學生的姓名以及該學生的平均成績 分析:完整解答過程如下 CREATE TABLE student_info( student_no INT PRIMARY KEY, student_name VARCHAR(20), student_age INT, student_sex VARCHAR(20) ) COMMIT; CREATE TABLE score_info( student_no INT, score_subject VARCHAR(20), score_score INT ) COMMIT; DROP TABLE score_info; DESC student_info; DESC score_info; INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1001,'baidu',18,'male'); INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1002,'TX',19,'male'); INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1003,'ali',20,'female'); INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1004,'google',21,'female'); INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1005,'windows',22,'male'); COMMIT; INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'java',80); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'php',81); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'ui',82); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'python',83); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'java',59); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'php',58); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'ui',61); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'python',53); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'java',61); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'php',62); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'ui',50); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'python',53); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'java',80); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'php',90); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'ui',100); INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'python',99); COMMIT; -- 巢狀子查詢 SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` = so.`student_no` GROUP BY so.`student_no` HAVING AVG(so.`score_score`) > 60; SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st -- 一會再用左聯接的方式進行查詢 SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so LEFT JOIN student_info st ON so.`student_no`=st.`student_no` GROUP BY so.`student_no` HAVING AVG(so.`score_score`) > 60; -- 只要是有一科不及格就GG的 -- 請用sql查詢出沒有掛科的學生的姓名 -- 以及該學生的平均成績 SELECT * FROM student_info st WHERE st.`student_no` NOT IN (SELECT so.`student_no` FROM score_info so WHERE so.`score_score` < 60); -- 巢狀子查詢 SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` = so.`student_no` GROUP BY so.`student_no` HAVING AVG(so.`score_score`) > 60; SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` NOT IN (SELECT so.`student_no` FROM score_info so WHERE so.`score_score` < 60) AND so.`student_no`=st.`student_no` GROUP BY so.`student_no`;