SARGA

. COM

Java Certified Programmers' Examination Samples

1) Given the following class definition:

class A {
protected int i;
A(int i) {
this.i = i;
}
}

Which of the following would be a valid inner class for this class?

Select all valid answers.

a) class B {
}

b) class B extends A {
}

c) class B {
B() {
System.out.println("i = " + i);
}
}

d) class B {
class A {
}
}

e) class A {
}


2) Given the following code,

Float f = new Float(0.9F);
Float g = new Float(0.9F);
Double d = new Double(0.9);

Which of the following conditions are valid and will return true?

a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))

<details> <category> 1 <count> 5 <type> 0 <answer> CE <explain> none </details>
</question>

3) Consider the following code,

public class Test {

public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}

static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}

What will be the output?

Answer:

a. a is One
b is Two

b. a is One
b is One

c. a is One more
b is One more

d. a is One more
b is Two

e. a is One more
b is Two more

4) Abstract method cannot be final. True or False ?

a) True
b) False

5) Abstract method cannot be static. True or False ?

a) True
b) False

6) Abstract class cannot have static methods. True or False ?

a) True
b) False

7) Abstract class cannot have final methods. True or False ?

a) True
b) False

8) A final class cannot have static methods. True or False ?

a) True
b) False

9) A final class cannot have abstract methods. True or False ?

a) True
b) False

10) A sample question provided by Sun

What would be the result of attempting to compile and run the following piece of code?

public class Test {
static int x;
public static void main (String args[]) {
System.out.println("Value is " + x);
}
}

A.The output "Value is 0" is printed.

B.An object of type NullPointerException is
thrown.

C.An "illegal array declaration syntax" compiler
error occurs.

D.A "possible reference before assignment"
compiler error occurs.

E.An object of type
ArrayIndexOutOfBoundsException is thrown.

11) A sample question provided by Sun

What should you use to position a Button within an application Frame so that the size of the Button is
NOT affected by the Frame size?

A.a FlowLayout

B.a GridLayout

C.the center area of a BorderLayout

D.the East or West area of a BorderLayout

E.the North or South area of a BorderLayout

12) A sample question provided by Sun

Which is the advantage of encapsulation?

A.Only public methods are needed.

B.No exceptions need to be thrown from any
method.

C.Making the class final causes no
consequential changes to other code.

D.It changes the implementation without
changing the interface and causes no
consequential changes to other code.

E.It changes the interface without changing the
implementation and causes no consequential
changes to other code.

13) A sample question provided by Sun

What can contain objects that have a unique key field of String type, if it is required to retrieve the
objects using that key field as an index?

A.Map

B.Set

C.List

D.Collection

E.Enumeration


14) A sample question provided by Sun

Which statement is true about a non-static inner class?

A.It must implement an interface.

B.It is accessible from any other class.

C.It can only be instantiated in the enclosing
class.

D.It must be final if it is declared in a method
scope.

E.It can access private instance variables in the
enclosing object.

15) A sample question provided by Sun

Which are keywords in Java?

A.NULL

B.sizeof

C.friend

D.extends

E.synchronized

Select all valid answers.

16) A sample question provided by Sun

Which declares an abstract method in an abstract Java class?

A.public abstract method();

B.public abstract void method();

C.public void abstract Method();

D.public void method() {abstract;}

E.public abstract void method() {}

17) A sample question provided by Sun

Which code fragments would correctly identify the number of arguments passed via command line to a
Java application, excluding the name of the class that is being invoked?

A.int count = args.length;

B.int count = args.length - l;

C.int count=0; while (args [count]
!=null) count ++;

D.int count=0; while
(!(args[count].equals(""))) count ++;}

18) A sample question provided by Sun

FilterOutputStream is the parent class for BufferedOutputStream, DataOutputStream and
PrintStream. Which classes are a valid argument for the constructor of a FilterOutputStream?

A.InputStream

B.OutputStream

C.File

D.RandomAccessFile

E.StreamTokenizer

19) A sample question provided by Sun

Given the following method body:

1)
2) {
3) if (atest()) {
4) unsafe();
5) }
6) else {
7) safe();
8) }
9) }

The method "unsafe" might throw an AWTException (which is not a subclass of RunTimeException). Which
correctly completes the method of declaration when added at line one?

A.public AWTException methodName()

B.public void methodName()

C.public void methodName() throw AWTException

D.public void methodName() throws AWTException

E.public void methodName() throws Exception

20) A sample question provided by Sun

Given a TextArea using a proportional pitch font and constructed like this:

TextField t = new TextArea("12345", 5, 5);

Which statement is true?

A.The displayed width shows exactly five characters
on each line unless otherwise constrained

B.The displayed height is five lines unless
otherwise constrained.

C.The maximum number of characters in a line will
be five.

D.The user will be able to edit the character
string.

E.The displayed string can use multiple fonts.

21) A sample question provided by Sun

Given this skeleton of a class currently under construction:

public class Example {
int x, y;

public Example(int a) {
//lots of complex computation
x=a;
}

public Example(int a, int b) {
// do everything the same as single argument
// version of constructor
//including assignment x = a
y = b;
}
}

What is the most concise way to code the "do everything..." part of the constructor taking two
arguments?

Short answer, like "one(a)" ,do not add the semicolon at the end,

22) A sample question provided by Sun

Which correctly create a two dimensional array of integers?

A.int a[][] = new int[10,10]
B.int a[10][10] = new int[][];
C.int a[][] = new int [10][10];
D.int []a[] = new int [10][10];
E.int []a[] = new int[10][10];

Select all correct answers.

23) A sample question provided by Sun

Which are keywords in Java?

A.sizeof

B.abstract

C.native

D.NULL

E.BOOLEAN

Select all valid answers.

24) A sample question provided by Sun

Which are correct class declarations? Assume in each case that the text constitutes the entire contents of
a file called Fred.java on a system with a case-significant file system.

A.public class Fred {
public int x = 0;
public Fred (int x) {
this.x = x;
}
}

B.public class fred
public int x = 0;
public fred (int x) {
this.x = x;
}
}

C.public class Fred extends MyBaseClass, MyOtherBaseClass {
public int x = 0;
public Fred (int xval) {
x = xval;
}
}

D.protected class Fred {
private int x = 0;
private Fred (int xval) {
x = xval;
}
}


E.import java.awt.*;
public class Fred extends Object {
int x;
private Fred (int xval) {
x = xval;
}
}

25) A sample question provided by Sun

A class design requires that a particular member variable must be accessible for direct access by any
subclasses of this class, but otherwise not by classes which are not members of the same package.
What should be done to achieve this?

A.The variable should be marked public

B.The variable should be marked private

C.The variable should be marked protected

D.The variable should have no special access modifier

E.The variable should be marked private and an accessor method provided

26) A sample question provided by Sun

Which correctly create an array of five empty Strings?

A.String a [] = new String [5];
for (int i = 0; i < 5; a[i++] = "");

B.String a [] = {"", "", "", "", ""};

C.String a [5];

D.String [5] a;

E.String [] a = new String [5];
for (int i = 0; i < 5; a[i++] = null);

27) A sample question provided by Sun

Which cannot be added to a Container?

A.an Applet

B.a Component

C.a Container

D.a Menu

E.a Panel


Answers:

1) A 2) CE 3) D 4) A 5) A 6)B 7)B 8)B 9)A 10)A

11)A 12)D 13)A 14)E 15)DE 16) B 17)A 18)B 19)DE 20)BD

21) "this(a)" 22) CDE 23) BC 24)AE 25) C 26)AB 27) D