Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
1. Which of the following can
B,C.
fill in the blank to make the
When declaring a class that uses generics, you
code com-pile?
must specify a name for the formal type parameter
public class News< >
(normally T or R). Java uses the standard rules for
{} (Choose all that apply)
naming a variable or class. A question mark is not
A. ?
allowed in a variable name, making I incorrect.
B. News
While it is common practice to use a single
C. Object
uppercase letter for the type parameter, this is
D. None of them
not required. It certainly isn't a good idea to use
existing class names like the News class being
declared here or the Object class built into java.
However, this is allowed, and Option B,C is
2. Does it compile?
correct.
Integer[] numbers = { new
Inte-ger(42)}; True
Object[] objects = numbers;
3. Does it compile?
List<String> numbers =
new Ar-rayList<>(); no
List<Object> objects = We cannot assign a List<String> to a
numbers; List<Object>.
4. Set<String> set = new
Hash-Set<>();
System.out.println(set.add(" true
Spar-row")); false
System.out.println(set.add("
Spar-row"));
What will be printed?
5. List<String> birds =
new Ar-rayList<>();
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
[hawk]
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
birds.add("hawk"); Set<Rabbit> rabbits =
birds.add("hawk"); new TreeSet<>();
birds.remove("hawk"); rabbits.add(new
System.out.println(birds); Rabbit());
System.out.println(rabbi
6. List<String> birds = ts);
new Ar-rayList<>();
birds.add("hawk");
birds.add("hawk");
birds.remove(100);
System.out.println(birds);
7. List<String> birds =
new Ar-rayList<>();
birds.add("hawk");
birds.add("hawk");
System.out.println(birds.
con-tains(1));
8. NavigableSet<Integer> set
= new TreeSet<>();
for (int i = 1; i <= 20; i++)
set.add(i);
System.out.println(set.lower(1
0));
System.out.println(set.high-
er(20));
System.out.println(set.floor(10
));
System.out.println(set.ceiling(
20));
9. static class Rabbit{ }
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
IndexOutOfBoundsException
false (signature: boolean contains(Object object))
9
null
10
20
B.
Compile but ClassCastException in runtime. Rabbit
does not implement Comparable. TreeSet is not
able to sort it into the proper position in the set.
100% Verified Answers |Already Graded A+
1. Which of the following can
B,C.
fill in the blank to make the
When declaring a class that uses generics, you
code com-pile?
must specify a name for the formal type parameter
public class News< >
(normally T or R). Java uses the standard rules for
{} (Choose all that apply)
naming a variable or class. A question mark is not
A. ?
allowed in a variable name, making I incorrect.
B. News
While it is common practice to use a single
C. Object
uppercase letter for the type parameter, this is
D. None of them
not required. It certainly isn't a good idea to use
existing class names like the News class being
declared here or the Object class built into java.
However, this is allowed, and Option B,C is
2. Does it compile?
correct.
Integer[] numbers = { new
Inte-ger(42)}; True
Object[] objects = numbers;
3. Does it compile?
List<String> numbers =
new Ar-rayList<>(); no
List<Object> objects = We cannot assign a List<String> to a
numbers; List<Object>.
4. Set<String> set = new
Hash-Set<>();
System.out.println(set.add(" true
Spar-row")); false
System.out.println(set.add("
Spar-row"));
What will be printed?
5. List<String> birds =
new Ar-rayList<>();
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
[hawk]
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
birds.add("hawk"); Set<Rabbit> rabbits =
birds.add("hawk"); new TreeSet<>();
birds.remove("hawk"); rabbits.add(new
System.out.println(birds); Rabbit());
System.out.println(rabbi
6. List<String> birds = ts);
new Ar-rayList<>();
birds.add("hawk");
birds.add("hawk");
birds.remove(100);
System.out.println(birds);
7. List<String> birds =
new Ar-rayList<>();
birds.add("hawk");
birds.add("hawk");
System.out.println(birds.
con-tains(1));
8. NavigableSet<Integer> set
= new TreeSet<>();
for (int i = 1; i <= 20; i++)
set.add(i);
System.out.println(set.lower(1
0));
System.out.println(set.high-
er(20));
System.out.println(set.floor(10
));
System.out.println(set.ceiling(
20));
9. static class Rabbit{ }
, Java OCP Part3. Generics and Collections Exam with all Correct &
100% Verified Answers |Already Graded A+
IndexOutOfBoundsException
false (signature: boolean contains(Object object))
9
null
10
20
B.
Compile but ClassCastException in runtime. Rabbit
does not implement Comparable. TreeSet is not
able to sort it into the proper position in the set.