java 常见考题

来源:互联网 发布:网络礼仪 编辑:程序博客网 时间:2024/05/02 01:42

1.下列那一行代码编译后不会出现警告或错误。

1) char c="a"; 2) byte b=257; 3) boolean b=null; 4) int i=10; 5float f=1.3;

2.下面这段代码编译时会发生什么情况。

public class MyClass { 

        public static void main(String arguments[]) { 

                amethod(arguments); 

                } 

        public void amethod(String[] arguments) { 

                System.out.println(arguments); 

                System.out.println(arguments[1]); 

        } 

}

1)     error Can't make static reference to void amethod. 
2)     error method main not correct 
3)     error array must include parameter 
4)     amethod must be declared with String 

3.byte 的取值范围是:

1)-128 127 2-255 256 3-65535 65536 4)根据不同的java虚拟机而定;

4.下面的命令的输出结果是什么。

java myprog good morning

public class myprog{ 

public static void main(String argv[])

        
                 System.out.println(argv[2]) 
        }
}
1)     myprog 
2)      good
3)      morning 
4)     Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"

5.下面哪些变量不是合法变量。

1) 2variable 2) variable2 3) _whatavariable 4) _3_  5) $anothervar 6) #myvar

6.当下面这段程序编译并运行时会出现什么结果。

public class MyClass{ 
        static int i; 
        public static void main(String argv[]){ 
                System.out.println(i); 
                } 
}
1)     Error Variable i may not have been initialized 2) null 3) 1 4) 0
7.       String s1=new String("Hello") 
String s2=new String("there"); 
String s3=new String();
下面哪条是正确的。
1)     s3=s1 + s2; 2) s3=s1-s2; 3) s3=s1 & s2; 4) s3=s1 && s2
8. 下面这段代码编译和运行时会发生什么情况。
abstract class MineBase { 
        abstract void amethod(); 
        static int i; 
}
public class Mine extends MineBase{ 
        public static void main(String argv[]){ 
                        int[] ar=new int[5]; 
                        for(i=0;i < ar.length;i++) 
                                System.out.println(ar[i]); 
                        } 
}
1)     05打印出来 2) Error: ar is used before it is initialized 
3) Error Mine must be declared abstract 4) IndexOutOfBoundes Error

9. Which two can be used to create a new Thread? (Choose Two)

1) Extend java.lang.Thread and override the run method.

2) Extend java.lang.Runnable and override the start method.

3) Implement java.lang.Thread and implement the run method.

4) Implement java.lang.Runnable and implement the run method.    

5) Implement java.lang.Thread and implement the start method

10.写出下面代码的运行结果。
          public class Pass{
        static int j=20;
        public static void main(String argv[]){
               int i=10;
                Pass p = new Pass();
                 p.amethod(i);
                System.out.println(i);    
                System.out.println(j);        
        }
        public void amethod(int x){
        x=x*2;
        j=j*2;
        }
  }
x=10y=40

11.class SuperClass{ public float aaa(){return 3.14f;}   }

public class Test extends SuperClass {

           public float aaa(){ return 5.14f; }

           public double aaa(double double1) {return double1;}

           public void aaa(){}           //error

public float aaa(float float1){return float1;}

}

//重载绝对不能方法名相同,而类型不同。

12.    public  int  bbb(){

                      static int i=0;    // error  

                      i++;

                      return i;

      }

静态量不能在方法中定,

13public static void main(String arg[]){

           short b=57, a=47;

           double c=(short)b/a*2.0; 

           int d =(short)b/a*2;

}

注意其中的区别。

14. public class Test  { 

           public static void main(String[] args) {

                      String s ;

                      Test test = new Test();

                      test.f(s);     //error

           }

           public void f(String s){

                     s="1223";

                      System.out.println(s);

           }

}

error: The local variable s may not have been initialized

1.员变量的初始化
量可以在定义处或者在构造方法中被式初始化。如果在都没有初始化,保留自初始化的默认值final型的量必式初始化。
态变量可以在定义处或者在静码块中被式初始化。如果在都没有初始化,那保留自初始化的默认值final型的静态变量必在定义处或者在静码块中被式初始化。
2. 局部量的初始
      局部量必在使用之前做式初始化。如果编译器能一个局部量在使用之前可能没有被初始化,编译器将报错
      如果局部量没有被初始化,并且在方法中一直没有被使用,编译和运行都会通

可以改正为:String s = null ;

15public class Test  { 

           public static void main(String[] args) {

                      int i = 0;

                      Test test = new Test();

                      test.f();

           }

           public void f(){

                     i++;                           //error

                      System.out.println(i);  //error

           }

}

error :  The local variable i is never read

16.  abstract class AAA{ int i=0;}

class SuperClass extends AAA{         i=3;  }  //error

public class Test  extends SuperClass {        

public static void main(String[] args) {

                      Test test = new Test();

                      test.f();

}

public void f(){

                     i++;

                      System.out.println(i);

}

}

error :  Syntax error on token "i", VariableDeclaratorId expected after this token

体内是声明量和函数的地方,你突然冒出一个 i=3; 自然是不行的.可以在方法中运用。

17.      public class Test  {      

           public static void main(String[] args) {

                      Test test = new Test();

                      if(test.f(2)){                 //error

                                  System.out.println("111");

                      }else{

                                  System.out.println("222");

                      }                               

           }

           public int f(int i){return i++;       }         

}

error: Type mismatch: cannot convert from int to boolean

JAVA中,if()用的是一个布表达式,而不是数字一点与C/C++不同。布型和数字型不能相互转换
在switch(expr1)句中,expr1必int型是赋值兼容的;byte,short或型可被自;不允使用float或long表达式。
case(expr2)句中,expr2是与int赋值兼容的常量表达式。当量或表达式的不能与任何case相匹配,将default后的程序代。如果没有break句作某一个case段的束句,程序继续执行下一个case的代码块,而不检查下一个case表达式的

18    public class Test  {          

           byte[] arry1,arry2[];

           byte[][] arry3;

           byte arry4[][];  

           public void f(){ 

                      arry1=arry2;   //error

                      arry2=arry3;

                      arry2=arry4;

           }         

}

error : Type mismatch: cannot convert from byte[][] to byte[]

19. abstract class AAA{  int i=0; }

public class Test extends AAA {      

           public static void main(String arg[]){

        int[] ar=new int[5];

        for(i=0;i < ar.length;i++)  //error

             System.out.println(ar[i]);

        }

}

error: Cannot make a static reference to the non-static field i

改正为abstract class AAA{ static int i=0; }即可

20.public int bbb(){               

              int i;                                              //error

              i++;

              return i;

}

error: The local variable i may not have been initialized

当变量被定义于函数之内,java会运用编译期间错误消息来告诉你没有被初始化。

 员变量的初始化

21.class CtorBase {

              CtorBase(int k) { System.out.println("Base Created");}          

}

public class Complex extends CtorBase {

              Complex(int k) { System.out.println("Complex Created");}   //error

              public static void main(String args[]) {

                            Complex a = new Complex(10);

              }

}

error: Implicit super constructor CtorBase() is undefined. Must explicitly invoke another constructor

Rule1编译是确保至少有一个构造函数;如果没有构造函数编译器会它构造一个无参的构造函数Rule2:如果有了一个构造函数,不管它是有参函数是无参函数,编译器都不会再它构造一个构造函数

  这过程中,编译器会去用其父的无参构造函数,如果其父没有默(无参)的构造函数构造程出编译不能通

 

22.class CtorBase {

              int i;

              CtorBase(int k) { this.i = k; }          

}

public class Complex extends CtorBase {

              Complex(int k) {

                               super(k);

              }

              public static void main(String args[]) {

                            Complex a = new Complex(10);

                            System.out.println("a.i="+a.i);       

}

}

结果:a.i=10

 

23.class CtorBase {

              int i;

              CtorBase(int k) { this.i = k; }          

        CtorBase() { }

}

public class Complex extends CtorBase {

              Complex(int k) {  }            

              public static void main(String args[]) {

                            Complex a = new Complex(10);

                            System.out.println("a.i="+a.i);

              }

}

结果:a.i=0

24。

public class Test{
 static int cnt=0;
 static int sum=0;
 int number;
 public static void main (String args []){
  A a = new A();
  B b = new B();
  Test test1 = new Test(11);
  Test test2 = new Test(22);
  test2 = new Test(33);
     System.out.println("cnt="+cnt);  
     System.out.println("sum="+sum);
     a.show();
     b.show();    
 }
 Test(int n){
  number=n;
  cnt++;
  sum+=number;
 }
}
 
 class A {
  int   a=2;
  void show(){
   System.out.println("a1="+ a);
   
  }
 }
 class B extends A{
  int a=3;
  void show(){
   System.out.println("a2="+ super.a);
   super.show();
   System.out.println("a3="+ a);   
  }
 }

 结果:

cnt=3
sum=66
a1=2
a2=2
a1=2
a3=3

25.

class ValHold{
 public int i = 10;
}

public class Demo{
 
 public void amethod(){
  ValHold v = new ValHold();
  another(v);
  System.out.println(v.i);
 }
 
 public void another(ValHold v){
  v.i = 20;
  ValHold vh = new ValHold();
  v =vh;
  System.out.println(v.i);
 }
 
 public static void main(String[] argv){
  Demo o = new Demo();
  o.amethod();
 }
}

结果:

10
20

  当一个引用量作参数传递给一个方法, 个方法内可以改变变量的,即改引用指向的,(中将vh赋给v)但是方法的束后,量恢原来的,量仍然指向原来的. (another(v)束之后,v又回到第一次ValHold v = new ValHold();指向的地址空.) 但是如果在方法内改了引用指向的象的数据(属性),当方法的束后,尽管引用仍然指向原来的,象的某个属性已被改(viv.i=20候就已被改,所以another束后,v.i经变成了20)

26.

public class Test {
        public static void main(String [] args){
                Base b = new Subclass();
                System.out.println(b.x);
                System.out.println(b.method());
        }
}
class Base{
        int x = 2;
        int method(){
           return x;
        }
}
class Subclass extends Base{
        int x = 3;
        int method(){
           return x;
        }
}
果:2 3
   声明b,你能访问象部分只是Base的部分; Subclass的特殊部分是藏的.是因为编译,e 是一个Base,而不是一个Subclass.但重写的方法除外。abstract和final相互排斥,前者用于,后者禁止抽象中的抽象方法不能static方法不能被override,只是被(hidden)了
27。
public class Test{
        public static String setFileType(String fname){
                int p=fname.indexOf('.');
                if(p>0) fname=fname.substring(0,p);
                fname+=".TXT";
                return fname;
        }
        public static void main(String args[]){
                String theFile="Program.java";
                String s = setFileType(theFile);
                System.out.println("Created "+theFile);
                System.out.println("Created "+s);
        }
}
Stringjava成安全的StringString的任一个操作都是先重新生成一个String的拷 然后对这个拷贝进行操作。 所以String在参数传递候可以看作是值传递 即如果你需要修改一个String并返回修改后的String, 你得要再去接一下返回
原创粉丝点击