1.
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
Correct Answer
C. Prints: 0,1,1,0
Explanation
The first try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented. The throws clause of m1 declares a ColorException, so the body may throw a ColorException or any subclass of ColorException. The second try block also contains two statements. The first invokes method m2, and the subsequent statement contains a post increment expression with the variable, d, as the operand. Method m2 does not throw an exception, so d is incremented, and the try block completes normally. Although the throws clause of m2 declares a WhiteException, there is no requirement to throw any exception.
2.
interface A {
void m1(); // 1
public void m2(); // 2
protected void m3(); // 3
private void m4(); // 4
}
Compile-time errors are generated at which lines?
Correct Answer(s)
C. 3
D. 4
Explanation
Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.
3.
Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing interface?
Correct Answer(s)
A. Abstract
F. Public
Explanation
All interfaces are implicitly abstract. The explicit application of the abstract modifier to an interface declaration is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public. Use of the access modifiers, private or protected, is contradictory and results in a compile-time error. In contrast, the modifiers, private and protected, are applicable to a member type declaration appearing within the body of a directly enclosing class. The modifier, final, is never applicable to an interface. The keyword, implements, is not a modifier.
4.
interface A {
int a = 1; // 1
public int b = 2; // 2
public static int c = 3; // 3
public static final int d = 4; // 4
}
Which field declaration results in a compile-time error?
Correct Answer
F. None of the above
Explanation
All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.
5.
Which of the following are not methods of the java.lang.String class?
Correct Answer(s)
A. Append
C. Delete
D. Insert
Explanation
The StringBuffer class has methods named append, delete and insert, but the String class does not. A typical trick question will attempt to invoke StringBuffer methods on a String instance
6.
class MWC106 {
static void m1(String s) {
s = s.trim(); s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
Correct Answer
A. Prints: A B C
Explanation
The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged.
7.
What is the result of compiling and running this program?
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}
Correct Answer
A. Prints "Mammal eats food"
Explanation
The method that will be called is the one
from class Mammal. The reasons are quite obvious.
8.
class MWC117 {
public static void main (String[] args) {
System.out.print(String.valueOf(1) + String.valueOf(2));
String s1 = "S1";
String s2 = s1.toString();
System.out.print("," + (s1==s2));
}}
What is the result of attempting to compile and run the program?
Correct Answer
D. Prints: 12,true
Explanation
The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String.
9.
class MWC200 {
public static void main (String[] args) {
String s1 = "ABC";
StringBuffer s2 = new StringBuffer(s1);
System.out.print(s2.equals(s1) + "," + s1.equals(s2));
}}
What is the result of attempting to compile and run the program?
Correct Answer
A. Prints: false,false
Explanation
StringBuffer.equals does not override the Object.equals method; so StringBuffer.equals just compares reference values. Since the reference variables s1 and s2 refer to different objects, the equals method of the StringBuffer instance s2 returns the value false. The String.equals method does override the Object.equals, method. The String.equals method returns false anytime the argument is not an instance of type String. The method invocation expression s1.equals(s2) produces the value false, because the argument is an instance of type StringBuffer
10.
import java.util.*;
class GFC112 {
public static void main (String[] args) {
Object m = new LinkedHashSet();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Set)+",");
System.out.print(m instanceof List);
}}
What is the result of attempting to compile and run the program?
Correct Answer
D. Prints: true,true,false
Explanation
LinkedHashSet does not implement the List interface. LinkedHashSet extends HashSet and implements Collection, Set, Cloneable and Serializable