Dan Chi Scholm: Exam 1
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? a. b. c. d. e. f.
Prints A.m1, A.m2, Compile-time error Compile-time error Compile-time error Compile-time error None of the above
A.m3, A.m3, A.m4, at line 1. at line 2. at line 3. at line 4.
Question 2 class GFM11{ public static void main (String[] args) { int x,y,z; System.out.println(x+y+z); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.
Prints nothing. Prints an undefined value. Prints: null Prints: 0 Run-time error Compile-time error 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. b.
Prints: ABC Prints: BCD
page: 1
Dan Chi Scholm: Exam 1
c. d. e. f. g. h. i.
Exam 1
Prints: CDE Prints: A B C Prints: B C D Prints: C D E Compile-time error Run-time error 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 compile-time error is generated at which line? a. b. c. d. e. f.
1 2 3 4 5 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 }} Compile-time errors are generated at which lines? a. b. c. d. e.
1 2 3 4 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]); }} What is the result of attempting to compile and run the program?
page: 2
Questions
Dan Chi Scholm: Exam 1
a. b. c. d. e.
Exam 1
Questions
Prints: 3,4,8 Prints: 7,2,6 Compile-time error Run-time error None of the above
Question 7 class A {A(int i) {}} // 1 class B extends A {} // 2 Which of the following statements are true? a. b. c. d.
The compiler attempts to create a default constructor for class A. The compiler attempts to create a default constructor for class B. Compile-time error at 1. Compile-time error at 2.
Question 8 Which of the following statements are true? a. b. c. d. e. f.
A constructor can invoke another constructor of the same class using the alternate constructor invocation, "this(argumentListopt);". A constructor can invoke itself using the alternate constructor invocation, "this(argumentListopt);". The alternate constructor invocation, "this(argumentListopt);", can legally appear anywhere in the constructor body. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation, "super(argumentListopt);". 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. A constructor is not permitted to throw an exception.
Question 9 class A {A() throws Exception {}} // 1 class B extends A {B() throws Exception {}} // 2 class C extends A {C() {}} // 3 Which of the following statements are true? a. b. c. d.
class A extends Object. Compile-time error at 1. Compile-time error at 2. Compile-time error at 3.
Question 10 Which of the following modifiers can be applied to a constructor? a. b. c. d. e.
private abstract final volatile native
page: 3
Dan Chi Scholm: Exam 1
f.
Exam 1
None of the above.
Question 11 Which of the following modifiers can be applied to the declaration of a field? a. b. c. d. e.
abstract final private protected public
Question 12 Which of the following modifiers can not be applied to a method? a. b. c. d. e. f.
abstract private protected public volatile 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)); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f.
Prints: 13 Prints: 4 Compile-time error at 1 Compile-time error at 2 Run-time error None of the above
Question 14 Which of the following modifiers can be applied to a class that is not a nested class? a. b. c. d. e. f.
Public Protected Private Abstract Static Final
page: 4
Questions
Dan Chi Scholm: Exam 1
Exam 1
Questions
Question 15 Which of the follow are true statements. a. b. c. d. e.
A nested class is any class that is declared within the body of another class or interface. A nested class can not be declared within the body of an interface declaration. An inner class is a nested class that is not static. A nested class can not be declared static. 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); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.
Prints: false,false Prints: false,true Prints: true,false Prints: true,true Compile-time error Run-time error 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. b. c. d. e. f.
Prints: XY Prints: YX Prints: XXYY Prints: YYXX Nothing is printed. 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
Which of the following statements is not true? a. b. c. d. e.
An interface that is declared within the body of a class or interface is known as a nested interface. A constant can be a member of an interface. A class declaration can be a member of an interface. A class that implements an interface must implement all of the methods declared within the interface. 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); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.
Prints: 0,255 Prints: 0,256 Prints: -127,128 Prints: -128,127 Compile-time error Run-time error None of the above
Question 20 class GRC1 {public static void main(String[] args) {}} // 1 class GRC2 {protected static void main(String[] args) {}} // 2 class GRC3 {private static void main(String[] args) {}} // 3 What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line? a. b. c. d. e. f.
Compile-time error at line 1. Compile-time error at line 2. Compile-time error at line 3. An attempt to run GRC1 from the command line fails. An attempt to run GRC2 from the command line fails. An attempt to run GRC3 from the command line fails.
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); }} What is the result of attempting to compile and run the program? a. b. c.
Prints: 0,0,0 Prints: 0,0,3 Prints: 0,3,0
page: 6
Dan Chi Scholm: Exam 1
d. e. f. g. h. i. j. k.
Exam 1
Prints: 0,3,3 Prints: 3,0,0 Prints: 3,0,3 Prints: 3,3,0 Prints: 3,3,3 Run-time error Compile-time error None of the above
Question 22 Which of the follow are true statements. a. b. c. d. e. f.
An anonymous class can extend only the Object class. An anonymous class can not implement an interface. An anonymous class declaration can not have an implements clause. An anonymous class declaration can name more than one interface in the implements clause. The class instance creation expression for an anonymous class must never include arguments. None of the above
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);} }} Which statements are true? a. b. c. d. e. f. g.
With assertions enabled it prints 210210-1 followed by an AssertionError message. With assertions enabled it prints 210210 followed by an AssertionError message. With assertions enabled it prints only 210210. With assertions enabled it prints nothing. With assertions disabled it prints 210210-1 With assertions disabled it prints only 210210 Assertions should not be used within the default case of a switch statement.
Question 24 Which of these words belong to the set of Java keywords? a. b. c. d. e. f.
transient serializable runnable run volatile externalizable
page: 7
Questions
Dan Chi Scholm: Exam 1
g.
Exam 1
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)); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f.
Prints: 1, 2, 3, 4, 8, Prints: 1, 2, 3, 4, 11, Prints: 3, 4, 1, 2, 11, Run-time error Compile-time error 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); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.
Prints: 127 128 255 256 Prints: 127 128 255 0 Prints: 127 -1 -127 0 Prints: 127 -128 -1 0 Run-time error Compile-time error None of the above
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 }} A compile-time error is generated at which line?
page: 8
Questions
Dan Chi Scholm: Exam 1
a. b. c. d. e.
Exam 1
Questions
1 2 3 4 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 }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.
Compile-time error at Run-time error at line Compile-time error at Run-time error at line Compile-time error at Run-time error at line None of the above
line 1. 1. line 2. 2. line 3. 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. b. c. d. e.
Prints: JMM101ABCDEF Prints: ABCDEF Compile-time error Run-time error 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
a. b. c. d. e. f. g. h. i. j.
Prints: 1, 2, 3, 4, 10, Prints: 1, 2, -3, 4, 4, Prints: 2, 2, -3, -3, -2, Prints: 2, 2, -3, 3, 4, Prints: 2, 3, -3, -2, 0, Prints: 2, 3, -3, 4, 6, Prints: 2, 3, 4, 5, 14, Run-time error Compile-time error 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 } Compile-time errors are generated at which lines? a. b. c. d. e. f. g. h.
1 2 3 4 5 6 7 8
page: 10
Exam 1
Questions