Java期中考试总结(甘琳凤)

考试中遇到的问题以及一些总结
1、下面不属于Java语言注释的是()。

A、 //
B、 /*.........*/
C  /**..........*/
D、 '''..........''''

我的答案:D
解析:D选项是在python中的注释

2、Java数据类型包括基本数据类型和引用数据类型,下面不属于引用类型的是()。

A、类(class)
B、接口(interface)
C、注解(@interface)
D、字符和布尔型

我的答案:D
解析:基本数据类型只有8种,可按照如下分类:
①整数类型:long、int、short、byte
②浮点类型:float、double
③字符类型:char
④布尔类型:boolean
引用数据类型非常多,大致包括:
类、 接口类型、 数组类型、 枚举类型、 注解类型、 字符串型。例如,String类型就是引用类型。
故选D

3、下面不属于Java合法标识符的是()

A、 Manager_name
B、_var
C、 $var
D、9_var

我的答案:D
解析:(1) 标识符由字母、数字、下划线“_”、美元符号“$”或者人民币符号“¥”组成,并百且首字母不能是数字。
(2) 不能把关键字和保留字作为标识符。
(3) 标识符没有长度限制。
(4) 标识符对大小写敏感。
D选项首字母是数字,则不合法。

4、表达式 -10 % -3的计算结果为()

A、1
B、-1
C、3
D、-3

我的答案:B
解析:取余运算的符号是根据被除数来决定的

5、表达式-192>>3的计算结果为()

A、 -24
B、 -23
C、 -232
D、-248

我的答案:A
解析:>>表示右移,如果该数为正,则高位补0,若为负数,则高位补1;

-192的二进制原码为1000 0000 1100 0000
-192的二进制反码为1111 1111 0011 1111
-192的二进制补码为1111 1111 0100 0000

-192>>3就是将-192的二进制补码右移三位,高位补1,那么得到:

右移两位后的补码为1111 1111 1110 1000
右移两位后的反码为1111 1111 1110 0111
右移两位后的原码为1000 0000 0001 1000

将得到的原码转换为十进制,那么-192>>3的结果为-24,故选A

6、阅读下面程序,下列描述正确的是()

public class Test {
   int x;
   public Test(String t) {
      System.out.println("Test");
   }
   public static void main(String[] args) {
     Test test = null;
     System.out.println(test.x);
   }
}
A、x尚未初始化,程序有编译错误。
B、test尚未初始化,程序有编译错误。
C、程序具有运行时NullPointerException,因为在执行test.x时test为空。
D、程序有编译错误,因为无法从定义对象的类创建对象。

我的答案:C
在这里插入图片描述
7、返回StringBuffer变量strBuf中最后一个字符的代码是()

A、strBuf.charAt(strBuf.capacity() - 1)
B、StringBuffer.charAt(strBuf.capacity() - 1)
C、StringBuffer.charAt(strBuf.length() - 1)
D、strBuf.charAt(strBuf.length() - 1)

我的答案:D

8、“AbA”.compareToIgnoreCase(“abC”) 返回值是()

A、 0
B、 2
C、 -2
D、 -1

我的答案:C
解析:compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。A比C小2,则返回-2

9、下面程序运行后,显示myCount.count的值是()。

public class Test {
    public static void main(String[] args) {
        Count myCount = new Count();
        int times = 0for (int i=0; i<100; i++)
            increment(myCount, times);
        System.out.println( "myCount.count =   " + myCount.count);
        System.out.println("times = "+ times);
    }
    public static void increment(Count c, int times) {
        c.count++;
        times++;
    }
}
class Count {
    int count;
    Count(int c) {
        count = c;
    }
    Count() {
        count = 1;
    }
}
A、99
B、101
C、100
D、 98

我的答案:B
运行结果如下:
在这里插入图片描述
10、分析以下将布尔值赋给even变量代码片段,下面选项描述正确的是()

Code 1: 
if (number % 2 == 0)
   even = true;
else 
   even = false;
Code 2: 
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
A、代码3有语法错误,因为您试图将数值类型赋给变量even
B、代码2有语法错误,因为条件表达式中不能产生true和false字面量值。
C、这三个都是正确的,但代码1是首选。
D、这三个都是正确的,但代码2是首选。
E、这三个都是正确的,但代码3是首选。

我的答案:E

11、分析下面代码,下列选项描述正确的是()。

public class Test {
   public static void main(String[] args) {
     System.out.println(xMethod(5, 500L));
   }

   public static int xMethod(int n, long l) {
     System.out.println("int, long");
     return n;
   }

   public static long xMethod(long n, long l) {
     System.out.println("long, long");
     return n;
   }
}
A、由于编译器无法区分要调用的xmethod,因此程序无法编译。
B、程序运行良好,但显示5以外的内容。
C、程序显示
   long,long
    5
D、 程序显示
    int,long
    5

我的答案:D
运行结果如下:
在这里插入图片描述
12、下面程序输出结果为()。

class Test {
  public static void main(String[] args) {
    int[] list1 = {1, 2, 3};
    int[] list2 = {1, 2, 3};
    list2 = list1;
    list1[0] = 0; list1[1] = 1; list2[2] = 2;
    for (int i = 0; i < list2.length; i++)
      System.out.print(list2[i] + " ");
  }
}
A、0 1 2 
B、1 2 3
C、1 1 1 
D、0 1 3 

我的答案:A
运行结果如下:
在这里插入图片描述

13、子类从其超类继承的方法是()

A、private method
B、protected method
C、public method
D、 a and c
E、b and c

我的答案:E

14、下面程序的运行结果是()

interface A {   
  void print();
}
 class C {}
 class B extends C implements A {  
  public void print() { }
}
 public class Test {
  public static void main(String[] args) {
    B b = new B();
    if (b instanceof A)
      System.out.println("b is an instance of A");
    if (b instanceof C)
      System.out.println("b is an instance of C");
  }
}
A、Nothing.
B、b is an instance of A.
C、b is an instance of C.
D、b is an instance of A followed by b is an instance of C.

我的答案:D
运行结果如下:
在这里插入图片描述

二:简答题

1:编写一个方法来查找可比较对象数组中的最大值(假设参数中的对象是可比较对象的实例)。方法签名如下:

public static Comparable max(Comparable[] a)
{
   Comparable mark_max=a[0];
   for(int i=0;i<a.length;i++)
   {
    if((mark_max.compareTo(a[i]))<0)
    {
         mark_max=a[i];
    }
 }
   return mark_max;
}

编写一个名为Hexagon的类,该类扩展GeometricObject并实现接口Comparable。假设六边形的六个边大小相等。六边形类定义如下:

public class Hexagon extends GeometricObject implements Comparable {
  private double side;
   /** Construct a Hexagon with the specified side */
  public Hexagon(double side) {
    // Implement it
      this.side = side;
  }
  /** Implement the abstract method findArea in
     GeometricObject */
  public double findArea() {
       // Implement it (4-23.png)
     return 3 * Math.sqrt(3) * side * side;
}
 /** Implement the abstract method findPerimeter in
     GeometricObject */
  public double findPerimeter() {
    // Implement it              
     return 6 * side;
  }
  /** Implement the compareTo method in
     the Comparable interface to  */
  public int compareTo(Object obj) {
    // Implement it (compare two Hexagons based on their areas)   
    if (this.side > ((Hexagon) obj).side)
	return 1;
else if (this.side == ((Hexagon) obj).side)
	return 0;
else
	return -1;
  }

}

2:下面的代码定义了一个媒体(Media)接口及其三个子接口:图书(Book)、视频(Video)和报纸(Newspaper),Library类是一个非泛型类,请使用泛型重新设计该类。

import java.util.List
import Java.util.ArrayList
interface Media{}
interface Book extends Media{}
interface video extends Media{}
interface Newspaper extends Media{}
public class Libaray{
private List re= new ArrayList();
public void addMedia(Media x){
  re.add(x);
}
public Media retrieveLast(){
   int size = re.size();
   if(size>0){
     return (Media)re.get(size-1);
   }
   return null;
   }
}

使用泛型

public class Library<T extends Media> {
    private List<T> resources = new ArrayList<T>();

    public void addMedia(T x) {
        resources.add(x);
    }
    public T retrieveLast() {
        int size = resources.size();
        if (size > 0) {
            return <T>resources.get(size - 1);
        }
        return null;
    }
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值