OCJP Practice Papers - Working With Java Data Types
OCJP Practice Papers - Working with Java Data Types
1. Which of the following declarations does not compile?
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?
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?
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?
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?
A. x1
B. x2
C. x3
D. None of the above
15. How many instance initializers are in this code?
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?
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?
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?
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?
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?
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?
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.
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?
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?
A. block
B. constructor
C. instance
D. The code does not compile.
38. How many of the following lines compile?
A. None
B. One
C. Two
D. Three
39. Which pairs of statements can accurately fill in the blanks in this table?
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:
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?
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?
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.
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?
A. None
B. One
C. Two
D. Three
47. Which is a valid constructor for this class?
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?
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?
A. a
B. ab
C. aab
D. None of the above
OCJP Practice Test, Certification Path, Tips/Tricks
- Oracle (OCJP) Java Certification Exam
- Java Certification Tips And Tricks
- OCJP Practice Papers - Java Basics
- OCJP Practice Papers - Operators And Decision Constructs
- OCJP Practice Papers-A Advanced Java Class Design
- OCJP Practice Papers Creating And Using Arrays
- OCJP Practice Papers Using Loop Constructs
- OCJP Practice Papers - Generics And Collections
- OCJP Practice Papers - Lambda Built-In Functional Interfaces
- OCJP Practice Papers Java Class Design
- OCJP Practice Papers - Java Stream API
- OCJP Practice Papers - Exceptions And Assertions
- OCJP Practice Papers - Date/Time API
- OCJP Practice Papers - Java I/O Fundamentals
- OCJP Practice Papers - Working With Methods And Encapsulation
- OCJP Practice Papers - Working With Inheritance
- OCJP Practice Papers - Handling Exceptions
- OCJP Practice Papers - Selected Classes From Java API
- OCJP Practice Papers - Java File I/O Nio.2
- OCJP Practice Papers - Java Concurrency