Sun Certification for Java2 Programming

QUESTION
what is the output of following code?
public class Test {
        public static void main(String[] args){
          int i = 4, j = 7;
          String k = "string";
          System.out.println(i+j+k+i+j);
        }
}

OPTION
a. 47sting47
b. 11string11
c. 47string11
d. 11string47
e. Non of above is correct

ANSWER
d


QUESTION
What will happen if compile and run the following code?
public class Test {
public static void main(String[] args){
    StringBuffer k = new StringBuffer("string");
    StringBuffer s = new StringBuffer("String");
      if (s.equals(k))
         System.out.println("Yes");
       else
          System.out.println("No");
       }
}

OPTION
a print out "Yes"
b print out "No"
c compile error
d run time exception
e. Non of above is correct

ANSWER
b


QUESTION
You design a GUI with lots of components whose fonts and background colours are different,
it looks very nice. Because Java is platform independent, its appearance
will be excectly the same in other platform.

OPTION
a. Yes
b. Yes, but only if use same Java version
c. No
d. No, because Java is not platform independent.

ANSWER
c


QUESTION
what is the result of the following code

long i = 1,j;
j = i<< 35;
System.out.println(j)

OPTION
a. copmpiling or run time error
b. 2 power 3 = 8
c. 2 power 5 = 32
d. 2 power 34
e. 2 power 35
f. 2 power 36

ANSWER
e


QUESTION
what will appear on screen after perform following code

public class Test{
public static void main(String[] args){
        From f = new Frame("Test");
        Button a = new Button("A");
        Button b = new Button("B");
        Button c = new Button("C");
        Button d = new Button("D");

        f.add(a);
        f.add(b);
        f.add(c);
        f.add(d);
        }
}

OPTION
a. A button label "D" in centre of a frame, occupying whole frame.
b. Four buttons label "A","B","C" and "D" lay one by one inside the frame.
c. there are four buttons on screen
d. see nothing on screen

ANSWER
d


QUESTION
what is  default layout of dialog component
 

OPTION
a. FlowLayout
b. BorderLayout
c. GridLayout
d. CardLayout
e. GridBagLayout

ANSWER
b

EXPLANATION
default layout for applet is FlowLayout
Frame has BorderLayout same as Dialog


QUESTION
int j;
for (int i =3,j=1;j++,i--;j<3)
   if (j== i) contiune;
System.out.println("i="+i+" j="+j)

what is the output of above code fragment.
 

OPTION
a. run time error or exception throw
b. compiling error
c. i = 1 j = 1
d  i = 2 j = 1
e. i = 3 j = 1
f. i = 2 j = 2
g. i = 2 j = 3
h. i = 3 j = 3

ANSWER
b

EXPLANATION
for statment initial error,
cannot mix initial and  assigment together.


QUESTION
class Outer{
        static class Inner{
       }
}

Given that in your class myApp, you need a reference to
Inner object. which of following code fragment shows the correct way to declare
and initialize a reference to a Inner object

OPTION
a. Outer.Inner myInner = new Outer.Inner()
b. Inner myInner = new Outer.Inner()
c. Outer.Inner myInner = Outer.new Inner()
d. Inner myInner =  Outer.new Inner()
f. Outer.Inner myInner = new Outer.new Inner()
e. Inner myInner = new Outer.new Inner()
g. Inner myInner = new Outer.Inner()
 

ANSWER
g


QUESTION
class Outer{
        class Inner{
        }
}

Given that in your class myApp, you need a reference to
Inner object. which of following code fragment shows the correct way to declare
and initialize a reference to a Inner object

OPTION
a. Outer.Inner myInner = new Outer.Inner()
b. Inner myInner = new Outer.Inner()
c. Outer.Inner myInner = new Outer.new Inner()
d. Inner myInner = new Outer.new Inner()
e. Outer outer = new Outer(); Outer.Inner myInner = new Outer.new Inner()
 

ANSWER
c
e


QUESTION
//file Test.java
class A implements Runnable{
        public void run(){
            i+=1;
        }
        void printi(){System.out.println("i="+i);}
        int i = 1;
}

public class Test{
 public static void main(String[] args){
                A a= new A();
                a.printi();
                Thread t = new(a);
                t.start();
                a.i +=1;
                a.print();
        }
}

compile and run above code, what will be output?

OPTION
a. compile error
b. run time error
c. i = 1, i = 1;
d. i = 1, i = 2;
h. output unpredictable

ANSWER
h

EXPLANATION
two thread shared the same data,
should use synchronized key word


QUESTION
class Parent{
        int i = 0;
        void amethod(){System.out.println("in Parent")};
}

class Child extends Parent{
        int i = 10;
        void amethod(){System.out.println("in Child")};
}

class Test{
public static void main(String[] args){
        Parent p = new Child();
        Child  c = new Child();
        System.out.print("i="+p.i+" ");
        p.amethod();
        System.out.print("i="+c.i+" ");
        c.amethod();
 }
}

what will appear in output of above code?

OPTION
a. i = 0 in Parent
b. i = 0 in Child
c. i = 10 in Parent
d. i = 10 in Child
c. compiler or run time error

ANSWER
b
d

EXPLANATION
only member method can be overriden, not member variable


QUESTION
class A{
   int j;
   A(int i){j = i;}
}

public class B extends A{
      B(int i){j=i*2;}
public static void main(String[] arges){
       A b = new B(1);
       System.out.println("b.j = "+b.j);
        }
}

what is output of above code?

OPTION
a. compile error
b. run time error
c. b.j = 1
d. b.j = 2

ANSWER
a

EXPLANATION
Deault constraction must be defined, if  subclass constraction
need to use it.


QUESTION
class Test{
public static void main(String[] arg){
        short i,j;
        i = 10;
        j= -i*10;
        System.out.println("j="+j);
       }
}

what will be output after compile and run about code.

OPTION
a. compile error
b. runtime error
c. j= -100
d. non of above is correct

ANSWER
a

EXPLANATION
in j = -i*10// "-" turn -i to be int


QUESTION
which of following statements are true. select all correct answer.
a). A private number(method or variables) only can be accessed under current instance.

b). A class number(class,method or variable) may have  one of following access modifier; public, default, protected or private.

c). The only access modifier you can put before a top level class is public.

d). Assume class C extends B, class B extends A, class A has a method action(), which is overridden by class B and class C. But inside an instance of class C, you still can get action() of class A version,by use "super.super.action()".

e). Protected feature  means it available to all classes in the same package, and available to all subclasses of the class

OPTION
a is correct
b is correct
c is correct
d is correct
e is correct
d none of them correct

ANSWER
c
e

EXPLANATION
a, private means private to class, i.e can accessed by any instance of that class
b, default is not a legal access modifier.
d, "super.super." is illegal.


QUESTION
variables a,b,c and results are type of long. what is the
best way to caculating results = a*b/c ?

OPTION
a). results = a*(b/c)
b). results = (a*b)/c
c). results = a/c*b
d). a,b,c will produce exactly the same results.

ANSWER
b

EXPLANATION
for integer 9/2 equal to 4


QUESTION
Assume a and result are float, i.e
float a, result;
after caculating as;
result = sqt(a);
How can you know that variable results is a NaN

OPTION
a). if (result >= Float.NaN)
b). if (result <= Float.NaN)
c). if (result == Flaot.NaN)
d). use try{} and catch to catch  exception
e). non of a,b,c and d are correct.

ANSWER
e

EXPLANATION
the correct  way to compare NaN is use Float.isNaN(float i)


QUESTION
which is legal?

OPTION
a.   boolean isboolean = null;
b.   char c = '\u4507';
c.   double d = 1.0/0.0;
d.   byte a = 256;
e.   String str = null;
f.   String str = "c:\";

ANSWER
b
c
e

EXPLANATION
a, boolean type only can take false or true as value
c, d= +infinity
d, out of range
f, correct is String str = "c:\\"


QUESTION
True or False;
Assign null to a reference variable which you have finished with it can improve performence.

OPTION
a. true
b. false

ANSWER
a


QUESTION
which is true?

OPTION
a). Shift operators may be applied only to oprands of either int or long
b). A array declare inside a mothed will not be initialized by system just like methed local variable.
c). JVM need Garbage Collection  because reference variables allocated on the stack while object is allocated on the heap.
d). Java has Garbage Collection ability, so software written by Java will
never have memory leaks or run out of memory problem

ANSWER
a
c

EXPLANATION
b false array always get initialzed by system
d false if you keep create object, sooner or later menory will run out.


QUESTION
class Test{
public static void main(String args[]){
        int total;
        for(int i = 0 ; i<10;){
                total += i;
                i++;
        }
        System.out.println("total = "+total+" : i = "+i);
        }
}

what is output of above code?

OPTION
a). compile error
b). run time error
c). output: total = 55 : i = 10
d). output: total = 55 : i = 9

ANSWER
a

EXPLANATION
System.out.println("total = "+total+" : i = "+i),
i out of scope


QUESTION
which of the following code is legal?

OPTION
a). int total = 0;
    for (int i=7, long j=0;i<10;j++)
        total += i;

b). int x =038
    System.out.println("x="+x);

c). public class A{
        public amethod(int i){
                public int j=0;
                j=i*2;
        }
    }

d). int x =034
    System.out.println("x="+x);

ANSWER
d


QUESTION
which of the following construction is legal

OPTION
a). RandomAccessFile("Text.txt", "rw")
b). RandomAccessFile("Text.txt", 'r')
c). RandomAccessFile("Text.txt", "r")
d). RandomAccessFile("Text.txt", "w")
e). RandomAccessFile("Text.txt", 'w')

ANSWER
a
c


QUESTION
which collections can have duplicates data

OPTION
a). Collection
b). List
c). Set
d). Map

ANSWER
a
b


QUESTION
Witch of the following statement is true.

OPTION
a). An object whose class implement Runnable interface, if you call run() in main thread instead of start(), run() will not execute.
b). The signature of main is, static public  main(String[] arge)
c). Assume a is a Array, statement a.length = 10, will get compiler error.

ANSWER
c


QUESTION
Witch of the following is legal.

OPTION
a). float a = 1;
b). float a = 1.0;
c). char a = \u0000;
d). char a = 12;
e). int i = 12; char a = i;

ANSWER
a
d

EXPLANATION
b. should be; float a = 1.0f;
c. should be; char a = '\u0000'
e. should be; char a = (char)i;


QUESTION
What is '\u0000' represented?

OPTION
a). null character
b). equivalent to decimal  0(zero)
c). space ascii
d). a,b,c and d all wrong

ANSWER
a
b


QUESTION
What is the ouput of the following code?
public class test {
    static StringBuffer str = new StringBuffer("Hello world");
    public static void amethod1(StringBuffer s){
        s = new StringBuffer("Good bye world");
}
    public static void amethod2(StringBuffer s){
         s.append(" !");
}

public static void main(String[] args){
     amethod1(str);
     System.out.println(str);
     amethod2(str);
     System.out.println(str);
}
}

OPTION
a). Hello world    Hello world !
b). Hello world   Hello world
c). Good by world   Good by world
d). Good by world   Good by world !
e). Good by world   Hello world
d). Hello world   Good by world
f). Good by world   Hello world !
g). Hello world   Good by world !

ANSWER
a


QUESTION
Assume str = "red", compile and run a program which will call the following code,
the results will be,

boolean amethod(String str){
switch (str){
    case "Red"
    case "red"
         System.out.println("It is Red!");
    case  "Blue"
    case "blue":
         System.out.println("It is Blue!");
         break;
    case "Green":
    case "green":
         System.out.println("It is Green!");
}
}

OPTION
a). output "It is Red!"
b). output "It is Blue!"
c). output "It is Green!"
d). output "It is Red! ", follow by "It is Blue!"
e). run time error.
f). compile time error.

ANSWER
f


QUESTION
Select  the best statment to replace the comment of line 1.
                try{
        // line 1
                        BufferedReader b = new BufferedReader(f);
                }
                catch(FileNotFoundException e){
                       System.out.println("FileNotFoundException "+e);
                }

OPTION
a). BufferedInputStream f = new BufferedInputStream("text.txt");
b). File f = new File("text.txt");
c). FileInputStream f = new FileInputStream("text.txt");
d). FileReader f = new FileReader("text.txt");

ANSWER
d


QUESTION
Which of the following statments is true?

OPTION
a).Java arrays may be assume as static arrays, that means size has to be specified at compile time.
b).Thread is a abstract class, as method void run() must be implmented.
c). With &,^ and | operations, the two operands may be of any types of boolean, byte, short, int and long, even mix of these types, just like +, - operatins.
d). With operations && and ||, they only can be apply to boolean operands.

ANSWER
a


QUESTION
Compile and run the following code, what will happen?
class Try{
 public static void main(String arg[]){
 int i,j,k;
  k = 2;
  for( i=0; i < k*2; j=(i++)*2)
      System.out.println("j="+j);
 }
}

OPTION
a). j=2 will appear in output
b). run time error
c). compiler error

ANSWER
c

EXPLANATION
compiler gives error message as " variable j might not have been initialized"


QUESTION
Select the best statement that can set int i to Label lab, so that lab can display variable i.
Assume;
Label lab = new Label();
int i = 100;

OPTION
a). lab.setText(i);
b). lab.setText(i.toString());
c). lab.setText(Integer.toString(i));
d). Integer ii = Integer(i); lab.setText(ii.toString());

ANSWER
c


QUESTION
What will be the output of the following code?

//file Test.java
class Parent{
 int i = 0;
        void amethod(){System.out.println("in Parent");};
}

class Child extends Parent{
 int i = 10;
 void amethod(){System.out.println("in Child");};
}

public class Test{
public static void main(String[] args){
 Parent p = new Child();
 Child  c;
        System.out.print("i="+p.i+" ");
 p.amethod();
 c = (Child)p;
        System.out.print("i="+c.i+" ");
 c.amethod();
 }
}

OPTION
a). i=10 in Child follow by i=0 in Child
b). i=10 in Child follow by i=0 in Child
c). i=0 in Child  follow by i=10 in Child
d). i=0 in Child  follow by i=0  in Child
e). Non of above is correct

ANSWER
c


QUESTION
what will be the output of the following code.

//File Test.java
class Parent{
        private void amethod(){System.out.println("in Parent");};

}
class Child extends Parent{
 void amethod(){System.out.println("in Child");};
}
public class Test{
public static void main(String[] args){
 Parent p = new Child();
 p.amethod();
 }
}

OPTION
a). in Parent will appear in output
b). in Child  will appear in output
c). run time error
d). compile error

ANSWER
d

EXPLANATION
compile error message as; amethod() has private access in Parent

Related:

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.