100% found this document useful (1 vote)
81 views19 pages

Java Certification Questions Overview

The document contains 20 multiple choice questions about Java concepts such as data types, operators, inheritance, polymorphism, and exceptions. The questions cover a wide range of fundamental and advanced Java topics to test certification level knowledge.

Uploaded by

LucianoCanonico
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
81 views19 pages

Java Certification Questions Overview

The document contains 20 multiple choice questions about Java concepts such as data types, operators, inheritance, polymorphism, and exceptions. The questions cover a wide range of fundamental and advanced Java topics to test certification level knowledge.

Uploaded by

LucianoCanonico
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
  • Java Certification Questions
  • Advanced Java Topics

Java Certification Questions from Apollo

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 = [Link];
F. int count = [Link] - 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(x<y); x,y float
KK. x=x&y; x,y float
LL. x=x<<1; x,y float
MM. byte z=(byte)x - y; x,y byte
11. What is the difference between the following 2 fragments of code and which couldcause a runtime
exception?
NN. if (s!=null) & ([Link]()>20)) {
[Link](s);
}
OO. if (s!=null) && ([Link]()>20)) {
[Link](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() {
[Link]=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[]) {


[Link]=2;
centerforward=10;
}
DDD. import [Link].*;
public class Screen extends Frame {
Screen() {
setSize(400,300);
}
public static void main(String args[]) {
Screen theScreen=new Screen();
[Link](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;
[Link]("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:
[Link]("Got a 1");
break;
case 2:
case 3:
[Link]("Got a 2 or 3");
break;
default:
[Link]("Got somthing other than 1,2, or 3");
break;
}
2. long y;
...
switch(y) {
case 1:
[Link]("Got a 1");
break;
case 2:
case 3:
[Link]("Got a 2 or 3");
break;
default:
[Link]("Got somthing other than 1,2, or 3");
break;
}
3. byte x;
...
switch(x) {
case 1:
[Link]("Got a 1");
break;
case 2:
case 3:
[Link]("Got a 2 or 3");
break;
default:
[Link]("Got somthing other than 1,2, or 3");
break;
}
4. int x=1;
int y=2;
int z=3;
int c;
...
switch(c) {
case x:
[Link]("Got a 1");
break;
case y:
case z:
[Link]("Got a 2 or 3");
break;
default:
[Link]("Got somthing other than 1,2, or 3");
break;
}
5. short x;
...
switch(x) {
case 3/3:
[Link]("Got a 1");
break;
case 2:
case 2+1:
[Link]("Got a 2 or 3");
break;
default:
[Link]("Got somthing other than 1,2, or 3");
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) {
[Link]("Caught in g()");
throw new Exception("thrown from g()");
}
2. public static void g()
try {
f();
}catch(Exception e) {
[Link]("Caught in g()");
throw new NullPointerException("thrown from g()");
}
3. public static void g() throws Throwable{
try {
f();
}catch(Exception e) {
[Link]("Inside g()");
throw [Link]();
}
public static void main(String []args) {
try {
g();
} catch(Exception e) {
[Link]("Caught in main");
[Link]();
}
}
23. What is the output for this code fragment?
public class Rethrow {
public static void g() throws Exception {
[Link]("Originates from g()");
throw new Exception("thrown from g()");
}
public static void main(String []args) {
try {
g();
}catch(Exception e) {
[Link]("Caught in main");
[Link]();
throw new NullPointerException("from main");
}
}
1. Originates from g()
Caught in main
[Link]: thrown from g()
at Rethrow.g([Link])
at [Link]([Link])

2. Caught in main
[Link]: thrown from g()
at Rethrow.g([Link])
at [Link]([Link])
3. Originates from g()
Caught in main
[Link]: thrown from g()
at Rethrow.g([Link])
at [Link]([Link])
[Link]: from main
at [Link]([Link]: 15)
4. Originates from g()
Caught in main
thrown from g()
at Rethrow.g([Link])
at [Link]([Link])
from main
at [Link]([Link]: 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) {
[Link]("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
[Link]("The composer is " + composer);
}
2. class MusicWork {
MusicWork(String s) {
[Link]("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
super(s);
[Link]("The composer is " + composer);
}
3. class MusicWork {
MusicWork(String s) {
[Link]("The name of this work is" + s);
}
class ClassicalWork extends MusicWork {
ClassicWork(String s, String composer) {
[Link]("This is a work of classical music");
[Link]("The composer is " + composer);
super(s);
}
4. class MusicWork {
MusicWork() {
[Link]("This is a work of music");
}
MusicWork(String name) {
this();
[Link]("The name of this work is" + name);
}

5. class MusicWork {
MusicWork() {
[Link]("This is a work of music");
}
MusicWork(String name) {
this();
[Link]("The name of this work is" + name);
}
MusicWork(String composer) {
this();
[Link]("The composer of this work is" + composer);
}
6. class MusicWork {
MusicWork() {
[Link]("This is a work of music");
}
MusicWork(String name) {
[Link]("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() {
[Link]("Enclosing a is " + a);
[Link]("y i s " + y);
}
}
public void CreateInner() {
Inner i=new Inner();
[Link]();
}
}
2. public class Outer {
String a;
public class Inner {
String b;
public void InnerMethod() {
[Link]("Enclosing a is " + a);
[Link]("b is " + b);
}
}
public void CreateInner() {
[Link] i=new [Link]();
[Link]();
}
}
3. public class Outer {
String a;
int k=1;
public static class Inner {
String b;
public void InnerMethod() {
[Link]("Enclosing a is " + a);
[Link]("b i s " + b);
}
}
public void CreateInner() {
[Link] i=new [Link]();
[Link]();
[Link]("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() {
[Link]("Enclosing a is " + a);
[Link]("b is " + b);
}
public void CreateInner() {
[Link] i=new [Link]();
[Link]();
[Link]("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. [Link]("w=" +w);
2. [Link]("z=" +z);
3. [Link]("p=" +p);
4. [Link]("q=" +q);
}
}
Inner that=new Inner();
[Link];
}
}
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() {
...
[Link]();
...
}
}
4. public void DoStuff {
public void run() {
...
[Link]();
}
}
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();
[Link](pi);
}
2. public void getMax {
[Link](2,2.1);
}
3. public void getTan {
double pi=3.1415926;
[Link](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 ([Link](s2)){
[Link]("Success");
} else {
[Link]("Failure");
}

2. String s1="Compare me";


String s2="Compare me";
if (s1==s2){
[Link]("Success");
} else {
[Link]("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
36. 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 ([Link](s2)){
[Link]("Success");
} else {
[Link]("Failure");
2. String s1=new String("Compare me");
String s2=new String("Compare me");
if (s1==s2){
[Link]("Success");
} else {
[Link]("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
37. List the 6(8) component methods you should know for the exam, and explain what they do.
38. Is a CheckBoxGroup a component? If not, what is it?
39. List the 4 elements that can populate a menu.
40. Which component is the only one allowed to contain a menu bar?
41. For a newly constructed frame, list the methods to be called to make it visible, in order.
42. List the 4 non-superclass container classes.
43. List the steps needed to create a menu bar containing a pull-down menu in order.
44. If the foreground and background color of a component is not set, what colors does it take on?
45. List the 11 visual components there are.
46. Explain how fonts are displayed in TextField and TextArea.
47. How do you release the non-memry resources of a frame?
48. Draw the inheritance diagram for Applet, Frame and Panel inheriting from Container.
49. What are the default Layout Managers for
1. Applet
2. Panel
3. Frame ?
50. Does calling setBounds() have any effect on a component? Give a reason why or why not.
51. Define the behavior of each of the Layout maangers : FlowLayout, GridLayout and BorderLayout wrt. the
preferred size of a component.
52. Define the interface for the constructor of each of these Layout managers:
1. FlowLayout
2. GridLayout
3. BorderLayout
53. Define the strategy for adding a listener to a component and give an example.
54. Define the strategy for explicitly enabling events for a component and give an example.
55. State the circumstances under which the GUI thread spontaneously calls paint(), with no impetus from the
program.
56. 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 [Link](), [Link]() 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. [Link] class
34. [Link] 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) & ([Link])>0));
B. [ ] if ((s != null) && ([Link])>0));
C. [ ] if ((s != null) | ([Link])>0));
D. [ ] if ((s != null) || ([Link])>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) ([Link]()*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. 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?
2. 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 () {
[Link]("Default constructor");
}
public BaseClass(String s) {
[Link]("Non-default constructor");
[Link](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?

Common questions

Powered by AI

Access modifiers like 'private', 'protected', and 'public' control the visibility and inheritance of class members in Java. A 'private' method in a superclass cannot be overridden by a subclass, making 'applyWax()' in 'DownhillSki' inaccessible if originally private in 'Ski' . However, 'protected' and 'public' methods can be overridden with some restrictions, as defined in . Visibilities must not be reduced in overriding methods, thus a 'public' method cannot legally be overridden to 'private' .

Runtime errors like NullPointerException dramatically disrupt Java program flow by halting execution unless pre-meditated via try-catch blocks. Such exceptions encapsulate bugs where reference assumptions fail, especially when null object methods are invoked, disrupting expected behaviors critically without prior null checks. Robust exception handling anticipates these by employing strategic catches allowing cleaner exits, debug patterns, or contingency logic rectifying runtime indeterminacies gleamed from logic loopholes. This exemplifies the necessity of validating data flow pathways continuously within Java infrastructures to mitigate adverse outcomes .

In Java GUI applications, repainting is triggered by changes in component display states, window changes, or programmatic requests implementing 'repaint()'. Layout managers (FlowLayout, GridLayout, BorderLayout) govern such repainting efficiently by influencing component sizes and placements based on preferred sizes, which direct resolutions of visibility and layout rendering. FlowLayout dictates sequential flow flexibility allowing components to wrap around as space dictates, GridLayout ensues equivalent division, and BorderLayout arranges components relative to directional location commands. These facilitative strategies leverage painting to ensure aesthetic consistency, display responsiveness, and usability .

Java packages help modularize code, manage namespaces, and control accessibility through organized, isolated class groupings. The 'Ski' package, for example, might contain skiing-related classes confining their behaviors logically, while the 'Transport' package isolates vehicle classes, preventing naming conflicts. Packages influence hierarchy structures by defining clearer import paths and access levels, such as higher modular access through 'protected' methods in a shared package context ('applyWax' in 'sportinggoods' system showing diverse method access). Such organizational structure assists in managing large projects with clear boundaries and developer collaboration scopes .

The 'final' keyword in Java disables inheritance and modification at different levels. When applied to a class, such as 'FootballGame', it prohibits any subclass from extending it . For methods, 'final' prevents overriding, preserving method behavior integrity across inheritance trees . For instance, 'public final void score()' in 'FootballGame' stops 'score()' from being overridden by any subclass method in 'AmericanFootballGame', thus maintaining consistent functional behavior across all derived classes.

In Java, an inner class, being non-static, retains an association with enclosing instances, inheriting access to all levels of its non-static context. Static inner classes, however, belong to the outer class type rather than objects, limiting direct non-static variable access and necessitating explicit outer instance qualifiers for such access. Static methods in inner classes, whether static or not, are usually illegal since non-static frames might lack necessary static simplification, demanding instance scope references facilitated through static outer classes. This enables method or variable scope access but cautiously manages potential static context restrictions .

In Java, arithmetic operations between differing primitives promote smaller types to the largest operand type ensuring precision and preventing data loss within the arithmetic expressions. Operations complicate when mixing those like byte, short, int, and float, defaulting two operands to at least int, if not larger upon one. This leads to implicit type conversions, often upwards but safely requiring explicit casts downwards to prevent data loss, such as 'byte b=i+f' without casting would be illegal due to 'i+f' converting to float. The implications mean developers must consciously handle data types to avoid automatic but sometimes information-losing promotions or type mismatch errors .

In Java, overloaded methods occur within the same class, differing in argument types, order, or number but not by return type alone, implying functionally similar semantics across variants. Overriding involves redefining a method from a superclass in a subclass to change or specify subclass-specific behavior while maintaining signature and return type compatibility or employing covariant return type functionality. Overloads enable the same method name to handle different data types or parameters, increasing versatility, whereas overrides ensure subclass polymorphism, tweaking superclass behavior pragmatically .

Explicitly specifying exceptions in method declarations serves as an interface contract, alerting users of possible checked exceptions despite internal handling attempts within try-catch blocks. Failing to specify these exceptions, particularly checked ones, can result in compilation errors since Java mandates awareness and handling predictability by the calling code. This contrasts with RuntimeExceptions, which do not require specification because they represent programmatic errors that shouldn't be caught directly or are anticipated (like index errors). As detailed in , explicit exception specifications are crucial in maintaining robust error handling across call stacks, especially if upper layers must accommodate lower thrown exceptions.

The '&' operator performs a bitwise AND operation on integer types and a logical AND on boolean types, evaluating both sides regardless, while '&&' is a short-circuit operator that investigates the left condition and only evaluates the right if the left is true. Using '&' for null checking causes partially constructed objects if the right-hand condition involves method calls on possibly-null references, potentially throwing a NullPointerException. For example, in "if ((s != null) & (s.length()>0))", if s is null, it explicitly evaluates s.length(), throwing a NullPointerException. '&&' avoids this by short-circuiting, thereby not attempting 's.length()' when 's != null' is false .

Java Certification Questions from Apollo
1.
Which of these is the correct declaration for main? 
A.
public static void main()
KK.x=x&y; x,y float 
LL. x=x<<1; x,y float 
MM.
byte z=(byte)x - y; x,y byte 
11. What is the difference between the followin
}
} 
VV.public class FootballGame { 
final Ball b=new Ball("Adidas");
void kick() {
b=new Ball("Nike");
}
} 
WW.
public class
CCC.
class FootballTeam {
int keeper=1; 
static int centerforward=9;
public static void main(String args[]) {
this.keeper=2;
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=
public static void main(String []args){
Apple a=new Apple();
Fruit f=a;
}
} 
YYY.
interface Fruit {
public Fruit();
}
class A
System.out.println("Got somthing other than 1,2, or 3");
break;
} 
3.
byte x; 
...
switch(x) {
case 1: 
System.out.println("G
2.
public static void g()
try {
f();
}catch(Exception e) {
System.out.println("Caught in g()");
throw new NullPointerExceptio
thrown from g()
at Rethrow.g(Rethrow.java:5)
at Rethrow.main(Rethrow.java:10)
from main
at Rethrow.main(Rethrow.java: 15) 
24
}
class VariableBodyTemperature extends ColdBloodedException{}
interface CanFly {
void fly();
}
class Bat extends Mammal impl

You might also like