






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Each problem is worth 10 points.
1. What is the difference between the “==” operator and the “equals()” method for String objects? 2. In writing a general purpose sort method in Java, the sort method needs to compare objects. There are two ways to give the sort method the code to compare objects. Briefly, describe the two ways.
3. Given class A and B below. class A { public void bar() { System.out.println( "A" ); } } class B extends A { public void bar() { System.out.println( "B" ); } } Parts a and b refer to the following two lines of code: A test = new B(); test.bar(); a. If all method access in Java were determined statically then what would the two lines of code above print out? b. If all method access in Java were determined dynamically then what would the two lines of code above print out? c. When is method access in Java determined statically?
6. Circle the assignment statements, "Line 1" through "Line 10", in the following code that illegally access a field. Why are they illegal accesses? package Exam; public class Parent { protected String protectedVar = "Protected"; String packageVar = "Package"; } package Exam; public class Uncle { public void SampleAccess( Parent parameter ) { parameter.protectedVar = "Line 1"; parameter.packageVar = "Line 2"; } } package Quiz; public class Aunt { public void SampleAccess( Exam.Parent parameter) { parameter.protectedVar = "Line 3"; parameter.packageVar = "Line 4"; } } package Quiz; public class Child extends Exam.Parent { public void SampleAccess( Exam.Parent parentType, Child childType) { parentType.protectedVar = "Line 5";; parentType.packageVar = "Line 6"; protectedVar = "Line 7"; packageVar = "Line 8"; childType.protectedVar = "Line 9"; childType.packageVar = "Line 10"; } }
7. In the following code, what are the values of a, b, c, and name when the System.out.println statement is executed. Explain the output. class DefaultValues { int a; String name; public void aMethod() { int b; int[] c = new int[5]; System.out.println( "Start Here"); } } 8. What does it mean to say that the ListIterator is fail-fast?
9b. Given the classes below, what will be the output of statement "new Child()"? Why? class Parent { public Parent() { System.out.println( "In Parent"); } { System.out.println( "In Parent Block"); } } class Child extends Parent { { System.out.println( "In Child block"); } public Child() { super(); System.out.println( "In Child"); } }
10a. What will be the result of trying to compiling and executing the following program if FooException is properly defined as a subclass of java.lang.Exception? Explain. class ExceptionQuestion { public void aMethod() { System.out.println( "In aMethod" ); throw new FooException(); } public static void main( String[] args ) { try { System.out.println( "Start" ); ExceptionQuestion x = new ExceptionQuestion(); x.aMethod(); System.out.println( "After method" ); } catch ( FooException error ) { System.out.println( "In handler" ); } System.out.println( "End" ); } }