Java Certification Questions
- Which of these is the correct declaration for main?
- public static void main();
- public void static main(String []args);
- public static void main(String args);
- public static void main(String args[]);
- 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?
- int count = args.length;
- int count = args.length - 1;
- int count = 0;
while (args[count] != null)
count
++;
- int count=0;
while (!(args[count].equals("")))
count ++;
- What will be the resulting types of the variables in the following
operations:
- a=x+y x char, y char
- a=x+y x long, y int
- a=x/y x byte, y char
- a=x%y x int, y char
- a=x&y x byte, y short
- Which of the following operations are legal in Java? x,y are
both boolean
- f(x==y);
- x=x-y;
- x=x&&y;
- x=~y;
- x^=y;
- Which of the following expressions are legal and which are not? Give
your reasons in each case.
- byte B=(byte)x; x boolean
- x=(char)(x-y); x char
- f(x&&y); x char
- x=(char)(x>>1); x char
- boolean b=(boolean) x; x char
- Which of the following expressions are legal and which are not? Give
your reasons in each case.
- x=(byte)(x % y); x byte
- x=(byte)(x>>1); x byte
- x&=y;
- boolean b=(boolean)x; x byte
- Which of the following expressions are legal and which are not? Give
your reasons in each case.
- x=x+y; x,y float
- f(x<y); x,y float
- x=x&y; x,y float
- x=x<<1; x,y float
- What is the difference between the following 2 fragments of code and
which couldcause a runtime exception?
- if (s!=null) & (s.length()>20)) {
System.out.println(s);
}
- if (s!=null) && (s.length()>20)) {
System.out.println(s);
}
- Which of the following code fragments are legal and which are not?
Explain.
- package sportinggoods;
class Ski {
void applyWax()
{...}
}
package sportinggoods;
class DownhillSki
extends Ski {
private applyWax() {...}
}
- package Transport;
class Vehicle {
protected void
Drive() {...}
}
package Transport;
class Car
extends Vehicle {
public void Drive() {...}
}
- In same file: class Vehicle {
public void Drive() {...}
}
class
Car extends Vehicle {
private void Drive() {...}
}
- In same file: class Ski {
private void applyWax() {...}
}
class
DownhillSki extends Ski {
void applyWax() {...}
}
- Which of the following code fragments are legal and which are not?
Explain.
- public final class FootballGame {
void score() {...}
}
class
AmericanFootballGame extends FootballGame{
void score() {...}
}
- public class FootballGame {
final Ball b=new Ball("Adidas");
void
test() {
b.diameter=10;
}
}
- public class FootballGame {
final Ball b=new Ball("Adidas");
void
kick() {
b=new Ball("Nike");
}
}
- public class FootballGame {
final void score() {...}
}
class
AmericanFootballGame extends FootballGame {
void score() {...}
}
- Which of the following code fragments are legal and which are not?
Explain.
- public class SportsTournament {
abstract void
finalgame() {}
void kickoffgame() {...}
}
class
WorldCup extends SportsTournament {
void finalgame() {...}
}
- abstract class Animal {
public abstract void travel() {}
public
abstract void feed() {}
}
class Mammal extends Animal
{
void
travel() {...}
}
- interface Animal {
void travel() {}
void feed() {}
}
class
Mammal implements Animal {
void travel() {}
}
- abstract public class Animal {
abstract void travel() {}
abstract
void feed() {}
}
class Mammal extends Animal
{
void
travel() {...}
void feed() {...}
} (2 things wrong with this
!!)
- Which of the following code fragments are legal and which are not?
Explain.
- class FootballTeam {
int keeper=1;
static int
centerforward=9;
public static void main(String
args[]) {
keeper=2;
centerforward=10;
}
- class FootballTeam {
int keeper=1;
static int
centerforward=9;
public static void main(String args[]) {
this.keeper=2;
centerforward=10;
}
- 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);
}
}
- class Rodent {
static void scavenge() {...}
}
class
Rat extends Rodent {
void scavenge() {...}
}
- class Store {
void countItems() {...}
static void
main(String args[]) {
countItems();
}
- Which of the following code fragments are legal and which are not?
Explain.
- float f=1.0;
int i=1;
byte b=i+f;
- int i=5;
long l=7;
float f=i*l;
- int i=5;
void calculate(float f) {...}
calculate(i);
- byte b;
char c;
c='k';
b=c;
- int i=1;
boolean negate(boolean b) {...}
negate(i);
- Which of the following code fragments are legal and which are not?
Explain.
- double d;
short s;
d=1.456;
s=d;
- double d=1.78;
float exponentiate(float f) {...}
exponentiate(d);
- boolean B=true;
int i=1;
B=i;
- char c='6';
int i=9;
int add(int x,int y);
add(c,i);
- boolean b=false;
int i=0;
b=(boolean)i;
- 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);
- b=517;i=233
- b=517;i=0
- b=5;i=0
- b=5;i=233
- Which of the following code fragments are legal and which are not?
Explain.
- class Fruit {
public Fruit() {...}
}
public
class Orange extends Fruit {
public Orange() {...}
public
static void main(String []args){
Fruit f=new Fruit();
Orange
o=f;
}
}
- class Fruit {
public Fruit() {...}
}
public
class Orange extends Fruit {
public Orange() {...}
public
static void main(String []args){
Orange o=new Orange();
Fruit
f=o;
}
}
- interface Fruit {
public Fruit();
}
public class
Apple implements Fruit {
public Apple() {...}
public static
void main(String []args){
Fruit f=new Fruit();
Apple a=f;
}
}
- interface Fruit {
public Fruit();
}
public class
Apple implements Fruit {
public Apple() {...}
public static
void main(String []args){
Apple a=new Apple();
Fruit f=a;
}
}
- 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;
}
}
Oleg Melnikov Questions
- Given the following definition:
String s = null;
Which code
fragment will cause an object of type NullPointerException to be
constructed:
- [ ] if ((s != null) & (s.length)>0));
- [ ] if ((s != null) && (s.length)>0));
- [ ] if ((s != null) | (s.length)>0));
- [ ] 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++)