0
点赞
收藏
分享

微信扫一扫

Java基础阅读题

梯梯笔记 2022-03-30 阅读 50


一、程序阅读题:(每小题3分,共81分)

1.写出以下程序的运行结果。

 public  class  EqualOrNot

 {

public static void main(String[] args) 

{ B  b1=new  B(5);

B  b2=new  B(5);

System.out.println(b1==b2);

System.out.println(b1.equals(b2));

}

}

class  B

int  x;

B( int  y){ x=y; }

}

Flase flase

2、写出以下程序的运行结果。

class   StaticTest {

static  int  x=1;

int y;

StaticTest(){

y++;

}

public  static  void  main(String  args[ ]){

      StaticTest  st=new StaticTest();

      System.out.println("x=" + x);

  System.out.println("st.y=" + st.y);

   st=new StaticTest();

  System.out.println("st.y=" + st.y);

      }

static  { x++;}

}

211

3、写出以下程序的功能。

public  class  TestArray{   

public  static  void   main(String  args[ ]){  

       int   i , j ;

int  a[ ] = {1,3,2,5,4};

    for  ( i = 0 ; i < a.length-1; i ++ ) {

    int  k = i;

    for  ( j = i ; j < a.length ;  j++ )

    if  ( a[j]>a[k] )  k = j;

   int  temp =a[i];

    a[i] = a[k]; a[k] = temp;  

}

    for  ( i =0 ; i<a.length; i++ )

    System.out.print(a[i]+"  ");

         System.out.println( );

}

}

54321

 4、写出以下程序的运行结果。

class MyException extends Exception{

public String toString( ){ return "negative"; }

}

public class ExceptionDemo{

public static void  mySqrt(int a) throws MyException {

if( a<0 ) throw  new MyException();

System.out.println(Math.sqrt(a));

public static void main( String args[] ){

try{

mySqrt(25 ); mySqrt(-5 ); }

catch( MyException e ){

System.out.println("Caught "+e);

}

}

}

 5.写出以下程序的运行结果。

import    java.io.*;

public  class  TestString

{     

public  static  void  main(String args[ ])

{    

StringC  s = new  StringC (“Hello  ","World!");

        System.out.println(s); 

     }

}

class   StringC {

  String   s1;

  String   s2;

  StringC( String  str1 , String  str2 ){

  s1 = str1;  

s2 = str2; 

}

  public   String   toString( ){ 

return  s1+s2;

}

}

 6.写出以下程序的功能。

class  Test {

public static void main(String[] args) {

String s;

char c;

int  upper,lower;

upper=lower=0;

s=args[0];

for (int i=0;i<s.length();i++){

c=s.charAt(i);

if(c>='a' && c<='z')

lower++;

if(c>='A' && c<='Z')

upper++;

}

System.out.println(upper+”,”+lower);

}

}

7.写出以下程序的运行结果。

class OverloadDemo{

     void   testOverload( int  i ){

        System.out.println(“int”);     

}

     void  testOverload(String  s){

          System.out.println(“String”); 

    }

     public  static  void main(String  args[ ]){

          OverloadDemo   a=new  OverloadDemo ( );

          char    ch=’x’;

a.testOverload(ch);

     }

 8.阅读以下程序,写出输出结果。

class  First{

public  First(){

aMethod();

}

public  void  aMethod(){

System.out.println(“in  First  class”);

}

}

public  class  Second  extends  First{

public  void  aMethod(){

System.out.println(“in  Second  class”);

}

public static void main(String[ ]  args){

new  Second( );

}

}

in  Second  class


9.写出以下程序的运行结果。

import java.io.*;

public class UseLabel

{ public static void main(String[] args)

{Loop:

   for(int  i=2; i<10;  i++)

    {   for(int  j=2;j<i;j++)

    if( i%j == 0) continue  Loop; 

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

}

  }

}

10、写出以下程序的运行结果。

public class ChangeStrDemo { 

public static void changestr(String str){ 

     str="welcome"; 

     } 

     public static void main(String[] args) { 

     String str="1234"; 

     changestr(str); 

     System.out.println(str); 

   }

}

 11、写出以下程序的运行结果。

public class FooDemo{ 

static boolean foo(char c) { 

System.out.print(c); 

  return true; 

 } 

 public static void main(String[] args ) { 

  int i =0; 

  for ( foo(’a’); foo(’b’)&&(i<2); foo(’c’)){ 

  i++ ; 

  foo(’d’); 

    } 

}


12.写出以下程序的运行结果。

public class Test1 {

  public static void changeStr(String str){

   str="welcome";

  }

  public static void main(String[] args) {

   String str="1234";

   changeStr(str);

   System.out.println(str);

  }

}

welcome 

13、写出以下程序的运行结果。

public class Inc{

  public static void main(String argv[]){

   Inc inc = new Inc();

   int i =0;

   inc.fermin(i);

   i = i++;

   System.out.println(i);

  }

  void fermin(int i){

   i++;

  }

}


14、运算符优先级问题,下面代码的结果是多少


1. package test;  

2.   

3. public class Test {  

4.     public static void main(String[] args) {  

5.         int k = 0;  

6.         int ret = ++k + k++ + ++k + k;  

7.         System.err.println(ret);  8

8.     }  




15、运算符问题,下面代码分别输出什么?

1. package test;  

2.   

3. public class Test {  

4.     public static void main(String[] args) {  

5.         int i1 = 10, i2 = 10;  

6.       System.err.println("i1 + i2 = " + i1 + i2);  20

7.       System.err.println("i1 - i2 = " + i1 - i2);  0

8.       System.err.println("i1 * i2 = " + i1 * i2);  400

9.       System.err.println("i1 / i2 = " + i1 / i2);  1

10.     }  

11. }  

16、下面代码的结果是什么?还是抛出异常?


1. package test;  

2.   

3. public class Test {  

4.       

5.     public void myMethod(String str) {  

6.         System.err.println("string");  

7.     }  

8.       

9.     public void myMethod(Object obj) {  

10.         System.err.println("object");  

11.     }  

12.       

13.     public static void main(String[] args) {  

14.         Test t = new Test();  

15.         t.myMethod(null);  

16.     }  

17. }  

String





17、假设今天是9月8日,下面代码输出什么?

1. package test;  


2. import java.util.Date;  


3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         Date date = new Date();  

7. System.err.println(date.getMonth() + " " + date.getDate());  

8.     }  

8月8号


18、下面代码的输出结果是什么? 

1. package test;   

2. public class Test {  

3.   

4.     public static void main(String[] args) {  

5.         double val = 11.5;  

6.         System.err.println(Math.round(val));  12

7.         System.err.println(Math.floor(val))11.0

8.         System.err.println(Math.ceil(val));  12.0    }  

9. }  

19、下面代码的结果是什么?

1. package test;  

2.   

3. public class Test extends Base {  

4.   

5.     public static void main(String[] args) {  

6.         Base b = new Test();  

7.         b.method();  

8.           

9.         Test t = new Test();  

10.         t.method();  

11.     }  

12.   

13.     @Override  

14.     public void method() {  

15.         System.err.println("test");  

16.     }  

17.       

18. }  

19.   

20. class Base {  

21.     public void method() throws InterruptedException {  

22.         System.err.println("base");  

23.     }  

24. }  

20、以下代码的结果是什么? 

1. package test;  

2.   

3. public class Test extends Base {  

4.   

5.     public static void main(String[] args) {  

6.         new Test().method();  

7.     }  

8.   

9.     public void method() {  

10.         System.err.println(super.getClass().getName());  

11.         System.err.println(this.getClass().getSuperclass().getName());  

12.     }  

13.       

14. }  

15.   

16. class Base {  

}  








21、true or false?

1. package test;  

2. public class Test {  

3.   

4.     public static void main(String[] args) {  

5.         String str1 = new String("abc");  

6.         String str2 = new String("abc");  

7.         System.err.println(str1.equals(str2));  

8.           

9.         StringBuffer sb1 = new StringBuffer("abc");  

10.         StringBuffer sb2 = new StringBuffer("abc");  

11.         System.err.println(sb1.equals(sb2));  

12.     }  

13. }  

True false

22、输出的结果是什么? 

1. package test;  

2.   

3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         System.err.println(new Test().method1());  

7.         System.err.println(new Test().method2());  

8.     }  

9.       

10.     public int method1() {  

11.         int x = 1;  

12.         try {  

13.             return x;  

14.         } finally {  

15.             ++x;  

16.         }  

17.     }  

18.       

19.     public int method2() {  

20.         int x = 1;  

21.         try {  

22.             return x;  

23.         } finally {  

24.             return ++x;  

25.         }  

26.     }  

27. }  

23、输出什么结果

1. package test;  

2.   

3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         System.err.println(method());  

7.     }  

8.       

9.     public static boolean method() {   

10.          try {   

11.             return true;   

12.         } finally {   

13.           return false;  

14.         }   

15.     }  

24、true or false?理由 

1. package test;  

2.   

3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         Integer i1 = 127;  

7.         Integer i2 = 127;  

8.         System.err.println(i1 == i2);  

9.           

10.         i1 = 128;  

11.         i2 = 128;  

12.         System.err.println(i1 == i2);  

13.     }  

}

False

false




25、true or false?理由 

1. package test;  

2.   

3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         String str1 = "a";  

7.         String str2 = "a";  

8.         String str3 = new String("a");  

9.           

10.         System.err.println(str1 == str2);  

11.         System.err.println(str1 == str3);  

12.         str3 = str3.intern();  

13.         System.err.println(str1 == str3);  

14.     }  

15. }  

True false true

 26、true or false?理由 

1. package test;  

2.   

3. public class Test {  

4.   

5.     public static void main(String[] args) {  

6.         System.err.println(12 - 11.9 == 0.1);  

7.     }  

true

27、下面为一个单例的实现代码,请指出代码中有几个错误或不合理之处,并改正。 

1. public class Test {  

2.       

3.     public Test instance = null;  

4.       

5.     public static Test getInstance() {  

6.         if (instance == null) {  

7.             instance = new Test();  

8.             return instance;  

9.         }  

10.     }  

11. } 

第一个问题:return 返回的值instance实在if语句的外面返回

二、简述题

1.简述Android消息机制中Looper,Handler,MessageQueque,Message 

在Android消息机制中Looper是的任务就是在列队里面循环取消息,Handler主要任务就是发送和处理消息,MessageQueque主要就是把消息列成队进行等待,Message就是列队里面中等待的的消息。


2. 实现设计模式中观察者模式



3. 实现设计模式之单例模式.

下面就用具体实例来说明这个单利

例:public class Singleputter{

Private  static  S s=null;

Private singleputter{


}

Public static Singleputter getInsatance(){

If(s==null){

S s=new S;

}

return s;

}

}


4.  MVC模式中的M,V,C代表什么.


1模型层(Model):对数据库的操作、对网络等的操作都应该在Model里面处理,当然对业务计算等操作也是必须放在的该层的。就是应用程序中二进制的数据。Model既可以通过xml(values目录下)生成,也可以硬编码的方式直接在代码中指定。View和Model通过Adapter来进行连接。典型的Adapter包括ArrayAdapter(可以Sort()操作)、CusorAdapter(从Cusor中查询到数据源),ListAdapter、SimpleAdapter(最常用)、SpinnerAdapter(它是一个接口,设置Spinner应用SimpleAdapter的setDropDownResource方法)。

2视图层(View):一般采用XML文件进行界面的描述,使用的时候可以非常方便的引入。当然,如何你对Android了解的比较的多了话,就一定可以想到在Android中也可以使用JavaScript+HTML等的方式作为View层,当然这里需要进行Java和JavaScript之间的通信,幸运的是,Android提供了它们之间非常方便的通信实现。View既可以通过xml(layout目录下)生成,也可以通过硬编码的方式直接通过代码生成。对于xml中的View资源,可以在代码中通过getViewById()的方法获得。



3控制层(Controller):Android的控制层的重任通常落在了众多的Acitvity的肩上,这句话也就暗含了不要在Acitivity中写代码,要通过Activity交割Model业务逻辑层处理,这样做的另外一个原因是Android中的Acitivity的响应时间是5s,如果耗时的操作放在这里,程序就很容易被回收掉。






举报

相关推荐

0 条评论