PS1

2018-11-17  本文已影响4人  空谷幽心

(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

上一篇下一篇

猜你喜欢

热点阅读