GoConqr has a new look! Let us know what you think.

Quiz

Chapters 12 and 13

Phil McCallion 11 questions

Take the quiz

Questions in this quiz (11)

  1. 1The output from the following code is __________.

    java.util.ArrayList<String> list = new java.util.ArrayList<>();
    list.add("New York");
    java.util.ArrayList<String> list1 = (java.util.ArrayList<String>)(list.clone());
    list.add("Atlanta");
    list1.add("Dallas");
    System.out.println(list);
    1. A[New York]
    2. B[New York, Atlanta]
    3. C[New York, Atlanta, Dallas]
    4. D[New York, Dallas]
  2. 2Show 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");
    }
    }
    1. ANothing.
    2. Bb is an instance of A.
    3. Cb is an instance of C.
    4. Db is an instance of A followed by b is an instance of C.
  3. 3Suppose A is an interface, B is an abstract class that partial implements A, and A is a concrete class with a default constructor that extends B. Which of the following is correct?
    1. AA a = new A();
    2. BA a = new B();
    3. CB b = new A();
    4. DB b = new B();
  4. 4Which of the following is correct?
    1. AAn abstract class does not contain constructors.
    2. BThe constructors in an abstract class should be protected.
    3. CThe constructors in an abstract class are private.
    4. DYou may declare a final abstract class.
    5. EAn interface may contain constructors.
  5. 5What 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");
    }
    }
    1. Anone
    2. B"The constructor of B is invoked"
    3. C"The default constructor of A is invoked" "The constructor of B is invoked"
    4. D"The default constructor of A is invoked"
  6. 6Analyze the following code:

    public class Test1 {
    public Object max(Object o1, Object o2) {
    if ((Comparable)o1.compareTo(o2) >= 0) {
    return o1;
    }
    else {
    return o2;
    }
    }
    }
    1. AThe program has a syntax error because Test1 does not have a main method.
    2. BThe program has a syntax error because o1 is an Object instance and it does not have the compareTo method.
    3. CThe program has a syntax error because you cannot cast an Object instance o1 into Comparable.
    4. DThe program would compile if ((Comparable)o1.compareTo(o2) >= 0)
      is replaced by (((Comparable)o1).compareTo(o2) >= 0).
    5. Eb and d are both correct.
  7. 7Which of the following statements regarding abstract methods is not true?
    1. AAn abstract class can have instances created using the constructor of the abstract class.
    2. BAn abstract class can be extended.
    3. CA subclass of a non-abstract superclass can be abstract.
    4. DA subclass can override a concrete method in a superclass to declare it abstract.
    5. EAn abstract class can be used as a data type.
  8. 8Which 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);
    }
    1. ARemove abstract in the setCode method declaration.
    2. BChange protected to public.
    3. CAdd abstract in the class declaration.
    4. Db and c.
  9. 9Analyze the following code.

    class Test {
    public static void main(String[] args) {
    Object x = new Integer(2);
    System.out.println(x.toString());
    }
    }
    1. AThe program has syntax errors because an Integer object is assigned to x.
    2. BWhen x.toString() is invoked, the toString() method in the Object class is used.
    3. CWhen x.toString() is invoked, the toString() method in the Integer class is used.
    4. DNone of the above.
  10. 10What exception type does the following program throw?
    public class Test {
    public static void main(String[] args) {
    Object o = new Object();
    String d = (String)o;
    }
    }
    1. AArithmeticException
    2. BNo exception
    3. CStringIndexOutOfBoundsException
    4. DArrayIndexOutOfBoundsException
    5. EClassCastException

Information

Description
CS 110 Quiz on Chapters 12 and 13, created by Phil McCallion on 16/07/2017.
Language
English
Views
0
Tags
cs 110
Rating
No ratings yet
Rate this quiz
Rate this quiz

Feedback

What is it about?

What do you think of the new look?

How do you like it?