Pages

Wednesday 20 February 2013

SCJP exam. The most common "Compilation fails" questions


Illegal package declaration (looks like import statement).

package com.mypackage.classes.*;  // problem

Out of scope of visibility

class OutOfScopeTest{
    static int x;
    public static void main(String[] args){
        for(int i = 0; i < 10; i++){
            x++;
        }
        System.out.println(x + i); // problem
    }
}

All local variables must be initialized before using.

class LocalVariableTest{
    public static void main(String[] args){
        Boolean b;
        if(b == null){ // problem
            b = false;
        }else{
            b = true;
        }    
    }
}

No enum declaration within any method's scope. 
Enum types must not be local.

class A {
    public static void doStuff(){
        enum Sex {MALE, FEMALE, SHEMALE}; // problem
        Sex sex = Sex.MALE;
    }
}

Non-static method cannot be referenced from a static context.

class C {
    static int x = 10;
    public static void main(String... args){
        doIt(new String(x));   // no problem     
        doIt(getString(x));   // problem  
    }
    static void doIt(String s){
        System.out.println(s);
    }
    String getString(int val){
        return "do: " + val;
    }
}

Unreachable statement

class AfterThrowTest{
    public static void main(String[] args){
        try{
            throw new IOException();
            System.out.println("After"); //problem
        }catch(Exception ex){}
    }
}

Nothing must be between try and catch blocks

class TryOrCatch {
    public void doStuff(){
        try{
            throw new Exception();
        }
        System.out.println("You lost!"); // problem
        catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println("You lost!"); // problem
        finally{
            System.exit(0);
        }
    }
}

Illegal inner class instantiation

class Outer extends Thread {
    @Override
    public void run(){
        System.out.println("Outer");
    }
    
    /** non-static regular inner class */
    class Inner extends Outer {
        @Override
        public void run(){
            System.out.println("Inner");
        }    
    }
    
    public static void main(String[] args){
        Outer ob1 = new Outer();
 
        /** static inner class instantiation for
         *  a non-static inner class */
        Outer.Inner obj2 = new Outer.Inner(); // problem
 
        /** correct non-static inner class instantiation */        
        Outer.Inner obj3 = ob1.new Inner(); // correct
        
        ob1.start();
        obj2.start();
        obj3.start();
    }
}

API mismatch between String and StringBuilder classes

class StringTest{
    public static void main(String[] args){
        String s = new String("Test");
        s.concat(" passed");
        System.out.println(s.length());
        
        StringBuilder sb = new StringBuilder("Test");
        sb.concat(s); // problem
        sb.append(s); // must be like this
        System.out.println(sb.length());
    }
}

Illegal collections class API

class Test{
    public static void main(String[] args){
        List list = new ArrayList();
        
        list.add(args[0]);
        
        System.out.println(list.size());        
        
        TreeMap tm = new TreeMap();
        tm.add("One"); //problem must be put(key, value) method

        System.out.println(tm.size());
    }
}

Wrong wrapper class chaining (API mismatch)

class IntegerTest{
    static Integer i;
    public static void main(String[] args){

        String ten = "10";

        int y = Integer.valueOf(ten).intValue();
        int x = i.parseInt(ten).intValue(); // problem
        
        System.out.println(y + x);
    }
}

Runnable is not a Thread.
To start a new thread of execution you need an instance of a Thread class.


class Runner implements Runnable{

    @Override
    public void run() {
        System.out.println("Run");
    }
    
    public static void main(String[] args){
        Runnable r = new Runner();
        r.start(); // problem
    }
}

Var-args. Ambiguous matching

class VarArgsTest{
    public static void main(String... args){
        go(new int[]{1,2,3});
        
        go(new Integer[]{1,2,3});
        go(new Integer[]{1, new Integer(2), Integer.valueOf(3)});
        
        /**Problem. Ambiguous. Both methods match */
        go(1,2,3,4,5); //problem 

        /**Problem. Ambiguous. Both methods match */
        go(new Integer(1),  
           new Integer(2), 
           Integer.valueOf(3)); 
       
        go(1,2,new int[]{3,4,5}); //problem no method matches
    }
    
    static void go(int... args){
        System.out.println("a");
    }
    
    static void go(Integer... args){
        System.out.println("b");
    }
}

and so on...

No comments:

Post a Comment