Java Review Questions, by Rajinder Yadav 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 Questions 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/jtest.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 use my notes | +--------------------------------------------------------+ I would suggest that you print out a copy of my notes and use them along with other reading material. I have done my best to give each topic it's own section so you can use my notes as a quick refresher. Once you are able to read my notes in it's entirety, I would suggest that you take a look at my java questions, they are designed to test you on what you already know from my notes and your reading. My review question can be picked up at: http://www.rajindery.com/javacert/jtest.txt I suggest that you save my review questions for the last week before taking your exam, you can then use my answer sheet which also contains the questions, together they will provide you a way to refresh your memory and keep you sharp! I've taken my free time to make this available for you, now please take some time to give back.... in the immortal words of Jerry Maguire, "Help me help you!" +--------------------------------------------------------+ | 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; 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; 3) will an empty sourcefile fur.java produce an error when compiled (t/f) 4) what type of value is a char a 8-bit signed b 16-bit signed c 16-nit unsigned d 32-bit signed e 32-bit unsigned 5) select all valid java keywords a) throws b) catches c) throw d) then 6) select all valid java keywords a) Void b) int c) sizeof d) interface 7) what is the bit depth of an integer value a) 8-bits b) 16-bits c) 7-bits d) 32-bits 8) select all valid java keywords a) if b) private c) finally d) final 9) select all valid java keywords a) try b) native c) break d) exit 10) select all valid java identifier a) _2big b) $name c) case d) a2b 11) select all valid java keywords a) super b) New c) byte d) bool 12) select all valid java keywords a) synchronized b) true c) false d) do 13) select all valid java keywords a) null b) goto c) delete d) jump 14) select all valid java identifier a) null b) int32 c) %item d) File_ptr 15) the operator >> works with unsigned values (t/f) 16) which ones are the short-circuit operators a) & b) ^ c) || d) == e) && f) += 17) is this valid (y/n) float = 10.0; 18) is this valid (y/n) int i = 127; byte b = i; 19) is this valid (y/n) byte b = 128; 20) is this valid (y/n) int i = 10; i += 5; 21) is this valid (y/n) int i = 1; char b = i?'a':'b'; 22) list all valid assignments a) char = "b"; b) char = \u0000; c) char = 018; d) char = 0x10; e) char = 'f'; f) char = 512; 23) is this valid (y/n) int i = 10; char c = i; 24) is this valid (y/n) char c = 0xff; int i = c; 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 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 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][]; 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 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 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 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 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 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 never end c) will not compile since a function cannot make a call to itself d) the output will be 45 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); } } 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 40) given the statement 'final int n=10;', we can say that 'n' is an interger literal (t/f) 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 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) breaks at line 1, cannot initialize a instance field from a static field b) breaks at line 2, cannot forward reference with static field c) compiles without error d) breaks at line 3, cannot initialize an int field with a char field e) breaks at line 4, cannot initialize like this 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' +--------------------------------------------------------+ | 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 2) can a source file contain many public classes (t/f) a) true b) flase 3) is the Object class the super-class for all java classes (t/f) 4) if a class is declared with no access modifier, is it visible to other classes in the same package (y/n) 5) are default class fields visible to classes outside package (y/n) 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 7) does a static inner class have access to public fields of it's outer class (y/n) 8) can an inner class be declared abstract (y/n) 9) can an interface have a non-abstract method? (y/n) 9) can an interface have a field member? (y/n) 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 11) can an interface implement multiple interfaces using the extends keyword (y/n) 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 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 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 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 16) can a ctor have a return statement (t/f) class A { A() { return; } } 17) can a ctor have a return value (y/n) 18) can a ctor be declared as private (y/n) 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; } 20) subclasses are created before their superclasses (t/f) 21) a ctor with not access modifier will have which access visibility a) public b) default c) same visibility as the class d) private 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); } } 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 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 <---(ans) b) delete line 2 c) delete lines 1 and 2 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 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 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 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 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 30) fields declared with no modifiers inside an interface are considered to be 'public static final' (t/f) 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!" 32) 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(I.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 display the string "Please Come In!" d) will display the string "Do not Enter!!! e) will not compile 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 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' 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 ob 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 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 37) a conversion from an reference type 'X' to a reference type Object is possible without requiring a cast (t/f) +--------------------------------------------------------+ | exception handling | +--------------------------------------------------------+ 1) all uncaught exceptions cause the running process to be killed (t/f) 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 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 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) { } 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 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 {} } 7) the RuntimeException class and all it's sub-classes are 'checked' exception (t/f) 8) a Error exception class and all it's sub-classes are 'unchecked' exception (t/f) 9) the Exception class is the super-class for all exception classes (t/f) 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 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 +--------------------------------------------------------+ | 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" 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" 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" 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" 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 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" 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 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 s) { s = null; // line 3 } } a) after line 1 b) after line 2 c) after line 3 d) never 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" +--------------------------------------------------------+ | package java.lang | +--------------------------------------------------------+ 1) what will happen if this program is compiled and run public class Test { public static void main(String[] args) { long l = Long.MAX_VALUE; ++l; long r = Math.abs(l); System.out.println(r); } } a) the output will be '0' b) an overflow exception will be thrown c) the output will be '-9223372036854775808' +--------------------------------------------------------+ | 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 the main thread and thread 'b' both exit e) never, since thread 'a' runs in a infinte loop and never dies +--------------------------------------------------------+ | java AWT components | +--------------------------------------------------------+ question to add * given a file named 'Test.java', determine if the following will compile and run (y/n) class Test { public static void main(String[] args) { TestClass tc = new TestClass("Hello!"); tc.out(); } } public class TestClass { String name; TestClass(String s) { name = s; } void out() { System.out.println(name); } } * a compiler error will result if we try to explicitly import the java.lang.* package since it gets imported implicitly by javac (t/f) * will the following code compile and run import java.lang.*; import java.lang.*; class Test { public static void main(String[] args) { TestClass tc = new TestClass("Hello!"); tc.out(); } } class TestClass { String name; TestClass(String s) { name = s; } protected void out() { System.out.println(name); } } a) it will compile and out put the string "Hello!" when run b) it will compile but throw a runtime exception since 'class Test' has not been declared public c) will not compile, since there is a duplicate import statement d) will compile but thrown a runtime exception since object 'tc' tries to access a method that's not public * what will happen when the following program is compiled and run public class Test { public static void main(String[] args) { new A().start(); new B().start(); } } class A extends Thread { public void run() { int i=10/0; } } class B extends Thread { public void run() { while(true) { System.out.println("Running..."); } } } a) will print the string "Running..." forever b) the process will terminate when a divide by zero exception is raised by one of the threads c) will not compile * what will happen when the following program is compiled and run public class Test { public static void main(String[] args) { String s = "Hello!"; s = "Goodbye"; System.out.println(s); } } a will not compile, strings are immutable so they cannot be assigned a 2nd string b will compile and output the string "Hello!" when run c will compile and output the string "Goodbye" when run