SARGA

. COM

Java Certified Programmers' Examination Samples



1) When you compile and execute the following program which lines will give compilation
and/or runtime errors.

01:public class Base{
02: private void test() {
03:
04: int i = 3;
05: int a[] = { 0, 1 };
06: Integer b[] = new Integer[i*i];
07: Long c[] = { new Long(1L) , new Long(2L) };
08: int d[] = new int[] { 10, 20 };
09: int e[] = new int[2] { 10, 20 };
10: Integer f[] = new Integer[i];
11:
12:}
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18:}

a) Invalid array initialization at line 05
b) Invalid array size at line 06. A local variable cannot be used as array size
c) Invalid array initialization at line 07
d) Invalid array initialization at line 08. Cannot new and initialize an array at same time
e) Error at line 09. Cannot specify array size and initialize an array at same time
f) Invalid array size at line 10. A local variable cannot be used as array size

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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}

////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

e) Runtime error. java.lang.ClassCastException


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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {

Child anObj = (Child)new Base();
anObj.test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

e) Runtime error. java.lang.ClassCastException




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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {

Object anObj = new Base();
((Base)anObj).test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Base.test()
Child.test()

c) Base.test()

d) Child.test()

e) Runtime error. java.lang.ClassCastException


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

////////////////////////////////////////////
class Base
{
static void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

static void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {
Child anObj = new Child();
anObj.test();

Base baseObj = (Base)anObj;
baseObj.test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Base.test()
Base.test()

d) Compilation error. Cannot override a static method

e) Runtime error. Cannot override a static method

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

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
super.test();
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Base.test()
Child.test()

d) Compilation error. super must be the first line in a method

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

////////////////////////////////////////////
class Base
{
static void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

static void test() {
System.out.println("Child.test()");
super.test();
}

static public void main(String[] a) {
test()
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Base.test()
Base.test()

d) Compilation error. Cannot call super from a static method

e) Runtime error. Cannot call super from a static method


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

////////////////////////////////////////////
class Base
{
static void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
Base.test(); //Call the parent method
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Compilation error. Cannot override a static method by an instance method

d) Runtime error. Cannot override a static method by an instance method


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

////////////////////////////////////////////
class Base
{
protected void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
super.test(); //Call the parent method
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Child.test()
Child.test()

c) Compilation error. Cannot make the access modifier more restrictive for test()

d) Compilation error. Cannot call super.test() since parent method is protected


12) What will be the output when you compile and execute the following program.
Exception is the base class for all the Exception classes.

////////////////////////////////////////////
import java.io.*;

class Base
{
void test() throws IOException {
System.out.println("Base.test()");
String a = null;

//Complex logic goes here
if(a == null)
throw new IOException("");
}

}
////////////////////////////////////////////
public class Child extends Base {

protected void test() throws Exception {
System.out.println("Child.test()");
Base.test(); //Call the parent method
throw new Exception("");
}

static public void main(String[] a) {
try {
new Child().test();
}
catch(Exception e) { }
}
}

Select most appropriate answer.

a) Child.test()
Base.test()

b) Compilation Error: The method void test() declared in class Child cannot
override the method of the same signature declared in class Base. Their
throws clauses are incompatible.

c) Compilation Error: The method void test() declared in class Child cannot override the
method of the same signature declared in class Base.
The access modifier is made more restrictive.

d) Runtime error. Cannot make the access modifier more restrictive for test()

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

////////////////////////////////////////////
import java.io.*;

class Base
{
synchronized void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
super.test(); //Call the parent method
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Base.test()

b) Compilation Error: The method void test() declared in class Child cannot
override the method of the same signature declared in class Base. The parent
method is synchronized and the child method is not.

c) Runtime Error: The method void test() declared in class Child cannot
override the method of the same signature declared in class Base. The parent
method is synchronized and the child method is not.

d) Base.test()
Child.test()

14) Which of the following lines will compile when added to the Child class
without making any other changes.

public class Child {

void test() {
System.out.println("Child.test()");
}
}

Select all valid answers.

a) abstract void test_a (int i);
b) abstract void test_b (int i) { };
c) abstract void test ();
d) final void test_d (int i);
e) native void test_e (int i);
f) private abstract void test_f(int i);

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

////////////////////////////////////////////

class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

Child(int i) { test (); }

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Child.test()
Child.test()

b) Compilation Error: No default constructor ( constructor matching Base())
found in class Base.

c) Runtime Error: No default constructor ( constructor matching Base())
found in class Base.

d) Compilation Error: Cannot call this() from a constructor.

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

////////////////////////////////////////////

class Base
{
Base(int i) { test(); }

void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

Child(int i) { test (); }

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

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

Select most appropriate answer.

a) Base.test()
Child.test()

b) Compilation Error: No default constructor ( constructor matching Base())
found in class Base.

c) Runtime Error: No default constructor ( constructor matching Base())
found in class Base.

d) Child.test()

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

01: public class Final
02:{
03: private int val = 0;
04:
05: void set (int i) { val = i; }
06:
07: static public void main(String a[])
08: {
09: final Final anObj = new Final();
10: anObj.set(10);
11: anObj.val = 10;
12:
13: Final bObj = new Final();
14: anObj = bObj;
15: }
16:}

Select all valid answers.

a) Compilation error at line 10. Cannot assign or change a final variable
b) Compilation error at line 11. Cannot assign or change a final variable
c) Compilation error at line 14. Cannot assign or change a final variable
d) No errors
e) Compilation error at line 11. Cannot access the private variable val;

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

////////////////////////////////////////////

01: class Base
02:{
03: final int MAX_SIZE;
04: Base(){
05: MAX_SIZE = 10;
06: }
07:
08: void test() {
09: MAX_SIZE++;
10: System.out.println("Base.test()");
11: }
12:
13:}

Select most appropriate answer.

a) Compilaton Error at Line 09: Can't assign a second value to a blank final variable: MAX_SIZE

b) Compilation Error at Line 03: Can't define a blank final variable: MAX_SIZE

c) Compilation Error at Line 05: Can't assign a value to a blank final variable: MAX_SIZE

d) No errors

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

////////////////////////////////////////////

01: class Base
02:{
03: static final int MAX_SIZE;
04: Base(){
05: MAX_SIZE = 10;
06: }
07:
08: void test() {
09: MAX_SIZE++;
10: System.out.println("Base.test()");
11: }
12:
13:}

Select all valid answers.

a) Compilation Error at Line 03: Blank final variable 'MAX_SIZE' may not have been
initialized. It must be assigned a value in an initializer, or in every constructor.
b) Compilation Error at Line 05: Can't assign a second value to a blank final variable:
c) Compilation Error at Line 09: Can't assign a second value to a blank final variable:
d) No errors

20) What are the proper ways to initialize the static variables SIZE and MIN_VALUE ?

01: class Base
02: {
03: static final int SIZE;
04: static float MIN_VALUE;
05:
06: Base() { }
07: void test() {
08: System.out.println("Base.test()");
09: }
10:
11:}

Select all valid answers.

a) Add the following lines to Base Class
static {
SIZE = 10;
MIN_VALUE = 10.3f;
}

b) Add the following lines to Base Class
{
SIZE = 10;
MIN_VALUE = 10.3f;
}

c) Add the following lines to Base Class constructor
SIZE = 10;
MIN_VALUE = 10.3f;

d) Modify lines 03 and 04 as
static final int SIZE = 10;
static float MIN_VALUE = 10.3f;


Answers:

1) E 2) 3) 4) D 5) D 6)C 7)A 8)A 9)D 10)C

11)C 12)B 13)D 14)E 15)A 16) B 17)C 18)E 19)ABC 20)AD