學(xué)習(xí)啦>創(chuàng)業(yè)指南>職場>面試題>

華為的java面試題及答案

時(shí)間: 護(hù)托1061 分享

  沒有想到華為的面試題就是非同一般,很多題不是一眼就能夠看得出來,下面就由學(xué)習(xí)啦小編為大家介紹一下華為的java面試題及答案的文章,歡迎閱讀。

  華為的java面試題及答案篇1

  QUESTION NO: 1

  publicclass Test1 {

  publicstaticvoid changeStr(String str){

  str="welcome";

  }

  publicstaticvoid main(String[] args) {

  String str="1234";

  changeStr(str);

  System.out.println(str);

  }

  }

  //輸出結(jié)果:1234

  //這里雖然是一個(gè)靜態(tài)方法,但是里面的變量是一個(gè)局部變量,

  //所以這里不因?yàn)槭庆o態(tài)方法,就誤認(rèn)為里面的變量也是靜態(tài)變量了

  QUESTION NO:2

  publicclass Test2 {

  staticboolean foo(char c) {

  System.out.print(c);

  returntrue;

  }

  publicstaticvoid main(String[] argv) {

  int i = 0;

  //for(65;88&&(i<2);67)

  for (foo('A'); foo('B') && (i < 2); foo('C')) {

  i++;

  foo('D');

  }

  }

  }

  /*

  What is the result?

  A. ABDCBDCB

  B. ABCDABCD

  C. Compilation fails.

  D. An exception is thrown at runtime.

  //輸出結(jié)果是:ABDCBDCB

  分析:FOR循環(huán)里面講究的條件要為真,與你的判斷式是什么沒有關(guān)系

  就像這里,雖然是打印的字母,但是卻不是false,所以可以執(zhí)行

  第一次進(jìn)行循環(huán):

  foo('A')打印字母A,(注:這里不是false條件就默認(rèn)為true條件)

  foo('B')打印字母B,i=0,比較(i < 2),條件為true,進(jìn)行循環(huán)體,foo('D')打印D

  foo('C')打印字母C

  第二次循環(huán):

  foo('B')打印B,i=1,比較(i < 2)為true,進(jìn)行循環(huán)體,foo('D')打印D

  foo('C')打印字母C

  第三次循環(huán):

  foo('B')打印字母B,i=2,比較(i < 2)為false,退出循環(huán),得結(jié)果

  */

  華為的java面試題及答案篇2

  1. class A {

  2. protected int method1(int a, int b) { return 0; }

  3. }

  Which two are valid in a class that extends class A? (Choose two)

  A. public int method1(int a, int b) { return 0; }

  B. private int method1(int a, int b) { return 0; }

  C. private int method1(int a, long b) { return 0; }

  D. public short method1(int a, int b) { return 0; }

  E. static protected int method1(int a, int b) { return 0; }

  publicclass B extends A{

  /**

  *@paramargs

  */

  //can not reduce the visibility of the inherited method from A

  //即不能夠使從類A中繼續(xù)來的方法的可見性降低

  //private int method1(int a, int b) { return 0; }

  //This static method cannot hide the instance method from A

  //靜態(tài)方法不能夠隱藏繼承于A的實(shí)例

  //static protected int method1(int a, int b) { return 0; }

  //返回類型與A中的該方法不一致

  //public short method1(int a, int b) { return 0; }

  /**

  *總結(jié):類的繼承中,如果要想重載父類的方法,必須要和父類中的返回類型、可見性等等都要操作一致

  *否則,程序就會報(bào)錯(cuò)。一定遵守子類要遵從于父類的原則

  *而我選擇的答案居然是privateintmethod1和staticprotectedint

  *我選擇第一個(gè)的錯(cuò)誤理由是:因?yàn)樵瓉頌楸Wo(hù)的,如果我這里設(shè)為public,那么就擴(kuò)展了其原來的可見性

  *本來原來就是對包外不可見的,現(xiàn)在變成對包外可見的了,所以就選擇的是private

  *選擇第二個(gè)的錯(cuò)誤理由是:都是保護(hù)的,這里只是變成了靜態(tài)的而已

  */

  //這里是寫了一個(gè)重載方法,因?yàn)閰?shù)類型不一致,不會報(bào)錯(cuò)

  privateint method1(int a, long b) { return 0; }

  //可見性可以增大,但是不能夠縮小,正確

  publicint method1(int a, int b) { return 0; }

  publicstaticvoid main(String[] args) {

  // TODO Auto-generated method stub

  }

  }

  華為的java面試題及答案篇3

  QUESTION NO: 1

  1. public class Outer{

  2. public void someOuterMethod() {

  3. // Line 3

  4. }

  5. public class Inner{}

  6. public static void main( String[]argv ) {

  7. Outer o = new Outer();

  8. // Line 8

  9. }

  10. }

  Which instantiates an instance of Inner?

  A. new Inner(); // At line 3

  B. new Inner(); // At line 8

  C. new o.Inner(); // At line 8

  D. new Outer.Inner(); // At line 8//new Outer().new Inner()

  答案如下:

  publicclass Outer {

  publicvoid someOuterMethod() {

  // Line 3

  new Inner();//放在這里不出錯(cuò)

  }

  publicclass Inner {

  }

  publicstaticvoid main(String[] argv) {

  Outer o= new Outer();

  // Line 8

  //o不能夠被解釋成為一種類型,出錯(cuò)

  //new o.Inner();

  /**

  *下面兩種用法,都報(bào)下面的錯(cuò)誤:

  *NoenclosinginstanceoftypeOuterisaccessible.

  *Mustqualifytheallocationwithanenclosinginstance

  *oftypeOuter(e.g.x.newA()wherexisaninstanceofOuter)

  */

  //new Outer.Inner();

  //new Inner();

  }

  }

  QUESTION NO: 2

  Which method is used by a servlet to place its session ID in a URL that is written to the servlet’s response output stream?

  (譯:那個(gè)方法是servlet用于將其session ID入在一個(gè)URL中,該URL寫入servlet的響應(yīng)輸出流)

  A. The encodeURL method of the HttpServletRequest interface.

  B. The encodeURL method of the HttpServletResponse interface.

  C. The rewriteURL method of the HttpServletRequest interface.

  D. The rewriteURL method of the HttpServletResponse interface.

華為的java面試題及答案

沒有想到華為的面試題就是非同一般,很多題不是一眼就能夠看得出來,下面就由學(xué)習(xí)啦小編為大家介紹一下華為的java面試題及答案的文章,歡迎閱讀。 華為的java面試題及答案篇1 QUESTION NO: 1 publicclass Test1 { publicstaticvoid chan
推薦度:
點(diǎn)擊下載文檔文檔為doc格式
3120554