Q1. Given:
What is the output?
A. 1Z0
B. 1Z0-808
C. An exception will be thrown.
D. Compilation fails due to error at line 3.
E. Compilation tails due to error at line 4.
Answer: E
Explanation:
Option E is the correct answer. Code fails to compile because there is no method called concert in StringBuilder class. The concert method is in String class. Hence option E is correct Here we should have used append method of StringBuilder class, in that case option B would be correct. https://docs.oracle.com/javase/tutorial/java/data/buffers.html
Q2. Given:
public class Natural {
private int i;
void disp() {
while (i <= 5) {
for (int i=1; i <=5;) {
System.out.print(i + " ");
i++;
}
i++;
}
}
public static void main(String[] args) {
new Natural().disp();
}
}
What is the result?
A. Prints 1 2 3 4 5 once
B. Prints 1 3 5 once
C. Prints 1 2 3 4 5 five times
D. Prints 1 2 3 4 5 six times
E. Compilation fails
Answer: D
Explanation: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Q3. Given:
Which inserted at line 11, will provide the following output?
[21, 15, 11]
A. list.removelf(e > e%2 != 0);
B. list.removelf(e -> e%2 != 0);
C. Ust.removelf(e -> e%2 = 0);
D. list.remove(e -> e%2 = 0);
E. None of the above.
Answer: C
Explanation:
In output we can see that only odd numbers present, so we need to remove only even numbers to get expected output. From Java SE 8, there is new method call removelf which takes predicate object and remove elements which satisfies predicate condition. Predicate has functional method call take object and check if the given condition met or not, if met it returns true, otherwise false. Option C we have passed correct lambda expression to check whether the number is odd or even that matches to the functional method of predicate interface. Option A is incorrect as it is invalid lambda expression. Option B is incorrect as it removes all odd numbers. Option D is incorrect as there is no remove method that takes predicate as argument. https://docs.oracle.eom/javase/8/docs/api/java/util/ArrayList.html
Q4. Given the code fragment:
Which option represents the state of the num array after successful completion of the outer loop?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q5. Which statement will empty the contents of a StringBuilder variable named sb?
A. sb.deleteAll();
B. sb.delete(0, sb.size());
C. sb.delete(0, sb.length());
D. sb.removeAll();
Answer: C
Q6. Given the code fragment:
What is the result?
A. Element 0 Element 1
B. Null element 0 Null element 1
C. Null Null
D. A NullPointerException is thrown at runtime.
Answer: D
Q7. Which two items can legally be contained within a java class declaration?
A. An import statement
B. A field declaration
C. A package declaration
D. A method declaration
Answer: B,D
Reference:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Q8. Given:
What is the result?
A. 200.0 : 100.0
B. 400.0 : 200.0
C. 400.0 : 100.0
D. Compilation fails.
Answer: C
Q9. Given the code fragment:
Which modification enables the code to print 54321?
A. Replace line 6 with System, out. print (--x) ;
B. At line 7, insert x --;
C. Replace line 6 with --x; and, at line 7, insert system, out. print (x);
D. Replace line 12 With return (x > 0) ? false: true;
Answer: B
Q10. Given the code from the Greeting.Java file:
Which set of commands prints Hello Duke in the console?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: C