You are on page 1of 21

Q U EST IO N NO : 7

G iven:
1. class A {
2. A() { }
3. }
4.
5. class B extends A {
6. }
W hich tw o statem ents are true? (C hoose tw o)

A . C lass B ’s c o nstructor is public.


B . C lass B ’s c o nstructor has no argu m ents.
C . C lass B ’s c onstructor in cludes a call to this(). D .
C lass B ’s constructor i n cludes a call to super().

Q U EST IO N NO : 10
Y ou w ant a class to have access to m em bers of another class in the sam e package. W hich
is the m ost restrictive access that accom plis hes this objective?

A. public
B. private
C. prote cted
D. transien t
E. d efault access

Q U EST IO N NO : 13
G iven:
1. public interface Foo {
2. int k = 4;
3. }
W hich three are equivalent to line 2? (C hoose three)

A. final int k = 4;
B. public int k = 4;
C. static int k = 4;
D. abstract int k = 4;
E. volatile int k = 4;
F. protected int k = 4;

Q U EST IO N NO : 14
G iven:
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(“x = “ + x);
5. }
6. }
W hat is the result?
A. x=0
B. x = 42
C. C o m p ilation fails because of a n error in lin e 2 of class T est2.
D. C o m p ilation fails because of a n error in lin e 3 of class T est1.
E. C om p ilation fails because of an error in lin e 4 of class T est2.

Q U EST IO N NO : 15
G iven:
1. class A {
2. protected int method1(int a, int b) { return 0; }
3. }
W hich tw o are valid in a class that extends class A? (C hoose tw o)

A. public int method1(int a, int b) { return 0; }


B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0: }
E. static protected int method1(int a, int b) { return 0; }

Q U EST IO N NO : 20
G iven:
1. class Super {
2. public float getNum() { return 3.0f; }
3. }
4.
5. public class Sub extends Super {

-11 -
310 - 035

6.
7. }
W hich m etho d, placed at line6, c auses com pilation to fail?

A. public void getNum() { }


B. public void getNum(double d) { }
C. public float getNum() { return 4.0f; }
D. public double getNum(float d) { return 4.0d; }

G iven:
1. public class SwitchTest {
2. public static void main(String[] args) {
3. System.out.println(“value = “ + switchIt(4));
4. }
5. public static int switchIt(int x) {
6. int j = 1;
7. switch (x) {
8. case 1: j++;
9. case 2: j++;
10. case 3: j++;
11. case 4: j++;
12. case 5: j++;
13. default: j++;
14. }
15. return j + x;
16. }
17. }
W hat is the result?

A. valu e = 3
B. valu e = 4
C. valu e = 5
D. valu e = 6
E. valu e = 7
F. valu e = 8

Q U EST IO N NO : 28
W hich three form part of co rrect array declarations? (Choo se three)

A. public int a []
B. static int [] a
C. public [] int a
D. private int a [3]
E. private int [3] a []
F. public final int [] a

Q U EST IO N NO : 33
G iven:
1. class Super {
2. public Integer getLenght() { return new Integer(4); }
3. }
4.
5. public class Sub extends Super {
6. public Long GetLenght() { return new Long(5); }
7.
8. public static void main(String[] args) {
9. Super sooper = new Super();
10. Sub sub = new Sub();
11. System.out.println(
12. sooper.getLenght().toString() + “,” +
13. sub.getLenght().toString() );
14. }
15. }
W hat is the output?

A. 4,4
B. 4,5
C. 5,4
D. 5,5
E. C om p ilation fails.

Q U EST IO N NO : 36
G iven:
1. class Base {
2. Base() { System.out.print(“Base”); }
3. }
4. public class Alpha extends Base {
5. public static void main( String[] args ) {
6. new Alpha();
7. new Base();
8. }
9. }
W hat is the result?

A. B ase
B. B aseB ase
C. C om p ilation fails.
D. T he cod e run s w ith no output.
E. A n exception is throw n at runtim e.

Q U EST IO N NO : 39
W hich tw o are valid declarations w ithin an interface definition? (Choose tw o)

A. void methoda();
B. public double methoda();
C. public final double methoda();
D. static void methoda(double d1);
E. protected void methoda(double d1);

A nsw er: A, B

Q U EST IO N NO : 40
W hich tw o allow the class T hing to be instantiated using new Thing()? (Choose t w o)

A . public class Thing {


}

-21 -
310 - 035

B . public class Thing {


public Thing() {}
}
C . public class Thing {
public Thing(void) {}
}
D . public class Thing {
public Thing(String s) {}
}
E . public class Thing {
public void Thing() {}
public Thing(String s) {}
}

A nsw er: A, B
Q U EST IO N NO : 44
G iven:
11. for (int i =0; i <3; i++) {
12. switch(i) {
13. case 0: break;
14. case 1: System.out.print(“one “);
15. case 2: System.out.print(“two “);
16. case 3: System.out.print(“three “);
17. }
18. }
19. System.out.println(“done”);
W hat is the result?

A. done
B. one tw o do n e
C. one tw o th ree don e
D. one tw o th ree tw o three don e
E. C om p ilation fails.

A nsw er: D
Q U EST IO N NO : 45
W hich three statem ent s are true? (Choose three)

A. T he d efau lt constru ctor initializes m ethod varia bles.


B. T he d efau lt constru ctor has the sam e access as its class.
C. T he d efau lt constru ctor in voked the no-arg constructor of the superclass.
D. If a class lacks a no-arg con stru ctor, the com piler alw ays creates a default constructor.
E. T he co m p iler creates a d efault constructor only w hen there are no other constructors
for the class.

-24 -
310 - 035

A nsw er: B, C, E
Q U EST IO N NO : 54
Y ou w ant to lim it access to a m ethod of a public class to m em bers of t he sam e class.
W hich access accom plishes this objective?

A. public
B. private
C. prote cted
D. transien t
E. d efault access

A nsw er: B
Q U EST IO N NO : 55
G iven:
11. switch(x) {
12. default:

-28 -
310 - 035

13. System.out.println(“Hello”);
14. }
W hich tw o are acceptable ty pes for x? (C hoose tw o)

A . byte
B . long
C . char
D . float
E . S hort
F . L ong

A nsw er: A , C
Q U EST IO N NO : 60
G iven:
1. public class Foo {
2. public void main( String[] args ) {
3. System.out.println( “Hello” + args[0] );
4. }
5. }

W hat is the result if this code i s executed w ith the com m and line?
java Foo world

A. H ello
B. H ello Foo
C. H ello w orld
D. C om p ilation fails.
E. T he cod e do es not run.

A nsw er: E
Q U EST IO N NO : 65
G iven:
1. public class Test {
2. public static void main(String Args[]) {
3. int i =1, j = 0;
4. switch(i) {
5. case 2: j +=6;

-33 -
310 - 035

6. case 4: j +=1;
7. default: j +=2;
8. case 0: j +=4;
9. }
10. System.out.println(“j =” +j);
11. }
12. }
W hat is the result?

A. 0
B. 2
C. 4
D. 6
E. 9
F. 13

A nsw er: D
G iven:
1. class Super {
2. public int i = 0;
3.
4. public Super(String text) {
5. i = 1;
6. }
7. }
8.
9. public class Sub extends Super {
10. public Sub(String text) {
11. i = 2;
12. }
13.
14. public static void main(String args[]) {
15. Sub sub = new Sub(“Hello”);
16. System.out.println(sub.i);
17. }
18. }
W hat is the result?

A. 0
B. 1
C. 2
D. C om p ilation fails.

A nsw er: C
T his code is perfectly legal and the answ er is C .
Q U EST IO N NO : 70
G iven:
1. public class X {
2. public X aMethod() { return this;}
3. }
1. public class Y extends X {
2.
3. }
W hich tw o m eth ods can be added to the definition of class Y ? (C hoo se tw o)

A. public void aMethod() {}


B. private void aMethod() {}
C. public void aMethod(String s) {}
D. private Y aMethod() { return null; }
E. public X aMethod() { return new Y(); }

A nsw er: C, E
Q U EST IO N NO : 72
Y ou w ant su bclasses in any package to hav e access to m em bers of a superclass. W hich is
the m ost restrictive access that accom plishes this objective?

A. public
B. private
C. prote cted
D. transien t
E. d efault access

A nsw er: C
Q U EST IO N NO : 75
G iven:
1. public class A {
2. void A() {
3. System.out.println(“Class A”);
4. }
5. public static void main(String[] args) {
6. new A();
7. }
8. }
W hat is the result?

A. C lass A
B. C om p ilation fails.
C. A n exception is throw n at lin e 2.
D. A n exception is throw n at line 6.
E. T he cod e ex ecutes w ith no output.

A nsw er: E
Q U EST IO N NO : 80
In w hich tw o cases does the com pile r supply a defau lt constructor for class A ? (C hoose
tw o)

A . class A {
}
B . class A {
public A() {}
}
C . class A {
public A(int x) {}
}
D . class Z {}
class A extends Z {
void A() {}
}

A nsw er: A , D

Q U EST IO N NO : 85
G iven:
12. float f[][][] = new float[3][][];
13. float f0 = 1.0f;
14. float[][] farray = new float[1][1];
W hat is valid?

A. f[0] = f0;
B. f[0] = farray;
C. f[0] = farray[0];
D. f[0] = farray[0][0];

A nsw er: B

Q U EST IO N NO : 89
G iven:
11. int i = 0;
12. while (true) {
13. if(i==4) {
14. break;
15. }
16. ++i;
17. }
18. System.out.println(“i=”+i);
W hat is the result?

A. i = 0
B. i = 3
C. i = 4
D. i = 5
E. C om p ilation fails.

A nsw er: C
Q U EST IO N NO : 91
G iven:
1. public class Test { }
W hat is the prototype of the defau lt constructor?

A. Test()
B. Test(void)
C. public Test()
D. public Test(void)
E. public void Test()
A nsw er: C
Q U EST IO N NO : 98
G iven:
1. public class Alpha{
2. public static void main( string[] args ){
3. if ( args.length == 2 ) {
4. if ( args.[0].equalsIgnoreCase(“-b”) )
5. System.out.println( new Boolean( args[1] ));
6. }
7. }
8. }

A nd the code is in voked by using the com m and:


java Alpha –b TRUE

W hat is the result?

A. true
B. null
C. false
D. C om p ilation fails.
E. T he cod e run s w ith no output.
F. A n exception is throw n at runtim e.

A nsw er: A
Q U EST IO N NO : 99
G iven:
11. int i = 0, j = 1;

-49 -
310 - 035

12. if ((i++ == 1) && (j++ == 2)) {


13. i = 42;
14 }
15. System.out.println(“i = “ + i + “, j = “ + j);

W hat is the result?

A. i = 1, j = 2
B. i = 1, j = 1
C. i = 42, j = 2
D. i = 42, j = 1
E. C om p ilation fails.

A nsw er: B

Q U EST IO N NO : 1 00
G iven:
1. public class X (
2. private static int a;
3. public static void main(String [] args) {
4. modify(a);
5. System.out.println(a);
6. }
7. public static void modify(int a) {
8. a++;
9. }
10 }

W hat is the result?

A. 0
B. 1
C. C om p ilation fails.
D. A n exception is throw n at runtim e.

A nsw er: A
Q U EST IO N NO : 1 01
G iven:
1. public class Test {
2. public static void add3 (Integer i) {
3. int val = i.intValue();
4. val += 3;

-50 -
310 - 035

5. i = new Integer(val);
6. }
7.
8. public static void main(String args[]) {
9. Integer i = new Integer(0);
10. add3(i);
11. System.out.println(i.intValue());
12 }
13 }

W hat is the result?

A. 0
B. 3
C. C om p ilation fails.
D. A n exception is throw n at runtim e.

A nsw er: A

Q U EST IO N NO : 1 02
G iven:
11. public static void main( String[] args ) {
12. Integer a = new Integer(10);
13. Integer b = new Integer(10);
14. Integer c = a;
15. int d = 10;
16. double e = 10.0;
17. }

W hich three evaluate to true? (C hoose three)

A. (a == c)
B. (d == e)
C. (b == d)
D. (a == b)
E. (b == c)
F. (d == 10.0)

A nsw er: A, B , F

Q U EST IO N NO : 103
G iven:

-51 -
310 - 035

11. String a = null;


12. a.concat(“abc”);
13. a.concat(“def”);
14. System.out.println(a);

W hat is the result?

A. abc
B. null
C. abcdef
D. C om p ilation fails.
E. T he cod e run s w ith no output.
F. A n exception is throw n at runtim e.

A nsw er: F
Explanation:
E xception in thread "m ain"
java.lang.N ullP ointerE xception
at X .m ain(X .java:12)

Q U EST IO N NO : 1 04
G iven that b and c refer to insta nces of w rapper classes, w hich tw o statem ents are
true? (Choose tw o)

A. b.equals(b) returns true.


B. b.equals(c) returns the sam e resu lt as b == c.
C. b.eqials(c) can return false even if c.equals(b) returns true.
D. b.equals(c) throw s an exception if b and c are different w rapp er types.
E. b.equals(c) return s false if the type of w rapper objects being c o m pared are
differen t.

A nsw er: B , C

Q U EST IO N NO : 1 05
G iven:
1. public class Test {
2. public static void main(String [] args) {
3. System.out.println(args.length > 4 &&
4. args[4].equals(“-d”));
5. }
6. }

If the program is invoked using the com m and line:

-52 -
310 - 035

java Test One Two Three –d

W hat is the result?

A. true
B. false
C. C om p ilation fails.
D. A n exception is throw n at runtim e.

A nsw er: D
T he correct answ er to th is question is D . T he args[4] generates a run tim e exception error
because there are only 4 strings and the expression args[4] p rints the 5 th String but like it w as
said earlier, there are only 4 strings.

Q U EST IO N NO : 1 06
G iven:
11. try {
12. if ((new Object))(.equals((new Object()))) {
13. System.out.println(“equal”);
14. )else{
15. System.out.println(“not equal”);
16. }
17. }catch (Exception e) {
18. System.out.println(“exception”);
19. }

W hat is the result?

A. equal
B. not equal
C. exception
D. C om p ilation fails.

A nsw er: D

Q U EST IO N NO : 1 07
W hich three dem o nstrate an “is a” relation ship? (Choo se three)

A . public class X { }
public class Y extends X { }
B . public interface Shape { }
public interface Rectangle extends Shape{ }
C . public interface Color { }
public class Shape { private Color color; }

-53 -
310 - 035

D . public interface Species { }


public class Animal { private Species species; }
E . public class Person { }
public class Employee {
public Employee(Person person) { }
F . interface Component { }
class Container implements Component {
private Component[] children;
}

A nsw er: A, B , F

Q U EST IO N NO : 1 08
G iven:
1. class BaseClass {
2. private float x = 1.of;
3. protected float getVar() { return x; }
4. }
5. class SubClass extends BaseClass {
6. private float x = 2.Of;
7. // insert code here
8. }

W hich tw o are valid exam ples of m etho d overridin g w hen inserted at line 7? (C hoose
tw o)

A. float getVar() { return x; }


B. public float getVar() { return x; }
C. public double getVar() { return x; }
D. protected float getVar() { return x; }
E. public float getVar(float f) { return f; }

A nsw er: B , D

Q U EST IO N NO : 1 09
G iven:
1. class A {
2. public byte getNumber() {
3. return 1;
4. }
5. }
6.
7. class B extends A {

-54 -
310 - 035

8. public short getNumber() {


9. return 2;
10. }
11.
12. public static void main(String args[]) {
13. B b = new B();
14. System.out.println(b.getNumber());
15. }
16. }

W hat is the result?

A. 1
B. 2
C . A n exception is throw n at runtim e.
D . C om p ilatio n fails because of a n error in lin e 8. E .
C o m p ilatio n fails because of a n error in lin e 14.

A nsw er: D
Explanation: getN u m b er() in B cannot override getN u m ber() in A ; attem pting to use
incom patible return type: sho rt to byte.

Q U EST IO N NO : 1 10
W hich tw o are benefits of fully encapsulating a class? (C hoose tw o)

A. P erform ance of class m ethods is im proved.


B. Im plem entation details of the class are hidd en.
C. A ccess m od ifiers can be o m itted on class data m em b ers.
D. C ode that uses the encap sulatio n class can access data m em bers directly.
E. Internal o p eratio n of th e class can be m odified w ithout im pacting clients of that class.

A nsw er: B , E
Q U EST IO N NO : 1 23
W hich tw o are valid declarations of a float? (Choose tw o)

A. float f = 1F;
B. float f = 1.0.;
C. float f = ‘1’;
D. float f = “1”;
E. float f = 1.0d;

A nsw er: A , C

Q U EST IO N NO : 1 24
G iven:
1. public class Test {
2. private static int[] x;
3. public static void main(String[] args) {
4. System.out.println(x[0]);
5. }
6. }

W hat is the result?

A. 0
B. null
C. C om p ilation fails.
D. A NullPointerException is thro w n at runtim e.
E. A n ArrayIndexOutOfBoundsException is throw n at runtim e.

A nsw er: D

Q U EST IO N NO : 1 25
G iven:
1. public class Test {
2. public static void main( String[] args) {
3. String foo = args[1];

-62 -
310 - 035

4. String bar = args[2];


5. String baz = args[3];
6. System.out.println(“baz = “ + baz);
7. }
8. }

A nd the com m a nd line invocation:


java Test red green blue

W hat is the result?

A. baz =
B. baz = null
C. baz = blue
D. C om p ilation fails.
E. A n exception is throw n at runtim e.

A nsw er: E
EX PLA N ATIO N : A java.lang.A rrayIndexO u tO fB oundsE xcep tion is throw n because of lin e
3, should b e args[0].
Q U EST IO N NO : 1 27
G iven:
11. public static void main(String[] args) {
12. Object obj = new Object() {
13. public int hashCode() {
14. returns 42;
15. }
15. };
17. System.out.println(obj.hashCode());

-63 -
310 -
035

18. }

W hat is the result?

A . 42
B . A n exception is throw n at runtim e.
C . C o m p ilatio n fails b ecause of a n error on lin e
12. D . C om p ilatio n fails because of a n error on
line 16. E . C o m pilatio n fails because of a n error
on line 17.

A nsw er: A
Q U EST IO N NO : 1 28
W hich tw o are reserved w ords in the Java program m in g language? (C hoose tw o)

A. run
B. import
C. default
D. implement

A nsw er: B , C

You might also like