Exam 1 Questions
Exam 1 Questions
Question 1
package com.dan.chisholm;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
class B {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
Assume that the above code appears in a single file named A.java. What is the result of attempting to
compile and run the above program?
Question 2
class GFM11{
public static void main (String[] args) {
int x,y,z;
System.out.println(x+y+z);
}}
a. Prints nothing.
b. Prints an undefined value.
c. Prints: null
d. Prints: 0
e. Run-time error
f. Compile-time error
g. None of the above
Question 3
class GRC10 {
public static void main (String[] s) {
System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
a. Prints: ABC
b. Prints: BCD
page: 1
Dan Chi Scholm: Exam 1 Exam 1 Questions
c. Prints: CDE
d. Prints: A B C
e. Prints: B C D
f. Prints: C D E
g. Compile-time error
h. Run-time error
i. None of the above
Question 4
class MCZ11 {
public static void main (String[] args) {
char a = '\c'; // 1
char b = '\r'; // 2
char c = '\"'; // 3
char d = '\b'; // 4
char e = '\''; // 5
}}
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 5
class MWC101 {
public static void main(String[] args) {
int[] a1 = new int[]; // 1
int a2[] = new int[5]; // 2
int[] a3 = new int[]{1,2}; // 3
int []a4 = {1,2}; // 4
int[] a5 = new int[5]{1,2,3,4,5}; // 5
}}
a. 1
b. 2
c. 3
d. 4
e. 5
Question 6
class MWC201 {
public static void main(String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.print(a1[0][2]+","+a1[1][0]+","+a1[2][1]);
}}
page: 2
Dan Chi Scholm: Exam 1 Exam 1 Questions
a. Prints: 3,4,8
b. Prints: 7,2,6
c. Compile-time error
d. Run-time error
e. None of the above
Question 7
Question 8
a. A constructor can invoke another constructor of the same class using the alternate constructor
invocation, "this(argumentListopt);".
b. A constructor can invoke itself using the alternate constructor invocation, "this(argumentListopt);".
c. The alternate constructor invocation, "this(argumentListopt);", can legally appear anywhere in the
constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass constructor
invocation, "super(argumentListopt);".
e. The number of constructor invocations that may appear in any constructor body can equal but not
exceed the number of alternate constructors declared in the same class.
f. A constructor is not permitted to throw an exception.
Question 9
Question 10
a. private
b. abstract
c. final
d. volatile
e. native
page: 3
Dan Chi Scholm: Exam 1 Exam 1 Questions
Question 11
a. abstract
b. final
c. private
d. protected
e. public
Question 12
a. abstract
b. private
c. protected
d. public
e. volatile
f. None of the above.
Question 13
class JSC201 {
static byte m1() {
final char c = '\u0001';
return c; // 1
}
static byte m3(final char c) {return c;} // 2
public static void main(String[] args) {
char c = '\u0003';
System.out.print(""+m1()+m3(c));
}}
a. Prints: 13
b. Prints: 4
c. Compile-time error at 1
d. Compile-time error at 2
e. Run-time error
f. None of the above
Question 14
Which of the following modifiers can be applied to a class that is not a nested class?
a. Public
b. Protected
c. Private
d. Abstract
e. Static
f. Final
page: 4
Dan Chi Scholm: Exam 1 Exam 1 Questions
Question 15
a. A nested class is any class that is declared within the body of another class or interface.
b. A nested class can not be declared within the body of an interface declaration.
c. An inner class is a nested class that is not static.
d. A nested class can not be declared static.
e. A named class is any class that is not anonymous.
Question 16
class A {
public static void main (String[] args) {
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + ",");
System.out.print(error instanceof Throwable);
}}
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
Question 17
class B {
private String name;
public B(String s) {name = s;}
public void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m(); System.gc();
}}
Which of the following are true statements and which of the following could be a result of attempting to
compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will finalize any objects that are eligible for garbage
collection.
Question 18
page: 5
Dan Chi Scholm: Exam 1 Exam 1 Questions
a. An interface that is declared within the body of a class or interface is known as a nested interface.
b. A constant can be a member of an interface.
c. A class declaration can be a member of an interface.
d. A class that implements an interface must implement all of the methods declared within the interface.
e. None of the above.
Question 19
class JJF1 {
public static void main (String args[]) {
System.out.print(Byte.MIN_VALUE+",");
System.out.print(Byte.MAX_VALUE);
}}
a. Prints: 0,255
b. Prints: 0,256
c. Prints: -127,128
d. Prints: -128,127
e. Compile-time error
f. Run-time error
g. None of the above
Question 20
What is the result of attempting to compile each of the three class declarations and invoke each main
method from the command line?
Question 21
class EBH201 {
public static void main (String[] args) {
int a = 1 || 2 ^ 3 && 5;
int b = ((1 || 2) ^ 3) && 5;
int c = 1 || (2 ^ (3 && 5));
System.out.print(a + "," + b + "," + c);
}}
a. Prints: 0,0,0
b. Prints: 0,0,3
c. Prints: 0,3,0
page: 6
Dan Chi Scholm: Exam 1 Exam 1 Questions
d. Prints: 0,3,3
e. Prints: 3,0,0
f. Prints: 3,0,3
g. Prints: 3,3,0
h. Prints: 3,3,3
i. Run-time error
j. Compile-time error
k. None of the above
Question 22
Question 23
class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}
Question 24
a. transient
b. serializable
c. runnable
d. run
e. volatile
f. externalizable
page: 7
Dan Chi Scholm: Exam 1 Exam 1 Questions
g. cloneable
Question 25
class EBH001 {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
m(m(1) - m(2) + m(3) * m(4));
}}
a. Prints: 1, 2, 3, 4, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Run-time error
e. Compile-time error
f. None of the above
Question 26
class Magenta {
static byte a = (byte)127;
static byte b = (byte)128;
static byte c = (byte)255;
static byte d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}
Question 27
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
public static void main(String args[]) {
Sub s1 = new Sub();
I2 i2 = s1; // 1
I1 i1 = s1; // 2
Base base = s1; // 3
Sub s2 = (Sub)base; // 4
}}
page: 8
Dan Chi Scholm: Exam 1 Exam 1 Questions
a. 1
b. 2
c. 3
d. 4
e. None of the above
Question 28
class Green {
public static void main (String args[]) {
int[] i = null; // 1
Cloneable c = i; // 2
i = (int [])c; // 3
}}
Question 29
class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}
java JMM101 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
a. Prints: JMM101ABCDEF
b. Prints: ABCDEF
c. Compile-time error
d. Run-time error
e. None of the above
Question 30
class EBH101 {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) + m(i++) + m(-i) + m(i++));
}}
What is the result of attempting to compile and run the above program?
page: 9
Dan Chi Scholm: Exam 1 Exam 1 Questions
a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Run-time error
i. Compile-time error
j. None of the above
Question 31
class Identifiers {
int i1; // 1
int _i2; // 2
int i_3; // 3
int #i4; // 4
int $i5; // 5
int %i6; // 6
int i$7; // 7
int 8i; // 8
}
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
page: 10