Java Certification Questions From Apollo
Java Certification Questions From Apollo
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() {...}
}
2. Caught in main
java.lang.Exception: thrown from g()
at Rethrow.g(Rethrow.java:5)
at Rethrow.main(Rethrow.java:10)
3. 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)
4. 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");
}
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. 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?
25. Exceptions
26. Objects and Classes
The return type of an method is not sufficinet enough to guarantee overloading. The arguments must be of
different type/order.
27. 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?
28. Objects and Classes
Constructors - non-default derived class constructors.
29. Objects and Classes
Constructors - non-default derived class constructors.
30. Inner classes
1. this reference and the construction of inner classes.
2. ""
3. Accessibility of enclosing class variables from within static inner class.
4. ""
31. Inner classes and the final keyword.
1. Formal parameter, non-final
2. Formal parameter, final
3. Local variable, non-final
4. Local variable, final
32. Threads
33. java.lang.Math class
34. java.lang wrapper classes.
35. ...
36. ..
37. Components
38. "
39. "
40. "
41. "
42. "
43. "
44. "
45. "
46. "
47. "
48. Layout Managers
49. "
50. "
51. "
52. "
53. Events
54. "
55. Painting
56. "
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: