PS1

(a) Which of the following statements is correct for a method that overrides the following method:(2 Points)

public void add(int a);
  1. The overriding method must return void
  2. The overriding method must return int
  3. The overriding method can return any type

ans: 1

(b) Which of the following statements is correct for a method that overloads the following method: (2 Points)

public void add(int a);
  1. The overloading method must return void
  2. The overloading method can return any type
  3. The overloading method must take int a as a parameter
  4. The overloading method can take any parameters

ans: 2

(c) Given the following classes de�ned in separate �les, what is the e�ect of
compiling and running class Test? (2 Points)

  class Vehicle {
    public void drive() {
      System.out.println("Vehicle: drive");
    }
  }
  class Car extends Vehicle {
    public void drive() {
      System.out.println("Car: drive");
    }
  }
  public class Test {

    public static void main(String[] args) {
      Vehicle v;
      Car c;
      v = new Vehicle();
      c = new Car();
      v.drive();
      c.drive();
      v = c;
      v.drive();
    }
  }
  1. Generates compile error at v = c
  2. Generates runtime error at v = c
  3. Prints: Vehicle : drive
    Car: drive
    Car: drive
  4. Prints: Vehicle : drive
    Car: drive
    Vehicle: drive

ans: 3

(d) What is wrong with the following code? (2 Points)

class MyException extends Exception {
}

public class Qb4ab {
  public void foo() {
    try {
      bar();
    } catch (MyException e) {
    } finally {
      baz();
    }
  }

  public void bar() throws MyException {
    throw new MyException();
  }

  public void baz() throws RuntimeException {
    throw new RuntimeException();
  }
}
  1. Since the method foo() does not catch the exception generated by the method baz(), it must declare the RuntimeException in a throws clause
  2. A try block cannot be followed by both a catch and a �nally block
  3. An empty catch block is not allowed
  4. A catch block cannot follow a finally block
  5. A finally block must always follow one or more catch blocks

ans: 5

(e) What is the result of compiling and running the following code: (2 Points)

abstract class MineBase {
  abstract void amethod();

  static int i;
}

/**
 * Mine
 */
public class Mine extends MineBase {
  public static void main(String[] args) {
    int[] arr = new int[5];
    for (int i = 0; i < arr.length; i++) {
      System.out.println(arr[i]);
    }
  }
}
  1. Prints a sequence of �ve zeros
  2. Generates runtime error since array arr has not been initialized
  3. Generates other error(s)

ans: 3

(f) Which statement, when inserted at (1), will raise a runtime exception? (2
Points)

/**
 * Q3ae4
 */
class A {
}

class B extends A {
}

class C extends A {
}

public class Q3ae4 {
  public static void main(String[] args) {
    A x = new A();
    B y = new B();
    C z = new C();

    // (1) insert code here
  }
}
  1. x = y;
  2. z = x;
  3. y = (B) x;
  4. z = (C) y;
  5. y = (A) y;

ans: 2, 4, 5编译报错, 没有运行时错误. 用jdk1.8测试.

(g) Which statement(s) are true about the following code? (2 Points)

class A {
  public A() {

  }

  public A(int i) {
    this();
  }
}

class B extends A {
  public boolean B(String msg) {
    return false;
  }
}

class C extends B {
  private C() {
    super();
  }

  public C(String msg) {
    this();
  }

  public C(int i) {
  }
}
  1. The code will fail to compile
  2. The constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C
  3. Class C deifines three constructors
  4. Objects of class B cannot be constructed
  5. At most one of the constructors of each class is called as a result of constructing an object of class C

ans: 2,3,5

(h) What is the output of the given function? (2 Points)

public void divide(int a, int b){
  try{
    int c = a/b;
  }catch(Exception e){
    System.out.print("Exception ");
  }finally{
    System.out.println("Finally");
  }
}
  1. Finally
  2. Exception
  3. Exception Finally
  4. No Output

ans: 1,3

(i) Which type constraints, when inserted at (1), will allow the class to compile?(3 Points)

class Interval<______> { // (1) INSERT TYPE CONTRAINT HERE
  private N lower, upper;

  public void update(N value) {
    if (lower == null || value.compareTo(lower) < 0) {
      lower = value;
    }
    if (upper == null || value.compareTo(upper) > 0) {
      upper = value;
    }
  }
}

1 N extends Object
2 N extends Comparable<N>
3 N extends Object & Comparable<N>
4 N extends Number
5 N extends Number & Comparable<N>
6 N extends Comparable<N> & Number
7 N extends Integer
8 N extends Integer & Comparable<N>

ans: 2,3,5,7

(j) Given the following interface declaration, which declaration is valid?
Points)


code_for_j.png

ans: 2

(k) Which parameter declarations can be inserted at (1) so that the program compiles without warning? (3 Points)

import java.util.*;

interface Wagger {
}

class Pet implements Wagger {
}

class Dog extends Pet {
}

class Cat extends Pet {
}

public class Q100_51 {
  public static void main(String[] args) {
    List<Pet> p = new ArrayList<Pet>();
    List<Dog> d = new ArrayList<Dog>();
    List<Cat> c = new ArrayList<Cat>();

    examine(p);
    examine(d);
    examine(c);
  }

  static void examine(List<? extends Pet> pets) {
    System.out.println(pets);
  }
}
  1. List<? extends Pet>
  2. List<? super Pet>
  3. List<? extends Wagger>
  4. List<? super Wagger>
  5. List<?>
  6. All of the above
    ans:1,3,5

(l) Given the following code, which statements are true about the program?(3 Points)

import java.io.Serializable;

class Person {
  protected transient String name;

  Person() {
    this.name = "NoName";
  }

  Person(String name) {
    this.name = name;
  }
}

class Student extends Person {
  protected long studNum;

  Student() {
  }

  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
}

public class GraduateStudent extends Student implements Serializable {
  private int year;

  GraduateStudent(String name, long studNum, int year) {
    super(name, studNum);
    this.year = year;
  }

  public String toString() {
    return "(" + name + "," + studNum + "," + year + ")";
  }
}
import java.io.*;

public class Q800_60 {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    GraduateStudent stud1 = new GraduateStudent("Aesop", 100, 1);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    GraduateStudent stud2 = (GraduateStudent) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}
  1. Fails to compile
  2. Compiles, but throws a runtime exception
  3. Prints (Aesop, 100, 1)(NoName, 0, 1)
  4. Prints (Aesop, 100, 1)(Aesop, 100, 1)
  5. Prints (Aesop, 100, 1)(null, 0, 1)
    ans: 3. 父类需要实现Serializable接口才会被序列化. 如果没有则需要有无参构造函数. 反序列化的时候需要用到.

(m) Which statements are true about the classes SupA, SubB, and SubC? (3 Points)

import java.util.*;

class SupA<T> {
  public List<?> fuddle() {
    return null;
  }

  public List scuddle(T t) {
    return null;
  }
}

class SubB<U> extends SupA<U> {
  public List fuddle() {
    return null;
  }

  public List<?> scuddle(U t) {
    return null;
  }
}

class SubC<V> extends SupA<V> {
  public List<V> fuddle() {
    return null;
  }

  public List<? extends Object> scuddle(V t) {
    return null;
  }
}
  1. Class SubB will not compile
  2. Class SubC will not compile
  3. Class SubB will compile
  4. Class SubC will compile
  5. Class SubB overloads the methods in class SupA
  6. Class SubC overloads the methods in class SupA
  7. Class SubB overrides the methods in class SupA
  8. Class SubC overrides the methods in class SupA
    ans: 3, 4, 7, 8

(n) Which interface is used to define a class that can execute within its own thread? (2 Points)

  1. Run
  2. Runnable
  3. Thread
  4. Threadable
  5. Executable
    ans: 2

(o) Which method is used to schedule a thread for execution?

  1. Init()
  2. Start()
  3. Run()
    ans: 2

(p) Which method(s) may cause a thread to stop execution? (3 Points)

  1. Sleep()
  2. Stop()
  3. Yield()
  4. Wait()
  5. Notify()
    ans: 1,3,4

(q) Given the following code, which of the following statements are true? (3
Points)

public class Agg {

  public static void main(String[] args) {
    Agg a = new Agg();
    a.go();
  }

  public void go() {
    DSRoss ds1 = new DSRoss("one");
    ds1.start();
  }
}

class DSRoss extends Thread {
  private String sTname = "";

  DSRoss(String s) {
    sTname = s;
  }

  public void run() {
    notwait();
    System.out.println("finished");
  }

  public void notwait() {
    while (true) {
      try {
        System.out.println("waiting");
        wait();
      } catch (InterruptedException e) {

      }
      System.out.println(sTname);
      notifyAll();
    }
  }
}

1 Fails to compile
2 Prints "waiting"
3 Prints "waiting" "finished"
4 Compiles, but throws a runtime exception

ans: 4

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,423评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,773评论 0 0
  • 《疯传》引言读后感 原来口口相传的美食、衣服、视频、思想等都是有六大感染力原则支撑的。 产品或思想需要包含社交货币...
    米线儿当妈笔记阅读 722评论 0 1
  • 观影时间:2019-1-13周日 中午12:00和芯蝶一起观看,芯渝在我的怀里睡觉。 工具:iPad mini 1...
    安妮1990阅读 358评论 0 0
  • 你丧失了生存空间,不是意味着你没有饭吃,而是在这种“超越基本收入/卡路里”的竞争中,你变得可有可无。“可有可无”是...
    gyl58365阅读 136评论 0 0