Q1. Given the code fragment:
What is the result?
A. 1:2:3:4:5:
B. 1:2:3:
C. Compilation fails.
D. An ArrayoutofBoundsException is thrown at runtime.
Answer: A
Q2. Given:
What is the result?
A. Good Day! Good Luck!
B. Good Day! Good Day!
C. Good Luck! Good Day!
D. Good Luck! Good Luck!
E. Compilation fails
Answer: E
Q3. Which of the following can fill in the blank in this code to make it compile? (Select 2 options.)
A. On line 1, fill in throws
B. On line 1, fill in throws new
C. On line 2, fill in throw new
D. On line 2, fill in throws
E. On line 2, fill in throws new
Answer: A,C
Explanation:
Option A and C are the correct answer.
In a method declaration, the keyword throws is used. So here at line 1 we have to use
option A.
To actually throw an exception, the keyword throw is used and a new exception is created,
so at line 2 we have to use throw and new keywords, which is option C. Finally it will look
like;
public void method() throws Exception {
throw new Exception0;
}
REFERENCE : httpsy/docs.oracle.com/javase/tutorial/essential/io/fileOps.html#exception
The correct answer is: On line 1, fill in throws. On line 2, fill in throw new
Q4. Given:
Class A { }
Class B { }
Interface X { }
Interface Y { }
Which two definitions of class C are valid?
A. Class C extends A implements X { }
B. Class C implements Y extends B { }
C. Class C extends A, B { }
D. Class C implements X, Y extends B { }
E. Class C extends B implements X, Y { }
Answer: A,E
Explanation: extends is for extending a class.
implements is for implementing an interface. Java allows for a class to implement many interfaces.
Q5. Which two statements correctly describe checked exception?
A. These are exceptional conditions that a well-written application should anticipate and recover from.
B. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
C. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
D. Every class that is a subclass of RuntimeException and Error is categorized as checked exception.
E. Every class that is a subclass of Exception, excluding RuntimeException and its subclasses, is categorized as checked exception.
Answer: B,D
Explanation: Checked exceptions:
* (B) represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
* are subclasses of Exception It's somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).
* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)
Reference: Checked versus unchecked exceptions
Q6. Given:
public class Test {
public static void main(String[] args) {
try {
String[] arr =new String[4];
arr[1] = "Unix";
arr[2] = "Linux";
arr[3] = "Solarios";
for (String var : arr) {
System.out.print(var + " ");
}
} catch(Exception e) {
System.out.print (e.getClass());
}
}
}
What is the result?
A. Unix Linux Solaris
B. Null Unix Linux Solaris
C. Class java.lang.Exception
D. Class java.lang.NullPointerException
Answer: B
Explanation: null Unix Linux Solarios
The first element, arr[0], has not been defined.
Q7. Given:
What is the result?
A. A B C D
B. A C D
C. A B C
D. A B D
E. A B D C
Answer: C
Q8. Given the code fragment:
int b = 3;
if ( !(b > 3)) {
System.out.println("square ");
}{
System.out.println("circle ");
}
System.out.println("...");
What is the result?
A. square...
B. circle...
C. squarecircle...
D. Compilation fails.
Answer: C
Q9. Given:
And given the code fragment:
What is the result?
A. 4W 100 Auto 4W 150 Manual
B. Null 0 Auto 4W 150 Manual
C. Compilation fails only at line n1
D. Compilation fails only at line n2
E. Compilation fails at both line n1 and line n2
Answer: E
Explanation:
On line n1 implicit call to parameterized constructor is missing and n2 this() must be the first line.
Q10. Consider
Integer number = Integer.valueOff 808.1");
Which is true about the above statement?
A. The value of the variable number will be 808.1
B. The value of the variable number will be 808
C. The value of the variable number will be 0.
D. A NumberFormatException will be throw.
E. It will not compile.
Answer: D
Explanation:
The Integer class value of 0 returns an Integer from given string. But we need to pass string which has correct format for integer otherwise it will throw a NumberFormatException. In this case we have passed string which is not an integer value (since what we passed is fractional number), so option D is correct.
Q11. Given:
What is the output?
A. 2015-03-27
B. 2015-04-27
C. 2015-02-27 D. Compilation fails due to error at line 6.
E. Compilation fails due to error at line 8.
Answer: A
Explanation:
To create we have used following method with LocalDate class;
public static LocalDate of(intyear, int month, int dayOfMonth)
Here we need to remember that month is not zero based so if you pass 1 for month, then
month will be January.
Then we have used period object of 1 day and add to date object which makes current date
to next day, so final output is 2015-03-27. Hence option A is correct.
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html
Q12. Which of the following data types will allow the following code snippet to compile?
A. long
B. double
C. int
D. float
E. byte
Answer: B,D
Explanation:
Option B and D are the correct answer.
Since the variables I and j are floats, resultant will be float type too. So we have to use float
or primitive type which can hold float, such a primitive type is double, it has wider range
and also can hold floating point numbers, hence we can use double or float for the blank.
As explained above options B and D are correct.
long and int can't be used with floating point numbers so option A is incorrect.
Option E is incorrect as it have smaller range and also can't be used with floating point
numbers.
hnpsy/docs.oracle.com/javase/tutorial/java/javaOO/variables.html
Q13. Given the following class declarations:
public abstract class Animal
public interface Hunter
public class Cat extends Animal implements Hunter
public class Tiger extends Cat
Which answer fails to compile?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: E
Explanation: Look at the right side of the declaration ArrayLIst() rather than ArrayList
Q14. Given the code fragment:
What is the result?
A. Reading Card Checking Card
B. Compilation fails only at line n1.
C. Compilation fails only at line n2.
D. Compilation fails only at line n3.
E. Compilation fails at both line n2 and line n3.
Answer: D
Q15. Given:
A. a, e
i, o
B. a, e
o, o
C. e, e
I, o
D. e, e
o, o
Answer: B