• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. 華為JAVA考試試題

        時間:2024-11-01 23:45:01 秀雯 JAVA認證 我要投稿

        華為JAVA考試試題

          在平平淡淡的日常中,只要有考核要求,就會有試題,試題是用于考試的題目,要求按照標準回答。什么樣的試題才是好試題呢?下面是小編精心整理的華為JAVA考試試題,歡迎閱讀,希望大家能夠喜歡。

        華為JAVA考試試題

          華為JAVA考試試題 1

          1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5后,輸出結果。

          程序要求:該字符串只包含小寫字母,若其值加5后的字符值大于z,將其轉換成從a開始的字符。

          package com.xcbeyond;

          /**

          * @author xcbeyond

          * 2015-5-7下午10:37:43

          * 1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5后,輸出結果。

          * 程序要求:該字符串只包含小寫字母,若其值加5后的字符值大于z,將其轉換成從a開始的字符。

          */

          public class StringParseASCII {

          public static void main(String[] args) {

          System.out.print(stringParseASCII("abx"));

          }

          public static String stringParseASCII(String str){

          StringBuffer result = new StringBuffer();

          char tmp;

          for(int i = 0;i

          tmp = (char)(str.charAt(i)+5);

          if(tmp > z) {

          result.append(a);

          }else {

          result.append(tmp);

          }

          }

          return result.toString();

          }

          }

          2.程序實現目標:求一個整型數組中元素的平均值,并統計其中大于和小于此平均值的元素的個數。

          程序要求:輸入:整型數組中的元素個數及各個元素。

          輸出:整型數組中元素的平均值,大于和小于此平均值的元素的個數。

          package com.xcbeyond;

          import java.util.Arrays;

          /**

          *

          * @author xcbeyond

          * 2015-5-7下午11:06:29

          *2.程序實現目標:求一個整型數組中元素的平均值,并統計其中大于和小于此平均值的元素的個數。

          *程序要求:

          * 輸入:整型數組中的元素個數及各個元素。

          * 輸出:整型數組中元素的平均值,大于和小于此平均值的元素的個數。

          */

          public class CountAvg {

          public static void main(String[] args) {

          int[] array = {1,23,4,13,6};

          System.out.println(Arrays.toString(array)+"的平均值:"+avg(array)+"\n" +

          "大于和小于平均值元素的個數分別為:"+Arrays.toString(countAvg(array)));

          }

          public static int[] countAvg(int[] array) {

          int gt = 0; //grater than

          int lt = 0; //less than

          int[] result = {0,0};

          int average = avg(array);

          for(int i = 0;i

          if(array[i]>average) {

          gt++;

          }else if(array[i]

          lt++;

          }

          }

          result[0] = gt;

          result[1] = lt;

          return result;

          }

          /**

          * average

          * @param array

          * @return

          */

          public static int avg(int[] array) {

          int average = 0;

          int sum = 0;

          for(int i = 0 ;i

          sum += array[i];

          }

          average = sum/array.length;

          return average;

          }

          }

          3、手動輸入一個存儲整數的數組,要求輸出數組里面的2個最大值。

          實例:

          輸入:1,2,5,9,84,3,2

          輸出:84,9

          package com.xcbeyond;

          import java.util.Arrays;

          /**

          * @author xcbeyond

          * 2015-5-7下午11:35:13

          *3、手動輸入一個存儲整數的數組,要求輸出數組里面的2個最大值。

          * 實例:

          * 輸入:1,2,5,9,84,3,2

          * 輸出:84,9

          */

          public class FindMaxTwoNum {

          public static void main(String[] args) {

          int[] array = {1,2,5,9,84,3,2};

          System.out.println("數組"+Arrays.toString(array)+"里面最大的2個數為:");

          findMaxTwoNum(array);

          //方法二:

          //

          }

          public static void findMaxTwoNum(int[] array) {

          int[] result = {0,0};

          for(int i = 0 ;i

          for(int j = 0;j

          if(array[j]

          int tmp;

          tmp = array[j];

          array[j] = array[j+1];

          array[j+1] = tmp;

          }

          }

          }

          System.out.println(array[0]+"、"+array[1]);

          }

          }

          4、回文數字判斷。

          題目描述:

          有這樣一類數字,他們順著看和倒著看是相同的數,例如:121,656,2332等,這樣的數字就稱為:回文數字。編寫一個函數,判斷某數字是否是回文數字。

          要求實現方法:

          public String isPalindrome(String strIn);

          【輸入】strIn: 整數,以字符串表示;

          【返回】true: 是回文數字;

          false: 不是回文數字;

          【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出

          package com.xcbeyond;

          import java.util.Scanner;

          /**

          * @author xcbeyond

          * 2015-5-10下午03:46:56

          *4、回文數字判斷。

          *題目描述:

          * 有這樣一類數字,他們順著看和倒著看是相同的數,例如:121,656,2332等,這樣的數字就稱為:

          * 回文數字。編寫一個函數,判斷某數字是否是回文數字。

          */

          public class IsPalindrome {

          public static void main(String[] args) {

          System.out.print("請輸入一個回文數字:");

          Scanner console = new Scanner(System.in);

          String numStr = console.nextLine();

          if(isPalindrome(numStr)) {

          System.out.println(numStr+"是回文數字!");

          }else{

          System.out.println(numStr+"不是回文數字!");

          }

          }

          public static boolean isPalindrome(String str){

          boolean result = false;

          for(int i = 0 ;i

          if(str.charAt(i) == str.charAt(str.length()-1-i)) {

          result = true;

          }

          }

          return result;

          }

          }

          5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的范圍是所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

          package com.xcbeyond;

          import java.util.HashSet;

          import java.util.Set;

          /**

          *

          * @author xcbeyond

          * 2015-5-10下午04:05:42

          *5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的范圍是

          * 所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

          */

          public class RandomStr {

          public static void main(String[] args) {

          SetsetStr = new HashSet();

          for(int i = 0 ;i<50;i++) {

          setStr.add(randomStr(5));

          }

          int count = 1;

          for(String i:setStr){

          System.out.print(i+" ");

          if(count%4 == 0) {

          System.out.println();

          }

          count++;

          }

          }

          /**

          * @param strLen:隨機字符串的長度

          */

          public static String randomStr(int strLen) {

          char[] str = new char[strLen];

          int i = 0;

          while(i

          int f = (int)Math.random()*3;

          if(f == 0) {

          str[i] = (char)(a + Math.random()*26);

          }else if(f == 1) {

          str[i] = (char)(A + Math.random()*26);

          }else {

          str[i] = (char)(0 + Math.random()*10);

          }

          i++;

          }

          return new String(str);

          }

          }

          6.手動輸入一個字符串,僅限小寫字母,統計并輸出每個字符在字符串中出現的次數,并輸出。提示(可以用Map)

          實例:

          輸入:aaabbbccc

          輸出:a 3

          b 3

          c 3

          package com.xcbeyond;

          import java.util.HashMap;

          import java.util.Map;

          /**

          *

          * @author xcbeyond

          * 2015-5-10下午04:47:45

          * 6.手動輸入一個字符串,僅限小寫字母,統計并輸出每個字符在字符串中出現的次數,并輸出。

          * 提示(可以用Map)

          * 實例:

          * 輸入:aaabbbccc

          * 輸出: a 3

          * b 3

          * c 3

          */

          public class GetCharCount {

          public static void main(String[] args) {

          String str = "aaabbbrcc";

          String reg = "^[a-z]*$";

          if (str.matches(reg)) {

          Mapmap = getCharCount(str);

          for (Map.Entrye : map.entrySet()) {

          System.out.println(e.getKey() + ": " + e.getValue());

          }

          }else {

          System.out.println("輸入的字符不合法,不是小寫字母");

          }

          }

          public static MapgetCharCount(String str) {

          Mapmap = new HashMap();

          char[] arr = str.toCharArray();

          for(int i = 0;i

          if(!map.containsKey(arr[i])) {

          map.put(arr[i], new Integer(1));

          }else {

          map.put(arr[i],map.get(arr[i])+1);

          }

          }

          return map;

          }

          }

          7、要求實現方法public String addTwoBigNumber(String s1,string s2)

          大數相加,注意處理異常

          public class Test{

          public String addTwoBigNumber(String s1,string s2)

          {

          return "";

          }

          public static void main(String[] args)

          {

          Test test = new Test();

          test.addTwoBigNumber("123456789","987654321")

          }

          }

          8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

          輸入:intArr = {{5,6,1,16},{7,3,9}}

          輸出:intArrs ={1,3}

          package com.xcbeyond;

          import java.util.Arrays;

          /**

          * @author xcbeyond

          * 2015-5-10下午09:09:20

          *8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

          * 輸入:intArr = {{5,6,1,16},{7,3,9}}

          * 輸出:intArrs ={1,3}

          */

          public class GetColMin {

          public static void main(String[] args) {

          int[][] arr = {{5,6,1,16},{7,3,9}};

          System.out.println(Arrays.toString(getColMin(arr)));

          }

          public static int[] getColMin(int[][] arr) {

          int[] minArr = new int[arr.length];

          for(int i = 0;i

          int[] tmp = arr[i];

          Arrays.sort(tmp);

          minArr[i] = tmp[0];

          }

          return minArr;

          }

          }

          9. 輸入:a aa,cat tiger.123dd

          輸出: tiger

          功能描述:鍵盤輸入一句話

          輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

          這句話只包含數字字母和標點。

          package com.xcbeyond;

          import java.util.ArrayList;

          import java.util.Scanner;

          /**

          *

          * @author xcbeyond

          * 2015-5-10下午09:45:03

          *9. 輸入:a aa,cat tiger.123dd

          * 輸出: tiger

          * 功能描述:鍵盤輸入一句話

          * 輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

          * 這句話只包含數字字母和標點。

          */

          public class GetLongString {

          public static void main(String[] args) {

          System.out.println("請輸入一句話:");

          Scanner console = new Scanner(System.in);

          String str = console.nextLine();

          System.out.println("最長的單詞為:"+getLongString(str));

          }

          public static String getLongString(String str) {

          String[] wordStr = str.split("[ ,.0-9]");

          int sum = 0;

          ArrayListresult = new ArrayList();

          for(int i = 0;i

          if(sum

          sum = wordStr[i].length();

          result.add(wordStr[i]);

          }

          }

          return result.get(result.size()-1);

          }

          }

          10. 功能描述:將字符串中的字母全部替換成字母的下一個字母,

          要是最后一位是z或Z則替換為a或A。

          輸入:aBxyZ

          輸出:bCyzA

          package com.xcbeyond;

          /**

          *

          * @author xcbeyond

          * 2015-5-10下午10:11:02

          *10. 功能描述:

          * 將字符串中的字母全部替換成字母的下一個字母,要是最后一位是z或Z則替換為a或A。

          * 輸入:aBxyZ

          * 輸出:bCyzA

          */

          public class NextString {

          public static void main(String[] args) {

          String str = "aBxyZ";

          System.out.println(nextString(str));

          }

          public static String nextString(String str) {

          String result = "";

          char[] arr = str.toCharArray();

          for(int i = 0;i

          if(arr[i] == z || arr[i] == Z) {

          arr[i] = (char)(arr[i]-25);

          }else if(arr[i]<=z&&arr[i]>=a || arr[i]<=z&&arr[i]>=A) { 1+2+3 = 6

          運行時間限制: 無限制

          內存限制: 無限制

          輸入: 0xff ff ff ff以內的整數

          輸出: NA

          樣例輸入: 123

          樣例輸出: 6

          package com.xcbeyond;

          /**

          *

          * @author xcbeyond

          * @date 2015/05/12 15:23:16

          */

          public class Demo36 {

          public static void main(String[] args) {

          int num = 123;

          System.out.println(bitSum(num));

          }

          public static int bitSum(int num) {

          int res = 0;

          if(num<10) {

          res = num;

          }else {

          res = num%10 + bitSum(num/10);

          }

          return res;

          }

          }

          37.提取不重復的整數

          描述: 輸入一個int型32位整數,按照從右向左的閱讀順序,返回一個不含重復數字的新的整數。

          運行時間限制: 10 Sec

          內存限制: 無限制

          輸入: 整數,如9876673

          注意:

          1、整數最后的0,請忽略,例如:輸入1750,輸出:571

          2、負數,保留-在前面,例如:輸入-175,輸出:-571

          輸出: 整數,如37689

          樣例輸入: 9876673

          樣例輸出: 37689

          package com.xcbeyond;

          /**

          *

          * @author xcbeyond

          * @date 2015/05/12 15:50:34

          */

          public class Demo37 {

          public static void main(String[] args) {

          int num = -12310;

          System.out.println(getConvertInt(num));

          }

          public static int getConvertInt(int num) {

          String str = String.valueOf(num);

          StringBuffer sb = new StringBuffer();

          boolean flg = true;

          if(str.charAt(0) == -) {

          flg = false;

          sb.append(str.charAt(0));

          }

          if(str.charAt(str.length()-1) != 0) {

          sb.append(str.charAt(str.length()-1));

          }

          for(int i = str.length()-2;i>0;i--) {

          sb.append(str.charAt(i));

          }

          if(flg) {

          sb.append(str.charAt(0));

          }

          return Integer.parseInt(sb.toString());

          }

          }

          華為JAVA考試試題 2

          1、Collection 和 Collections的區別?

          Collection是集合的根接口,其下有set及list

          Collections是集合的算法。

          2、Set里的元素是不能重復的,那么用什么方法來區分重復與否呢? 是用==還是equals()? 它們有何區別?用contains來區分是否有重復的對象。還是都不用?

          在比較時先調用hashCode方法,如果不相同,證明不相等。

          如果相同,再調用equals方法,如果equals方法相同,證明相等,不相同,證明不相等。

          ==:主要用在基本數據類型及引用

          equals:主要是對象或對象引用的比較。

          集合中是否包含某一個元素用contains來判斷。

          3、List,Set,Map是否繼承自Collection接口?

          List,set繼承于Collection

          Map沒有繼承于Collection,其相對是獨立的。

          屬于Collection類型的對象,可以通過構造函數將一個集合構造成另外一個集合。

          4、面向對象的特征有哪些方面?

          1.抽象:

          找共性,將共有的屬性、方法放到父類中

          2.繼承:

          子類繼承于父類,具有父類的所有屬性與方法,可以重用,也可以覆蓋。

          3.封裝:

          一個類包括多個屬性及方法。

          4. 多態性:

          5、String是最基本的數據類型嗎?

          基本數據類型包括byte、int、char、long、float、double、boolean和short。

          java.lang.String類是final類型的,因此不可以繼承這個類、不能修改這個類。為了提高效率節省空間,我們應該用StringBuffer類

          6、int 和 Integer 有什么區別?

          int 是基本數據類型,不是對象,占一個內存空間,沒有方法。與其同類的有long,char,doble

          Integer是封裝類,具有方法及屬性。與其同類的有Long,Double.Float

          7、運行時異常與一般異常有何異同?

          運行時異常:JVM拋出的異常,代碼中不用處理。

          一般異常:用戶拋出的異常,如果用throws 聲明了,調用這個方法的代碼必須對其處理。

          8、&和&&的區別?

          &:與: 左邊若為false右邊還執行。

          &&:短路與,左邊若為false右邊不執行。

          9、final,finally,finalize的區別?

          final 用于聲明屬性,方法和類,分別表示屬性不可變,方法不可覆蓋,類不可繼承。

          finally是異常處理語句結構的一部分,表示總是執行。

          finalize是Object類的一個方法,在垃圾收集器執行的時候會調用被回收對象的此方法,可以覆蓋此方法提供垃圾收集時的其他資源回收,例如關閉文件等。

          10、heap和stack有什么區別?

          棧是一種線形集合,其添加和刪除元素的操作應在同一段完成。棧按照后進先出的方式進行處理。堆是棧的一個組成元素

          11、Static Nested Class 和 Inner Class的不同?

          Static Nested Class是被聲明為靜態(static)的內部類,它可以不依賴于外部類實例被實例化。而通常的內部類需要在外部類實例化后才能實例化。

          12、GC是什么? 為什么要有GC?

          GC是垃圾收集的意思(Gabage Collection),內存處理是編程人員容易出現問題的地方,忘記或者錯誤的內存回收會導致程序或系統的不穩定甚至崩潰,Java提供的GC功能可以自動監測對象是否超過作用域從而達到自動回收內存的目的,Java語言沒有提供釋放已分配內存的顯示操作方法。

          13、short s1 = 1; s1 = s1 + 1;有什么錯? short s1 = 1; s1 += 1;有什么錯?

          short s1 = 1; s1 = s1 + 1; (s1+1運算結果是int型,需要強制轉換類型)

          short s1 = 1; s1 += 1;(可以正確編譯)

          14、Math.round(11.5)等於多少? Math.round(-11.5)等於多少?

          Math.round(11.5)==12

          Math.round(-11.5)==-11

          round方法返回與參數最接近的長整數,參數加1/2后求其floor.

          15、Java有沒有goto?

          java中的保留字,現在沒有在java中使用。

          16、給出一個你最常見到的runtime exception ArithmeticException,ArrayStoreException,BufferOverflowException,BufferUnderflowException,CannotRedoException,CannotUndoException,ClassCastException,CMMException,ConcurrentModificationException,DOMException,EmptyStackException,IllegalArgumentException,IllegalMonitorStateException,IllegalPathStateException,IllegalStateException,ImagingOpException,IndexOutOfBoundsException,MissingResourceException,NegativeArraySizeException,NoSuchElementException,NullPointerException,ProfileDataException,ProviderException,RasterFormatException,SecurityException,SystemException,UndeclaredThrowableException,UnmodifiableSetException,UnsupportedOperationException

          一般異常:

          IOException

          FileNotFoundException

          SqlException

          17、數組有沒有length()這個方法? String有沒有length()這個方法?

          數組沒有length()這個方法,有length這個屬性

          String有length()這個方法.

          18、構造器Constructor是否可被override?

          構造器Constructor不能被繼承,因此不能重寫Overriding,但可以被重載Overloading。

          19、是否可以繼承String類?

          String類是final類故不可以繼承。

          20、swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?

          switch(expr1)中,expr1是一個整數表達式。因此傳遞給 switch 和 case 語句的.參數應該是 int、 short、 char 或者 byte。long,string 都不能作用于swtich。

          21、try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執行,什么時候被執行,在return前還是后?

          會執行,在return前執行。

          22、兩個對象值相同(x.equals(y) == true),但卻可有不同的hash code,這句話對不對?

          不對,有相同的 hash code

          這是java語言的定義:

          1) 對象相等則hashCode一定相等;

          2) hashCode相等對象未必相等

          23、四種會話跟蹤技術?

          Cookie

          Session

          Hidden

          url 重寫

          24、寫一個Singleton出來。

          Singleton模式主要作用是保證在Java應用程序中,一個類只有一個實例存在。

          一般Singleton模式通常有幾種種形式:

          第一種形式: 定義一個類,它的構造函數為private的,它有一個static的private的該類變量,在類初始化時實例話,通過一個public的getInstance方法獲取對它的引用,繼而調用其中的方法。

          public class Singleton {

          private Singleton(){}

          //在自己內部定義自己一個實例,是不是很奇怪?

          //注意這是private 只供內部調用

          private static Singleton instance = new Singleton();

          //這里提供了一個供外部訪問本class的靜態方法,可以直接訪問

          public static Singleton getInstance() {

          return instance;

          }

          }

          第二種形式:

          public class Singleton {

          private static Singleton instance = null;

          public static synchronized Singleton getInstance() {

          //這個方法比上面有所改進,不用每次都進行生成對象,只是第一次

          //使用時生成實例,提高了效率!

          if (instance==null)

          instance=new Singleton();

          return instance; }

          }

          其他形式:

          定義一個類,它的構造函數為private的,所有方法為static的。

          一般認為第一種形式要更加安全些

          25、Java中的異常處理機制的簡單原理和應用。

          原理: 有錯直接轉到異常處理部分或向上拋出。

          應用:JAVA的異常就是錯誤,有兩種,一種是運行時,編碼可以不用捕捉。一種是一般異常,如果throws聲明了,必須進行處理。

          26、描述一下JVM加載class文件的原理機制?

          JVM中類的裝載是由ClassLoader和它的子類來實現的,Java ClassLoader 是一個重要的Java運行時系統組件。它負責在運行時查找和裝入類文件的類。

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【華為JAVA考試試題】相關文章:

        華為Java上機考試題03-29

        java考試試題及答案10-25

        Java考試格林模擬試題03-23

        華為認證考試試題及答案03-05

        2017年java考試模擬試題03-06

        java認證考試試題及答案03-04

        2017年JAVA考試試題及答案02-26

        2016年Java認證考試題03-08

        2017年Java認證考試試題03-04

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 制服丝袜中文字幕自拍有码 | 亚洲精品国产第一综合99久久 | 中文欧美亚洲欧日韩范冰冰 | 少妇按摩中文字幕 | 精品日韩国产欧美在线观看 | 一本精品日本在线视频精品 |

            華為JAVA考試試題

              在平平淡淡的日常中,只要有考核要求,就會有試題,試題是用于考試的題目,要求按照標準回答。什么樣的試題才是好試題呢?下面是小編精心整理的華為JAVA考試試題,歡迎閱讀,希望大家能夠喜歡。

            華為JAVA考試試題

              華為JAVA考試試題 1

              1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5后,輸出結果。

              程序要求:該字符串只包含小寫字母,若其值加5后的字符值大于z,將其轉換成從a開始的字符。

              package com.xcbeyond;

              /**

              * @author xcbeyond

              * 2015-5-7下午10:37:43

              * 1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5后,輸出結果。

              * 程序要求:該字符串只包含小寫字母,若其值加5后的字符值大于z,將其轉換成從a開始的字符。

              */

              public class StringParseASCII {

              public static void main(String[] args) {

              System.out.print(stringParseASCII("abx"));

              }

              public static String stringParseASCII(String str){

              StringBuffer result = new StringBuffer();

              char tmp;

              for(int i = 0;i

              tmp = (char)(str.charAt(i)+5);

              if(tmp > z) {

              result.append(a);

              }else {

              result.append(tmp);

              }

              }

              return result.toString();

              }

              }

              2.程序實現目標:求一個整型數組中元素的平均值,并統計其中大于和小于此平均值的元素的個數。

              程序要求:輸入:整型數組中的元素個數及各個元素。

              輸出:整型數組中元素的平均值,大于和小于此平均值的元素的個數。

              package com.xcbeyond;

              import java.util.Arrays;

              /**

              *

              * @author xcbeyond

              * 2015-5-7下午11:06:29

              *2.程序實現目標:求一個整型數組中元素的平均值,并統計其中大于和小于此平均值的元素的個數。

              *程序要求:

              * 輸入:整型數組中的元素個數及各個元素。

              * 輸出:整型數組中元素的平均值,大于和小于此平均值的元素的個數。

              */

              public class CountAvg {

              public static void main(String[] args) {

              int[] array = {1,23,4,13,6};

              System.out.println(Arrays.toString(array)+"的平均值:"+avg(array)+"\n" +

              "大于和小于平均值元素的個數分別為:"+Arrays.toString(countAvg(array)));

              }

              public static int[] countAvg(int[] array) {

              int gt = 0; //grater than

              int lt = 0; //less than

              int[] result = {0,0};

              int average = avg(array);

              for(int i = 0;i

              if(array[i]>average) {

              gt++;

              }else if(array[i]

              lt++;

              }

              }

              result[0] = gt;

              result[1] = lt;

              return result;

              }

              /**

              * average

              * @param array

              * @return

              */

              public static int avg(int[] array) {

              int average = 0;

              int sum = 0;

              for(int i = 0 ;i

              sum += array[i];

              }

              average = sum/array.length;

              return average;

              }

              }

              3、手動輸入一個存儲整數的數組,要求輸出數組里面的2個最大值。

              實例:

              輸入:1,2,5,9,84,3,2

              輸出:84,9

              package com.xcbeyond;

              import java.util.Arrays;

              /**

              * @author xcbeyond

              * 2015-5-7下午11:35:13

              *3、手動輸入一個存儲整數的數組,要求輸出數組里面的2個最大值。

              * 實例:

              * 輸入:1,2,5,9,84,3,2

              * 輸出:84,9

              */

              public class FindMaxTwoNum {

              public static void main(String[] args) {

              int[] array = {1,2,5,9,84,3,2};

              System.out.println("數組"+Arrays.toString(array)+"里面最大的2個數為:");

              findMaxTwoNum(array);

              //方法二:

              //

              }

              public static void findMaxTwoNum(int[] array) {

              int[] result = {0,0};

              for(int i = 0 ;i

              for(int j = 0;j

              if(array[j]

              int tmp;

              tmp = array[j];

              array[j] = array[j+1];

              array[j+1] = tmp;

              }

              }

              }

              System.out.println(array[0]+"、"+array[1]);

              }

              }

              4、回文數字判斷。

              題目描述:

              有這樣一類數字,他們順著看和倒著看是相同的數,例如:121,656,2332等,這樣的數字就稱為:回文數字。編寫一個函數,判斷某數字是否是回文數字。

              要求實現方法:

              public String isPalindrome(String strIn);

              【輸入】strIn: 整數,以字符串表示;

              【返回】true: 是回文數字;

              false: 不是回文數字;

              【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出

              package com.xcbeyond;

              import java.util.Scanner;

              /**

              * @author xcbeyond

              * 2015-5-10下午03:46:56

              *4、回文數字判斷。

              *題目描述:

              * 有這樣一類數字,他們順著看和倒著看是相同的數,例如:121,656,2332等,這樣的數字就稱為:

              * 回文數字。編寫一個函數,判斷某數字是否是回文數字。

              */

              public class IsPalindrome {

              public static void main(String[] args) {

              System.out.print("請輸入一個回文數字:");

              Scanner console = new Scanner(System.in);

              String numStr = console.nextLine();

              if(isPalindrome(numStr)) {

              System.out.println(numStr+"是回文數字!");

              }else{

              System.out.println(numStr+"不是回文數字!");

              }

              }

              public static boolean isPalindrome(String str){

              boolean result = false;

              for(int i = 0 ;i

              if(str.charAt(i) == str.charAt(str.length()-1-i)) {

              result = true;

              }

              }

              return result;

              }

              }

              5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的范圍是所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

              package com.xcbeyond;

              import java.util.HashSet;

              import java.util.Set;

              /**

              *

              * @author xcbeyond

              * 2015-5-10下午04:05:42

              *5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的范圍是

              * 所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

              */

              public class RandomStr {

              public static void main(String[] args) {

              SetsetStr = new HashSet();

              for(int i = 0 ;i<50;i++) {

              setStr.add(randomStr(5));

              }

              int count = 1;

              for(String i:setStr){

              System.out.print(i+" ");

              if(count%4 == 0) {

              System.out.println();

              }

              count++;

              }

              }

              /**

              * @param strLen:隨機字符串的長度

              */

              public static String randomStr(int strLen) {

              char[] str = new char[strLen];

              int i = 0;

              while(i

              int f = (int)Math.random()*3;

              if(f == 0) {

              str[i] = (char)(a + Math.random()*26);

              }else if(f == 1) {

              str[i] = (char)(A + Math.random()*26);

              }else {

              str[i] = (char)(0 + Math.random()*10);

              }

              i++;

              }

              return new String(str);

              }

              }

              6.手動輸入一個字符串,僅限小寫字母,統計并輸出每個字符在字符串中出現的次數,并輸出。提示(可以用Map)

              實例:

              輸入:aaabbbccc

              輸出:a 3

              b 3

              c 3

              package com.xcbeyond;

              import java.util.HashMap;

              import java.util.Map;

              /**

              *

              * @author xcbeyond

              * 2015-5-10下午04:47:45

              * 6.手動輸入一個字符串,僅限小寫字母,統計并輸出每個字符在字符串中出現的次數,并輸出。

              * 提示(可以用Map)

              * 實例:

              * 輸入:aaabbbccc

              * 輸出: a 3

              * b 3

              * c 3

              */

              public class GetCharCount {

              public static void main(String[] args) {

              String str = "aaabbbrcc";

              String reg = "^[a-z]*$";

              if (str.matches(reg)) {

              Mapmap = getCharCount(str);

              for (Map.Entrye : map.entrySet()) {

              System.out.println(e.getKey() + ": " + e.getValue());

              }

              }else {

              System.out.println("輸入的字符不合法,不是小寫字母");

              }

              }

              public static MapgetCharCount(String str) {

              Mapmap = new HashMap();

              char[] arr = str.toCharArray();

              for(int i = 0;i

              if(!map.containsKey(arr[i])) {

              map.put(arr[i], new Integer(1));

              }else {

              map.put(arr[i],map.get(arr[i])+1);

              }

              }

              return map;

              }

              }

              7、要求實現方法public String addTwoBigNumber(String s1,string s2)

              大數相加,注意處理異常

              public class Test{

              public String addTwoBigNumber(String s1,string s2)

              {

              return "";

              }

              public static void main(String[] args)

              {

              Test test = new Test();

              test.addTwoBigNumber("123456789","987654321")

              }

              }

              8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

              輸入:intArr = {{5,6,1,16},{7,3,9}}

              輸出:intArrs ={1,3}

              package com.xcbeyond;

              import java.util.Arrays;

              /**

              * @author xcbeyond

              * 2015-5-10下午09:09:20

              *8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

              * 輸入:intArr = {{5,6,1,16},{7,3,9}}

              * 輸出:intArrs ={1,3}

              */

              public class GetColMin {

              public static void main(String[] args) {

              int[][] arr = {{5,6,1,16},{7,3,9}};

              System.out.println(Arrays.toString(getColMin(arr)));

              }

              public static int[] getColMin(int[][] arr) {

              int[] minArr = new int[arr.length];

              for(int i = 0;i

              int[] tmp = arr[i];

              Arrays.sort(tmp);

              minArr[i] = tmp[0];

              }

              return minArr;

              }

              }

              9. 輸入:a aa,cat tiger.123dd

              輸出: tiger

              功能描述:鍵盤輸入一句話

              輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

              這句話只包含數字字母和標點。

              package com.xcbeyond;

              import java.util.ArrayList;

              import java.util.Scanner;

              /**

              *

              * @author xcbeyond

              * 2015-5-10下午09:45:03

              *9. 輸入:a aa,cat tiger.123dd

              * 輸出: tiger

              * 功能描述:鍵盤輸入一句話

              * 輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

              * 這句話只包含數字字母和標點。

              */

              public class GetLongString {

              public static void main(String[] args) {

              System.out.println("請輸入一句話:");

              Scanner console = new Scanner(System.in);

              String str = console.nextLine();

              System.out.println("最長的單詞為:"+getLongString(str));

              }

              public static String getLongString(String str) {

              String[] wordStr = str.split("[ ,.0-9]");

              int sum = 0;

              ArrayListresult = new ArrayList();

              for(int i = 0;i

              if(sum

              sum = wordStr[i].length();

              result.add(wordStr[i]);

              }

              }

              return result.get(result.size()-1);

              }

              }

              10. 功能描述:將字符串中的字母全部替換成字母的下一個字母,

              要是最后一位是z或Z則替換為a或A。

              輸入:aBxyZ

              輸出:bCyzA

              package com.xcbeyond;

              /**

              *

              * @author xcbeyond

              * 2015-5-10下午10:11:02

              *10. 功能描述:

              * 將字符串中的字母全部替換成字母的下一個字母,要是最后一位是z或Z則替換為a或A。

              * 輸入:aBxyZ

              * 輸出:bCyzA

              */

              public class NextString {

              public static void main(String[] args) {

              String str = "aBxyZ";

              System.out.println(nextString(str));

              }

              public static String nextString(String str) {

              String result = "";

              char[] arr = str.toCharArray();

              for(int i = 0;i

              if(arr[i] == z || arr[i] == Z) {

              arr[i] = (char)(arr[i]-25);

              }else if(arr[i]<=z&&arr[i]>=a || arr[i]<=z&&arr[i]>=A) { 1+2+3 = 6

              運行時間限制: 無限制

              內存限制: 無限制

              輸入: 0xff ff ff ff以內的整數

              輸出: NA

              樣例輸入: 123

              樣例輸出: 6

              package com.xcbeyond;

              /**

              *

              * @author xcbeyond

              * @date 2015/05/12 15:23:16

              */

              public class Demo36 {

              public static void main(String[] args) {

              int num = 123;

              System.out.println(bitSum(num));

              }

              public static int bitSum(int num) {

              int res = 0;

              if(num<10) {

              res = num;

              }else {

              res = num%10 + bitSum(num/10);

              }

              return res;

              }

              }

              37.提取不重復的整數

              描述: 輸入一個int型32位整數,按照從右向左的閱讀順序,返回一個不含重復數字的新的整數。

              運行時間限制: 10 Sec

              內存限制: 無限制

              輸入: 整數,如9876673

              注意:

              1、整數最后的0,請忽略,例如:輸入1750,輸出:571

              2、負數,保留-在前面,例如:輸入-175,輸出:-571

              輸出: 整數,如37689

              樣例輸入: 9876673

              樣例輸出: 37689

              package com.xcbeyond;

              /**

              *

              * @author xcbeyond

              * @date 2015/05/12 15:50:34

              */

              public class Demo37 {

              public static void main(String[] args) {

              int num = -12310;

              System.out.println(getConvertInt(num));

              }

              public static int getConvertInt(int num) {

              String str = String.valueOf(num);

              StringBuffer sb = new StringBuffer();

              boolean flg = true;

              if(str.charAt(0) == -) {

              flg = false;

              sb.append(str.charAt(0));

              }

              if(str.charAt(str.length()-1) != 0) {

              sb.append(str.charAt(str.length()-1));

              }

              for(int i = str.length()-2;i>0;i--) {

              sb.append(str.charAt(i));

              }

              if(flg) {

              sb.append(str.charAt(0));

              }

              return Integer.parseInt(sb.toString());

              }

              }

              華為JAVA考試試題 2

              1、Collection 和 Collections的區別?

              Collection是集合的根接口,其下有set及list

              Collections是集合的算法。

              2、Set里的元素是不能重復的,那么用什么方法來區分重復與否呢? 是用==還是equals()? 它們有何區別?用contains來區分是否有重復的對象。還是都不用?

              在比較時先調用hashCode方法,如果不相同,證明不相等。

              如果相同,再調用equals方法,如果equals方法相同,證明相等,不相同,證明不相等。

              ==:主要用在基本數據類型及引用

              equals:主要是對象或對象引用的比較。

              集合中是否包含某一個元素用contains來判斷。

              3、List,Set,Map是否繼承自Collection接口?

              List,set繼承于Collection

              Map沒有繼承于Collection,其相對是獨立的。

              屬于Collection類型的對象,可以通過構造函數將一個集合構造成另外一個集合。

              4、面向對象的特征有哪些方面?

              1.抽象:

              找共性,將共有的屬性、方法放到父類中

              2.繼承:

              子類繼承于父類,具有父類的所有屬性與方法,可以重用,也可以覆蓋。

              3.封裝:

              一個類包括多個屬性及方法。

              4. 多態性:

              5、String是最基本的數據類型嗎?

              基本數據類型包括byte、int、char、long、float、double、boolean和short。

              java.lang.String類是final類型的,因此不可以繼承這個類、不能修改這個類。為了提高效率節省空間,我們應該用StringBuffer類

              6、int 和 Integer 有什么區別?

              int 是基本數據類型,不是對象,占一個內存空間,沒有方法。與其同類的有long,char,doble

              Integer是封裝類,具有方法及屬性。與其同類的有Long,Double.Float

              7、運行時異常與一般異常有何異同?

              運行時異常:JVM拋出的異常,代碼中不用處理。

              一般異常:用戶拋出的異常,如果用throws 聲明了,調用這個方法的代碼必須對其處理。

              8、&和&&的區別?

              &:與: 左邊若為false右邊還執行。

              &&:短路與,左邊若為false右邊不執行。

              9、final,finally,finalize的區別?

              final 用于聲明屬性,方法和類,分別表示屬性不可變,方法不可覆蓋,類不可繼承。

              finally是異常處理語句結構的一部分,表示總是執行。

              finalize是Object類的一個方法,在垃圾收集器執行的時候會調用被回收對象的此方法,可以覆蓋此方法提供垃圾收集時的其他資源回收,例如關閉文件等。

              10、heap和stack有什么區別?

              棧是一種線形集合,其添加和刪除元素的操作應在同一段完成。棧按照后進先出的方式進行處理。堆是棧的一個組成元素

              11、Static Nested Class 和 Inner Class的不同?

              Static Nested Class是被聲明為靜態(static)的內部類,它可以不依賴于外部類實例被實例化。而通常的內部類需要在外部類實例化后才能實例化。

              12、GC是什么? 為什么要有GC?

              GC是垃圾收集的意思(Gabage Collection),內存處理是編程人員容易出現問題的地方,忘記或者錯誤的內存回收會導致程序或系統的不穩定甚至崩潰,Java提供的GC功能可以自動監測對象是否超過作用域從而達到自動回收內存的目的,Java語言沒有提供釋放已分配內存的顯示操作方法。

              13、short s1 = 1; s1 = s1 + 1;有什么錯? short s1 = 1; s1 += 1;有什么錯?

              short s1 = 1; s1 = s1 + 1; (s1+1運算結果是int型,需要強制轉換類型)

              short s1 = 1; s1 += 1;(可以正確編譯)

              14、Math.round(11.5)等於多少? Math.round(-11.5)等於多少?

              Math.round(11.5)==12

              Math.round(-11.5)==-11

              round方法返回與參數最接近的長整數,參數加1/2后求其floor.

              15、Java有沒有goto?

              java中的保留字,現在沒有在java中使用。

              16、給出一個你最常見到的runtime exception ArithmeticException,ArrayStoreException,BufferOverflowException,BufferUnderflowException,CannotRedoException,CannotUndoException,ClassCastException,CMMException,ConcurrentModificationException,DOMException,EmptyStackException,IllegalArgumentException,IllegalMonitorStateException,IllegalPathStateException,IllegalStateException,ImagingOpException,IndexOutOfBoundsException,MissingResourceException,NegativeArraySizeException,NoSuchElementException,NullPointerException,ProfileDataException,ProviderException,RasterFormatException,SecurityException,SystemException,UndeclaredThrowableException,UnmodifiableSetException,UnsupportedOperationException

              一般異常:

              IOException

              FileNotFoundException

              SqlException

              17、數組有沒有length()這個方法? String有沒有length()這個方法?

              數組沒有length()這個方法,有length這個屬性

              String有length()這個方法.

              18、構造器Constructor是否可被override?

              構造器Constructor不能被繼承,因此不能重寫Overriding,但可以被重載Overloading。

              19、是否可以繼承String類?

              String類是final類故不可以繼承。

              20、swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?

              switch(expr1)中,expr1是一個整數表達式。因此傳遞給 switch 和 case 語句的.參數應該是 int、 short、 char 或者 byte。long,string 都不能作用于swtich。

              21、try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執行,什么時候被執行,在return前還是后?

              會執行,在return前執行。

              22、兩個對象值相同(x.equals(y) == true),但卻可有不同的hash code,這句話對不對?

              不對,有相同的 hash code

              這是java語言的定義:

              1) 對象相等則hashCode一定相等;

              2) hashCode相等對象未必相等

              23、四種會話跟蹤技術?

              Cookie

              Session

              Hidden

              url 重寫

              24、寫一個Singleton出來。

              Singleton模式主要作用是保證在Java應用程序中,一個類只有一個實例存在。

              一般Singleton模式通常有幾種種形式:

              第一種形式: 定義一個類,它的構造函數為private的,它有一個static的private的該類變量,在類初始化時實例話,通過一個public的getInstance方法獲取對它的引用,繼而調用其中的方法。

              public class Singleton {

              private Singleton(){}

              //在自己內部定義自己一個實例,是不是很奇怪?

              //注意這是private 只供內部調用

              private static Singleton instance = new Singleton();

              //這里提供了一個供外部訪問本class的靜態方法,可以直接訪問

              public static Singleton getInstance() {

              return instance;

              }

              }

              第二種形式:

              public class Singleton {

              private static Singleton instance = null;

              public static synchronized Singleton getInstance() {

              //這個方法比上面有所改進,不用每次都進行生成對象,只是第一次

              //使用時生成實例,提高了效率!

              if (instance==null)

              instance=new Singleton();

              return instance; }

              }

              其他形式:

              定義一個類,它的構造函數為private的,所有方法為static的。

              一般認為第一種形式要更加安全些

              25、Java中的異常處理機制的簡單原理和應用。

              原理: 有錯直接轉到異常處理部分或向上拋出。

              應用:JAVA的異常就是錯誤,有兩種,一種是運行時,編碼可以不用捕捉。一種是一般異常,如果throws聲明了,必須進行處理。

              26、描述一下JVM加載class文件的原理機制?

              JVM中類的裝載是由ClassLoader和它的子類來實現的,Java ClassLoader 是一個重要的Java運行時系統組件。它負責在運行時查找和裝入類文件的類。