Java Certification Questions 1.
Which of these is the correct declaration for main? A.public static void main(); B.public void static main(String []args); C.public static void main(String args); D.public static void main(String args[]); 2. Which code fragments would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked? What is wrong with the code fragments that are incorrect and what errors do they produce? E.int count = args.length; F.int count = args.length - 1; G.int count = 0; while (args[count] != null) count ++; H.int count=0; while (!(args[count].equals(""))) count ++; 3. Represent the value of -192 in binary format, outlining the steps. 4. What is the value of -7%-2? I.1 J.-1 K.0 5. What is the value of 8.7% 2.3 ? L.1.6 M.1.8 N.1.1 O.6.4 6. What will be the resulting types of the variables in the following operations: P.a=x+y x char, y char Q.a=x+y x long, y int R.a=x/y x byte, y char S.a=x%y x int, y char T.a=x&y x byte, y short 7. Which of the following operations are legal in Java? x,y are both boolean U.f(x==y); V.x=x-y; W.x=x&&y; X.x=~y; Y.x^=y; 8. Which of the following expressions are legal and which are not? Give your reasons in each case. Z.byte B=(byte)x; x boolean AA.x=(char)(x-y); x char BB.f(x&&y); x char CC.x=(char)(x>>1); x char DD.boolean b=(boolean) x; x char 9. Which of the following expressions are legal and which are not? Give your reasons in each case. EE.x=(byte)(x % y); x byte FF.x=(byte)(x>>1); x byte GG.x&=y; HH.boolean b=(boolean)x; x byte 10. Which of the following expressions are legal and which are not? Give your reasons in each case.
II.x=x+y; x,y float JJ.f(x20)) { System.out.println(s); } OO.if (s!=null) && (s.length()>20)) { System.out.println(s); } 12. Which of the following code fragments are legal and which are not? Explain. PP.package sportinggoods; class Ski { void applyWax() {...} } package sportinggoods; class DownhillSki extends Ski { private applyWax() {...} } QQ.package Transport; class Vehicle { protected void Drive() {...} } package Transport; class Car extends Vehicle { public void Drive() {...} } RR.In same file: class Vehicle { public void Drive() {...} } class Car extends Vehicle { private void Drive() {...} } SS.In same file: class Ski { private void applyWax() {...} } class DownhillSki extends Ski { void applyWax() {...} } 13. Which of the following code fragments are legal and which are not? Explain. TT.public final class FootballGame { void score() {...} } class AmericanFootballGame extends FootballGame{ void score() {...} } UU.public class FootballGame { final Ball b=new Ball("Adidas");
void test() { b.diameter=10; } } VV.public class FootballGame { final Ball b=new Ball("Adidas"); void kick() { b=new Ball("Nike"); } } WW.public class FootballGame { final void score() {...} } class AmericanFootballGame extends FootballGame { void score() {...} } 14. Which of the following code fragments are legal and which are not? Explain. XX.public class SportsTournament { abstract void finalgame() {} void kickoffgame() {...} } class WorldCup extends SportsTournament { void finalgame() {...} } YY.abstract class Animal { public abstract void travel() {} public abstract void feed() {} } class Mammal extends Animal { void travel() {...} } ZZ.interface Animal { void travel() {} void feed() {} } class Mammal implements Animal { void travel() {} } AAA.abstract public class Animal { abstract void travel() {} abstract void feed() {} } class Mammal extends Animal { void travel() {...} void feed() {...} } (2 things wrong with this !!) 15. Which of the following code fragments are legal and which are not? Explain. BBB.class FootballTeam { int keeper=1; static int centerforward=9; public static void main(String args[]) { keeper=2;
centerforward=10; } CCC.class FootballTeam { int keeper=1; static int centerforward=9; public static void main(String args[]) { this.keeper=2; centerforward=10; } DDD.import java.awt.*; public class Screen extends Frame { Screen() { setSize(400,300); } public static void main(String args[]) { Screen theScreen=new Screen(); theScreen.setVisible(true); } } EEE.class Rodent { static void scavenge() {...} } class Rat extends Rodent { void scavenge() {...} } FFF.class Store { void countItems() {...} static void main(String args[]) { countItems(); } 16. Which of the following code fragments are legal and which are not? Explain. GGG.float f=1.0; int i=1; byte b=i+f; HHH.int i=5; long l=7; float f=i*l; III.int i=5; void calculate(float f) {...} calculate(i); JJJ.byte b; char c; c='k'; b=c; KKK.int i=1; boolean negate(boolean b) {...} negate(i); 17. Which of the following code fragments are legal and which are not? Explain. LLL.double d; short s; d=1.456; s=d; MMM.double d=1.78; float exponentiate(float f) {...} exponentiate(d);
NNN.boolean B=true; int i=1; B=i; OOO.char c='6'; int i=9; int add(int x,int y); add(c,i); PPP.boolean b=false; int i=0; b=(boolean)i; 18. What will be the result of the following code fragment: short s=517; byte b=(byte)s; long l=233; int i=(int) l; System.out.println("b=" + b + ";i=" + i); QQQ.b=517;i=233 RRR.b=517;i=0 SSS.b=5;i=0 TTT.b=5;i=233 19. Which of the following code fragments are legal and which are not? Explain. UUU.class Fruit { public Fruit() {...} } public class Orange extends Fruit { public Orange() {...} public static void main(String []args){ Fruit f=new Fruit(); Orange o=f; } } VVV.class Fruit { public Fruit() {...} } public class Orange extends Fruit { public Orange() {...} public static void main(String []args){ Orange o=new Orange(); Fruit f=o; } } WWW.interface Fruit { public Fruit(); } public class Apple implements Fruit { public Apple() {...} public static void main(String []args){ Fruit f=new Fruit(); Apple a=f; } } XXX.interface Fruit { public Fruit(); } public class Apple implements Fruit { public Apple() {...}
public static void main(String []args){ Apple a=new Apple(); Fruit f=a; } } YYY.interface Fruit { public Fruit(); } class Apple implements Fruit { public Apple() {...} } class Orange implements Fruit { public Orange() {...} } public class MyFruit { public static void main(String []args){ Orange o=new Orange(); Fruit f=o; Apple a=f; } } 20. Which of the following code fragments are legal and which are not? Explain. ZZZ.int i; for (i=5,int j=10; i<10;j--) { } AAAA.int i,j; for (i=0,j=10;i<10, j>0;i++,j--) { } BBBB.int i,k; for (i=0,k=9;(i<10 && k>0);i++,j--) { } CCCC.int i,j; for (i=0;j=10;i<10;i++,j--) { } 21. Which of the following code fragments are legal and which are not? Explain. 1. float x; ... switch(x) { case 1: System.out.println("Got a 1"); break; case 2: case 3: System.out.println("Got a 2 or 3"); break; default: System.out.println("Got somthing other than 1,2, or 3"); break; } 2. long y; ... switch(y) { case 1: System.out.println("Got a 1"); break; case 2: case 3: System.out.println("Got a 2 or 3"); break; default:
System.out.println("Got somthing other than 1,2, break; } 3. byte x; ... switch(x) { case 1: System.out.println("Got a 1"); break; case 2: case 3: System.out.println("Got a 2 or 3"); break; default: System.out.println("Got somthing other than 1,2, break; } 4. int x=1; int y=2; int z=3; int c; ... switch(c) { case x: System.out.println("Got a 1"); break; case y: case z: System.out.println("Got a 2 or 3"); break; default: System.out.println("Got somthing other than 1,2, break; } 5. short x; ... switch(x) { case 3/3: System.out.println("Got a 1"); break; case 2: case 2+1: System.out.println("Got a 2 or 3"); break; default: System.out.println("Got somthing other than 1,2, break; } 22. Which of the following code fragments are legal and which are not? Explain. 1. public static void g() try { f(); }catch(Exception e) { System.out.println("Caught in g()"); throw new Exception("thrown from g()"); }
or 3");
or 3");
or 3");
or 3");
2.
public static void g() try { f(); }catch(Exception e) { System.out.println("Caught in g()"); throw new NullPointerException("thrown from g()"); } 3. public static void g() throws Throwable{ try { f(); }catch(Exception e) { System.out.println("Inside g()"); throw e.fillInStackTrace(); } public static void main(String []args) { try { g(); } catch(Exception e) { System.out.println("Caught in main"); e.printStackTrace(); } } 23. What is the output for this code fragment? public class Rethrow { public static void g() throws Exception { System.out.println("Originates from g()"); throw new Exception("thrown from g()"); } public static void main(String []args) { try { g(); }catch(Exception e) { System.out.println("Caught in main"); e.printStackTrace(); throw new NullPointerException("from main"); } } 1. Originates from g() Caught in main java.lang.Exception: thrown from g() at Rethrow.g(Rethrow.java:5) at Rethrow.main(Rethrow.java:10) 2.
3.
4.
Caught in main java.lang.Exception: thrown from g() at Rethrow.g(Rethrow.java:5) at Rethrow.main(Rethrow.java:10) Originates from g() Caught in main java.lang.Exception: thrown from g() at Rethrow.g(Rethrow.java:5) at Rethrow.main(Rethrow.java:10) java.lang.NullPointerException: from main at Rethrow.main(Rethrow.java: 15) Originates from g() Caught in main
thrown from g() at Rethrow.g(Rethrow.java:5) at Rethrow.main(Rethrow.java:10) from main at Rethrow.main(Rethrow.java: 15) 24. Which of the following code fragments are legal and which are not? Explain. 1. abstract class Shape { Shape() throws ZeroSizeException {} abstract void draw() throws ZeroSizeException; } class Circle { void draw() throws ZeroSizeException {..} void getCircumference() throws ZeroSizeException, NegativeRadiusException{..} 2. abstract class Shape { Shape() throws ZeroSizeException {} abstract void draw() throws ZeroSizeException; } class Circle { void draw() {..} void getCircumference() throws ZeroSizeException, NegativeRadiusException {..} } 3. abstract class Shape { Shape() throws ZeroSizeException {} abstract void draw() throws ZeroSizeException; } class Circle { void draw() throws ZeroSizeException, NegativeRadiusException {..} } 4. class Mammal { Mammal() throws ColdBloodedException{} void GiveBirth() {} } interface CanFly { void fly(); } class Bat extends Mammal implements CanFly { Bat() throws ColdBloodedException, NoWingsException {} void GiveBirth() {...} } 5. class Mammal { Mammal() throws ColdBloodedException{} void GiveBirth() {} } interface CanFly { void fly(); } class Bat extends Mammal implements CanFly { Bat() throws NoWingsException {} void GiveBirth() {...} void fly() {...} } 6. class ColdBloodedException extends Exception {} class LaysEggsException extends Exception {}
class Mammal { Mammal() throws ColdBloodedException, LaysEggsException{...} void GiveBirth() {} } class VariableBodyTemperature extends ColdBloodedException{} interface CanFly { void fly(); } class Bat extends Mammal implements CanFly { Bat() throws VariableBodyTemperature, NoWingsException {} void GiveBirth() {...} void fly() {...} } 25. Which of the following code fragments are legal and which are not? Explain. public class BaseClass { BaseClass() {...} public void method() throws IOException { ... } } 1. public class CaseOne extends BaseClass { CaseOne() {...} public void method() throws IOException { ... } } 2. public class CaseTwo extends BaseClass { CaseTwo() {...} public void method() { ... } } 3. public class CaseThree extends BaseClass { CaseThree() {...} public void method() throws EOFException,MalformedURLException{ ... } } 4. public class CaseFour extends BaseClass { CaseFour() {...} public void method() throws IOException, IllegalAccessException{ ... } } 5. public class CaseFive extends BaseClass { CaseFive() {...} public void method() throws Exception { ... } } 26. Which of the following code fragments are legal and which are not? Explain. 1. public class StockServer { public StockServer(String company, int Shares,double currentPrice, double cashOnHand) { ... }
public double buy(int numberOfShares, double pricePerShare) { ... } public float buy(int numberOfShares, double pricePerShare) { ... } } 2. public class StockServer { public StockServer(String company, int Shares,double currentPrice, double cashOnHand) { ... } public double buy(int numberOfShares, double pricePerShare) { ... } public float buy(long numberOfShares, double pricePerShare) { ... } } 3. public class StockServer { public StockServer(String company, int Shares,double currentPrice, double cashOnHand) { ... } public double buy(int numberOfShares, double pricePerShare) { ... } public double buy(int numberOfShares, float pricePerShare) { ... } } 4. public class StockServer { public StockServer(String company, int Shares,double currentPrice, double cashOnHand) { ... } public double buy(int numberOfShares, double pricePerShare) { ... } public double buy(double pricePerShare, int numberOfShares) { ... } } 27. For each of the following code fragments, pleae indicate which has an overriden vs. overloaded method and explain why. 1. abstract class Shape { public Shape (); void draw(); } class Circle extends Shape { public Circle() { ...} void draw(double x,double y, double radius) {...} } 2. abstract class Shape { public Shape (); void draw();
} class Circle extends Shape { public Circle() { ...} void draw() {...} } 3. abstract class Mammal { public Mammal(); Mammal giveBirth(); } class Dog extends Mammal { public Dog () {...} Dog giveBirth() {...} } 4. abstract class Mammal { public Mammal(); Mammal giveBirth(); } class Dog extends Mammal { public Dog () {...} Dog giveBirth(int no_of_pups) {...} } 28. Which of the following code fragments are legal and which are not? Explain. 1. class MusicWork { MusicWork(String s) { System.out.println("The name of this work is" + s); } class ClassicalWork extends MusicWork { ClassicWork(String s, String composer) { System.out.println("The composer is " + composer); } 2. class MusicWork { MusicWork(String s) { System.out.println("The name of this work is" + s); } class ClassicalWork extends MusicWork { ClassicWork(String s, String composer) { super(s); System.out.println("The composer is " + composer); } 3. class MusicWork { MusicWork(String s) { System.out.println("The name of this work is" + s); } class ClassicalWork extends MusicWork { ClassicWork(String s, String composer) { System.out.println("This is a work of classical music"); System.out.println("The composer is " + composer); super(s); } 4. class MusicWork { MusicWork() { System.out.println("This is a work of music"); } MusicWork(String name) { this(); System.out.println("The name of this work is" + name);
} 5.
class MusicWork { MusicWork() { System.out.println("This is a work of music"); } MusicWork(String name) { this(); System.out.println("The name of this work is" + name); } MusicWork(String composer) { this(); System.out.println("The composer of this work is" + composer); } 6. class MusicWork { MusicWork() { System.out.println("This is a work of music"); } MusicWork(String name) { System.out.println("The name of this work is" + name); this(); } 29. If you create a non-default derived constructor and don't call the base class constructor will the compiler call the default base class constructor automatically? (Assume that the default constructor is defined for the base class). What about if it is not defined? What about the case of a default derived constructor, does the compiler call the default base class constructor (Eckel Chap 6). 30. Which of the following code fragments are legal and which are not? Explain. 1. public class Outer { String a; public class Inner { String b; public void InnerMethod() { System.out.println("Enclosing a is " + a); System.out.println("y i s " + y); } } public void CreateInner() { Inner i=new Inner(); i.InnerMethod(); } } 2. public class Outer { String a; public class Inner { String b; public void InnerMethod() { System.out.println("Enclosing a is " + a); System.out.println("b is " + b); } } public void CreateInner() { Outer.Inner i=new Outer.Inner(); i.InnerMethod(); } }
3.
public class Outer { String a; int k=1; public static class Inner { String b; public void InnerMethod() { System.out.println("Enclosing a is " + a); System.out.println("b i s " + b); } } public void CreateInner() { Outer.Inner i=new Outer.Inner(); i.innerMethod(); System.out.println("This is the value of k: " + k); } } 4. public class Outer { static String a; static int k; public Outer { k++; } public class Inner { String b; public void InnerMethod() { System.out.println("Enclosing a is " + a); System.out.println("b is " + b); } public void CreateInner() { Outer.Inner i=new Outer.Inner(); i.InnerMethod(); System.out.println("This is the instance no: " + k); } } } 31. Which of the following code fragments are legal and which are not? Explain. public class Outer { public static void main(){ ... } public void go(int w,final int z) { int p=w-z; final int q=w+z; } class Inner { public void method { 1. System.out.println("w=" +w); 2. System.out.println("z=" +z); 3. System.out.println("p=" +p); 4. System.out.println("q=" +q); } } Inner that=new Inner(); that.method; } }
32. Which of the following code fragments are legal and which are not? Explain. 1. public class NewThread extends Thread { public void run() { ... wait(); ... } } 2. public class SomeStuff { public void run() { ... suspend(); ... } } 3. public class SomeStuff { public void run() { ... Thread.suspend(); ... } } 4. public void DoStuff { public void run() { ... Thread.yield(); } } 5. public class NewThread extends Thread { public void run() { ... sleep(100); ... } } 33. Which of the following code fragments are legal and which are not? Explain. 1. public void doMath { double pi=3.1415926; Math mObj=new Math(); mobj.sin(pi); } 2. public void getMax { java.Math.max(2,2.1); } 3. public void getTan { double pi=3.1415926; java.Math.tan(pi); } 34. Which of the following code fragments are legal and which are not? Explain. 1. Character c= new Character("x"); 2. int primitive=1234; Integer wrappedInt=new Integer(primitive); 3. int primitiveInt=123; Float wrappedFloat=new Float(primitiveInt);
4.
Vector v=new Vector(); for (int i=0;i<10;i++) v[i]=i; 5. Long wLong=new Long("here"); 6. Boolean wBoolean=new Boolean("junk"); 35. Given the following code fragments, which of the following a,b,c,d are true? 1. String s1="Compare me"; String s2="Compare me"; if (s1.equals(s2)){ System.out.println("Success"); } else { System.out.println("Failure"); } 2.
36.
37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.
String s1="Compare me"; String s2="Compare me"; if (s1==s2){ System.out.println("Success"); } else { System.out.println("Failure"); } a) Both I and II print Failure b) Both I and II print Success c) I print Success, II prints Failure d) I prints Failure, II prints Success Given the following code fragments, which of the following a,b,c,d are true? 1. String s1=new String("Compare me"); String s2=new String("Compare me"); if (s1.equals(s2)){ System.out.println("Success"); } else { System.out.println("Failure"); 2. String s1=new String("Compare me"); String s2=new String("Compare me"); if (s1==s2){ System.out.println("Success"); } else { System.out.println("Failure"); } a) Both I and II print Failure b) Both I and II print Success c) I print Success, II prints Failure d) I prints Failure, II prints Success List the 6(8) component methods you should know for the exam, and explain what they do. Is a CheckBoxGroup a component? If not, what is it? List the 4 elements that can populate a menu. Which component is the only one allowed to contain a menu bar? For a newly constructed frame, list the methods to be called to make it visible, in order. List the 4 non-superclass container classes. List the steps needed to create a menu bar containing a pull-down menu in order. If the foreground and background color of a component is not set, what colors does it take on? List the 11 visual components there are. Explain how fonts are displayed in TextField and TextArea. How do you release the non-memry resources of a frame? Draw the inheritance diagram for Applet, Frame and Panel inheriting from Container. What are the default Layout Managers for
50. 51. 52.
53. 54. 55. 56.
1. Applet 2. Panel 3. Frame ? Does calling setBounds() have any effect on a component? Give a reason why or why not. Define the behavior of each of the Layout maangers : FlowLayout, GridLayout and BorderLayout wrt. the preferred size of a component. Define the interface for the constructor of each of these Layout managers: 1. FlowLayout 2. GridLayout 3. BorderLayout Define the strategy for adding a listener to a component and give an example. Define the strategy for explicitly enabling events for a component and give an example. State the circumstances under which the GUI thread spontaneously calls paint(), with no impetus from the program. Why is there a need for the repaint(0 method and what does it do?
Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.
. . . . . . . . . . . . . . . . . . . . . Exceptions 1. The need for an exception specification when an exception is thrown from a method, even within the confines of a try{...}catch block. 2. No need for an exception specification when the exception thrown is a RuntimeException. 3. Need to specify an exception specification for Throwable for any calling method in the call stack above a method that throws an exception object using fillInStackTrace() 23. Exceptions. Correct form of output when System.out.println(), e.printStackTrace() is called, also if a Runtime Exception gets all the way to main without being called, printStackTrace() is called for that exception as the program exits. 24. Exception Restrictions 1. Method in derived class not in base class that throws exception. 2. Method in derived class choosing not to throw any exceptions, even if base class version does 3. Derived method in derived class/interface trying to add to exception interface from that of base class. 4. Constructor in derived class/interface throwing additional exceptions to that in the base class.
5.
25. 26. 27.
28. 29. 30.
31.
32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56.
Constructor in derived class/interface throwing additional exceptions to that in the base class, but not declaring base-class exceptions in exception specification. 6. Derived Class Method that uses an exception derived from that used by the base-class version. 7. If you're dealing with a derived class object, which exceptions are you forced to catch? What if the derived class object is upcast to the base type? Exceptions Objects and Classes The return type of an method is not sufficinet enough to guarantee overloading. The arguments must be of different type/order. Objects and Classes. An overloaded method must be strict. For a subclass method that overrides a method in its parent class, is it legal to return a type which is the subtype of the class that is returned by the overriden method in the parent class? Objects and Classes Constructors - non-default derived class constructors. Objects and Classes Constructors - non-default derived class constructors. Inner classes 1. this reference and the construction of inner classes. 2. "" 3. Accessibility of enclosing class variables from within static inner class. 4. "" Inner classes and the final keyword. 1. Formal parameter, non-final 2. Formal parameter, final 3. Local variable, non-final 4. Local variable, final Threads java.lang.Math class java.lang wrapper classes. ... .. Components " " " " " " " " " " Layout Managers " " " " Events " Painting "
Oleg Melnikov Questions 1.
Given the following definition: String s = null; Which code fragment will cause an object of type NullPointerException to be constructed: A.[ ] if ((s != null) & (s.length)>0)); B.[ ] if ((s != null) && (s.length)>0)); C.[ ] if ((s != null) | (s.length)>0)); D.[ ] if ((s != null) || (s.length)>0));
In Heller's book, it states that for object reference casting, the new typemust be a superclass/interface of the class being converted, otherwise a runtime class cast exception will result. But consider the following code fragment: abstract class Shape{ void draw(); ... } class Circle extends Shape { public Circle() {...} void draw() {...} } class Square extends Shape { public Square() {...} void draw() {...} } class Triangle extends Shape { public Triangle() {...} void draw() {...} } public class MyShapes{ public static Shape randShape() { switch((int) (Math.random()*3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); public static void main(String[] args) Shape[] s=new Shape[5]; for (int i=0;i<5;i++)
Questions for the Discussion Group 1.
2.
Inner Classes: I am trying to figure out the distinction between having a static inner class and having an inner class that has static methods. I know that for a static inner class, its methods can be called without any reference to an enclosing class handle. But is it possible to have a non-static inner class that has a static method defined? If so, what does this mean? That there is no need for an inner class object? Constructors: Suppose I define a base class with 2 constructors, one a default constructor with no arguments and one with an argument list as in: class BaseClass { public BaseClass () { System.out.println("Default constructor"); } public BaseClass(String s) { System.out.println("Non-default constructor"); System.out.println(s); }
} Then I define a subclass of the above class: public Subclass extends BaseClass { public Subclass(String s) { ... } My question is this: Am I compelled to make the call to super(s) in the above constructor for SubClass(String s)? If not, and I do not make the call, will the compiler on its own, call the default base constructor super() for me, since I have defined it?