OCJP Practice Papers - Working with Java Data Types

1. Which of the following declarations does not compile?

<br/>A. double num1, int num2 = 0;
<br/>B. int num1, num2;
<br/>C. int num1, num2 = 0;
<br/>D. int num1 = 0, num2 = 0;
<p>
<strong>2. What is the output of the following?</strong>
public static void main(String... args) {
String chair, table = "metal";
chair = chair + table;
System.out.println(chair);
}


A. metal
B. metalmetal
C. nullmetal
D. The code does not compile.

3. Which is correct about an instance variable of type String?
A. It defaults to an empty string.
B. It defaults to null.
C. It does not have a default value.
D. It will not compile without initializing on the declaration line.

4. Which of the following is not a valid variable name?
A. _blue
B. 2blue
C. blue$
D. Blue

5. Which of these class names best follows standard Java naming conventions?
A. fooBar
B. FooBar
C. FOO_BAR
D. F_o_o_B_a_r

6. How many of the following methods compile?

public String convert(int value) {
return value.toString();
}p
ublic String convert(Integer value) {
return value.toString();
}p
ublic String convert(Object value) {
return value.toString();
}

A. None
B. One
C. Two
D. Three

7. Which of the following does not compile?
A. int num = 999;
B. int num = 9_9_9;
C. int num = _9_99;
D. None of the above; they all compile.

8. Which of the following is a wrapper class?
A. int
B. Int
C. Integer
D. Object

9. What is the result of running this code?

public class Values {
integer a = Integer.valueOf("1");
public static void main(String[] nums) {
integer a = Integer.valueOf("2");
integer b = Integer.valueOf("3");
System.out.println(a + b);
}
}

A. 4
B. 5
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

10. Which best describes what the new keyword does?
A. Creates a copy of an existing object and treats it as a new one
B. Creates a new primitive
C. Instantiates a new object
D. Switches an object reference to a new one

11. Which is the first line to trigger a compiler error?

double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4

A. p1
B. p2
C. p3
D. p4

12. Which of the following lists of primitive types are presented in order from smallest tolargest data type?
A. byte, char, float, double
B. byte, char, double, float
C. char, byte, float, double
D. char, double, float, bigint

13. Which of the following is not a valid order for elements in a class?
A. Constructor, instance variables, method names
B. Instance variables, constructor, method names
C. Method names, instance variables, constructor
D. None of the above: all orders are valid.

14. Which of the following lines contains a compiler error?

String title = "Weather"; // line x1
int hot, double cold; // line x2
System.out.println(hot + " " + title); // line x3

A. x1
B. x2
C. x3
D. None of the above

15. How many instance initializers are in this code?

1: public class Bowling {
2: { System.out.println(); }
3: public Bowling () {
4: System.out.println();
5: }
6: static { System.out.println(); }
7: { System.out.println(); }
8: }

A. None
B. One
C. Two
D. Three

16. Of the types double, int, and short, how many could fill in the blank to have this codeoutput 0?

public static void main(String[] args) {
_______defaultValue;
System.out.println(defaultValue);
}

A. None
B. One
C. Two
D. Three

17. What is true of the finalize() method?
A. It may be called zero or one times.
B. It may be called zero or more times.
C. It will be called exactly once.
D. It may be called one or more times.

18. Which of the following is not a wrapper class?
A. Double
B. Integer
C. Long
D. String

19. Suppose you have the following code. Which of the images best represents the state ofthe references right before the end of the main method, assuming garbage collectionhasnt run? Figure 1

1: public class Link {
2: private String name;
3: private Link next;
4: public Link(String name, Link next) {
5: this.name = name;
6: this.next = next;
7: }
8: public void setNext(Link next) {
9: this.next = next;
10: }
11: public Link getNext() {
12: return next;
13: }
14: public static void main(String... args) {
15: Link link1 = new Link("x", null);
16: Link link2 = new Link("y", link1);
17: Link link3 = new Link("z", link2);
18: link2.setNext(link3);
19: link3.setNext(link2);
20: link1 = null;
21: link3 = null;
22: }
23: }

A. Option A
B. Option B
C. Option C
D. Option D

20. Which type can fill in the blank? _______ pi = 3.14;
A. byte
B. float
C. double
D. short

21. What is the first line in the following code to not compile?

public static void main(String[] args) {
int Integer = 0; // k1
Integer int = 0; // k2
Integer ++; // k3
int++; // k4
}

A. k1
B. k2
C. k3
D. k4

22. Suppose foo is a reference to an instance of a class. Which of the following is not trueabout foo.bar?
A. bar is an instance variable.
B. bar is a local variable.
C. It can be used to read from bar.
D. It can be used to write to bar.

23. Which of the following is not a valid class declaration?
A. class building {}
B. class Cost$ {}
C. class 5MainSt {}
D. class _Outside {}

24. Which of the following can fill in the blanks to make this code compile?

_______d = new_______ (1_000_000_.00);

A. double, double
B. double, Double
C. Double, double
D. None of the above

25. Which is correct about a local variable of type String?
A. It defaults to an empty string.
B. It defaults to null.
C. It does not have a default value.
D. It will not compile without initializing on the declaration line.

26. Of the types double, int, long, and short, how many could fill in the blank to have thiscode output 0?

static _______defaultValue;
public static void main(String[] args) {
System.out.println(defaultValue);
}

A. One
B. Two
C. Three
D. Four

27. Which of the following is true about primitives?
A. You can call methods on a primitive.
B. You can convert a primitive to a wrapper class object simply by assigning it.
C. You can convert a wrapper class object to a primitive by calling valueOf().
D. You can store a primitive directly into an ArrayList.

28. What is the output of the following?

Integer integer = new Integer(4);
System.out.print(integer.byteValue());
System.out.print("-");
int i = new Integer(4);
System.out.print(i.byteValue());

A. 4-0
B. 4-4
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

29. Given the following code, fill in the blank to have the code print bounce.

public class TennisBall {
public TennisBall() {
System.out.println("bounce");
}
public static void main(String[] slam) {
_____________________
}
}

A. TennisBall;
B. TennisBall();
C. new TennisBall;
D. new TennisBall();

30. Which of the following correctly assigns animal to both variables? I. String cat = "animal", dog = "animal"; II. String cat = "animal"; dog = "animal"; III. String cat, dog = "animal"; IV. String cat, String dog = "animal";
A. I
B. I, II
C. I, III
D. I, II, III, IV

31. Which two primitives have wrapper classes that are not merely the name of theprimitive with an uppercase letter?
A. byte and char
B. byte and int
C. char and int
D. None of the above

32. Which of the following is true about String instance variables?
A. They can be set to null.
B. They can never be set from outside the class they are defined in.
C. They can only be set in the constructor.
D. They can only be set once per run of the program.

33. Which statement is true about primitives?
A. Primitive types begin with a lowercase letter.
B. Primitive types can be set to null.
C. String is a primitive.
D. You can create your own primitive types.

34. How do you force garbage collection to occur at a certain point?
A. Call System.forceGc()
B. Call System.gc()
C. Call System.requireGc()
D. None of the above

35. How many of the String objects are eligible for garbage collection right before the endof the main method?

public static void main(String[] fruits) {
String fruit1 = new String("apple");
String fruit2 = new String("orange");
String fruit3 = new String("pear");
fruit3 = fruit1;
fruit2 = fruit3;
fruit1 = fruit2;
}

A. None
B. One
C. Two
D. Three

36. Which of the following can fill in the blanks to make this code compile? _______d = new_______ (1_000_000.00);
A. double, double
B. double, Double
C. Double, double
D. None of the above

37. What does the following output?

1: public class InitOrder {
2: public String first = "instance";
3: public InitOrder() {
4: first = "constructor";
5: }
6: { first = "block"; }
7: public void print() {
8: System.out.println(first);
9: }
10: public static void main(String... args) {
11: new InitOrder().print();
12: }
13: }

A. block
B. constructor
C. instance
D. The code does not compile.

38. How many of the following lines compile?

int i = null;
Integer in = null;
String s = null;

A. None
B. One
C. Two
D. Three

39. Which pairs of statements can accurately fill in the blanks in this table?

Variable Type Can be called within the class from what type of method
Instance Blank 1:_______
Static Blank 2: _______

A. Blank 1: an instance method only, Blank 2: a static method only
B. Blank 1: an instance or static method, Blank 2: a static method only
C. Blank 1: an instance method only, Blank 2: an instance or static method
D. Blank 1: an instance or static method, Blank 2: an instance or static method

40. Which of the following does not compile?
A. double num = 2.718;
B. double num = 2._718;
C. double num = 2.7_1_8;
D. None of the above; they all compile.

41. Which of the following lists of primitive numeric types is presented in order fromsmallest to largest data type?
A. byte, short, int, long
B. int, short, byte, long
C. short, byte, int, long
D. short, int, byte, long

42. Fill in the blank to make the code compile:

package animal;
public class Cat {
public String name;
public static void main(String[] meow) {
Cat cat = new Cat();
______________ = "Sadie";
}
}

A. cat.name
B. cat-name
C. cat.setName
D. cat[name]

43. Which of the following is the output of this code, assuming it runs to completion?

package store;
public class Toy {
public void play() {
System.out.print("play-");
}
public void finalizer() {
System.out.print("clean-");
}
public static void main(String[] fun) {
Toy car = new Toy();
car.play();
System.gc();
Toy doll = new Toy();
doll.play();
}
}

A. play-
B. play-play-
C. play-clean-play-
D. play-play-clean-clean-

44. Which is the most common way to fill in the blank to implement this method?

public class Penguin {
private double beakLength;
public static void setBeakLength(Penguin p, int b) {
_____________________________
}
}

A. p.beakLength = b;
B. p['beakLength'] = b;
C. p[beakLength] = b;
D. None of the above

45. Fill in the blanks to indicate whether a primitive or wrapper class can be assignedwithout the compiler using the autoboxing feature.

_______first = Integer.parseInt("5");
_______second = Integer.valueOf("5");

A. int, int
B. int, Integer
C. Integer, int
D. Integer, Integer

46. How many objects are eligible for garbage collection right before the end of the mainmethod?

1: public class Person {
2: public Person youngestChild;
3:
4: public static void main(String... args) {
5: Person elena = new Person();
6: Person diana = new Person();
7: elena.youngestChild = diana;
8: diana = null;
9: Person zoe = new Person();
10: elena.youngestChild = zoe;
11: zoe = null;
12: }
13: }

A. None
B. One
C. Two
D. Three

47. Which is a valid constructor for this class?

public class TennisBall {
}

A. public TennisBall static create() { return new TennisBall(); }
B. public TennisBall static newInstance() { return new TennisBall():}
C. public TennisBall() {}
D. public void TennisBall() {}

48. Which of the following is not a possible output of this code, assuming it runs tocompletion?

package store;
public class Toy {
public void play() {
System.out.print("play-");
}
public void finalize() {
System.out.print("clean-");
}
public static void main(String[] args) {
Toy car = new Toy();
car.play();
System.gc();
Toy doll = new Toy();
doll.play();
}
}

A. play-
B. play-play-
C. play-play-clean-
D. play-play-clean-clean-

49. Which converts a primitive to a wrapper class object without using autoboxing?
A. Call the asObject() method
B. Call the constructor of the wrapper class
C. Call the convertToObject() method
D. Call the toObject() method

50. What is the output of the following?

package beach;
public class Sand {
public Sand() {
System.out.print("a");
}
public void Sand() {
System.out.print("b");
}
public void run() {
new Sand();
Sand();
}
public static void main(String... args) {
new Sand().run();
}
}

A. a
B. ab
C. aab
D. None of the above

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency