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 toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.
Today, you can find my book on the retail web site of the company that prints it and distributes it.
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 code appears in a single file named A.java. What is the result of attempting to compile and run the program?
| a. | Prints: A.m1, A.m2, A.m3, A.m4, |
| b. | Compile-time error at 1. |
| c. | Compile-time error at 2. |
| d. | Compile-time error at 3. |
| e. | Compile-time error at 4. |
| f. | None of the above |
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
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
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]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3,4,8 |
| b. | Prints: 7,2,6 |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
Which of the following modifiers can be applied to the declaration of a field?
| a. | abstract |
| b. | final |
| c. | private |
| d. | protected |
| e. | public |
Which of the following modifiers can not be applied to a method?
| a. | abstract |
| b. | private |
| c. | protected |
| d. | public |
| e. | volatile |
| f. | None of the above. |
class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
}
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3
}}
What is the result of attempting to compile and run the program?
| 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 |
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 |
class A {A(int i) {}} // 1
class B extends A {} // 2
Which of the following statements are true?
| a. | The compiler attempts to create a default constructor for class A. |
| b. | The compiler attempts to create a default constructor for class B. |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
Which of the following modifiers can be applied to a constructor?
| a. | private |
| b. | abstract |
| c. | final |
| d. | volatile |
| e. | native |
| f. | None of the above. |
class MWC102 {
public static void main (String[] args) {
byte[] a = new byte[1]; long[] b = new long[1];
float[] c = new float[1]; Object[] d = new Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0,null |
| b. | Prints: 0,0,0.0,null |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class MWC202 {
public static void main(String[] args) {
int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}};
int []a2[] = {{1,2},{3,4,5},{6,7,8,9}};
int a3[][] = {{1,2},{3,4,5},{6,7,8,9}};
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 14 |
| b. | Prints: 16 |
| c. | Prints: 1,5,9 |
| d. | Prints: 2,4,8 |
| e. | Prints: 2,5,9 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Which of the following modifiers can be applied to the declaration of a field?
| a. | static |
| b. | synchronized |
| c. | transient |
| d. | volatile |
| e. | native |
Which of the following modifiers can not be applied to a method?
| a. | final |
| b. | static |
| c. | synchronized |
| d. | transient |
| e. | native |
| f. | None of the above. |
class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 24 |
| b. | Prints: 6 |
| c. | Compile-time error at 1. |
| d. | Compile-time error at 2. |
| e. | Run-time error |
| f. | None of the above |
Which of the follow statements is true.
| a. | An anonymous class can be declared abstract. |
| b. | A local class can be declared abstract. |
| c. | An abstract class can be instantiated. |
| d. | An abstract class is implicitly final. |
| e. | An abstract class must declare at least one abstract method. |
| f. | An abstract class can not extend a concrete class. |
Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class?
| a. | Do not declare any constructors. |
| b. | Do not use a return statement in the constructor. |
| c. | Declare all constructors using the keyword void to indicate that nothing is returned. |
| d. | Declare all constructors using the private access modifier. |
| e. | None of the above. |
Which of the following modifiers can be applied to a constructor?
| a. | protected |
| b. | public |
| c. | static |
| d. | synchronized |
| e. | transient |
// Class A is declared in a file named A.java.
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 D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A.m1, A.m2, A.m3, A.m4, |
| b. | Compile-time error at 1. |
| c. | Compile-time error at 2. |
| d. | Compile-time error at 3. |
| e. | Compile-time error at 4. |
class MWC103 {
public static void main(String[] args) {
int[] a1 = {1,2,3};
int []a2 = {4,5,6};
int a3[] = {7,8,9};
System.out.print(a1[1]+","+a2[1]+","+a3[1]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 12 |
| b. | Prints: 15 |
| c. | Prints: 18 |
| d. | Prints: 1,4,7 |
| e. | Prints: 2,5,8 |
| f. | Prints: 3,6,9 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MWC203 {
public static void main(String[] args) {
int[] a1[] = {new int[]{1,2},new int[]{3,4,5}};
int []a2[] = new int[][]{{1,2},{3,4,5}};
int a3[][] = {{1,2},new int[]{3,4,5}};
System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 14 |
| b. | Prints: 16 |
| c. | Prints: 1,2,4 |
| d. | Prints: 2,3,4 |
| e. | Prints: 2,3,5 |
| f. | Prints: 2,4,5 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |