• Parth

OCJP/SCJP Questions From Language Fundamental Topic

Q: 1 Given

10. class Foo{

11. static void alpha(){/*more code here*/}

12. void beta(){/* more code here*/} 13. }

Which two statements are true?(Choose two.)

A. Foo.beta() is a valid invocation of beta().

B. Foo.alpha() is a valid invocation of alpha().

C. Method beta() can directly call method alpha().

D. Method alpha() can directly call method beta().



Q: 2 Given:

12. public class CodePlanet {

13.

14.static public void main(String[] args){

15. for(int x=1; x<args.length; x++)

16. System.out.print(args[x] +” ”);

17. }

18. }

19. }

And the command line invocation:

Java CodePlanet a b c

What is the result?

A. a b

B. b c

C. a b c

D. Compilation Fails

E. An Exception thrown at runtime.

Q: 3 Given:

15. public class CodePlanet {

16. public static void main(String[] args){

17. for(int x=1; x<args.length; x++)

18. System.out.print(args[x]+” ”);

19. }

20. }

21. }

And two separate command line invocations:

Java CodePlanet

Java CodePlanet 1 2 3 4

What is the results?

A. No output is produced.

1 2 3

B. No output is produced

2 3 4

C. No output is produced.

1 2 3 4

D. An Exception is thrown at runtime.

1 2 3

Code Planet Technologies, Pratap Nagar ,Jaipur Mob-9806584240

– 3 –

Code Planet Technologies OCJP Material

E. An Exception is thrown at runtime.

2 3 4

F. An Exception is thrown at runtime.

1 2 3 4

Q: 4 Given a class CodePlanet:

1. package utils;

2.

3. public class CodePlanet {

4. public static String twice(String s) { return s + s;}

5. }

And given another class Technologies

1. //insert code here

2.

3. public class Technologies

4. public static void main(String[] args)

5. System.out.println(twice(“pizza”));

6. }

7. }

Which code should be inserted at line 1 of Technologies.java to compile and run Technologies to print

“pizzapizza” ?

A. import utils.*;

B. static import utils.*;

C. import utils.CodePlanet.*;

D. static import utils. CodePlanet.*;

E. import utils. CodePlanet.twice();

F. import static utils. CodePlanet.twice;

G. static import utils. CodePlanet.twice;

Q: 5 A JavaBeans component has the following field:

11.private boolean enabled;

Which two pairs of method declarations follow the JavBeans standard for accessing this

Field? (Choose two.)

A. public void setEnabled(boolean enabled)

public boolean getEnabled()

B. public void setEnabled(boolean enabled)

public void isEnabled

C. public void setEnabled(boolean enabled)

public boolean isEnabled()

D. public boolean setEnabled(boolean enabled)

public boolean getEnabled

Q: 6 Given classes defined in two different files:

1. package util;

2. public class BitUtils

3. public static void process(byte[]){/* more code here */}

4. }

1. package app

2. public class someApp{

3. public static void main(String[] args ) {

4.byte[] bytes =new byte[256];

5. //insert code here

6. }

7. }

Code Planet Technologies, Pratap Nagar ,Jaipur Mob-9806584240

– 4 –

Code Planet Technologies OCJP Material

What is required at line 5 in class somApp to use the process method of BitUtils

A. process(bytes);

B. BitUtils. process(bytes);

C. util.BitUtils. process(bytes);

D. SomeApp cannot use methods in BitUtils

E. import util.BitUtils.*; process(bytes);

Q: 7 Given

enum Example { ONE, TWO ,THREE}

Which statement is true?

A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.

B. The expression (ONE <TWO) is guaranteed to be trueand ONE>compareTo(TWO) is guaranteed to be

less than one.

C. The Example values cannot be used in a raw java.util.HashMap; instead the programmer must use a

java.util.EnumMap.

D. The Example values can be used in a java.util.SortedSet, but the set will not be sorted because

enumerated types do not implement java.lang.Comparable .

Q: 8 Given:

11. public abstract class Shape {

12. private int x;

13. private int y;

14. public abstract void draw();

15. public void set Anchor(int x, int y){

16. this.x =x;

17.this.y= y;

18. }

19. }

Which two classes use the Shape class correctly? (Choose two)

A. public class Circle implements Shape {

private int radius;

}

B. public abstract class Circle extends Shape {

private int radius;

}

C. public class Circle extends Shape {

private int radius;

public void draw(); }

D. public abstract class Circle implements Shape {

private int radius ;

public void draw(); }

E. public class Circle extends Shape {

private int radius;

public void draw(){/* more code here */}

F. public abstract class Circle implements Shape {

private int radius;

 public void draw(){/* more code here */}

Q: 09 Given:

10. class Nav{

11. public enum Direction { NORTH, SOUTH , EAST ,WEST}

Code Planet Technologies, Pratap Nagar ,Jaipur Mob-9806584240

– 5-

Code Planet Technologies OCJP Material

12. }

13. pubic class Sprite {

14. //insert code here

15. }

Which code, inserted at line 14, allows the Sprite class to compile?

A. Direction d= NORTH;

B. Nav.Direction d=NORTH;

C. DIRECTION d =Direction.NORTH;

D. Nav.Direction d=NAv.Direction.NORTH

Q: 10 Click the exhibit button

Which three statements are true? (Choose Three.)

10. interface Foo{

11.  int bar();

13. }

14. public class Beta {

15.

16. class A implements Foo {

17.  public int bar() { return 1; }

18. }

19.

20. public int fubar(Foo foo) {

21.  return foo.bar(); }

22. public void testFoo() {

23.

24. class A implements Foo {

25.  public int bar() { return 2; }

26. }

27.

28.  System.out.println( fubar( new A() ));

29. }

30.

31. public static void main(String[] args)

32. {

33. new Beta().testFoo();

34. }

35. }

A.  Compilation Fails.

B. The code compiles and the output is 2.

C. If lines 16,17 and 18 were removed, compilation would fail.

D. If lines 24,25 and 26 were removed, compilation would fail.

E. If lines 16,17 and 18 were removed, the could would compile and the output would be 2.

F. If lines 24,25 and 26 were removed, the could would compile and the output would be 1

ANSWERS

1. B, C 2. B 3. B 4. F 5. A, C 6. C 7. A 8. B, E 9. D 10. B, E, F