(a) Which of the following statements is correct for a method that overrides the following method:(2 Points)
public void add(int a);
- The overriding method must return void
- The overriding method must return int
- 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);
- The overloading method must return void
- The overloading method can return any type
- The overloading method must take int a as a parameter
- 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();
}
}
- Generates compile error at v = c
- Generates runtime error at v = c
- Prints: Vehicle : drive
Car: drive
Car: drive - 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();
}
}
- Since the method foo() does not catch the exception generated by the method baz(), it must declare the RuntimeException in a throws clause
- A try block cannot be followed by both a catch and a �nally block
- An empty catch block is not allowed
- A catch block cannot follow a finally block
- 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]);
}
}
}
- Prints a sequence of �ve zeros
- Generates runtime error since array arr has not been initialized
- 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
}
}
- x = y;
- z = x;
- y = (B) x;
- z = (C) y;
- 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) {
}
}
- The code will fail to compile
- 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
- Class C deifines three constructors
- Objects of class B cannot be constructed
- 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");
}
}
- Finally
- Exception
- Exception Finally
- 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
3 N extends Object & Comparable
4 N extends Number
5 N extends Number & Comparable
6 N extends Comparable
7 N extends Integer
8 N extends Integer & Comparable
ans: 2,3,5,7
(j) Given the following interface declaration, which declaration is valid?
Points)
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 p = new ArrayList();
List d = new ArrayList();
List c = new ArrayList();
examine(p);
examine(d);
examine(c);
}
static void examine(List extends Pet> pets) {
System.out.println(pets);
}
}
- List extends Pet>
- List super Pet>
- List extends Wagger>
- List super Wagger>
- List>
- 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();
}
}
- Fails to compile
- Compiles, but throws a runtime exception
- Prints (Aesop, 100, 1)(NoName, 0, 1)
- Prints (Aesop, 100, 1)(Aesop, 100, 1)
- 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 {
public List> fuddle() {
return null;
}
public List scuddle(T t) {
return null;
}
}
class SubB extends SupA {
public List fuddle() {
return null;
}
public List> scuddle(U t) {
return null;
}
}
class SubC extends SupA {
public List fuddle() {
return null;
}
public List extends Object> scuddle(V t) {
return null;
}
}
- Class SubB will not compile
- Class SubC will not compile
- Class SubB will compile
- Class SubC will compile
- Class SubB overloads the methods in class SupA
- Class SubC overloads the methods in class SupA
- Class SubB overrides the methods in class SupA
- 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)
- Run
- Runnable
- Thread
- Threadable
- Executable
ans: 2
(o) Which method is used to schedule a thread for execution?
- Init()
- Start()
- Run()
ans: 2
(p) Which method(s) may cause a thread to stop execution? (3 Points)
- Sleep()
- Stop()
- Yield()
- Wait()
- 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