您的位置:首页 > 编程语言 > Java开发

笔试_java基础5

2017-09-13 11:03 381 查看

一、选择题(共20题)

1.Given:

1. public class Delta {

2. static boolean foo(char c) {

3. System.out.print(c);

4. return true;

5. }

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

7. int i =0;

8. for ( foo(‘A’); foo(‘B’)&&(i<2); foo(‘C’)){

9. i++ ;

10. foo(‘D’);

12. }

13. }

14. }

What is the result?

A. ABDCBDCB

B. ABCDABCD

C. Compilation fails. //编译失败。

D. An exception is thrown at runtime. //在运行时抛出异常。

 

 

2.Given:

1. public class Test {

2. public int aMethod() {

3. static int i = 0; //报错
,如果将这行移至到方法上面,就不报错了

4. i++;

5. return i;

6. }

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

8. Test test = new Test();

9. test.aMethod();

10. int j = test.aMethod();

11. System.out.println(j);

12. }

13. }

What is the result?

A. 0

B. 1

C. 2

D. Compilation fails.

 

 

3.Given:

1. public class Test {

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

3. String str = NULL;    //要小写的null

4. System.out.println(str);

5. }

6. }

What is the result?

A. NULL

B. Compilation fails.

C. The code runs with no output. //代码运行没有输出

D. An exception is thrown at runtime.

 

 

4.Given:

Which two allow the class Thing to be instantiated using new Thing()? (Choose two)

//实例化

A. public class Thing {

}

B. public class Thing {

public Thing() {}

}

C. public class Thing {

public Thing(void) {}

}

D. public class Thing {

public Thing(String s) {}

}

E. public class Thing {

public void Thing() {}

public Thing(String s) {}

}

 

 

 

 

 

 

 

 

5.Given:

Which three statements are true? (Choose three)

A. The default constructor initializes method variables. //默认构造函数初始化方法变量。

B. The default constructor has the same
access as its class.  //访问

C. The default constructor invoked the no-arg constructor of the
superclass.

//默认构造函数调用父类的无参数构造函数的。

D. If a class lacks a no-arg constructor, the compiler always creates a default constructor.

E. The compiler creates a default constructor only when there are no other constructors

for the class.

BCE

 

6.Given:

1. public class ReturnIt {

2. Type methodA(byte x, double y) {

3. return (long)x / y * 2;

4. }

5. }

What is the narrowest valid return Type for methodA in line2?

  //最低   //有效

A. int

B. byte

C. long

D. short

E. float

F. double

 

 

7.Given:

Which two cause a compiler error? (Choose two)

A. int[] scores = {3, 5, 7};

B. int [][] scores = {2,7,6}, {9,3,45};

C. String cats[] = {“Fluffy”, “Spot”, “Zeus”};

D. boolean results[] = new boolean [3] {true, false, true};

E. Integer results[] = {new Integer(3), new Integer(5), new

Integer(8)};

F. String[] dogs = new String[]{new String(“Fido”),new

String(“Spike”), new String(“Aiko”)};

 

 

8.Given:

11. int i = 0, j = 5;

12. tp:

13. for (;;) {

14. i++;

15. for(;;) {

16. if (i> --j) {

17. break tp;

18. break tp; //两次break,不清楚什么意思,肯定会报错

19. }

20. }

21. System.out.println(“i=” +“i ,j =”+j); //这句根本没有任何意义,执行不到这一行,肯定也会报错

What is the result?

A. i = 1, j = 0

B. i = 1, j = 4

C. i = 3, j = 4

D. i = 3, j = 0

E. Compilation fails.

 

 

9.Given:

1. public class A {

2. void A() {

3. System.out.println(“Class A”);

4. }

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

6. new A();

7. }

8. }

What is the result?

A. Class A

B. Compilation fails.

C. An exception is thrown at line 2.

D. An exception is thrown at line 6.

E. The code executes with no output.

 

 

10.Given:

12. float f[][][] = new float[3][][];

13. float f0 = 1.0f;

14. float[][] farray = new float[1][1];

What is valid? //有效的

A. f[0] = f0;

B. f[0] = farray;

C. f[0] = farray[0];

D. f[0] = farray[0][0];

 

 

11.Given:

1. public class Test { }

What is the prototype of the default constructor?
//标准、模范

A. Test()

B. Test(void)

C. public Test()

D. public Test(void)

E. public void Test()

 

 

12.Given:

What produces a compiler error?

A. class A {

public A(int x) {}

}

B. class A {

}

class B extends A {

B() {}

}

C. class A {

A() {}

}

class B {

public B() {}

}

D. class Z {

public Z(int x) {}

}

class A extends Z {//继承,,Z类没有无参构造函数

}

 

 

13.Given:

1. public class Alpha{

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

3. if ( args.length == 2 ) {

4. if ( args[0].equalsIgnoreCase(“-b”) )

5. System.out.println( new Boolean( args[1] ));

6. }

7. }

8. }

And the code is invoked by using the command:  //调用

java Alpha –b TRUE

What is the result?

A. true

B. null

C. false

D. Compilation fails.

E. The code runs with no output.

F. An exception is thrown at runtime.

 

 

14.Given:

1. public class X (

2. private static int a;

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

4. modify(a);

5. System.out.println(a);

6. }

7. public static void modify(int a) {

8. a++;

9. }

10 }

What is the result?

A. 0

B. 1

C. Compilation fails.

D. An exception is thrown at runtime.

 

 

15.Given:

1. public class Test {

2. public static void add3 (Integer i) {

3. int val = i.intValue();

4. val += 3;

5. i = new Integer(val);

6. }

7.

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

9. Integer i = new Integer(0);

10. add3(i);

11. System.out.println(i.intValue());

12 }

13 }

What is the result?

A. 0

B. 3

C. Compilation fails.

D. An exception is thrown at runtime.

 

 

16.Given:

11. String a = null;

12. a.concat(“abc”);

13. a.concat(“def”);

14. System.out.println(a);

What is the result?

A. abc

B. null

C. abcdef
D. Compilation fails.

E. The code runs with no output.

F. An exception is thrown at runtime.

 

17.Given:

1. public class Test {

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

3. System.out.println(args.length > 4 &&

4. args[4].equals(“-d”));

5. }

6. }

If the program is invoked using the command line:   //调用

java Test One Two Three –d

What is the result?

A. true

B. false

C. Compilation fails.

D. An exception is thrown at runtime.

 

 

18.Given:

1. public class Foo {

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

3. StringBuffer a = new StringBuffer (“A”);

4. StringBuffer b = new StringBuffer (“B”);

5. operate (a,b);

6. System.out.println(a + “,” +b);

7. }

8. static void operate (StringBuffer x, StringBuffer y) {

9. x.append (y);

10. y = x;

11. }

12. }

What is the result?

A. The code compiles and prints “A,B”.

B. The code compiles and prints “A,A”.

C. The code compiles and prints “B,B”.

D. The code compiles and prints “AB,B”.

E. The code compiles and prints “AB,AB”.

F. The code does not compile because “+” cannot be overloaded for StringBuffer.

 

 

19.Given:

1. public class Test {

2. private static int[] x;

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

4. System.out.println(x[0]);

5. }

6. }

What is the result?

A. 0

B. null

C. Compilation fails.

D. A NullPointerException
is thrown at runtime.

E. An ArrayIndexOutOfBoundsException is thrown at runtime.

 

 

20.Given:

 

11. String a = “ABCD”;

12. String b = a.toLowerCase();

13. b.replace(‘a’, ‘d’);

14. b.replace(‘b’, ‘c’);

15. System.out.println(b);

What is the result?

A. abcd

B. ABCD

C. dccd

D. dcba

E. Compilation fails.

F. An exception is thrown at runtime.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二、填空题(共4题)

1.代码如下:

1. Public class test {

2. public static void stringReplace (String text) {

3. text = text.replace (‘j’ , ‘i’);

4. }

5.

6. public static void bufferReplace (StringBuffer text) {

7. text = text.append (“C”);

8. }

9.

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

11. String textString = new String (“java”);

12. StringBuffer textBuffer = new StringBuffer (“java”);

13.

14. stringReplace (textString);

15. bufferReplace (textBuffer);

16.

17. System.out.println (textString + textBuffer);

18. }

19. }

运行结果是( iavajavaC )

 

Answer: javajavaC

 

 

2.名词解释:JDK是 (
java开发工具包),   //Java Development Kit

 JRE是 ( java运行环境  ),//Java
Runtime Environment

 JVM是(java虚拟机
)   //Java Virtual Machine

 

Answer: Java Development Kit

Java Runtime Environment

Java Virtual Machine

 

 

3.JAVA中==和equals()方法的区别是( )?

==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。       equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。

        ==比较的是2个对象的地址,而equals比较的是2个对象的内容。显然,当equals为true时,==不一定为true。

Answer: ==可以用来比较基本数据类型和引用数据类型,当用来比较基本数据类型时,比较的是基本数据类型的值是否相同,而用来比较引用数据类型时,比较的是这两个对象是否为同一个对象;equals方法只能用来比较引用数据类型,没有被重写之前,可以用来判断比较的两个对象是否为同一个对象,重写之后,用来判断这两个对象中的值或内容是否相同。

 

 

4.下面是一个类的定义,请将(?)补充完整    

 

class  Student{       

String name;       

int age; 

       Student(___String____ name, int a){

          ____this__.name=name;          

this.age=a;       



}

 

Answer: StudentStringthis

 

三、编码题(共5题)

1.题目:

在程序中,经常要对时间进行操作,但是并没有时间类型的数据。那么,我们可以自己实现一个时间类,来满足程序中的需要。

 

定义名为MyTime的类,

 

其中应有三个整型成员:时(hour),分(minute),秒(second),为了保证数据的安全性,这三个成员变量应声明为私有private。 

 

为MyTime类定义构造方法,以方便创建对象时初始化成员变量。

 

再定义diaplay方法,用于将时间信息打印出来,以及定义一个test方法,用于校验时间是否正确。

 

为MyTime类添加以下方法,分别对时、分、秒进行加和减运算: 

addSecond(int sec) 

addMinute(int min) 

addHour(int hou) 

subSecond(int sec) 

subMinute(int min) 

subHour(int hou) 

 

 

package com.lanqiao.test;

 

class MyTime {  

    private int hour;  

    private int minute;  

    private int second;  

  

    public MyTime(int hour,
int minute,
int second) {  

        this.hour =
hour;  

        this.minute =
minute;  

        this.second =
second;  

    }  

  

    public void setHour(int hour) {  

        this.hour =
hour;  

    }  

  

    public void setMinute(int minute) {  

        this.minute =
minute;  

    }  

  

    public void setSecond(int second) {  

        this.second =
second;  

    }  

  

    public int getHour() {  

        return this.hour;  

    }  

  

    public int getMinute() {  

        return this.minute;  

    }  

  

    public int getSecond() {  

        return this.second;  

    }  

  

    //打印时、分、秒  

    public void diaplay() {  

        String HH;  

        String MM;  

        String SS;  

        if(this.hour < 10) {  

             HH =
"0" + this.hour;  

        } else {  

             HH =
"" +  this.hour;  

        }  

        if(this.minute < 10) {  

             MM =
"0" + this.minute;  

        } else {  

             MM =
"" +  this.minute;  

        }  

        if(this.second < 10) {  

             SS =
"0" + this.second;  

        } else {  

             SS =
"" + this.second;  

        }  

        System.out.println(HH +
":" +
MM + ":" +
SS);  

    }  

  

    //对秒进行加运算。  

    public void addSecond(int sec) {  

        int ss =
sec % 60;  

        int mm = (sec / 60) % 60;  

        int hh =
sec / (60 * 60);  

        if ((this.second +
ss) < 60) {  

            this.second +=
ss;  

            this.minute +=
mm;  

            if(this.minute > 60) {  

                this.minute =
this.minute % 60;  

                this.hour++;  

                this.hour +=
hh;  

                if(this.hour > 24) {  

                    this.hour =
this.hour % 24;  

                }  

            }  

            this.hour +=
hh;  

            if(this.hour > 24 ){  

                this.hour =
this.hour % 24;  

            }   

        } else {  

            this.second = (this.second +
ss) % 60;  

            this.minute++;  

            this.minute +=
mm;  

            if(this.minute > 60) {  

                this.minute =
this.minute % 60;  

                this.hour++;  

                this.hour +=
hh;  

                if(this.hour > 24) {  

                    this.hour =
this.hour % 24;  

                }  

            }  

            this.hour +=
hh;  

            if(this.hour > 24 ){  

                this.hour =
this.hour % 24;  

            }  

        }  

        diaplay();  

    }  

  

    //对分进行加运算。  

    public void addMinute(int min) {  

        int mm =
min % 60;  

        int hh =
min /60;  

        if ((this.minute +
mm) < 60) {  

            this.minute +=
mm;  

            this.hour +=
hh;  

            if(this.hour > 24) {  

                this.hour =
this.hour % 24;  

            }  

        } else {  

            this.minute = (this.minute +
mm) % 60;  

            this.hour++;  

            this.hour +=
hh;  

            if(this.hour > 24) {  

                this.hour =
this.hour % 24;  

            }  

        }  

        diaplay();  

    }  

  

    //对时进行加运算。  

    public void addHour(int hou) {  

        if ((this.hour +
hou) < 24) {  

            this.hour +=
hou;  

        } else {  

            this.hour = (this.hour +
hou) % 24;  

        }  

        diaplay();  

    }  

  

    //对秒进行减运算。  

    public void subSecond(int sec) {  

        int ss =
sec % 60;  

        int mm = (sec / 60) % 60 ;  

        int hh =
sec / (60 * 60);  

        if ((this.second -
ss) >= 0) {  

            this.second -=
ss;  

            this.minute -=
mm;  

            if(this.minute < 0) {  

                this.minute =
this.minute + 60;  

                this.hour--;  

                if(this.hour < 0) {  

                    this.hour += 24;  

                }  

            }  

            this.hour -=
hh;  

            if(this.hour < 0) {  

                this.hour += 24;  

            }  

        } else {  

            this.second =
this.second + 60 -ss;  

            this.minute--;  

            if((this.minute -
mm) >= 0) {  

                this.minute -=
mm;  

            } else {  

                this.minute = (this.minute + 60 -mm)
% 60;  

                this.hour--;  

                if((this.hour -
hh) >= 0) {  

                    this.hour -=
hh;  

                } else {  

                    this.hour = (this.hour  -hh)
% 24 + 24;  

                }  

            }  

        }  

        diaplay();  

    }  

  

    //对分进行减运算。  

    public void subMinute(int min) {  

        int mm =
min % 60;  

        int hh =
min / 60;  

        if ((this.minute -
mm) >= 0) {  

            this.minute -=
mm;  

            this.hour -=
hh;  

            if(this.hour < 0) {  

                this.hour =
this.hour % 24 + 24;  

            }  

        } else {  

            this.minute = (this.minute + 60 -mm)
% 60;  

            this.hour--;  

            if((this.hour -
hh) >= 0) {  

                this.hour -=
hh;  

            } else {  

                this.hour = ((this.hour -hh)
% 24 + 24) % 24;  

            }  

        }  

        diaplay();  

    }  

  

    //对时进行减运算。  

    public void subHour(int hou) {  

        hou =
hou %24;  

        if((this.hour -
hou) >= 0) {  

            this.hour -=
hou;  

        } else {  

            this.hour = (this.hour + 24 -hou)
% 24;  

        }  

        diaplay();  

    }  

}  

 

 

package com.lanqiao.test;

 

public class MyTimeTest {

 public static void main(String[]
args) {  

        MyTime time = new MyTime(10, 10, 10);  

        time.diaplay();  

        time.addSecond(15);  

        time.addMinute(35);  

        time.addHour(12);  

  

        time.subSecond(15);  

        time.subMinute(25);  

        time.subHour(15);  

    }  

}

 

 

2.题目:

编写Java程序,模拟简单的计算器。  

定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。

编写构造方法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtration)、乘(multiplication)、除(division)等公有成员方法,

分别对两个成员变量执行加、减、乘、除的运算。  

再新建一个测试类Test,在main方法中创建Number类的对象,调用各个方法,并显示计算结果。

 

public class Number {

private int n1,n2;

public Number(){}

public Number(int n1,
int n2){

this.n1 =
n1;

this.n2 =
n2;

}

public void addition(){

System.out.println("和为:"+(n1+n2));

}

public void subtration(){

System.out.println("差为:"+(n1-n2));

}

public void multiplication(){

System.out.println("积为:"+(n1*n2));

}

public void division(){

System.out.println("商为:"+(n1/n2));

}

}

 

 

public class Test {

public static void main(String[]
args) {

Number num = new Number(8,2);

num.addition();

num.subtration();

num.multiplication();

num.division();

}

}

 

 

3.编写程序:将下面的一段文本中的各个单词的字母顺序翻转,

“To be or not to be",将变成"oT eb ro ton ot eb."

 

public class B3 {

public static void main(String[]
args) {

String s =
"To be or not to be";

String arr[] =
s.split(" ");

StringBuffer sb = new StringBuffer();

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

StringBuffer sb2 = new StringBuffer(arr[i]);

sb2.reverse();

sb.append(sb2);

if(i ==
arr.length-1){

sb.append(".");

}else{

sb.append(" ");

}

}

System.out.println("原字符串为:"+s);

System.out.println("每个单词反转后的字符串为:"+sb);

}

}

 

 

4.写一个方法判断一个字符串是否对称,如“asdfgfdsa”。

 

import java.util.Scanner;

public class B4 {

 

public static void main(String[]
args) {

/*

方法一:

boolean flag=false;

Scanner input = new Scanner(System.in);

System.out.println("请输入一串字符串:");

String s = input.next();

int le = s.length();//获取字符串长度

int c = le/2;//获取字符串一半的长度

char[] ch = s.toCharArray();//将字符串转换为数组

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

if(i<c){//因为是判断对称,所以比较一半就行,当超过一半的长度,说明对称

if(ch[i]==ch[le-i-1]){//第一位与最后一位比较,第二位与倒数第二位比较,以此类推,如果相等,则继续比较,否则不对称

continue;

}else{

flag=true;

break;

}

}

}

if(!flag){

System.out.println("对称");

}else{

System.out.println("不对称");

}

*/

//方法二:

Scanner input = new Scanner(System.in);

System.out.println("请输入一串字符串:");

String s =
input.next();

StringBuffer sb =new StringBuffer(s);

String s1 =
sb.reverse().toString();

if(s.equals(s1)){

System.out.println("字符串对称");

}else{

System.out.println("字符串不对称");

}

}

}

 

 

5.写一个程序,实现以下需求:

String s=”name=zhangsan age=18 classNo=090728”;

将上面的字符串拆分,结果如下:

zhangsan 18 090728

 

public class B5 {

public static void main(String[]
args) {

String s=”name=zhangsan age=18 classNo=090728”;

String[] arr =
s.split(" ");

StringBuffer sb =new StringBuffer();

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

String[] arr2 =arr[i].split("=");

sb.append(arr2[1]);

sb.append(" ");

 }

System.out.println(sb);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: