Q1. 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
Q2. Which of the following can fill in the blank in this code to make it compile?
A. abstract
B. final
C. private
D. default
E. int
Answer: C
Explanation:
From Java SE 8, we can use static and/or default methods in interfaces, but they should be non abstract methods. SO in this case using default in blank is completely legal. Hence option C is correct. Option A is incorrect as given method is not abstract, so can't use abstract there. Options B and E are incorrect as we can't have non abstract method interface if they are not default or static. httpsy/docs.oracle.com/javase/tutorial/iava/landl/defaultmethods.html
Q3. Given:
public class TestLoop {
public static void main(String[] args) {
int array[] = {0, 1, 2, 3, 4};
int key = 3;
for (int pos = 0; pos < array.length; ++pos) {
if (array[pos] == key) {
break;
}
}
System.out.print("Found " + key + "at " + pos);
}
}
What is the result?
A. Found 3 at 2
B. Found 3 at 3
C. Compilation fails
D. An exception is thrown at runtime
Answer: C
Explanation: The following line does not compile: System.out.print("Found " + key + "at " + pos);
The variable pos is undefined at this line, as its scope is only valid in the for loop. Any variables created inside of a loop are LOCAL TO THE LOOP.
Q4. Given:
Which code fragment, when inserted at line 7, enables the code print true?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q5. Given:
What is the result?
A. 400 200
B. 200 200
C. 400 400
D. Compilation fails.
Answer: A
Q6. 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
Q7. Given:
class MarksOutOfBoundsException extends IndexOutOfBoundsException { }
public class GradingProcess {
void verify(int marks) throws IndexOutOfBoundsException {
if (marks > 100) {
throw new MarksOutOfBoundsException();
}
if (marks > 50) {
System.out.print("Pass");
} else {
System.out.print("Fail");
}
}
public static void main(String[] args) {
int marks = Integer.parseInt(args[2]);
try {
new GradingProcess().verify(marks));
} catch(Exception e) {
System.out.print(e.getClass()); } } }
And the command line invocation:
Java grading process 89 50 104
What is the result?
A. Pass
B. Fail
C. Class MarketOutOfBoundsException
D. Class IndexOutOfBoundsException
E. Class Exception
Answer: C
Explanation: The value 104 will cause a MarketOutOfBoundsException
Q8. Given:
public class MyClass {
public static void main(String[] args) {
while (int ii = 0; ii < 2) {
ii++;
System.out.println("ii = " + ii);
}
}
}
What is the result?
A. ii = 1 ii = 2
B. Compilation fails
C. The program prints nothing
D. The program goes into an infinite loop with no output
E. The program goes to an infinite loop outputting: ii = 1 ii = 1
Answer: B
Explanation: The while statement is incorrect. It has the syntax of a for statement.
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
while (expression) { statement(s) } The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Reference: The while and do-while Statements
Q9. 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
Q10. Given the code fragment:
public class Test {
public static void main(String[] args) {
boolean isChecked = false;
int arry[] = {1,3,5,7,8,9};
int index = arry.length;
while ( <code1> ) {
if (arry[index-1] % 2 ==0) {
isChecked = true;
}
<code2>
}
System.out.print(arry(index]+", "+isChecked));
}
}
Which set of changes enable the code to print 1, true?
A. Replacing <code1> with index > 0 and replacing <code2> with index--;
B. Replacing <code1> with index > 0 and replacing <code2> with --index;
C. Replacing <code1> with index > 5 and replacing <code2> with --index ;
D. Replacing <code1> with index and replacing <code2> with --index ;
Answer: A
Explanation:
Note: Code in B (code2 is --index;). also works fine.