100% found this document useful (33 votes)
213 views

Introduction To Java Programming Comprehensive Version 9th Edition Liang Test Bank

This document contains a test bank for the 9th edition of the textbook "Introduction to Java Programming: Comprehensive Version" by Y. Daniel Liang. The test bank contains multiple choice and true/false questions to assess understanding of Java programming concepts covered in the textbook. It is divided into three parts testing basic Java syntax, object-oriented programming concepts, and specific multiple choice questions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (33 votes)
213 views

Introduction To Java Programming Comprehensive Version 9th Edition Liang Test Bank

This document contains a test bank for the 9th edition of the textbook "Introduction to Java Programming: Comprehensive Version" by Y. Daniel Liang. The test bank contains multiple choice and true/false questions to assess understanding of Java programming concepts covered in the textbook. It is divided into three parts testing basic Java syntax, object-oriented programming concepts, and specific multiple choice questions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Java Programming

Comprehensive Version 9th Edition


Liang Test Bank
Visit to Download in Full: https://testbankdeal.com/download/introduction-to-java-prog
ramming-comprehensive-version-9th-edition-liang-test-bank/
Name:_______________________ CSCI 1302 OO Programming
Armstrong Atlantic State University
(50 minutes) Instructor: Y. Daniel Liang

Part I:

(A)
B’s constructor is invoked
A’s constructor is invoked

(B)
(a) The program has a syntax error because x does not have the compareTo
method.

(b) The program has a syntax error because the member access operator (.) is
executed before the casting operator.

(C)
(1) false
(2) true
(3) false (because they are created at different times)
(4) true

(D) Will statement3 be executed?


Answer: No.

If the exception is not caught, will statement4 be executed?


Answer: No.

If the exception is caught in the catch clause, will statement4 be


executed?
Answer: Yes.

(E) The method throws a checked exception. You have to declare to throw
the exception in the method header.

Part II:

public static Comparable max(Comparable[] a) {


Comparable result = a[0];

for (int i = 1; i < a.length; i++)


if (result.compareTo(a[i]) < 0)
result = a[i];

1
return result;
}

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 getArea in


GeometricObject */
public double getArea() {
// Implement it ( area = 3 * 3 * side * side )
return 3 * Math.squr(3) * side * side;
}

/** Implement the abstract method getPerimeter in


GeometricObject */
public double getPerimeter() {
// 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;
}
}

Part III: Multiple Choice Questions:

1. A subclass inherits _____________ from its superclass.

a. private method
b. protected method
c. public method
d. a and c
e. b and c
Key:e

2
#
2. Show the output of running the class Test in the following code:

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.
Key:d

#
3. When you implement a method that is defined in a superclass, you
__________ the original method.

a. overload
b. override
c. copy
d. call
Key:b

#
4. What is the output of running the class C.

public class C {
public static void main(String[] args) {
Object[] o = {new A(), new B()};
System.out.print(o[0]);
System.out.print(o[1]);
}
}

class A extends B {
public String toString() {
return "A";
}
}

3
class B {
public String toString() {
return "B";
}
}

a. AB
b. BA
c. AA
d. BB
e. None of above
Key:a

#
5. What is the output of running class C?

class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}

class B extends A {
public B(String s) {
System.out.println(s);
}
}

public class C {
public static void main(String[] args) {
B b = new B("The constructor of B is invoked");
}
}
a. none
b. "The constructor of B is invoked"
c. "The default constructor of A is invoked" "The constructor of B
is invoked"
d. "The default constructor of A is invoked"
Key:c

#
6. Analyze the following code:

public class Test1 {


public Object max(Object o1, Object o2) {
if ((Comparable)o1.compareTo(o2) >= 0) {
return o1;
}
else {
return o2;
}
}
}

a. The program has a syntax error because Test1 does not have a main
method.

4
b. The program has a syntax error because o1 is an Object instance
and it does not have the compareTo method.
c. The program has a syntax error because you cannot cast an Object
instance o1 into Comparable.
d. The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e. b and d are both correct.
Key:e

#
7. The method _____ overrides the following method:

protected double xMethod(int x) {…};

a. private double xMethod(int x) {…}


b. protected int xMethod(double x) {…}
c. public double xMethod(double x) {…}
d. public double xMethod(int x) {…}
Key:d

#
8. Which of the following possible modifications will fix the errors in
this code?

public class Test {


private double code;

public double getCode() {


return code;
}

protected abstract void setCode(double code);


}

a. Remove abstract in the setCode method declaration.


b. Change protected to public.
c. Add abstract in the class declaration.
d. b and c.
Key:c

#
9. Analyze the following code.

class Test {
public static void main(String[] args) {
Object x = new Integer(2);
System.out.println(x.toString());
}
}

a. The program has syntax errors because an Integer object is


assigned to x.
b. When x.toString() is invoked, the toString() method in the Object
class is used.
c. When x.toString() is invoked, the toString() method in the
Integer class is used.
d. None of the above.

5
Key:c

#
10. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = new Object();
String d = (String)o;
}
}

a. ArithmeticException
b. No exception
c. StringIndexOutOfBoundsException
d. ArrayIndexOutOfBoundsException
e. ClassCastException
Key:e

#
11. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = null;
System.out.println(o.toString());
}
}

a. ArrayIndexOutOfBoundsException
b. ClassCastException
c. NullPointerException
d. ArithmeticException
e. StringIndexOutOfBoundsException
Key:c

You might also like