SCJP Mock Exam

This series of questions is intended to help you prepare for Sun Certified Java Programmer (1.4) exam. Now this practice quiz contains only 20 questions.But it will be frequently updated with more questions. I know that the exam has grown to cover new Java versions, but these questions are valid too as basics are basics and will not change.

 

SCJP mock exam sample questions and answers

You have the following code in a file called Test.java

class Base{
    public static void main(String[] args){
        System.out.println("Hello");
    }
}

public class Test extends Base{}

What will happen if you try to compile and run this?

 
 
 
 

What is the result of trying to compile and run the following code.

public final static void main(String[] args){
    double d = 10.0 / -0;
    if(d == Double.POSITIVE_INFINITY)
        System.out.println("Positive infinity");
    else
        System.out.println("Negative infinity");
}
 
 
 
 

What is the result that will be printed out ?

void aMethod(){
    float f = (1 / 4) * 10;
    int i = Math.round(f);
    System.out.println(i);
}
 
 
 
 

Which of the following are valid declarations?

Note : None of the literals used here contain the character O they are all zeroes.

 
 
 
 

What is the result of trying to compile and run this program.

public class Test{
    public static void main(String[] args){
        int[] a = {1};
        Test t = new Test();
        t.increment(a);
        System.out.println(a[a.length - 1]);
    }
    void increment(int[] i){
        i[i.length - 1]++;
    }
}
 
 
 
 

What will happen if you try to compile and run this ?

public class Test{
   static{
       print(10);
   }
   static void print(int x){
       System.out.println(x);
       System.exit(0);
   }
}
 
 
 
 

What is the result of trying to compile and run the following code.

public static void main(String[] args){
    double d = 10 / 0;
    if(d == Double.POSITIVE_INFINITY)
        System.out.println("Positive infinity");
    else
        System.out.println("Negative infinity");
}
 
 
 
 

What is the result of attempting to compile and run this

interface A{
    void aMethod();
}
public class Test implements A{
    void aMethod(){
        System.out.println("hello");
    }
public static void main(String[] args){
    Test t = new Test();
        t.aMethod();
    }
}
 
 
 
 

What is the result of attempting to compile and run this code

public class Test{
    int i;
    Test(){
        i++;
    }
    public static void main(String[] args){
        Test t = new Test();
        System.out.println("value = " t.i);
    }
}
 
 
 
 

What is the result of attempting to compile and run the following

public class Test{
    int arr[] = new int[8];
    public static void main(String[] args){
        Test t = new Test();
        System.out.println(t.arr[7]);
    }
}
 
 
 
 

What is the result of trying to compile and run this

public class Test{
    public static void main(String[] args){
        int a;
        Test t = new Test();
        for(int j=0; j < 2 ; j++){
            for(int i=0; i <4 ; i++){
               a = j;
            }
            System.out.println(i);
        }
    }
}
 
 
 
 

What is the result of attempting to compile and run this code ?

public class Test {
    public static void main(String[] args){
        int j = 0;
        for(; j < 3; j++){
            if (j==1) break out;
                System.out.print(j + "\n");
        }
        out:{System.out.println("bye");}
    }
}
 
 
 
 

What is the result of attempting to compile and run this code ?

class A extends Exception{}
class B extends A{}
class C extends B{}
public class Test {
    static void aMethod() throws C{ throw new C(); }
    public static void main(String[] args){
        int x = 10;
        try { aMethod(); }
        catch(A e) { System.out.println("Error A");}
        catch(B e) { System.out.println("Error B");}
    }
}
 
 
 
 

What is the result of trying to compile and run this?

public class Test {
    public static void main(String[] args){
        for(int i=0; i < 2; i++){
            continue;
            System.out.println("Hello world");
        }
    }
}
 
 
 
 

Consider this code.

public class Test {
    public static void main(String[] args){
        for(int j = 0; j < 1 ; j++){
            if (j < 1) continue innerLoop;
                innerLoop: 
                for(int i = 0; i < 2; i++){
                    System.out.println("Hello world");
                }
        }
    }
}

What is the result of attempting to compile and run this.

 
 
 
 

What will the output be ?

public static void main(String[] args){
    char c = '\u0042';
    switch(c) {
        default:
            System.out.println("Default");
        case 'A':
            System.out.println("A");
        case 'B':
            System.out.println("B");
        case 'C':
            System.out.println("C");
    }
}
 
 
 
 

What wil be printed out when this method runs ?

void getCount(){
    int counter = 0;
        for (int i=10; i>0; i--) {
            int j = 0;
            while (j > 10) {
                if (j > i) break;
                   counter++;
                j++;
            }
        }
    System.out.println(counter);
}
 
 
 
 

Is this code legal ?

class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
    void thrower() throws ExceptionB{
       throw new ExceptionB();
    }
    public static void main(String[] args){
        Test t = new Test();
        try{t.thrower();}
        catch(ExceptionA e) {}
        catch(ExceptionB e) {}
    }
}
 
 

Is this legal ?

class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
    void thrower() throws ExceptionA{
        throw new ExceptionA();
    }
    public static void main(String[] args){
        Test t = new Test();
        try{t.thrower();}
        catch(ExceptionB e) {}
    }
}
 
 

Is this legal ?

class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
class A{
    void thrower() throws ExceptionA{
        throw new ExceptionA();
    }
}
public class B extends A{
    void thrower() throws ExceptionB{
        throw new ExceptionB();
    }
}
 
 

Question 1 of 20

Page Visitors: 371

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently the Senior Technical Engineer at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Principal Architect to facilitate incorporating Spring as Corporate Standard in the organization.

One thought on “SCJP Mock Exam

Leave a Reply

Your email address will not be published. Required fields are marked *