Java Review Question & Answers These are my review question I made while preparing for my SCJP2 Exam. In following with the Linux/OpenSource/OpenDoc philosophy, I am making my java question available for free to the public under the GNU FDL (GNU Free Documentation License). Java Review Question & Answers Copyright (c) 2002 Rajinder Yadav Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with the no Front-Cover Texts, and with no Back-Cover Texts being LIST. A copy of the license is included in the section entitled "GNU Free Documentation License". web: www.rajindery.com email:rajindery@yahoo.com A copy of the GNU FDL can be obtained at, http://www.gnu.org/licenses/fdl.html If you have signed the SUN JAVA PROGRAMMER CERTIFICATION EXAM Non-disclosure form, please do not make changes or modifications to this document other than for personal use. You are still granted to make copies and redistribute the document. This restriction has been added to assure that this document is allowed to be freely available without and legal restriction to others. If you have not signed a "SUN JAVA PROGRAMMER CERTIFICATION EXAM Non-disclosure form" and would like to contribute to the growth of this document by adding, correcting, or modifying this document, you are free to do so. To get the latest copy of this document please visit: http://www.rajindery.com/javacert/answers.txt If you have come across this document from another source, please send me an email stating where you obtained your copy, thank you. You may send your comments, suggestions and corrections to: Rajinder Yadav email: rajindery@yahoo.com +--------------------------------------------------------+ | How to help out and give back | +--------------------------------------------------------+ First please read the FDL agreement contained in this document. If you found these my review questions to be helpful, please share them with others so they may also benefit in their preparation for the SCJP2 Exam, better yet direct them to this site so they can pick up the latest copy! To give back, I would encourage you strongly to come up with a few 'tough' questions of your own, and send them to me for inclusion in this document. Please do not send me copies of someone else's original work! Other ways to help, * help translate the documents and test questions for another languages * proof read for typos, errors, clarification * review information for technical accuracy * help maintain all information current, advise on deprecated APIs, etc. btw, when you make your first million, please don't forget about me :^D +--------------------------------------------------------+ | java basics | +--------------------------------------------------------+ 1) which packages get imported implicitly, select all valid answers a) java.lang.Vector; b) java.util.*; c) java.langs.*; d) java.lang.Object; e) java.lang.Math; Answer: b, d, e Comments: everything thing in the java.util package get imported, so that is why d) and e) are correct. The Vector class belongs to the java.util package, the is no java.utils package 2) which packages get imported implicitly, select all valid answers a) java.lang.boolean b) java.lang.String; c) java.lang.Int; d) java.langs.Float; e) java.math.BigDecimal; f) java.lang.Void; Answer: b,d,f Comments: class being with a uppercase letter so a is incorrect, there is not Int package but there an Integer package 3) will an empty sourcefile fur.java produce an error when compiled (t/f) Answer: false Comments: an empty file will compile without error 4) what type of value is a char a 8-bit signed b 16-bit signed unicode c 16-bit unsigned d 32-bit signed e 32-bit unsigned unicode Answer: c Comments: there is no such thing as a 'signed unicode', and unicode values are 16-bit 5) select all valid java keywords a) throws b) catches c) throw d) then Answer: a,c Comments: 6) select all valid java keywords a) Void b) int c) sizeof d) interface Answer: b,d Comments: 7) what is the bit depth of an integer value a) 8-bits b) 16-bits c) 7-bits d) 32-bits Answer: d Comments: 8) select all valid java keywords a) if b) private c) finally d) final Answer: a,b,c,d Comments: 9) select all valid java keywords a) try b) native c) break d) exit Answer: a,b,c Comments: 10) select all valid java identifier a) _2big b) $name c) case d) a2b Answer: a,b,d Comments: case is java keyword, can't use keywords for identifiers 11) select all valid java keywords a) super b) New c) byte d) bool Answer: a,c Comments: bool does not exit in java, but boolean does 12) select all valid java keywords a) synchronized b) true c) false d) do Answer: a, d Comments: true and false are boolean literal values 13) select all valid java keywords a) null b) goto c) delete d) jump Answer: b Comments: null is a java literal value 14) select all valid java identifier a) null b) int32 c) %item d) File_ptr Answer: b,d Comments: null is a java literal 15) the operator >> works with unsigned values (t/f) Answer: false Comments: 16) which ones are the short-circuit operators a) & b) ^ c) || d) == e) && f) += Answer: e Comments: 17) is this valid (y/n) float = 10.0; Answer: n Comments: 10.0 is a double literal value 18) is this valid (y/n) int i = 127; byte b = i; Answer: n Comments: will report a compiler error 19) is this valid (y/n) byte b = 128; Answer: n Comments: will report an error since a the max value a byte can hold is 127 20) is this valid (y/n) int i = 10; i += 5; Answer: y Comments: i += 5; is a shorthand operator for, i = i + 5; 21) is this valid (y/n) int i = 1; char b = i?'a':'b'; Answer: n Comments: i must be a boolean value 22) list all valid assignments a) char = "b"; b) char = \u0000; c) char = 018; d) char = 0x10; e) char = 'f'; f) char = 512; Answer: c,d,e,f Comments: a) and b) are incorrect since a char must be enclosed in a single quote 23) is this valid (y/n) int i = 10; char c = i; Answer: n Comments: since int is a signed type, it cannot be assignment to a char which is unsigned without a typecast 24) is this valid (y/n) char c = 0xff; int i = c; Answer: y Comments: 25) what will be the output from the following program public class Test { boolean b; int i=0; public static void main(String[] args) { for(i=0; i<10; ++i) { System.out.println(b); } } } a) will not compile since variable b is not initialized before use b) prints 'true' ten times c) prints 'false' tem times d) compiler error stating non-static access from a static context Answer: d Comments: 26) what results from the following statements boolean b = false; (8 == 3) || (b = true); a) won't compile b) b will be false c) b will be true Answer: c Comments: with the '||' short-circuited operator, the r-value get evaluated if the l-value is false 27) list all valid java array declarations a) char []a = new int[5]; b) int a[5] = {1,2,3,4,5}; c) int n[] = new[] {5,5,5}; d) int* p = int[10]; e) long []a[]; f) long a[3][]; Answer: c,e Comments: a) is incorrect since you can't init a char array with int values :^), b) and f) are wrong since you can't declare a value, d) is wrong since java has no pointers 28) what will happen if the following code is compiled and run public class Test { public static void main(String[] args) { byte b = -1; switch(b) { default: System.out.println("Z"); case 0: System.out.println("A"); break; case 1: System.out.println("B"); break; } } } a) will output Z b) will not compile since default must be the last test case c) will output Z and A d) will not compile since argument to switch must be type int Answer: c Comments: 29) what results from the following statements boolean b = false; (1 == 1) && (b = true); a) won't compile b) b will be false c) b will be true Answer: c Comments: with the '&&' short-circuited operator, the r-value get evaluated if the l-value is true 30) what will happen if the following program is compiled and run public class Test { public static void main(String[] args) { float f[] = new float[10]; for(int n=0; n>33; a) n will have a value of 1 b) n will be some negative number c) a compiler error, cannot left shift more then the bit depth of a value d) n will be zero Answer: a Comments: if the shift value is larger than the bit depth of a value, java shifts the values value%bit-depth times, so n>>33 == n>>33%32 == n>>1 35) In method init() how many objects are created class A {} void init() { A a1; A a2[] = new[5]; } a) 0 objects b) 1 object c) 2 objects d) 5 objects e) 6 objects Answer: 1 Comments: a1 is a declaration of a reference, a2 creates one array object of size 5 (yes an array is an object) 36) what will be the result if the following program is compiled and run? public class Test { public static void main(String[] args) { int n = methodX(8); System.out.println(n); } static int methodX(int j) { int i; switch(j) { case 0: i=-12; break; case 8: i=++j; break; } return i--; } } a) the value 8 will be output b) the value 9 will be output c) the value 7 will be output d) will not compile Answer: d Comments: a compiler error result since there exist values for j such that it's possible that variable i will not get initialized before it's returned. 37) what will happend if this program is compiled and run public class Test { public static void main(String[] args) { System.out.println(sum(5)); } static int sum(int i) { if(i == 0) { return 0; } return i+sum(i-1); } } a) the output will be 9 b) no output, the program will crash c) will not compile since a function cannot make a call to itself d) the output will be 45 Answer: d Comments: sum calls itself recursively with a value (i-1) each time, the recursing stop when i == 0 38) assumming Quicksort is a static final class that implements the quicksort algorithm, will this program compile. (y/n) import com.yadav.utils.Quicksort; import com.acme.Quicksort; public class Test { public static void main(String[] args) { int n[] = {12,6,29,-3,3,66,21}; Quickstort.int(n); } } Answer: n Comments: the program imports two 'Quicksort' packages, so the statement 'Quickstort.int(n);' will be ambiguous 39) what will happen if this program is compiled and run public class Test { public static void main(String[] args) { int i[][] = new int[2][]; i[0] = new int[3]; i[1] = new int[5]; System.out.println(i.length); } } a) will result in a compile error b) will output the value 10 c) will output the value 8 d) will output the value 2 Answer: d Comments: 40) given the statement 'final int n=10;', we can say that 'n' is an interger literal (t/f) Answer: f Comments: a literal is a non-variable constant value, in this question '10' is consider to be a literval value of type int 41) what will happen if the following program is compiled and run public class Test { public static void main(String[] args) { int a[] = new int[5]; System.out.println(a instanceof Object); } } a) an runtime exception will be thrown b) the output will be 'true' c) the output will be 'false' d) will not compile Answer: b Comments: an array is considered to be an object in java 42) will the variable initialization process complete correctly in this code? (select all valid answers) public class Test { static char c1; char cc =c1; // line 1 static char c2 = c3; // line 2 int i = c1; // line 3 static char c3 = 'r'; public static void main(String[] args) { } static { // line 4 c1 = c3; } } a) will break at line 1, cannot initialize a instance field from a static field b) will break at line 2, cannot forward reference with static field c) will compile without error d) will break at line 3, cannot initialize an int field with a char field e) will break at line 4, cannot initialize like this Answer: b Comments: 43) what will happen if this program is compiled and run public class Test { static int b1 = initMe(); static int b2 = 10; public static void main(String[] args) { int b3 = b1 + b2; System.out.println(b3); } static int initMe() { return b2; } } a) will output the value '20' b) will not compile c) will output the value '10' Answer: c Comments: +--------------------------------------------------------+ | java classes | +--------------------------------------------------------+ 1) what is a top-level class, select the most appropriate answer a) the first class declared in the sourcefile b) a static inner class c) a public inner class d) a public non-inner class Answer: d Comments: 2) can a source file contain many public classes (t/f) a) true b) flase Answer: a Comments: only a single'top-level' class can exist is a source file) 3) is the Object class the super-class for all java classes (t/f) Answer: t Comments: 4) if a class is declared with no access modifier, is it visible to other classes in the same package (y/n) Answer: y Comments: 5) are default class fields visible to classes outside package (y/n) Answer: y Comments: 6) inorder to create a class that can be subclassed, it must: (select all valid answers) a) be declared public b) cannot be an abstract class c) it must not have a private constructor d) cannot be an inner-class e) should not be a static class Answer: c, e Comments: 7) does a static inner class have access to public fields of it's outer class (y/n) Answer: n Comments: non-static access cannot be made from a static context 8) can an inner class be declared abstract (y/n) Answer: y Comments: 9) can an interface have a non-abstract method? (y/n) Answer: n Comments: 9) can an interface have a field member? (y/n) Answer: y Comments: all interface fields are public static final 10) if a class implements an interface, which of the following are true a) the class must be non-static b) the class cannot be abstract c) the class cannot be declared as final d) the class needs to be in the same package as the interface e) an abstract class dose not need to implement the interface methods Answer: e Comments: e) is correct since an abstract class doesn't need to implement the methods of an interface 11) can an interface implement multiple interfaces using the extends keyword (y/n) Answer: y Comments: 12) list two things wrong with the following code interface I {} interface J extends I{} interface K {} public class A {} public class B {} class C extends A, B implements I, K {} a) an interface must be declared 'public abstract' b) cannot have multiple top-level public classes c) an interface can't extend another interface d) a class cannot implment more than one interface e) a class cannot subclass more than one class Answer: b, e Comments: 13) what is the result if the following code is compiled and run public class Test { public static void main(String[] args) { B b = new B(0); } } class A { int a; A(int a) { this.a = a; // line 1 } } class B extends A { B(int a) { System.out.println("I'm alive!"); } } a) will compile and display the string "I'm alive!" b) will not compile since statement at line 1 is illegal c) will not compile since a default no-argument ctor is missing in class B d) will compile but throw an exception when run because of a missing no-arg ctor in class A e) will not compile since a default no-argument ctor is missing in the base class A Answer: e Comments: 14) if the following code is compiled and run, what will be the result public class Test { public static void main(String[] args) { A.B b = new A.B(); // line 1 } } class A { static class B { // line 2 B() { // line 3 System.out.println("I'm Alive!"); } } } a) will not compile since line 1 is not valid b) will run and display the string "I'm Alive!" c) will break at line 3, static classes cannot declare a ctor d) will break at line 3, static classes cannot declare a ctor Answer: b Comments: 15) how can we change the code so it will compile and run public class Test { public static void main(String[] args) { B b = new B(); } } class A { //A() {} // line 1 A(int a) {} } class B extends A { //B() {} // line 2 B(int a) { //super.A(a); // line 3 //super(a); // line 4 } } a) uncomment line 1 b) uncomment line 2 c) uncomment line 3 d) uncomment lines 1 and 2 e) uncomment line 4 Answer: d Comments: 16) can a ctor have a return statement (y/n) class A { A() { return; } } Answer: y Comments: 17) can a ctor have a return value (y/n) Answer: n Comments: 18) can a ctor be declared as private (y/n) Answer: y Comments: 19) can the following class be compiled and created using 'A = new A();' (y/n) class A { int b; A() { this(10); } A(int b) { this.b = b; } Answer: y Comments: 20) subclasses are created before their superclasses (t/f) Answer: f Comments: 21) a ctor with not access modifier will have which access visibility a) public b) default c) same visibility as the class d) private Answer: c Comments: 22) will the following code compile and run (y/n) public class Test { static int i; public static void main(String[] args) { System.out.println(i); } } Answer: y Comments: 23) what will happen if the following code is compiled and run public class Test { { System.out.println("A"); } Test() { System.out.println("C"); } public static void main(String[] args) { System.out.println("D"); Test t = new Test(); } static { System.out.println("B"); } } a) will output the lines A,B,C,D b) will output the lines A,C,D,B c) will output the lines B,D,C,A d) will output the lines D,C,A,B e) will output the lines B,D,A,C f) will will a compiler error Answer: e Comments: 24) to make the following code compile and run, what needs to be done public class Test { public static void main(String[] args) { B b = new B(0); } } class A { A(int a) { } } class B extends A { B() { super(1); } B(int i) { int i=0; // line 1 this(); // line 2 } } a) delete line 1 b) delete line 2 c) delete lines 1 and 2 Answer: a Comments: 25) what needs to be added at 'XX' to make class A compile without error interface I { void init(); } 'XX' class A implements I{ } a) static b) public c) final d) abstract Answer: d Comments: 26) if the following code is compiled what will happen abstract class A { void init() { System.out.println("XXX"); } } class B extends A { } a) will compile without error b) will not compile since class B does not declare method 'void init()' c) will not compile since class B must be declared as abstract d) will not compile, cannot extend a abstract class e) will not compile, abstract class A cannot declare a non-abstrace method Answer: a Comments: 27) what will happen if the following code is compiled and run public class Test implements I { public static void main(String[] args) { System.out.println(s); } } interface I { String s = "Welcome!"; } a) the string "Welcome!" will be displayed b) will not compile c) will compile but throw an exception when run Answer: a Comments: 28) what will happen if the following code is compiled and run public class Test implements I { static String s = "Do not Enter!!!"; public static void main(String[] args) { System.out.println(s); } } interface I { String s = "Welcome!"; } a) the string "Welcome!" will be displayed b) will not compile c) will compile but throw an exception when run d) the string "Do not Enter!!!" will be displayed Answer: d Comments: 29) what will happen if the following code is compiled and run public class Test { String s = "Hello!"; public static void main(String[] args) { System.out.println(s); } } a) will display the string "Hello!" b) will compile but throw an exception with it's runs c) will not compile Answer: c Comments: 30) fields declared with no modifiers inside an interface are considered to be 'public static final' (t/f) Answer: t Comments: 31) what will happen if the following code is compiled and run public class Test implements I { static String s = "Do not Enter!!!"; public static void main(String[] args) { String s = "Please Come In!"; System.out.println(Test.s); } } interface I { String s = "Welcome!"; } a) will display the string "Welcome!" b) will compile but throw an exception with it's runs c) will not compile d) will display the string "Do not Enter!!! e) will display the string "Please Come In!" Answer: d Comments: 32) what will happen if the following code is compiled and run a) will display the string "Welcome!" b) will compile but throw an exception with it's runs c) will display the string "Please Come In!" d) will display the string "Do not Enter!!! e) will not compile Answer: a Comments: 33) what will happen if the following code is compiled public class Test implements I { public static void main(String[] args) { B b = new B(); } } class A { private A() { } } class B extends A { } a) since ctor's are not inherited, will compile without any error b) will not compile, since ctor in class A is declared private c) will not compile, since a top-level class cannot declare a private ctor d) will not compile, since it is not primitted to declare a ctor as private Answer: b Comments: 34) what will happen is the following code is compiled and run public class Test { public static void main(String[] args) { Shape s = new Circle(); // line 1 Box b = new Box(); s.draw(); b.draw(); } } abstract class Shape { void draw() { System.out.println("Shape"); } } class Circle extends Shape { void draw() { System.out.println("Circle"); } } class Box extends Shape { protected void draw() { // line 2 System.out.println("Box"); } } a) will show the strings "Shape" and "Box" b) will show the strings "Shape" and "Shape" c) will not compile because of line 1 d) will show the strings "Circle" and "Shape" e) will compile but throw an exception when run f) will show the strings "Circle" and "Box" g) will not compile because of line 2, because of access modifier 'protected' Answer: f Comments: 35) if class A extends class B and implements an interface I, which of the following statement is true a) class A is a subclass of class Object b) an object of class A is an instance of I c) an object of class A is an instance of B d) an object of class B is an instance of A e) B is a superclass of A Answer: a,b,c,e Comments: 36) a method declared in a subclass having the same name, return type and argument list is an example of a) overloading a method b) overridding a method c) is not allowed Answer: b Comments: 37) a conversion from a reference type 'X' to an reference type Object is possible without requiring a cast (t/f) Answer: t Comments: all classes are a subclass of Object, thus any object can be assigned (up-cast) to a reference of type Object +--------------------------------------------------------+ | exception handling | +--------------------------------------------------------+ 1) all uncaught exceptions cause the running process to be killed (t/f) Answer: f Comments: an uncaught exception causes the current thread to die, only when the last non-daemon thread die will a process exit 2) what lines will be output public class Test { public static void main(String[] args) { try { System.out.println("A"); int i=5/0; System.out.println("B"); } catch(ArithmeticException e) { System.out.println("C"); } catch(Exception e) { System.out.println("D"); } finally { System.out.println("E"); } System.out.println("F"); } } a) lines A,E,F b) lines A,B,E,F c) lines A,C,E,F d) lines A,C,B,E,F e) Lines A,C f) lines A,E Answer: c Comments: 3) what is the most likely output public class Test { public static void main(String[] args) { try { System.out.println("A"); int i=5/0; System.out.println("B"); } finally { System.out.println("E"); } System.out.println("F"); } } a) A Exception in thread "main" java.lang.ArithmeticException: / by zero b) A E Exception in thread "main" java.lang.ArithmeticException: / by zero c) A E F Exception in thread "main" java.lang.ArithmeticException: / by zero Answer: b Comments: 4) given the following Excepetion class heirarchy, is the following try/catch block valid (y/n) Exception | +-RuntimeException | +-ArithmeticException try { } catch(Exception e) { } catch (ArithmeticException e) { } Answer: n Comments: all narrow exception must be caught before more general ones 5) if an exception is caught inside a function call a) exception continues after the line the exception took place b) exception continues after the exception handler block c) the method returns after exception handler completes Answer: b Comments: 6) given the following Excepetion class heirarchy, will class B compile (t/f) Exception | +-RuntimeException | +-ArithmeticException class A { void sum(int a, int b) throws Exception {} } class B extends A { void sum(int a, int b) throws ArithmeticException {} } Answer: y Comments: an overridden method can specify a narrower exception to throw 7) the RuntimeException class and all it's sub-classes are 'checked' exception (t/f) Answer: f Comments: 8) a Error exception class and all it's sub-classes are 'unchecked' exception (t/f) Answer: t Comments: 9) the Exception class is the super-class for all exception classes (t/f) Answer: f Comments: java.lang.Throwable is the super-class 10) what will be the output public class Test { public static void main(String[] args) { try { try { int i=5/0; } finally { System.out.println("A"); } } catch(ArithmeticException e) { System.out.println("B"); } finally { System.out.println("X"); } } } a) strings A B X b) strings B X c) string B X A Answer: a Comments: 11) given the following class relationships, select all valid answers interface I {} class A {} class B extends A{} class C extends A implements I {} a) class B extends A is an example of a 'is-a' relationship b) class C implements I is an example of a 'has-a' relationship c) class C is said to contain class A d) class A is the superclass of class C e) class B derives from class A f) class B is a subclass of class C Answer: a,d,e Comments: +--------------------------------------------------------+ | string immutability & garbage collection | +--------------------------------------------------------+ 1) what will be the output of the following program public class Test { public static void main(String[] args) { String s = "good"; s.concat("bye"); System.out.println(s); } } a) string "good" b) string "goodbye" Answer: a Comments: 2) what will be the output of the following program public class Test { public static void main(String[] args) { String s1 = "good"; String s2; s2 = setString(s1); System.out.println(s1); System.out.println(s2); } static String setString(String s) { s = "bye"; return s; } } a) strings "good" and "good" b) strings "bye" and "bye" c) strings "good" and bye" Answer: c Comments: 3) what will be the output of the following program public class Test { public static void main(String[] args) { String s1 = "good"; String s2; s2 = setString(s1); System.out.println(s1); System.out.println(s2); } static String setString(String s) { s.concat("bye"); return s; } } a) strings "goodbye" and "goodbye" b) strings "good" and "goodbye" c) strings "good" and "good" Answer: c Comments: 4) what will be the output of the following program public class Test { public static void main(String[] args) { String s1 = "good"; String s2; s2 = setString(s1); System.out.println(s1); System.out.println(s2); } static String setString(String s) { s = s.concat("bye"); return s; } } a) strings "goodbye" and "goodbye" b) strings "good" and "goodbye" c) strings "good" and "good" Answer: b Comments: 5) when is the earliest that string "good" become eligible for GC public class Test { public static void main(String[] args) { String s1 = new String("good"); String s2; s2 = dup(s1); s2 = null; // line 3 s1 = dup(s2); // line 4 } static String dup(String s) { String ss = s; s = null; // line 1 return ss; } // line 2 } a) after line 1 b) after line 2 c) after line 3 d) after line 4 e) never Answer: d Comments: 6) what will the output be public class Test { public static void main(String[] args) { String s1 = new String("good"); String s2 = s1.toString(); System.out.println("s1 == s2 " + (s1 == s2)); } } a) the string "s1 == s2 false" b) the string "s1 == s2 true" Answer: b Comments: 7) what will be the output of the following program public class Test { public static void main(String[] args) { String s1 = "Good" + "bye"; String s2 = "Good" + "bye"; String s3 = "Goodbye"; System.out.println("s1 == s2 : " + (s1 == s2)); System.out.println("s1 == s3 : " + (s1 == s3)); } } a) s1 == s2 : false s1 == s3 : false b) s1 == s2 : true s1 == s3 : false c) s1 == s2 : true s1 == s3 : true d) s1 == s2 : false s1 == s3 : true Answer: c Comments: 8) when will string s1 become eligible for garbage collection? public class Test { static String s; public static void main(String[] args) { String s1 = new String("Mewow!"); s = s1; // line 1 s1 = null; // line 2 killString(s); } static void killString(String ss) { ss = null; // line 3 } } a) after line 1 b) after line 2 c) after line 3 d) never Answer: d Comments: c) is not correct since killString() is passed a 'copy' of static reference field 's' :^) 9) what will be the output public class Test { static String s; public static void main(String[] args) { String s1 = new String("First"); String s2 = new String("Second"); swapString(s1, s2); System.out.println(s1+s2); } static void swapString(String s1, String s2) { String temp = s1; s1 = s2; s2 = temp; } } a) the string "FirstSecond" b) the string "SecondFirst" Answer: a Comments: since a 'copy' of the references 's1' and 's2' are passed to swapString(), the original references are not affected +--------------------------------------------------------+ | package java.lang | +--------------------------------------------------------+ 1) what will happen if this program is compiled and run public class Test { public static void main(String[] args) { long k = Long.MAX_VALUE; ++k; long r = Math.abs(k); System.out.println(r); } } a) the output will be '0' b) an overflow exception will be thrown c) the output will be '-9223372036854775808' Answer: c Comments: the increment operator will cause k to overflow resulting in the most significant bit to turn on, which means k == Long.MIN_VALUE, this value has no positive representation! +--------------------------------------------------------+ | threads | +--------------------------------------------------------+ if this program is run, when will the process terminate public class Test { public static void main(String[] args) { A a = new A(); B b = new B(); a.start(); b.start(); } // line 1 } // line 2 class A extends Thread { A() { setDaemon(true); } public void run() { // loop forever while(true) { System.out.println("Still running..."); } } } class B extends Thread { public void run() { // loop for a little while for(int i=0; i<1000; ++i) { System.out.println("String going..."); } } // line 4 } a) after line 1 b) after line 2 c) after line 4 d) only after both the main thread and thread 'b' exit e) never, since thread 'a' runs in a infinte loop and never dies Answer: d Comments: a process terminates when all 'non-daemon' threads exit, thread a has a call in it's ctor to make it into a daemon thread :^D +--------------------------------------------------------+ | java AWT components | +--------------------------------------------------------+