Professional Documents
Culture Documents
Exam 6 Questions
Exam 6 Questions
Question 1
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. Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
Question 2
class MCZ29 {
public static void main (String[] args) {
char a = '\a'; // 1
char b = '\b'; // 2
char c = '\f'; // 3
char d = '\n'; // 4
char e = '\r'; // 5
}}
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 3
class JSC204 {
static int m1(short s) {return s;} // 1
static int m2(float f) {return f;} // 2
public static void main(String[] args) {
short s = 3; float f = 5.0f;
System.out.print(""+m1(s)+m2(f));
}}
a. Prints: 35.0
b. Prints: 8.0
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Run-time error
f. None of the above
Page: 1
Dan Chisholm Exam 6 Questions
Question 4
a. abstract
b. final
c. public
d. protected
e. private
f. static
g. synchronized
h. transient
Question 5
a. Prints: 0,0,1,0,0,1
b. Prints: 0,1,0,0,0,0
c. Prints: 0,1,1,0,0,1
d. Prints: 0,1,0,0,0,1
e. Prints: 1,1,1,0,0,1
f. Compile-time error
g. Run-time error
h. None of the above
Question 6
Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing class?
a. abstract
b. extends
Page: 2
Dan Chisholm Exam 6 Questions
c. final
d. private
e. protected
f. public
Question 7
class JJF4 {
public static void main(String args[]) {
System.out.print(Long.toHexString(Byte.MAX_VALUE)+",");
System.out.print(Long.toHexString(Character.MAX_VALUE)+",");
System.out.print(Long.toHexString(Short.MAX_VALUE));
}}
a. Prints: f,ff,7f
b. Prints: f,ff,ff
c. Prints: 7f,ffff,7fff
d. Prints: ff,ffff,ffff
e. Prints: 7fff,ffffff,7fffff
f. Prints: ffff,ffffff,ffffff
g. Compile-time error
h. Run-time error
i. None of the above
Question 8
class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}
a. Prints: 1,3
b. Prints: 2,3
c. Prints: 1,4
d. Prints: 2,4
e. Run-time error
f. Compile-time error
g. None of the above
Question 9
A class can not be called "tightly encapsulated" unless which of the following is true?
Page: 3
Dan Chisholm Exam 6 Questions
e. Accessor methods are used to prevent fields from being set with invalid data.
f. None of the above
Question 10
Which of the following methods names the InterruptedException in its throws clause?
a. join
b. notify
c. notifyAll
d. resume
e. run
f. sleep
g. start
h. stop
i. suspend
j. yield
k. wait
Question 11
class D {
public static void main(String[] args) {
double d1 = Math.ceil(0.5);
double d2 = Math.ceil(1.5);
System.out.print(d1 + "," + d2);
}}
a. Prints: 0.0,1.0
b. Prints: 0.0,2.0
c. Prints: 1.0,1.0
d. Prints: 1.0,2.0
e. Compile-time error
f. Run-time error
g. None of the above
Question 12
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)));
}}
a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Compile-time error
f. Run-time error
Page: 4
Dan Chisholm Exam 6 Questions
Question 13
class JMM103 {
public static void main(String args[]) {
byte b = -1;
switch(b) {
case 0: System.out.print("zero "); break;
case 100: System.out.print("100 "); break;
case 1000: System.out.print("1000 "); break;
default: System.out.print("Default ");
}}}
a. Prints: zero
b. Prints: 100
c. Prints: 1000
d. Prints: Default
e. Run-time error
f. Compile-time error
g. None of the above
Question 14
a. Assertions should never be placed at any point in the code that should not be reached under normal circumstances.
b. The compiler will generate an error if an assert statement is "unreachable" as defined by the Java Language
Specification.
c. A catch clause should not be used to catch an AssertionError.
d. AssertionError is a checked exception.
Question 15
class Q {
private int id;
public void finalize() {System.out.print(id);}
public Q(int i) {id = i;}
}
class R {
public static void main(String[] args) {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(i); // 1
}
System.gc(); // 2
}}
When the processing of line 2 begins, how many objects of type Q that were created at line 1 are eligible for garbage
collection?
a. 0
b. 1
c. 9
Page: 5
Dan Chisholm Exam 6 Questions
d. 10
e. Indeterminate.
f. Compile-time error
g. Run-time error
h. None of the above
Question 16
class GFM14 {
static byte a; static short b; static char c;
static int d; static long e; static String s;
public static void main(String[] args) {
System.out.println(a+","+b+","+(int)c+","+d+","+e+","+s);
}}
a. Prints: 0,0,0,0,0,null
b. Prints: 0,0,0,0,0,
c. Prints: 0,0, ,0,0,
d. Compile-time error
e. Run-time error
f. None of the above
Question 17
class EBH005 {
public static void main (String[] s) {
byte b = 127; b <<= 2;
System.out.println(b);
}}
a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Run-time error
i. Compile-time error
j. None of the above
Question 18
class Teal {
public static void main (String[] args) {
byte b = 1; // 1
long l = 1000; // 2
b += l; // 3
}}
Page: 6
Dan Chisholm Exam 6 Questions
a. 1
b. 2
c. 3
d. None of the above
Question 19
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
public static void main(String []args) {
Base[] base = {new Base()}; // 1
Sub sub[] = {new Sub()}; // 2
Object obj = sub; // 3
base = obj; // 4
}}
a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 20
class Black {
public static void main(String args[]) {
int[] i = {1,2,3,4,5}; // 1
long[] l = new long[5]; // 2
for (int j = 0; j < l.length(); j++) { // 3
l[j] = i[j]; // 4
}}}
a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 21
class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}}
Page: 7
Dan Chisholm Exam 6 Questions
a. Prints: Object
b. Prints: String
c. Compile-time error
d. Run-time error
e. None of the above
Question 22
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}}
Question 23
class D {
D() {System.out.print("D");}
class Z {Z(){System.out.print("Z");}}
public static void main(String args[]) {
new D.Z();
}}
a. Prints: D
b. Prints: Z
c. Prints: DZ
d. Prints: ZD
e. Run-time error
f. Compile-time error
g. None of the above
Question 24
Page: 8
Dan Chisholm Exam 6 Questions
a. Prints: A
b. Prints: B
c. Prints: C
d. Compile-time error
e. Run-time error
f. None of the above
Question 25
class Green {
public static void main(String args[]) {
final String a = "A";
final String b = "B";
String c = a+b;
String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}}
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 26
class R{
public static void main (String[] args) {
StringBuffer s1 = new StringBuffer("ABCDEFG");
s1.setCharAt(7,'H');
System.out.print(s1 + "," + s1.length());
}}
a. Prints: ABCDEFGH
b. Prints: ABCDEFH
c. Compile-time error
d. Run-time error
e. None of the above
Page: 9
Dan Chisholm Exam 6 Questions
Question 27
Which of these lists contains at least one word that is not a Java keyword?
Question 28
class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}
a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Run-time error
f. Compile-time error
g. None of the above
Question 29
class EBH104 {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
What is the result of attempting to compile and run the above program?
Page: 10
Dan Chisholm Exam 6 Questions
i. Compile-time error
j. None of the above
Question 30
class GFC305 {
static void m1(int[] i1, int[] i2) {
int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
a. Prints: 1,1
b. Prints: 1,3
c. Prints: 3,1
d. Prints: 3,3
e. Run-time error
f. Compile-time error
g. None of the above
Page: 11