Java Certification Questions

  1. Which of these is the correct declaration for main?
    1. public static void main();
    2. public void static main(String []args);
    3. public static void main(String args);
    4. 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?
    1. int count = args.length;
    2. int count = args.length - 1;
    3. int count = 0;
      while (args[count] != null)
      count ++;
    4. int count=0;
      while (!(args[count].equals("")))
      count ++;
  3. What will be the resulting types of the variables in the following operations:
    1. a=x+y x char, y char
    2. a=x+y x long, y int
    3. a=x/y x byte, y char
    4. a=x%y x int, y char
    5. a=x&y x byte, y short
  4. Which of the following operations are legal in Java? x,y are both boolean
    1. f(x==y);
    2. x=x-y;
    3. x=x&&y;
    4. x=~y;
    5. x^=y;
  5. Which of the following expressions are legal and which are not? Give your reasons in each case.
    1. byte B=(byte)x; x boolean
    2. x=(char)(x-y); x char
    3. f(x&&y); x char
    4. x=(char)(x>>1); x char
    5. boolean b=(boolean) x; x char
  6. Which of the following expressions are legal and which are not? Give your reasons in each case.
    1. x=(byte)(x % y); x byte
    2. x=(byte)(x>>1); x byte
    3. x&=y;
    4. boolean b=(boolean)x; x byte
  7. Which of the following expressions are legal and which are not? Give your reasons in each case.
    1. x=x+y; x,y float
    2. f(x<y); x,y float
    3. x=x&y; x,y float
    4. x=x<<1; x,y float
  8. What is the difference between the following 2 fragments of code and which couldcause a runtime exception?
    1. if (s!=null) & (s.length()>20)) {
      System.out.println(s);
      }
    2. if (s!=null) && (s.length()>20)) {
      System.out.println(s);
      }
  9. Which of the following code fragments are legal and which are not? Explain.
    1. package sportinggoods;
      class Ski {
      void applyWax() {...}
      }

      package sportinggoods;
      class DownhillSki extends Ski {
      private applyWax() {...}
      }
    2. package Transport;
      class Vehicle {
      protected void Drive() {...}
      }

      package Transport;
      class Car extends Vehicle {
      public void Drive() {...}
      }

    3. In same file: class Vehicle {
      public void Drive() {...}
      }

      class Car extends Vehicle {
      private void Drive() {...}
      }
    4. In same file: class Ski {
      private void applyWax() {...}
      }

      class DownhillSki extends Ski {
      void applyWax() {...}
      }
  10. Which of the following code fragments are legal and which are not? Explain.
    1. public final class FootballGame {
      void score() {...}
      }
      class AmericanFootballGame extends FootballGame{
      void score() {...}
      }
    2. public class FootballGame {
      final Ball b=new Ball("Adidas");
      void test() {
      b.diameter=10;
      }
      }
    3. public class FootballGame {
      final Ball b=new Ball("Adidas");
      void kick() {
      b=new Ball("Nike");
      }
      }
    4. public class FootballGame {
      final void score() {...}
      }
      class AmericanFootballGame extends FootballGame {
      void score() {...}
      }
  11. Which of the following code fragments are legal and which are not? Explain.
    1. public class SportsTournament {
      abstract void finalgame() {}
      void kickoffgame() {...}
      }
      class WorldCup extends SportsTournament {
      void finalgame() {...}
      }
    2. abstract class Animal {
      public abstract void travel() {}
      public abstract void feed() {}
      }
      class Mammal extends Animal
      {
      void travel() {...}
      }
    3. interface Animal {
      void travel() {}
      void feed() {}
      }
      class Mammal implements Animal {
      void travel() {}
      }
    4. abstract public class Animal {
      abstract void travel() {}
      abstract void feed() {}
      }
      class Mammal extends Animal
      {
      void travel() {...}
      void feed() {...}
      } (2 things wrong with this !!)
  12. Which of the following code fragments are legal and which are not? Explain.
    1. class FootballTeam {
      int keeper=1;
      static int centerforward=9;


      public static void main(String args[]) {
      keeper=2;
      centerforward=10;
      }
    2. class FootballTeam {
      int keeper=1;
      static int centerforward=9;

      public static void main(String args[]) {
      this.keeper=2;
      centerforward=10;
      }
    3. 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);
      }
      }
    4. class Rodent {
      static void scavenge() {...}
      }
      class Rat extends Rodent {
      void scavenge() {...}
      }
    5. class Store {
      void countItems() {...}
      static void main(String args[]) {
      countItems();
      }
  13. Which of the following code fragments are legal and which are not? Explain.
    1. float f=1.0;
      int i=1;
      byte b=i+f;
    2. int i=5;
      long l=7;
      float f=i*l;
    3. int i=5;
      void calculate(float f) {...}
      calculate(i);
    4. byte b;
      char c;
      c='k';
      b=c;
    5. int i=1;
      boolean negate(boolean b) {...}
      negate(i);
  14. Which of the following code fragments are legal and which are not? Explain.
    1. double d;
      short s;
      d=1.456;
      s=d;
    2. double d=1.78;
      float exponentiate(float f) {...}
      exponentiate(d);
    3. boolean B=true;
      int i=1;
      B=i;
    4. char c='6';
      int i=9;
      int add(int x,int y);
      add(c,i);
    5. boolean b=false;
      int i=0;
      b=(boolean)i;
  15. 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);
    1. b=517;i=233
    2. b=517;i=0
    3. b=5;i=0
    4. b=5;i=233
  16. Which of the following code fragments are legal and which are not? Explain.
    1. class Fruit {
      public Fruit() {...}
      }
      public class Orange extends Fruit {
      public Orange() {...}
      public static void main(String []args){
      Fruit f=new Fruit();
      Orange o=f;
      }
      }

    2. class Fruit {
      public Fruit() {...}
      }
      public class Orange extends Fruit {
      public Orange() {...}
      public static void main(String []args){
      Orange o=new Orange();
      Fruit f=o;
      }
      }
    3. interface Fruit {
      public Fruit();
      }
      public class Apple implements Fruit {
      public Apple() {...}
      public static void main(String []args){
      Fruit f=new Fruit();
      Apple a=f;
      }
      }
    4. interface Fruit {
      public Fruit();
      }
      public class Apple implements Fruit {
      public Apple() {...}
      public static void main(String []args){
      Apple a=new Apple();
      Fruit f=a;
      }
      }
    5. 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

  1. Given the following definition:
    String s = null;
    Which code fragment will cause an object of type NullPointerException to be constructed:
    1. [ ] if ((s != null) & (s.length)>0));
    2. [ ] if ((s != null) && (s.length)>0));
    3. [ ] if ((s != null) | (s.length)>0));
    4. [ ] 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++)