7 Given: 1. class A { 2. A() { } 3. } 4. 5. class B extends A { 6 ...

Post on 10-Jan-2023

1 views 0 download

Transcript of 7 Given: 1. class A { 2. A() { } 3. } 4. 5. class B extends A { 6 ...

Q U ESTIO N NO : 7G iven:1. class A {2. A() { }3. }4.5. class B extends A {6. }W hich tw o statem ents are true? (Choose tw o)

A . C lass B ’s c onstructor is public .B . C lass B ’s c onstructor has no argum ents.C . C lass B ’s c onstructor includes a call to th is(). D .C lass B ’s constructor i ncludes a call to super().

Q U ESTIO N NO : 10Y ou w ant a class to have access to m em bers of another class in the sam e package. W hichis the m ost restrictive access that accom plishes this objective?

A . pub licB . privateC . p ro te ctedD . transien tE . defau lt access

Q U ESTIO N NO : 13G 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 ESTIO N NO : 14G 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 = 0B . x = 42C . C om pila tion fails because of a n erro r in lin e 2 of class T es t2 .D . C om pila tion fails because of a n erro r in lin e 3 of class T es t1 .E . C om pila tion fails because of an erro r in lin e 4 of class T es t2 .

Q U ESTIO N NO : 15G 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 ESTIO N NO : 20G 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 . value = 3B . value = 4C . value = 5D . value = 6E . value = 7F . value = 8

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

A . public int a []B . static int [] aC . public [] int aD . private int a [3]E . private int [3] a []F . public final int [] a

Q U ESTIO N NO : 33G 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,4B . 4 ,5C . 5 ,4D . 5 ,5E . C om pila tion fa ils .

Q U ESTIO N NO : 36G 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 aseB . B aseB aseC . C om pila tion fa ils .D . T he code run s w ith no output.E . A n excep tion is th row n at run tim e.

Q U ESTIO N NO : 39W 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 ESTIO N NO : 40

W hich tw o allow the class Thing 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, BQ U ESTIO N NO : 44G 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 . doneB . one tw o do neC . one tw o th ree doneD . one tw o th ree tw o th ree doneE . C om pila tion fa ils .

A nsw er: DQ U ESTIO N NO : 45W hich three statem ent s are true? (Choose three)

A . T he defau lt constructor in itializ es m ethod varia b les.B . T he defau lt constructor has the sam e access as its c lass.C . T he defau lt constructor in voked the no-arg constructor of the superclass.D . If a cla ss lacks a no-arg constructor, the com piler alw ays create s a defau lt constructor.E . T he com piler creates a defau lt constructo r only w hen there are no o ther constructors

for the class.

-24 -

310 - 035

A nsw er: B, C, E

Q U ESTIO N NO : 54Y ou w ant to lim it access to a m ethod of a public class to m em bers of t he sam e c lass.W hich access accom plishes this objective?

A . pub licB . privateC . p ro te ctedD . transien tE . defau lt access

A nsw er: B

Q U ESTIO N NO : 55G 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 . byteB . longC . charD . floatE . S hortF . L ong

A nsw er: A , CQ U ESTIO N NO : 60G 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 elloB . H ello FooC . H ello w orldD . C om pila tion fa ils .E . T he code does not run .

A nsw er: EQ U ESTIO N NO : 65G 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 . 0B . 2C . 4D . 6E . 9F . 13

A nsw er: DG 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 . 0B . 1C . 2D . C om pila tion fa ils .

A nsw er: CT his code is perfec tly lega l and the answ er is C .

Q U ESTIO N NO : 70G 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 ethods can be added to the definition of class Y ? (Choose 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 ESTIO N NO : 72Y ou w ant su bclasses in any package to hav e access to m em bers of a superclass . W hich isthe m ost restrict ive access that accom plishes this objective?

A . pub licB . privateC . p ro te ctedD . transien tE . defau lt access

A nsw er: C

Q U ESTIO N NO : 75G 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 AB . C om pila tion fa ils .C . A n excep tion is th row n a t line 2 .D . A n excep tion is th row n at line 6 .E . T he code executes w ith no output.

A nsw er: EQ U ESTIO N NO : 80In w hich tw o cases does the com pile r supply a defau lt constructor for class A? (Choosetw 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 ESTIO N NO : 85G 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 ESTIO N NO : 89G 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 = 0B . i = 3C . i = 4D . i = 5E . C om pila tion fa ils .

A nsw er: C

Q U ESTIO N NO : 91G 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: CQ U ESTIO N NO : 98G 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 . trueB . nullC . falseD . C om pila tion fa ils .E . T he code run s w ith no output.F . A n excep tion is th row n at run tim e.

A nsw er: A

Q U ESTIO N NO : 99G 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 = 2B . i = 1, j = 1C . i = 42, j = 2D . i = 42, j = 1E . C om pila tion fa ils .

A nsw er: B

Q U ESTIO N NO : 1 00G 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 . 0B . 1C . C om pila tion fa ils .D . A n excep tion is th row n at run tim e.

A nsw er: A

Q U ESTIO N NO : 1 01G 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 . 0B . 3C . C om pila tion fa ils .D . A n excep tion is th row n at run tim e.

A nsw er: A

Q U ESTIO N NO : 1 02G 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 ESTIO N NO : 103G 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 . abcB . nullC . abcdefD . C om pila tion fa ils .E . T he code run s w ith no output.F . A n excep tion is th row n at run tim e.

A nsw er: FExplanation:E xcep tion in th read "m ain"java.lang .N u llP oin terE xceptionat X .m ain(X .java:12)

Q U ESTIO N NO : 1 04G iven that b and c refer to instances of w rapper classes, w hich tw o statem ents aretrue? (Choose tw o)

A . b.equals(b) re turns true.B . b.equals(c) re tu rns the sam e resu lt as b == c.C . b.eqials(c) can retu rn false even if c.equals(b) returns true.D . b.equals(c) th row s an excep tion if b and c are d iffe ren t w rapp er types.E . b.equals(c) re tu rn s false if the type of w rapper objects being c om pared are

d iffe ren t.

A nsw er: B , C

Q U ESTIO N NO : 1 05G 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 . trueB . falseC . C om pila tion fa ils .D . A n excep tion is th row n at run tim e.

A nsw er: DT he correc t answ er to th is question is D . T he args[4] genera tes a run tim e excep tion errorbecause there are only 4 strings and the expression args[4] p rin ts the 5 th S tring but l ike it w assaid earlier, there are on ly 4 strings.

Q U ESTIO N NO : 1 06G 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 . equalB . not equalC . exceptionD . C om pila tion fa ils .

A nsw er: D

Q U ESTIO N NO : 1 07W 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 ESTIO N NO : 1 08G 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 here8. }

W hich tw o are valid exam ples of m ethod overriding w hen inserted at line 7? (C hoosetw 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 ESTIO N NO : 1 09G 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 . 1B . 2C . A n excep tion is th row n at run tim e.D . C om pilatio n fa ils because of a n error in lin e 8 . E .C o m pilatio n fa ils because of a n error in lin e 14.

A nsw er: DExplanation: ge tN u m ber() in B canno t override getN u m ber() in A ; a ttem p ting to useinco m p atib le re turn type: sho rt to by te.

Q U ESTIO N NO : 1 10W hich tw o are benefits of fully encapsulating a class? (Choose tw o)

A . P erfo rm ance of class m ethods is im proved .B . Im p lem entation de tails o f the class are h idd en.C . A ccess m od ifiers can be om itted on class d ata m em bers .D . C ode that uses the encapsulation class can access da ta m em bers d irec tly .E . In ternal o peration o f the class can be m odified w ithou t im p acting c lien ts o f tha t class.

A nsw er: B , E

Q U ESTIO N NO : 1 23W 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 ESTIO N NO : 1 24G 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 . 0B . nullC . C om pila tion fa ils .D . A NullPointerException is throw n at runtim e.E . A n ArrayIndexOutOfBoundsException is th row n at runtim e .

A nsw er: D

Q U ESTIO N NO : 1 25G 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 = nullC . baz = blueD . C om pila tion fa ils .E . A n excep tion is th row n at run tim e.

A nsw er: EEX PLA N ATIO N: A java.lang .A rrayIndexO u tO fB oundsE xcep tion is throw n because of li ne3 , should b e args[0].

Q U ESTIO N NO : 1 27G 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 . 42B . A n excep tion is th row n at run tim e.C . C om pilatio n fa ils because of a n error on line12. D . C om pilatio n fa ils because of a n error onlin e 16 . E . C om pilatio n fa ils because of a n erroron line 17 .

A nsw er: A

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

A . runB . importC . defaultD . implement

A nsw er: B , C