SARGA

. COM

Java Certified Programmers' Examination Samples

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

public class Base{

private void test() {
int i = 1;

switch (i) {

case 0:
System.out.println("zero");
break;

case 1:
System.out.println("one");

case 2:
System.out.println("two");

default:
System.out.println("default");

}

}

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

}

Select most appropriate answer.

a) one
b) one, default
c) one, two, default
d) default
e) Compilation Error. switch cannot take an int as an argument

2) For what value of i, the following program will output "Less than 20"

public class Base{

private void test() {

int i ;

if(i < 10)
System.out.println("Less than 10");
else
if ( i < 20)
System.out.println("Less than 20");
else
System.out.println("Not less than 10");

}

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

}

Select all appropriate answers.

a) 0
b) 10
c) 11
d) 19.999;
e) (int)19.99
f) None

3) What are the valid types for the given variable i ?

public class Base{


private void test() {

// i = 1;

switch (i) {

}
}

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

}

Select most appropriate answer.

a) boolean, byte, char, short, int, long
b) boolean, byte, char, short, int
c) byte, char, short, int
d) boolean, byte, char, short, int, long, float, double
e) Only int

4) For what value of i, the following program will output "Not Greater than 10"

public class Base{

private void test() {

int i ;

if(i > 10)
System.out.println("Greater than 10");
else
if ( i < 10)
System.out.println("Less than 10");
else
if ( i > 20)
System.out.println("Greater than 20");
else
System.out.println("Not Greater than 10");

}

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

}

Select most appropriate answer.

a) 0
b) 10
c) 11
d) 100
e) None

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

public class Base{

private void test() {

boolean flag = false;

if(flag){
System.out.println("001 Flag is true");
}
else {
if (flag)
System.out.println("002 Flag is true");
else
System.out.println("003 Flag is false");
}
}

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

}

Select most appropriate answer.

a) 001 Flag is true
b) 002 Flag is false
c) 003 Flag is false
d) None

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

public class Base{

private void test() {

boolean flag = false;

if(flag==true){
System.out.println("001 Flag is true");
}
else {
if (flag=true)
System.out.println("002 Flag is true");
else
System.out.println("003 Flag is false");
}

}

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

}

Select most appropriate answer.

a) 001 Flag is true
b) 002 Flag is true
c) 003 Flag is false
d) None

7) For what value of flag the following program will ouput "003 Flag is false"

01: public class Base{
02:
03: private void test() {
04:
05: boolean flag = false;
06:
07: if(flag==true){
08: System.out.println("001 Flag is true");
09: }
10: else {
11: if (flag=true)
12: System.out.println("002 Flag is true");
13: else
14: System.out.println("003 Flag is false");
15: }
16:
17: }
18:
19: static public void main(String[] a) {
20: new Base().test();
21: }
22:
23: }

Select most appropriate answer.

a) true
b) false
c) 0
d) None

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

public class Base{

private void test() {

for(int i = 0; i < 5; i++){

if (i < 2)
continue;

if ( i == 4)
break;

System.out.println("Value of i:" + i);
}
}

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

}

Select all valid answers

a) Value of i: 0
b) Value of i: 1
c) Value of i: 2
d) Value of i: 3
e) Value of i: 4
f) None

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

public class Base{

private void test() {

one:
for (int i = 0; i < 3; i ++) {

two:
for(int j = 0; j < 3; j++){

if (i == 2)
continue two;

if ( j == 2)
continue one;

System.out.println("Values : " + i + "," + j);
}
}
}

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

}

Select all valid answers

a) Values : 0,0
b) Values : 0,1
c) Values : 1,0
d) Values : 1,1
e) Values : 2,0
f) Values : 2,1

10) A label for the continue statement

a) must be before a for, do or while loop (other than white space(s) and comments)
b) must be before an if, else, for, do or while statements (other than white space(s) and comments)
c) should not be before another label
d) must be before an opening brace like '{'
e) None of the above

Select all valid answers

11) A label for the break statement

a) must be before a for, do or while loop (other than white space(s) and comments)
b) must be before an if, else, for, do or while statements (other than white space(s) and comments)
c) should not be before another label
d) must be before a for,do,while loop or a block inside a loop (other than white space(s) and comments)
e) None of the above

Select the most appropriate answer

12) A try statement must always associate with a

a) catch
b) None of these
c) throws
d) throw
e) finally
f) catch, finally or both

Select all valid answers

13) A catch statement must always associate with a

a) try block
b) finally block
c) throws
d) throw

Select all valid answers

14) What will be the output when you compile and execute the following program
Note that String a is null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = "b";

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
return;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
return;
}

System.out.println("End processing");
}

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

}


Select all valid answers

a) End of try block
b) Caught MalformedURLException
c) Caught EOFException
d) End processing
e) None

15)

What will be the output when you compile and execute the following program
Note that String a and String b are null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = null;

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
return;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
return;
}

System.out.println("End processing");
}

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

}


Select all valid answers

a) End of try block
b) Caught MalformedURLException
c) Caught EOFException
d) End processing
e) None

16)


What will be the output when you compile and execute the following program
Note that String a is null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = "b";

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
return;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
return;
}
finally {
System.out.println("End finally");
}

System.out.println("End processing");
}

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

}


Select all valid answers

a) End of try block
b) Caught MalformedURLException
c) Caught EOFException
d) End finally
e) End processing
f) None

17)

What will be the output when you compile and execute the following program
Note that String a is null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = "b";

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
}
catch (EOFException e) {
System.out.println("Caught EOFException");
}
finally {
System.out.println("End finally");
}

System.out.println("End processing");
}

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

}

Select all valid answers

a) End of try block
b) Caught MalformedURLException
c) Caught EOFException
d) End finally
e) End processing
f) None

18)

What will be the output when you compile and execute the following program
Note that String a is null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = "b";

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
System.exit(-1);
}
catch (EOFException e) {
System.out.println("Caught EOFException");
System.exit(-2);
}
finally {
System.out.println("End finally");
}

System.out.println("End processing");
}

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

}

Select all valid answers

a) End of try block
b) Caught MalformedURLException
c) Caught EOFException
d) End finally
e) End processing
f) None

19)


The method test() throws an IOException. How do you specify that ?

import java.io.*;
import java.net.*;

public class Base{

private void test()
{
String a = null;

if(a == null)
//throws IOException
}

}

Select all valid answers

a) private void test() throw IOException
b) private void test() throw new IOException("")
c) private void test() throws IOException()
d) private void test() throws IOException
e) private void test() raise IOException

20)

Which of the following are correct way of throwing an exception inside a method?

Select all valid answers

a) new throw Exception("")
b) throw new Exception("")
c) throws IOException()
d) throws IOException
e) catch(Exception e { }

21)

The method test() throws MalformedURLException and EOFException. IOException is the parent
class of MalformedURLException and EOFException.

What changes will make the following code to compile with out any errors.

import java.io.*;
import java.net.*;

public class Base{

private void test() {
String a = null;
String b = "b";

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");

// Complex processing
}

}

Select all valid answers

a) private void test() throws IOException
b) private void test() throws EOFException, throws MalformedURLException
c) private void test() throws EOFException throws MalformedURLException
d) private void test() throws EOFException, MalformedURLException
e) private void test() throws EOFException MalformedURLException

22)

What will be the output when you compile and execute the following program
Note that String a is null;

import java.io.*;
import java.net.*;

public class Base{

private void test() {

String a = null;
String b = "b";

try {

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");
// Complex processing

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
}
catch (EOFException e) {
System.out.println("Caught EOFException");
}
finally {

try {
System.out.println("End finally " + a.trim());
}
catch(Exception e) {
System.out.println("End finally with an exception");
}
}

System.out.println("End processing");
}

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

}

Select all valid answers

a) End finally
b) Caught MalformedURLException
c) Caught EOFException
d) End finally with an exception
e) End processing
f) Compilation error. Cannot add try/catch block inside a finally block

23)

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

import java.io.*;
import java.net.*;

public class Base{

private void test() throws IOException {

String a = null;
String b = "b";

try {

// Complex processing

if(a==null)
throw new MalformedURLException("test");

if(b ==null)
throw new EOFException("test");

// Complex processing

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
throw e;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
throw e;
}
finally {

System.out.println("End finally ");
}

System.out.println("End processing");
}

static public void main(String[] a) {

try {
new Base().test();
}
catch (Exception e) { System.out.println("Exception at main()");}
}

}

Select all valid answers

a) End finally
b) Caught MalformedURLException
c) Caught EOFException
d) Exception at main()
e) End processing
f) Compilation error. Cannot throw an exception from an exception block

24)

Which of the following statements about garbage collection are true?

Select all valid answers

a) Only objects with no references will be garbage collected
b) Unused objects can be immediately garbage collected by setting them to null
c) Unused objects can be immediately garbage collected by executing 'System.gc()'
d) Setting an object to null will make it availabe for garbage collection
e) Garbage collection cannot be forced

25)

At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }

Select most appropriate answer

a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12

26)


At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }

Select most appropriate answer

a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11

Answers:

1) C 2) BCE 3) C 4) B 5) C 6)B 7)D 8)CD 9)ABCD 10)A

11)D 12)F 13)A 14)B 15)B 16)BD 17)BDE 18)B 19)D 20)B

21) AD 22) BDE 23)ABD 24)ADE 25) D 26)E