Professional Documents
Culture Documents
Question 1
public class Basics {} // 1
public class Basics2 {} // 2
public class Basics3 {} // 3
public class Basics4 {} // 4
Suppose these are top-level class declarations and not nested class declarations; and
suppose that all of the declarations are contained in one file named Basics.java. A
compile-time error is not generated at which line?
a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 2
Which of these words belongs to the set of Java keywords?
a. qualified
b. record
c. repeat
d. restricted
e. label
f. to
g. type
h. until
i. value
j. virtual
k. xor
l. None of the above
Question 3
class MCZ30 {
public static void main (String[] args) {
char a = '\t'; // 1
char b = '\\'; // 2
char c = '\?'; // 3
char d = '\"'; // 4
char e = '\''; // 5
}}
Page: 1
Dan Chisholm Exam 7 Questions
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 4
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));
}}
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
Question 5
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")));
Page: 2
Dan Chisholm Exam 7 Questions
}}
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
Question 6
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));
}}
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
Question 7
class D {
public static void main (String[] args) {
Boolean b1 = new Boolean("trUE"); // 1
Boolean b2 = new Boolean("What's This?"); // 2
Page: 3
Dan Chisholm Exam 7 Questions
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
Question 8
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
}}
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. Compile-time error at 6
g. Run-time error at 1
h. Run-time error at 2
i. Run-time error at 3
j. Run-time error at 4
k. Run-time error at 5
l. Run-time error at 6
m. None of the above
Page: 4
Dan Chisholm Exam 7 Questions
Question 9
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));
}}
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
Question 10
Which implementation of the List interface provides for the fastest insertion of a new
element into the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above
Question 11
import java.util.*;
class E {
public static void main (String args[]) {
AbstractSet a1 = new TreeSet();
System.out.print((a1 instanceof Set)+",");
System.out.print(a1 instanceof SortedSet);
}}
Page: 5
Dan Chisholm Exam 7 Questions
a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. None of the above
Question 12
class A {
private int[] val;
private int hash;
public int hashCode() {
int h = hash;
if (h == 0) {
int len = val.length;
for (int i = 0; i < len; i++) {
h = 31*h + val[i];
}
hash = h;
}
return h;
}
// The equals method has been omitted for clarity.
A (int[] val) {this.val = (int[])val.clone();}
public static void main (String[] args) {
A a = new A(new int[]{1,2,3});
System.out.print(a.hashCode());
}}
a. Prints: 1026
b. Prints: 1091
c. Prints: 31806
d. Compile-time error
e. Run-time error
f. None of the above
Question 13
class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {} // 3
a. Compile-time error at 1.
b. Compile-time error at 2.
Page: 6
Dan Chisholm Exam 7 Questions
c. Compile-time error at 3.
d. None of the above
Question 14
class JSC101 {
void m1() {
public int a; // 1
protected int b; // 2
private int c; // 3
static int d; // 4
transient int e; // 5
volatile int f; // 6
final int g = 1; // 7
}}
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
Question 15
Which of the following statements is not true?
Question 16
class JSC205 {
static int m1(int i) {return i;} // 1
static void m2(int i) {return i;} // 2
static int m3(int i) {return;} // 3
public static void main(String[] args) {
System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}
Page: 7
Dan Chisholm Exam 7 Questions
a. Prints: 123
b. Prints: 6
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
Question 17
Which of the following modifiers can be applied to a local class?
a. public
b. protected
c. private
d. abstract
e. static
f. final
Question 18
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
Page: 8
Dan Chisholm Exam 7 Questions
a. Prints: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compile-time error
g. Run-time error
h. None of the above
Question 19
Which of the following is a modifier that can be applied to an interface that is a member
of a directly enclosing class or interface?
a. static
b. synchronized
c. transient
d. volatile
e. implements
f. None of the above.
Question 20
class JJF5 {
public static void main(String args[]) {
System.out.print(Integer.toHexString(Integer.MIN_VALUE)+",");
System.out.print(Integer.toHexString(Integer.MAX_VALUE));
}}
a. Prints: 0000,ffff
b. Prints: 00000000,ffffffff
c. Prints: 7fff,8000
d. Prints: 8000,7fff
e. Prints: 7fffffff,80000000
f. Prints: 80000000,7fffffff
g. Compile-time error
h. Run-time error
i. None of the above
Question 21
Page: 9
Dan Chisholm Exam 7 Questions
class MWC105 {
static boolean b1;
public static void main(String[] args) {
boolean[] array = new boolean[1];
boolean b2;
System.out.print(b1+",");
System.out.print(array[0]+",");
System.out.print(b2);
}}
a. Prints: true,true,true
b. Prints: false,false,false
c. Prints: null,null,null
d. Prints: false,true,false
e. Compile-time error
f. Run-time error
g. None of the above
Question 22
class MWC205 {
public static void main(String[] args) {
int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i].length+",");
}}}
a. Prints: 2,3,4,0,
b. Prints: 1,2,5,0,
c. Compile-time error
d. Run-time error
e. None of the above
Question 23
abstract class A {
private int x = 4;
private int y = 2;
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
public abstract int math();
}
Page: 10
Dan Chisholm Exam 7 Questions
class B {
static A a1 = new A(2,1) {
public A(int i1, int i2) {x(i1);y(i2);};
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.math());
}}
a. Prints: 8
b. Prints: 3122
c. Compile-time error
d. Run-time error
e. None of the above
Question 24
class JMM104 {
public static void main (String args[]) {
char c = 'b';
switch(c) {
case 'a': System.out.print("1");
case 'b': System.out.print("2");
case 'c': System.out.print("3");
default: System.out.print("4");
}}}
a. Prints: 3
b. Prints: 34
c. Prints: 234
d. Prints: 1234
e. Run-time error
f. Compile-time error
g. None of the above
Question 25
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
Page: 11
Dan Chisholm Exam 7 Questions
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Question 26
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}
Which object is not eligible for garbage collection after method m1 returns?
a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above
Question 27
class GFM15 {
static int a; static float b; static double c;
static boolean d; static String s;
public static void main(String[] args) {
Page: 12
Dan Chisholm Exam 7 Questions
System.out.println(a+","+b+","+c+","+d+","+s);
}}
a. Prints: 0,0,0,false,null
b. Prints: 0,0,0,false,
c. Prints: 0,0.0,0.0,false,null
d. Prints: 0,0.0,0.0,false,
e. Prints: 0,0.0,0.0,true,null
f. Prints: 0,0.0,0.0,true,
g. Prints: 0,0,0,true,null
h. Prints: 0,0,0,true,
i. Compile-time error
j. Run-time error
k. None of the above
Question 28
class EBH007{
public static void main (String[] s) {
byte b = 5;
System.out.println(b<<33);
}}
a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Run-time error
g. Compile-time error
h. None of the above
Question 29
class Sienna {
static double a;
static float b;
static int c;
static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
Page: 13
Dan Chisholm Exam 7 Questions
}}
a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above
Question 30
class EBH105 {
static int m(int i) {
System.out.print(i + ",");
return 0;
}
public static void main (String[] args) {
int i = 0;
i = i++ + m(i);
System.out.print(i);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 0,0
b. Prints: 1,0
c. Prints: 0,1
d. Prints: 1,1
e. Run-time error
f. Compile-time error
g. None of the above
Page: 14