The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.
Which of the instance creation expressions produce a run-time error?
| a. | new Float('A') |
| b. | new Float("A") |
| c. | new Float(1L) |
| d. | new Float("1L") |
| e. | new Float(0x10) |
| f. | new Float("0x10") |
| g. | new Float("010") |
class C {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
Which of the following class instance creation expressions would generate a run-time error?
| a. | new Short("1") |
| b. | new Short("-1") |
| c. | new Short("+1") |
| d. | new Short("1.0") |
| e. | new Short("0x1") |
| f. | new Short("011") |
class E {
public static void main (String[] args) {
Byte b1 = new Byte("1"), b2 = new Byte("1");
System.out.print((b1==b2)+","+(b1.equals(b2)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class B {
public static void main (String args[]) {
Integer a = new Integer(256);
byte b = a.byteValue();
short c = a.shortValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+","+c+","+ (e+f+g+h == 4 * 256));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,false |
| b. | Prints: 0,0,true |
| c. | Prints: 0,-1,false |
| d. | Prints: 0,-1,true |
| e. | Prints: -1,0,false |
| f. | Prints: -1,0,true |
| g. | Prints: -1,-1,false |
| h. | Prints: -1,-1,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.MAX_VALUE;
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class F {
static String m(long i) {return "long";}
static String m(Long i) {return "Long";}
static String m(double i) {return "double";}
static String m(Double i) {return "Double";}
static String m(String i) {return "String";}
public static void main (String[] args) {
System.out.print(m(Long.parseLong("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: long |
| b. | Prints: Long |
| c. | Prints: double |
| d. | Prints: Double |
| e. | Prints: String |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class E {
public static void main (String args[]) {
String s1 = Float.toString(1.0); // 1
String s2 = Float.toString(1.0f); // 2
String s3 = Float.toString(0xf); // 3
String s4 = Float.toString(010); // 4
String s5 = Float.toString('A'); // 5
}}
What is the result of attempting to compile and run the program?
| a. | Compile-time error at 1 |
| b. | Compile-time error at 2 |
| c. | Compile-time error at 3 |
| d. | Compile-time error at 4 |
| e. | Compile-time error at 5 |
| f. | Run-time error at 1 |
| g. | Run-time error at 2 |
| h. | Run-time error at 3 |
| i. | Run-time error at 4 |
| j. | Run-time error at 5 |
| k. | None of the above |
class D {
public static void main (String[] args) {
Boolean b1 = new Boolean("trUE"); // 1
Boolean b2 = new Boolean("What's This?"); // 2
Boolean b3 = new Boolean(null); // 3
System.out.print(b1 + "," + b2 + "," + b3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class F {
public static void main (String[] args) {
Short s1 = new Short("1"), s2 = new Short("1");
int a1 = s1.hashCode(), b1 = s2.hashCode();
System.out.print((s1==s2)+","+(s1.equals(s2))+","+(a1==b1));
}}
What is the result of attempting to compile and run the program?
| a. | false,false,false |
| b. | false,false,true |
| c. | false,true,false |
| d. | false,true,true |
| e. | true,false,false |
| f. | true,false,true |
| g. | true,true,false |
| h. | true,true,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class E {
public static void main (String[] args) {
Byte b1 = new Byte("1"), b2 = new Byte("1");
int a = b1.hashCode(), b = b2.hashCode();
System.out.print((b1.equals(b2))+","+(a==b));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class E {
static String m(int i) {return "int primitive";}
static String m(Integer i) {return "Integer instance";}
static String m(double i) {return "double primitive";}
static String m(Double i) {return "Double instance";}
static String m(String i) {return "String instance";}
public static void main (String[] args) {
System.out.print(m(Integer.parseInt("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: int primitive |
| b. | Prints: double primitive |
| c. | Prints: Double instance |
| d. | Prints: String instance |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Which of the following methods are static members of the java.lang.Double class?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
class F {
static String m(long i) {return "long";}
static String m(Long i) {return "Long";}
static String m(double i) {return "double";}
static String m(Double i) {return "Double";}
static String m(String i) {return "String";}
public static void main (String[] args) {
System.out.print(m(Long.parseLong("1L")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: long |
| b. | Prints: Long |
| c. | Prints: double |
| d. | Prints: Double |
| e. | Prints: String |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class E {
static String m(float f) {return "f";}
static String m(Float f) {return "F";}
public static void main (String[] args) {
System.out.print(m(Float.parseFloat("1")));
System.out.print(m(Float.floatValue()));
System.out.print(m(Float.valueOf("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: fff |
| b. | Prints: ffF |
| c. | Prints: fFf |
| d. | Prints: fFF |
| e. | Prints: Fff |
| f. | Prints: FfF |
| g. | Prints: FFf |
| h. | Prints: FFF |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class D {
public static void main (String[] args) {
Boolean b1 = Boolean.valueOf("trUE"); // 1
Boolean b2 = Boolean.valueOf("Even more true"); // 2
Boolean b3 = Boolean.valueOf(null); // 3
System.out.print((b1==b2) + ",");
System.out.print((b2==b3) + ",");
System.out.println(b3==b1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class E {
static String m(short s) {return "p";}
static String m(Short s) {return "S";}
public static void main (String[] args) {
Short s1 = new Short("1");
System.out.print(m(s1.shortValue())+",");
System.out.print(m(Short.parseShort("1"))+",");
System.out.print(m(Short.valueOf("1")));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: p,p,p |
| b. | Prints: p,p,S |
| c. | Prints: p,S,p |
| d. | Prints: p,S,S |
| e. | Prints: S,p,p |
| f. | Prints: S,p,S |
| g. | Prints: S,S,p |
| h. | Prints: S,S,S |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class F {
public static void main (String[] args) {
System.out.print(Byte.MIN_VALUE+","+Byte.MAX_VALUE);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: -128,127 |
| b. | Prints: -127,128 |
| c. | Prints: 0,255 |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |