Q1. Given: What is the result?
A. Null
B. class java.lang.ArraylndexOutOfBoundsException
C. class java.lang.NullPointerException
D. class java.lang.Exception
E. Compilation fails.
Answer: E
Explanation:
error: incompatible types e = new Exception(); required: RuntimeException found: Exception
Q2. Given:
What is the result?
A. peach orange apple
B. peach orange
C. apple orange
D. The program does not compile.
E. The program generates an exception at runtime.
Answer: D
Explanation:
int compare(T obj1, T obj2) 0 if equal positive if obj1 greater negative if obj2 greater The compiler has a problem with the line: public boolean compare(String s1, String s2) { return s1.length() > s2.length(); error: <anonymous comparetest.CompareTest$1> is not abstract and does not override abstract method compare(String,String) in Comparator
new Comparator<String>() {
Error: compare(String,String) in <anonymous comparetest.CompareTest$1> cannot
implement compare(T,T)
in Comparator
public boolean compare(String s1, String s2) {
return type boolean is not compatible with int
where T is a type-variable:
T extends Object declared in interface Comparator
Q3. Given the code fragment:
Assume the method printNums is passed a valid array containing data. Why is this method not producingoutput on the console?
A. There is a compilation error.
B. There is a runtime exception.
C. The variable number is not initialized.
D. Standard error is mapped to another destination.
Answer: D
Explanation:
The code compiles fine.
The code runs fine.
The errorstream can be redirected.
Note:
System.out.println -> Sends the output to a standard output stream. Generally monitor.
System.err.println -> Sends the output to a standard error stream. Generally monitor. err is
the "standard" erroroutput stream. This stream is already open and ready to accept output
data.
Typically this stream corresponds to display output or another output destination specified
by the hostenvironment or user. By convention, this output stream is used to display error
messages or other informationthat should come to the immediate attention of a user even if the principal output stream, the value of thevariable out, has been redirected to a file or other destination that is typically not continuously monitored.
Reference:java.lang.System
Q4. Given:
StringBuffer b = new StringBuffer("3");
System.out.print(5+4+b+2+1);
What is the result?
A. 54321
B. 9321
C. 5433
D. 933
E. Output is Similar to: 9java.lang.StringBuffer@100490121.
F. Compilation fails.
Answer: F
Explanation:
The code will not compile.
The print function cannot handle the mixture of integers and strings.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -Erroneous tree type
Q5. Given: What is the result?
A. false false
B. true false
C. true true
D. Compilation fails
E. An exception is thrown at runtime
Answer: A
Explanation:
(this == obj) is the object implementation of equals() and therefore FALSE, if the reference
points to variousobjectsand then the super.equals() is invoked, the object method equals()
what still result in FALSEbetter override of equals() is to compare the attributes like:
public boolean equals (Object obj) {
if (obj != null){
Product p = (Product)obj;
return this.id == p.id;
}
return false;
}
Q6. Given the code fragment: What is the result, if the file myfile.txt does not exist?
A. A runtime exception is thrown at line 4
B. A runtime exception is thrown at line 7
C. Creates a new file and prints no output
D. Compilation fails
Answer: D
Explanation:
!! Compilation fails if FileNotFoundException is tried to catch (Line 12)
(The exception FileNotFoundException is already caught by the alternative IOException)
if this is removed will be thrown a FileNotFoundException at line 4.
Q7. Given the code fragment:
Assume that the SQL queries return records. What is the result of compiling and executing this code fragment?
A. The program prints employee IDs
B. The program prints customer IDs
C. The program prints Error
D. Compilation fails on line ***
Answer: C
Explanation:
!!! The given Code prints Error -- the second query clears the ResultSet !? ErrorMessage: Operation notallowed after ResultSet closed
It would print A, if second Query i set to rs = stmt.executeQuery("SELECT ID FROM Customer"); // Line *** It would print B, if Line *** is missing. // The program compiles and runs fine. Both executeQuery statements will run. The first executeQuery statement (ResultSet rs = stmt.executeQuery(query);) will set the rs Resultset. It will be used in the while loop. EmployIDswill be printed. Note: Executes the given SQL statement, which returns a single ResultSet object. Parameters:sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement Returns:a ResultSet object that contains the data produced by the given query; never null
Q8. What will the following class print when run?
A. javajava
B. lavajava
C. javajavac
D. lavajavac
E. None of these.
Answer: C
Q9. Given this error message when running your application:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name
MessageBundle, locale
And given that the MessageBundle.properties file has been created, exists on your disk, and is properlyformatted.
What is the cause of the error message?
A. The file is not in the environment path.
B. The file is not in the classpath.
C. The file is not in the javapath.
D. You cannot use a file to store a ResourceBundle.
Answer: D
Explanation:
ResourceBundle.getBundle is using a resource name; it isn't called ResourceBundle for
nothing.
You can create a custom ClassLoader and use that to load the data.
Q10. Given:
Which two classes correctly override the getDepth method?
A. public class deep extends Deeper {
protected Integer getDepth(){
return 5;
}}
B. public class deep extends Deeper {
public Double getDepth() {
return 5d;
}}
C. public class deep extends Deeper {
public String getDepth () {
}}
D. public class deep extends Deeper {
public Long getDepth (int d) {
return 5L;
}}
E. public class deep extends Deeper {
public Short getDepth () {
return 5;
}}
Answer: A,E
Explanation:
Note: The abstract class Number is the superclass of classes Byte, Double, Float, Integer, Long, and Short.
Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.
When class C extends B, we say that C is a "subclass" of B, and B is the "superclass" of C. This is called inheritence, because C inherited from B.