Quiz
ArrayList
Question 1 Which of the following statement(s) are true about ArrayList<E>? (Select all that are correct)
[x] An ArrayList<String> provides type-safety by only allowing adding objects of type String.
[ ] The number of elements in an ArrayList cannot be changed after it is instantiated.
[x] The number of elements currently in an ArrayList is available using the size() method.
[ ] The indexing operator [] can be used to get/set elements of an ArrayList, just like it can be used on an array.
JDK Packages
Question 2 Which of these JDK packages contains the primary Collection classes and interfaces?
[ ] java.lang
[ ] java.awt
[x] java.util
[ ] java.collection
Exceptions
Question 3 Which of these exceptions is thrown in cases when the file specified for writing is not found?
[ ] FileInputException
[ ] InvalidFileException
[x] FileNotFoundException
[ ] FileException
Unique Elements
Question 4 Which of the following classes enforces the constraint that every element is unique?
[x] TreeSet<T>
[ ] ArrayList<T>
[ ] Stack<T>
[ ] Array
Set Implementation
PartialQuestion 5 Which of the following Java Collection Framework (JCF) classes implement Set<E>? (Select all that are correct)
[ ] UpSet<E>
[x] TreeSet<E>
[ ] MapSet<E>
[x] EnumSet<E>
[x] HashSet<E>
List Implementation
Question 6 Which of the following collection classes implements the List<E> interface? (Select all that are correct)
[ ] Set<E>
[x] LinkedList<E>
[ ] Map<K,V>
[x] ArrayList<E>
Map Implementation
PartialQuestion 7 Which of the following classes implement the Map<K,V> interface? (Select all that are correct)
[ ] StateMap<K,V>
[x] TreeMap<K,V>
[ ] LinkedMashHap<K,V>
[x] HashMap<K,V>
[x] EnumMap<K,V>
Map Definition
Question 8 Which of the following best describes a map in the Java Collections Framework?
[ ] A map is a collection of unique values where each value is associated with one or more keys.
[x] A map is a collection of unique keys where each key is associated with a value.
[ ] A map is a special collection used for geo-location.
[ ] A map is a collection of unique keys where each key is associated with one or more values.
Set Definition
Question 9 Which statement best describes the Java Collections Framework's Set<E> ?
[ ] A Set is a collection of values which must not intersect with any other Set.
[ ] A Set is a collection that must contain duplicate values.
[x] A Set is a collection that contains no duplicate elements.
[ ] A Set is a collection of no more than 6 tennis games, not counting tie- breakers.
Exception Handling
Question 10 Which of these keywords is used to manually/explicitly throw an exception?
[x] throw
[ ] finally
[ ] invoke
[ ] throws
Throws Keyword
Question 11 Throws keyword is used for handling checked exceptions. By using throws, we can declare multiple exceptions at one time
[x] True
[ ] False
I/O Stream
Question 12 What is an I/O Stream in Java?
[ ] I/O Stream is a sequence of characters that exist as a single object.
[ ] I/O Stream is a container object that holds a fixed number of values of a single type.
[x] I/O Stream is a conceptually endless flow of data
[ ] I/O Stream is a framework that provides an architecture to store and manipulate the group of objects.
InputStream
Question 13 The InputStream class is used to read data _________ from a data source.
[ ] String by String
[ ] Character by Character
[ ] Array by Array
[x] Byte by Byte
Scanner Class
Question 14 The scanner class provides methods __________ and__________ which can be used to read a file line by line until an "End of File" (EOF) character is reached. (Select all that are correct)
[ ] readingdata()
[x] hasNextLine()
[ ] readingnextline()
[x] nextLine()
General Purpose Stream
Question 15 In Java, the stream that is intended for general purpose IO(Input/output), not usually character data, is called ___________
[ ] Character Stream
[ ] Writer
[ ] Reader
[x] Byte Stream
Java I/O Stream Classes
Question 16 The ______________ package contains the Java I/O stream classes
[ ] java.file
[x] java.io
[ ] java.input
[ ] java.inout
Java NIO
PartialQuestion 17 The core components of Java NIO are: (Select all that are correct)
[x] Buffers
[ ] Scanner
[x] Selectors
[x] Non-blocking I/O
[x] Channels
Functional Interface
Question 18 What is a Functional Interface in Java?
[ ] An interface that contains one or more abstract method
[ ] An interface that contains no abstract method
[ ] An interface that contains more than one abstract method
[x] An interface that contains only one abstract method
Lambda Expressions
Question 19 What is the output of the following program?
@FunctionalInterface
interface MyFunctionalInterface {
public Integer sqr(int a);
default String sqr(String a) {
return a + a;
}
}
public class Test {
public static void main(String[] args) {
MyFunctionalInterface fi = n -> {
return n * n;
};
System.out.println(fi.sqr(5));
}
}
[x] 25
[ ] 55
[ ] RedundantMethodException
[ ] Unresolved compilation problems: Duplicate default methods named sqr with the parameters (String) and (int) are inherited from the type MyFunctionalInterface
Explanation: The program defines a functional interface MyFunctionalInterface
with an abstract method sqr
that takes an integer argument and returns an integer. The interface also has a default method sqr
that takes a string argument and returns a string. The Test
class implements the interface by defining a lambda expression that squares an integer and assigns it to a variable fi
. The lambda expression is then called with the argument 5, printing the result 25 to the console.
Lambda Expression
Question 20 Which of the following is a valid lambda expression?
[x] (int i) -> i;
[ ] () -> return;
[ ] (int i) -> i++; return i;
[ ] String a, String b -> System.out.print(a+ b);
Question 21 Which of the following is correct lambda syntax? (Select all that apply).
[ ] x->{x+y;};
[x] (x, y) -> {return x*y; };
[ ] x , y- > x+y;
[x] ( x, y) -> x+y;
Functional Interface Annotation
Question 22 Can we define a Functional Interface without using the "@FunctionalInterface" annotation?
[x] True
[ ] False
Last updated