Q1. Given the code fragment:
What is the result?
A. 2 4
B. 0 2 4 6
C. 0 2 4
D. Compilation fails
Answer: C
Q2. Given:
public class Test {
public static void main(String[] args) {
int arr[] = new int[4];
arr[0] = 1;
arr[1] = 2;
arr[2] = 4;
arr[3] = 5;
int sum = 0;
try {
for (int pos = 0; pos <= 4; pos++) {
sum = sum + arr[pos];
}
} catch (Exception e) {
System.out.println("Invalid index");
}
System.out.println(sum);
}
}
What is the result?
A. 12
B. Invalid Index
C. Invalid Index
D. Compilation fails
Answer: B
Explanation: The loop ( for (int pos = 0; pos <= 4; pos++) { ), it should be pos <= 3, causes an exception, which is caught. Then the correct sum is printed.
Q3. Which three are advantages of the Java exception mechanism?
A. Improves the program structure because the error handling code is separated from the normal program function
B. Provides a set of standard exceptions that covers all the possible errors
C. Improves the program structure because the programmer can choose where to handle exceptions
D. Improves the program structure because exceptions must be handled in the method in which they occurred
E. Allows the creation of new exceptions that are tailored to the particular program being created
Answer: A,C,E
Q4. Given:
What is the result?
A. The sum is 2
B. The sum is 14
C. The sum is 15
D. The loop executes infinite times
E. Compilation fails
Answer: E
Q5. Given:
What is the result?
A. 6 7 8
B. 7 8 9
C. 0 1 2
D. 6 8 10
E. Compilation fails
Answer: A
Q6. 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
Q7. 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
Q8. Given:
public class App { // Insert code here System.out.print("Welcome to the world of Java"); } }
Which two code fragments, when inserted independently at line // Insert code here, enable the program to execute and print the welcome message on the screen?
A. static public void main (String [] args) {
B. static void main (String [] args) {
C. public static void Main (String [] args) {
D. public static void main (String [] args) {
E. public void main (String [] args) {
Answer: A,D
Explanation:
Incorrect:
Not B: No main class found.
Not C: Main method not found
not E: Main method is not static.
Q9. Give:
What is the result?
A. 1525
B. 13
C. Compilation fails
D. An exception is thrown at runtime
E. The program fails to execute due to runtime error
Answer: D
Q10. Given the code fragment:
System.out.println(2 + 4 * 9 - 3); //Line 21
System.out.println((2 + 4) * 9 - 3); // Line 22
System.out.println(2 + (4 * 9) - 3); // Line 23
System.out.println(2 + 4 * (9 - 3)); // Line 24
System.out.println((2 + 4 * 9) - 3); // Line 25
Which line of codes prints the highest number?
A. Line 21
B. Line 22
C. Line 23
D. Line 24
E. Line 25
Answer: B
Explanation: The following is printed: 35 51
35
26
35
Q11. Given:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q12. Given the code fragment
int var1 = -5;
int var2 = var1--;
int var3 = 0;
if (var2 < 0) {
var3 = var2++;
} else {
var3 = --var2;
}
System.out.println(var3);
What is the result?
A. – 6
B. – 4
C. – 5
D. 5
E. 4
F. Compilation fails
Answer: C
Q13. Given the code fragment:
What is the result?
A. Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
B. Execution terminates In the second catch statement, and caught an Exception is printed to the console.
C. A runtime error is thrown in the thread "main".
D. Execution completes normally, and Ready to us. is printed to the console.
E. The code fails to compile because a throws keyword is required.
Answer: C
Q14. Given the code fragment:
What is the result?
A. 100
B. 101
C. 102
D. 103
E. Compilation fails
Answer: E
Q15. Given the fragments:
Which line causes a compilation error?
A. Line n1
B. Line n2
C. Line n3
D. Line n4
Answer: A