SARGA

. COM

Java Certified Programmers' Examination Samples

1) What will be the output when you compile and execute the following program.

class Base extends Thread {
public void run() {
System.out.println("Start running..");
}

static public void main(String[] a) {
Base anObj = new Base();
anObj.start();
}
}

Select most appropriate answer.

a) Start running..

b) Start running..
Start running..
Start running..
Start running..
..............
in an infinite loop

c) Compilation error. Cannot extend Thread class
d) Compilation error. Cannot find the method start() in the Base class.
e) No output

2) What will be the output when you compile and execute the following program.

class Base extends Thread {
public void run() {
while(true)
System.out.println("Start running..");
}

static public void main(String[] a){
Base anObj = new Base();
anObj.start();
}
}

Select most appropriate answer.

a) Start running..

b) Start running..
Start running..
Start running..
Start running..
..............
in an infinite loop

c) Compilation error. Cannot extend Thread class
d) Compilation error. Cannot find the method start() in the Base class.
e) No output

3) What will be the output when you compile and execute the following program.

class Base extends Thread {
static public void main(String[] a) {
Base anObj = new Base();
anObj.start();
}
}

Select most appropriate answer.

a) Compilation error. Class Base must be defined abstract. It does not define run() method.
b) No Errors.
c) Compilation error. Cannot extend Thread class
d) Compilation error. Cannot find the method start() in the Base class.
e) class Base is an abstract class. It cannot be instantiated

4) What will be the output when you compile and execute the following program.

class Threadable implements Runnable {
public void run() {
while(true)
System.out.println("Start running..");
}
}

class Base {

static public void main(String[] a) {
Threadable aFace = new Threadable();
Thread aThread = new Thread(aFace);
aThread.start();
}
}

Select most appropriate answer.

a) Start running..

b) Start running..
Start running..
Start running..
Start running..
..............
in an infinite loop

c) Compilation error. You should extend Thread class
d) Compilation error. Cannot find the method start() in the Thread class.
e) No output

5) What will be the output when you compile and execute the following program.

class Threadable implements Runnable {
public void run() {
for(int i=0; i < 3; i++)
System.out.println("Threadable: Running..");
}
}

class Base extends Thread {

public void run() {
for(int i=0; i < 3; i++)
System.out.println("Base: Running..");
}

static public void main(String[] a) {
Threadable aFace = new Threadable();
Thread aThread = new Thread(aFace);
aThread.start();

new Base().start();
}
}

Select most appropriate answer.

a) Three times "Threadable: Running.." and three time "Base: Running.."
b) Threadable: Running.. in a loop
c) Base: Running.. in a loop
d) Depends on the Operating System and or Virtual Machine
e) Compilation error: Cannot Implement Runnable interface and extend Thread class at same time.

6) What java keyword or method is used to lock an object in a monitor

a) lock
b) sleep
c) yield
d) synchronized
e) final
f) wait

Select most appropriate answer.

7) What method is used by one thread to give up control to another thread

a) run()
b) sleep()
c) yield()
d) synchronized
e) start()

Select all valid answers.

8) A monitor thread has executed wait() method. Which of the following statements are true?

a) Causes current thread to wait until another thread invokes the notify() or notfyAll()
b) Monitor should call wait() on another object's thread to get control
c) wait() should only be called by a thread that is the owner of that object's monitor.
d) This monitor will get control when some other thread interrupts, or a certain amount
of real time has elapsed.

Select all valid answers.


9) What is the correct ordering for import, class and package declarations in a single java source file

Select most appropriate answer.

a) package, import, class
b) any order
c) package, class, import
d) import, package, class

10) What tags are mandatory when creating HTML to display an applet

Select most appropriate answer.

a) name, height, width
b) code, name
c) codebase, height, width
d) code, height, width
e) None of the above

11)


What will be the output when you compile the following code in a file called
Graph.java and execute the following program HTML file from using a browser.

import java.applet.*;
import java.awt.*;

public class Graph extends Applet{

public void init(){
System.out.println(getParameter("SCALE"));
}
}

/////// HTML File Starts

<applet code=Graph.class height=400 width=400 parameter AXIS=10 >
</applet>

/////// HTML File Ends

If you use a browser like Netscape you will see the output on your Java Console.
Select the most appropriate answer.

a) Error: no such parameter
b) 0
c) null
d) 10
e) Runtime Error: java.lang.NullPointerException

12)

Which of the following statements are true?

a) An abstract method cannot be final
b) An abstract method cannot be static
c) An abstract method cannot be private
d) An abstract method must be specified inside an abstract class only
e) An abstract method cannot throw any exceptions

Select all valid answers.

13)

What tags are mandatory when creating HTML to display an applet

Select most appropriate answer.

a) name, height, width
b) code, name
c) codebase, height, width
d) code, height, width
e) None of the above

14)

What will be the output when you compile the following code in a file called
Graph.java and execute the following program HTML file using a browser.

import java.applet.*;
import java.awt.*;

public class Graph extends Applet{

public void init(){
System.out.println(getParameter("SCALE"));
}
}

/////// HTML File Starts

<applet code=Graph.class height=400 width=400 parameter AXIS=10 >
</applet>

/////// HTML File Ends

If you use a browser like Netscape you will see the output on your Java Console.
Select the most appropriate answer.

a) Error: no such parameter
b) 0
c) null
d) 10
e) Runtime Error: java.lang.NullPointerException

15)

Which of the following statements are true?

a) An abstract method cannot be final
b) An abstract method cannot be static
c) An abstract method cannot be private
d) An abstract method must be specified inside an abstract class only
e) An abstract method cannot throw any exceptions

Select all valid answers.

16)


Which of the following Math class usage are correct

a) Math anObj = new Math();
int i = anObj.abs(-101);

b) int i = new Math().abs(-101);

c) int i = Math.abs(-101);

d) class MyMath extends Math {

}
Select all valid answers.

17) What will be the output when you compile and execute the following program.

public class Base {

void test() {
int i = -10;
long l = -99;
float f = -12.346f;
double d = -122.908;

System.out.println("abs(-10) : " + Math.abs(i) );
System.out.println("abs(-99L) : " + Math.abs(l) );
System.out.println("abs(-12.346) : " + Math.abs(f) );
System.out.println("ceil(-122.908) : " + Math.ceil(d) );
System.out.println("floor(-122.908): " + Math.floor(d) );
System.out.println("round(-12.346) : " + Math.round(f) );
System.out.println("round(-122.908): " + Math.round(d) );
System.out.println("round(0) : " + Math.round(0) );
System.out.println("round(-0) : " + Math.round(-0) );
System.out.println("ceil (-0) : " + Math.ceil(-0) );
System.out.println("ceil (0) : " + Math.ceil(0) );

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a)
abs(-10) : 10
abs(-99L) : 99
abs(-12.346) : 12.346
ceil(-122.908) : -122.0
floor(-122.908): -123.0
round(-12.346) : -12
round(-122.908): -123
round(0) : 0
round(-0) : 0
ceil (-0) : 0.0
ceil (0) : 0.0

b)
abs(-10) : 10
abs(-99L) : 99
abs(-12.346) : 12.346
ceil(-122.908) : -122
floor(-122.908): -123
round(-12.346) : -12
round(-122.908): -123
round(0) : 0
round(-0) : 0
ceil (-0) : 0
ceil (0) : 0

C)
abs(-10) : 10
abs(-99L) : 99
abs(-12.346) : 12.346
ceil(-122.908) : -123.0
floor(-122.908): -122.0
round(-12.346) : -12
round(-122.908): -122
round(0) : 0
round(-0) : 0
ceil (-0) : -1
ceil (0) : 1

18)

What will be the output when you compile and execute the following program.

public class Base {

void test() {

float f;

System.out.println("abs(-0) : " + Math.abs(-0) );
System.out.println("abs(+0) : " + Math.abs(+0) );
System.out.println("round(1.5) : " + Math.round(1.5) );
System.out.println("floor(-0) : " + Math.floor(-0) );
System.out.println("floor(+0) : " + Math.floor(+0) );
System.out.println("ceil (-1.5) : " + Math.ceil(-1.5) );
System.out.println("ceil (-1.99) : " + Math.ceil(-1.99) );
System.out.println("ceil (1.5) : " + Math.ceil(1.5) );
System.out.println("ceil (1.99) : " + Math.ceil(1.99) );

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a)
abs(-0) : 0
abs(+0) : 0
round(1.5) : 2
floor(-0) : 0.0
floor(+0) : 0.0
ceil (-1.5) : -1.0
ceil (-1.99) : -1.0
ceil (1.5) : 2.0
ceil (1.99) : 2.0

b)
abs(-0) : 0
abs(+0) : 0
round(1.5) : 1
floor(-0) : 0.0
floor(+0) : 0.0
ceil (-1.5) : -1.0
ceil (-1.99) : -2.0
ceil (1.5) : 1.0
ceil (1.99) : 2.0
c)
abs(-0) : 0
abs(+0) : 0
round(1.5) : 2
floor(-0) : -0.0
floor(+0) : 0.0
ceil (-1.5) : -1.0
ceil (-1.99) : -1.0
ceil (1.5) : 2.0
ceil (1.99) : 2.0

19)

For what value(s) of 'f', the following program will print all four System.out.println lines

public class Base {

void test() {

float f; // what should be the value of f

if (Math.abs(f) == Math.round(f))
System.out.println("Math.abs(f) == Math.round(f)");

if (Math.ceil(f) == Math.round(f))
System.out.println("Math.ceil(f) == Math.round(f)");

if (Math.floor(f) == Math.round(f))
System.out.println("Math.floor(f) == Math.round(f)");

if (Math.floor(f) == Math.ceil(f))
System.out.println("Math.floor(f) == Math.ceil(f)");

}

static public void main(String[] a) {
new Base().test();
}
}

Select all valid answers.

a) 1.0f
b) -0
c) .999f
d) None

20)


What will be the output when you compile and execute the following program.

public class Base {

void test() {
String aStr = "OneString";

System.out.println(aStr.substring(4));

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a) String
b) OneString
c) tring
d) TRING

21) What will be the output when you compile and execute the following program.

public class Base {

void test() {
String aStr = "OneString";

System.out.println(aStr.substring(3,7));

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a) Strin
b) String
c) trin
d) Stri

22)

What will be the output when you compile and execute the following program.

public class Base {

void test() {
String aStr = " OneString ";

aStr.trim();
aStr.toUpperCase();

System.out.println("[" + aStr + "]");

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a) [ONESTRING]
b) [OneString]
c) [ OneString ]

23)

What will be the output when you compile and execute the following program.

public class Base {

void test() {
String aStr = "ONE";
String bStr = aStr;
String cStr = new String (aStr);

if(aStr == bStr)
System.out.println("aStr == bStr");

if(aStr == cStr)
System.out.println("aStr == cStr");

if(aStr.equals(cStr))
System.out.println("aStr.equals(cStr)");

if(aStr == aStr.toUpperCase())
System.out.println("aStr == aStr.toUppercase()");

if("One".equalsIgnoreCase(aStr))
System.out.println("One.equalsIgnoreCase(aStr)");

if("ONE" == aStr)
System.out.println("ONE == aStr");

}

static public void main(String[] a) {
new Base().test();
}
}

Select most appropriate answer.

a)
aStr == bStr
aStr.equals(cStr)
aStr == aStr.toUppercase()
One.equalsIgnoreCase(aStr)
ONE == aStr

b)
aStr == bStr
aStr == cStr
aStr.equals(cStr)
aStr == aStr.toUppercase()
One.equalsIgnoreCase(aStr)
ONE == aStr

c)
aStr.equals(cStr)

d)
aStr.equals(cStr)
One.equalsIgnoreCase(aStr)

24)

What method will be used get the first position of a character 'C' from a String aStr?

String aStr = "OneCString";

a) aStr.substring("C");
b) aStr.charAt('C');
c) aStr.indexOf('C');
d) aStr.indexOf("C");

Select all valid answers.

25)

What method will be used get the character at location 2 from a String aStr?

String aStr = "OneCString";

a) aStr.substring(2);
b) aStr.charAt(2);
c) aStr.indexOf(2);
d) aStr.lastIndexOf(2);

Select all valid answers.


Answers:

1) A 2) B 3) B 4) B 5) A 6)D 7)BC 8)AC 9)A 10)C

11)C 12)ABCD 13)D 14)C 15)ABCD 16)C 17)A 18)A 19)AB 20)C

21)D 22) C 23) A 24) CD 25)B