0

Abstract Classes and Implementing Interfaces

Description: Abstract Classes and Implementing Interfaces
Number of Questions: 15
Created by:
Tags: Abstract Classes and Implementing Interfaces Java/Oracle /C/C++ Java Basics of Java Programming
Attempted 0/15 Correct 0 Score 0

______________ collection class extends the AbstractList for a collection that uses sequential access, rather than the random access of its elements.

  1. Abstract

  2. AbstractSequentialList

  3. AbstractList

  4. LinkedList

  5. AbstractSet


Correct Option: B
Explanation:

This collection class extends the AbstractList for a collection that uses sequential access, rather than the random access of its elements.

Which of the following classes implements a dynamic array by extending the AbstractList?

  1. TreeSet

  2. ArrayList

  3. HashSet

  4. AbstractList

  5. None of these


Correct Option: B
Explanation:

This class implements a dynamic array by extending the AbstractList.

When we implement the Runnable interface in a class, then we must define _____ method.

  1. init()

  2. main()

  3. start()

  4. run()

  5. none of the above


Correct Option: D
Explanation:

This method is declared in Runnable interface

Which of the following statements regarding the packages is correct in Java?

  1. Packages contain a single package statement.

  2. Packages can contain any number of import statements.

  3. Packages contain a single public class consisting of main() function declaration.

  4. Packages contain any number of classes private to the package.

  5. Packages can contain any number of package declarations.


Correct Option: C
Explanation:

This statement is the essential one as execution of the program starts from main() function, which is to be defined in any particular class with public access.

Which of the following interfaces in Java servlets indicates that the servlet is thread safe?

  1. ServletRequest

  2. ServletResponse

  3. SingleThreadModel

  4. Servlet

  5. ServletConfig


Correct Option: C
Explanation:

This interface in Java servlets indicates that the servlet is thread safe.

Which of the following methods in Map.Entry interface in Java returns the value for the map entry?

  1. getKey()

  2. getValue()

  3. hashCode()

  4. none of these


Correct Option: B
Explanation:

This method in Map.Entry interface in Java returns the value for the map entry.

Which of the following methods, declared by Map interface, returns a Set that contains the entries in the Map interface?

  1. containsKey()

  2. containsValue()

  3. entrySet()

  4. none of these


Correct Option: C
Explanation:

This method declared by Map interface, returns a Set that contains the entries in the Map interface.

In Java, which of the following is implemented by extending AbstractSequentialList class?

  1. AbstractCollection

  2. AbstractList

  3. LinkedList

  4. None of these


Correct Option: C
Explanation:

This class in Java implements a linked list by extending AbstractSequentialList class.

Which of the following statements are true?

  1. The Iterator interface declares only three methods: hasNext, next and remove.
  2. The ListIterator interface extends both the List and Iterator interfaces.
  3. The ListIterator interface provides forward and backward iteration capabilities.
  4. The ListIterator interface provides the ability to modify the List during iteration.
  5. The ListIterator interface provides the ability to determine its position in the List.
  1. 2, 3, 4 and 5

  2. 1, 3, 4 and 5

  3. 3, 4 and 5

  4. 1, 2 and 3


Correct Option: B
Explanation:

The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities and the ability to determine the position of the Iterator in the List.

Which of the following in collection classes in Java implements a set stored in a tree and also extends AbstractSet class?

  1. ArrayList

  2. TreeSet

  3. AbstractSet

  4. HashSet


Correct Option: B
Explanation:

This class in collection classes in Java implements a set stored in a tree and also extends AbstractSet class.

Which of the following methods in ListIterator class returns true if there is a next element otherwise returns false?

  1. add()

  2. hasPrevious()

  3. next()

  4. hasNext()


Correct Option: D
Explanation:

This method in ListIterator class returns true if there is a next element otherwise returns false.

What will be the output of the program?

interface Count {
 short counter = 0;
 void countUp();
}
public class TestCount implements Count {
 public static void main(String[] args) {
  TestCount t = new TestCount();
  t.countUp();
 }
 public void countUp() {
  for (int x = 6; x > counter; x--, ++counter) /* Line 10 */ {
   System.out.print(" " + counter);
  }
 }
}
  1. 0 1 2

  2. 1 2 3

  3. 0 1 2 3

  4. Compilation error


Correct Option: D
Explanation:

The code will not compile because the variable counter is an interface variable that is by default final static. The compiler will complain at line 10 when the code attempts to increment counter. So it provides compilation error.

What will be the output of the program?

public abstract class AbstractTest {
 public int getNum() {
  return 45;
 }
 public abstract class Bar {
  public int getNum() {
   return 38;
  }
 }
 public static void main(String[] args) {
  AbstractTest t = new AbstractTest() {
   public int getNum() {
    return 22;
   }
  };
  AbstractTest.Bar f = t.new Bar() {
   public int getNum() {
    return 57;
   }
  };
  System.out.println(f.getNum() + +t.getNum());
 }
}
  1. 57 22

  2. 45 38

  3. 45 57

  4. An exception occurs at runtime


Correct Option: A
Explanation:

You can define an Inner class as abstract, which means you can instantiate only concrete subclasses of the abstract Inner class. The object refered by the variable is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides thegetNum() method (to return 57). Remember that to instantiate a Bar instance, we need an instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.

___________ interface allows a designer to provide a graphical user interface through which bean may be configured.

  1. Customizer

  2. AppletInitializer

  3. BeanInfo

  4. DesignMode

  5. PropertyEditor


Correct Option: A
Explanation:

This interface allows a designer to provide a graphical user interface through which bean may be configured.

Which of the following interfaces in Java Beans API allows a designer to specify information about the properties, events and methods of a bean?

  1. Customizer

  2. Visibility

  3. BeanInfo

  4. DesignMode

  5. None of these


Correct Option: C
Explanation:

This interface in Java Beans API allows a designer to specify information about the properties, events and methods of a bean.

- Hide questions