Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PDF
Which of the following code will make an instance of this class immutable.
E. All of Above.
https://www.certification-questions.com/
Option A,C are correct.
https://www.certification-questions.com/
Q2. Consider the following code:
1. class SuperClass{
2. protected void method1(){
3. System.out.print("M SuperC");
4. }
5. }
6.
7. class SubClass extends SuperClass{
8. private void method1(){
9. System.out.print("M SubC");
10. }
11.
12. public static void main(String[] args){
13. SubClass sc = new SubClass();
14. sc.method1();
15. }
16. }
F. M SubC.
G. M SuperC.
H. M SuperCM SubC.
I. Compilation fails.
None of above.
Option D is correct.
The code fails to compile at line 8, cannot reduce the visibility of the inherited method from SuperClass.
Exam objective : Encapsulation and Subclassing - Creating and use Java subclasses.
https://www.certification-questions.com/
Q3. Given the following class:
E. None Of Above
Option C is correct.
Option A,B, and D are incorrect they are not functional interface, so C is the only valid option.
Exam objective : Concurrency - Creating worker threads using Runnable and Callable.
https://www.certification-questions.com/
Q4 which of the following database url are correct:
A. jdbc:mysql://localhost:3306
B. jdbc:mysql://localhost:3306/sample
C. jdbc:mysql://localhost:3306/sample/user=root?password=secret
D. jdbc:mysql://localhost:3306/sample?user=root&password=secret
E. All
jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]...
Exam objective : Database Applications with JDBC - Connecting to a database by using a JDBC driver.
https://www.certification-questions.com/
Q5. Given the following code:
F. 1 2 3 4 5
A. 0 1 2 3 4
B. 0 2 4 6 8
Option D is correct.
The method signature "int readLine(String fmt, Object... args)" doesn't exist,
the right method return a String so line 5 will give a compile error and option D is right.
Exam objective : I/O Fundamentals - Read and write data from the console
https://www.certification-questions.com/
Q6. Given
A. 18
B. 7
C. 6
E. None of above
Option C is correct.
The class "Singleton" is not a real singleton class , infact at each method invocation "getInstance()" a new object is
created, and so s1, s2, s3 count instance variable are 3, 2, 1 and then option C is correct.
Exam objective : Java Class Design - Create and use singleton classes and immutable classes
https://www.certification-questions.com/
Q7. Given
A. 4612663
B. 5713674
C. 3661264
Option B is correct.
The Program is applying a map function to the stream generated from list. For each Integer element "i" the function
return a new String with value i+1. The stream is then reduced to a String by the concatenation function
"String::concat". So Option B is correct, and A ,C, D, E are incorrect.
Exam objective : Collections Streams, and Filters - Iterating through a collection using lambda syntax
https://www.certification-questions.com/
Q8.Which of the following are correct override of Object class
B. I, II, III.
C. I, II.
D. III, IV.
E. All.
Option B is correct.
The Object class has all the method signature specified above so the override is possible on all options less IV
because is declared final in Object class, so B is correct.
Exam objective : Java Class Design - Override hashCode, equals, and toString methods from Object class
https://www.certification-questions.com/
Q9. Given
A. 2
B. 3
D. An exception is thrown.
E. None of Above.
Option C is correct.
C is correct because the variable "e" is a generic "T" type so the compile has no knowledge of method "compareTo",
to make it compile line 2 need to be changed in :
https://www.certification-questions.com/
Q10. Given
A. start initial
B. initial start
C. initial
Option E is correct.
Because you cannot declare static initializers in an anonymous class, the compilation fail at line 6, so E is correct and
A, B, C, D incorrect.
https://www.certification-questions.com/
Q11. Consider the following class.
1. final class A{
2. private String s;
3. public A(String s){
4. this.s = s;
5. }
6. public String toString(){ return s; };
7. public void setA(String a){ this.s+= a; };
8. }
9.
10. public final class Immutable {
11. private final A a;
12. public Immutable(A a){
13. this.a = a;
14. }
15. public String toString(){ return a.toString();};
16. public static void main(String[] args){
17.
18. A a = new A("Bye");
19. Immutable im = new Immutable(a);
20. System.out.print(im);
21.
22. a.setA(" bye");
23. System.out.print(a);
24. }
25. }
A. Bye bye
B. Bye Bye
C. ByeBye bye
D. Compilation fail
E. None of Above
Option B is correct.
The object "im" is not really an immutable object. The class Don't provide "setter" methods, field is final and private,
don't allow subclasses to override methods, unfortunately in constructur Object a is mutable and pass by reference
without make a protection copy of the object.
https://www.certification-questions.com/
Q12. Given
1. // Code Here
2. @Override
3. public void run() {
4. for(int i = 0;i<10;i++)
5. System.out.print(i);
6. }
7. }
8.
9. public class Test {
10. public static void main(String args[]) {
11. Task t = new Task();
12. Thread thread = new Thread(t);
13. thread.start();
14. }
15. }
0123456789
E. None Of Above
Option B is correct.
Exam objective : Concurrency - Creating worker threads using Runnable and Callable.
https://www.certification-questions.com/
Q13. Given
A. 0123456789
B. 12345678910
Option E is correct.
We are creating a Callable object with an anonymous class at line 5, syntax is correct so option C is incorrect. Passing
the object "c" to an executor and get as return type a Future to wait for thread ends. But at line 17 method wait of
Class Future doesn't exist so E is correct.
Exam objective : Concurrency - Creating worker threads using Runnable and Callable.
https://www.certification-questions.com/
Q14. Which of the followings are valid Executors factory methods:
A. I, II, III
C. I, IV, V
D. II, IV, IV
E. All
Option D is correct.
https://www.certification-questions.com/
Q15. Given
1. class A {
2. private String s;
3. public A(String in){ this.s = in;}
4.
5. public boolean equals(Object in){
6. if(getClass() != in.getClass()) return false;
7. A a = (A)in;
8. boolean ret = (this.s.equals(a.s));
9. return ret;
10. }
11. };
12.
13. public class Program {
14. public static void main(String[] args) {
15. A a1 = new A("A");
16. A a2 = new A("A");
17. if(a1 == a2) System.out.println("true");
18. else System.out.println("false");
19. }
20. }
A. true
B. false
E. None of above
Option B is correct.
In class A the method equals is been override in a correct way, anyway the operator "==" will continue to compare
object reference, so option B is correct.
Exam objective : Java Class Design - Override hashCode, equals, and toString methods from Object class
https://www.certification-questions.com/
Q16. Given the class definition
1. class G<T> {
2. private T t;
3.
4. public void set(T t) { this.t = t; }
5. public T get() { return t; }
6. }
F. I, II, III.
G. I, II, IV.
H. I, III.
I. I, III, IV.
J. All.
Option D is correct.
A type variable can be any non-primitive type and another type variable, so option I,IV are correct and V incorrect,
option II is incorrect because miss the type variable, option III is correct because is the raw type of G<T>.
https://www.certification-questions.com/
Q17. Given
1. class Person{
2. private String name;
3. private int age;
4. Person(String name, int age){
5. this.name = name;
6. this.age = age;
7. }
8. public String toString(){ return name + " " + age; }
9.
10. public static Comparator<Person> COMPARATOR = new Comparator<Person>() {
11. public int compare(Person o1, Person o2) {
12. return (o1.age - o2.age);
13. }
14. };
15. }
16. public class Program {
17.
18. public static void main(String[] args) {
19. List<Person> list = new ArrayList<Person>();
20. list.add(new Person("John",50));
21. list.add(new Person("Frank",15));
22. list.add(new Person("Adam",20));
23.
24. Collections.sort(list,Person.COMPARATOR);
25. list.forEach(a -> System.out.print(a.toString()+" "));
26. }
27. }
Option C is correct.
With the use of The Comparator Object defined in line 10 we sort objects in an order other than their natural
ordering, that is the age instance attribute, than option C is correct meanwhile A and B incorrect. The code has no
compile error and no exception will throw so option D e E are incorrect.
https://www.certification-questions.com/
Q18. Given the following code
1. interface A {
2. public void m();
3. };
4. public class Program {
5. public static void main(String[] args) {
6. int y = 0;
7. //Code Here
8. }
9. }
I. new A(){
void m1(){};
public void m(){};
};
II. new A(){
static int x;
public void m(){};
};
III. new A(){
public void m(){ y++; };
};
IV. new A(){
static final int x = 0;
public void m(){};
};
F. I, II, III.
G. I, IV.
H. I, II.
I. II, IV.
J. All.
Option B is correct.
An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively
final, so III is incorrect. An anonymous class can have static members provided that they are constant variables, then
II is incorrect. Option I and IV are valid declaration, so B is correct.
https://www.certification-questions.com/
Q19. Given
1. class Person{
2. private String name;
3. private int age;
4. Person(String name, int age){
5. this.name = name;
6. this.age = age;
7. }
8.
9. public String toString(){ return name + " " + age; }
10.
11. public static Comparator<Person> COMPARATOR = new Comparator<Person>() {
12. public int compare(Person o1, Person o2) {
13. return (o1.age - o2.age);
14. };
15. };
16. };
17. public class Program {
18.
19. public static void main(String[] args) {
20. Set<Person> list = new TreeSet<Person>(Person.COMPARATOR);
21. list.add(new Person("John",50));
22. list.add(new Person("Frank",15));
23. list.add(new Person("Adam",15));
24. list.forEach(a -> System.out.print(a.toString()+" "));
25. }
26. }
A. Frank 15 John 50
Option A is correct.
TreeSet is a SortSet so we need to pass a Comparator object in the constructor or make the object Person
Comparable. The Set collection doesn't allow duplicate keys and method "compare" is used for object equality, but
we are comparing only age attribute and than Option A is correct and B and C are incorrect. The code has no compile
error and no exception will throw so option D e E are incorrect.
D. An exception is thrown.
E. None of Above.
Option A is correct.
Option A is correct as the code fails to compile, the method "execute" doesn't return a ResultSet but true if the first
object that the query returns is a ResultSet object, to make the code compile the right method is "executeQuery".
Exam objective : Database Applications with JDBC - Submitting queries and get results from the database.
https://www.certification-questions.com/
Q21. Given
A. int n = <Integer>Program.count(a);
B. int n = Program.<Integer>count(a);
C. int n = Program.count<Integer>(a);
D. int n = Program.count(a);
E. All
Option A and C are incorrect syntax for generic method invocation, option B is the correct syntax for generic method
invocation, type inference allows you to invoke a generic method as an ordinary method so option D is correct as
well.
https://www.certification-questions.com/
Q22.Which of the following Statement are true
II. An anonymous class cannot access local variables in its enclosing scope that are not
IV. An anonymous class can have static members provided that they are constant variables.
A. I, II, III.
B. I, II.
D. III, IV.
E. All.
Option E is correct.
https://www.certification-questions.com/
Q23. Given
1. class A {
2. private String s;
3. public A(String s){ this.s = s;};
4. public String toString(){ return s;}
5. }
6. public class Program {
7.
8. public static void main(String[] args) {
9.
10. List<A> list = new ArrayList<A>();
11. list.add(new A("flower"));
12. list.add(new A("cat"));
13. list.add(new A("house"));
14. list.add(new A("dog"));
15. Collections.sort(list);
16. list.forEach(a -> System.out.print(a.toString()+" "));
17. }
18. }
Option D is correct.
Object "list" is a type A ArrayList, the Collections "sort" method has signature as following:
So the code will not compile because class A doesn't implement Comparable interface, and than D
https://www.certification-questions.com/
Q24. Which of those are valid Standard Streams
I. System.in
II. System.error
III. System.out
IV. System.input
V. System.output
A. I.
B. II, IV, V.
C. I, II, III.
D. I, III.
E. Any of above.
Option D is correct.
System.error, System.input and System.output are not valid syntax, so the correct answer is D.
Exam objective : I/O Fundamentals - Read and write data from the console
https://www.certification-questions.com/
Q25. Which is/are true?
II. Programs use byte streams to perform input and output of 8-bit bytes.
III. Character stream I/O automatically translates this internal format to and from the
and OutputStreamWriter.
B. II, III.
D. III, IV.
E. All.
Option E is correct.
Exam objective : I/O Fundamentals - Using streams to read and write files
https://www.certification-questions.com/