Chapter 2 Working with Java Datatypes

¡Supera tus tareas y exámenes ahora con Quizwiz!

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: car[name]

A: cat.name

1. Which of the following declarations does not compile ? A: double num1, int num2 = 0; B: int num1, num2; C: int num1, num2 = 0; D: int num1 = 0, num2 = 0;

A: double num1, int num2 = 0;

44. Which is the most common way to fill in the blank to implement this method? public class Pengion { 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.

A: p.beakLength = b;

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

B: 2blue

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

B: FooBar variable class constant constant

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.

B: double num = 2._718;

14. Which of the following lines contains a compiler error? String title = "Weather"; // line x1 int hot, double cold; // line x2 System.out.print(hot + " " + title); // line x3 A: x1 B: x2 C: x3 D: None of the above

B: x2

38. How may of the following lines compile? int i = null; Integer in = null; String s = null; A: None B: One C: Two D: Three

C: Two Objects may ave a null reference.

19. Diagram Here A: B: C: D:

Diagram Here

39. Diagram here A: B: C: D:

Diagram here

1: A 2: D 3: B 4: B 5: B 6: C 7: C 8: C 9: C 10: C 11: D 12: A 13: D 14:B 15: C 16: A 17: A 18: D 19: C 20: C 21: B 22 B 23: C 24: D 25: C 26: C 27: B 28: C 29: D 30: A 31: C 32: A 33: A 34: D 35: C 36: B 37: B 38: C 39: C 40: B 41: A 42: A 43: B 44: A 45: B 46: B 47: C 48: A 49: B 50:C

1: A 2: D 3: B 4: B 5: B 6: C 7: C 8: C 9: C 10: C 11: D 12: A 13: D 14:B 15: C 16: A 17: A 18: D 19: C 20: C 21: B 22 B 23: C 24: D 25: C 26: C 27: B 28: C 29: D 30: A 31: C 32: A 33: A 34: D 35: C 36: B 37: B 38: C 39: C 40: B 41: A 42: A 43: B 44: A 45: B 46: B 47: C 48: A 49: B 50:C

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

A: I: String cat = "animal", dog = "animal";

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 may be called exactly once. D: It may be called one or more times.

A: It may be called zero or one times.

16. Of the types double, int, and short, how many could fill in the blank to have this code output 0? public static void main(String[] args) { ______ defaultValue; System.out.print(defaultValue); } A: None B: One C: Two D: Three

A: None Local variables are not initialized.

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.

A: Primitive types begin with a lowercase letter.

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.

A: They can be set to null.

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

A: byte, char, float, double byte 8 boolean T or F char 16 double 64 float 32 int 32 long 64 short 16

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

A: byte, short, int, long

48. Which of the following is not a possible output of this code, assuming it runs to completion? 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-clean-play- D: play-play-clean-clean-

A: play- Finalize > exit.

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

B: Call the constructor of the wrapper class

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.

B: It defaults to null.

46. How many objects are eligible for garbage collection right before the end of the main method? 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.yougestChild = 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

B: One On line 9 all 3 objects have references. On line 10 elena is given reference to zoe (replaces reference on line 7). Notice the wording here, right before the end of the main method, so before line 11. On line 10 the diana object is eligible for garbage collection.

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.

B: You can convert a primitive to a wrapper class object simply by assigning it.

22. Suppose foo is a reference to an instance of a class. Which of the following is 'NOT' true about 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.

B: bar is a local variable. Foo foo = new Foo(); foo.bar dot notation used to reference instance variable

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.print(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

B: constructor Super SI SV II IV CONSTRUCTOR

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

B: double, Double A constructor can only be called with a class name rather than a primitive. Autoboxing.

45. Fill in the blanks to indicate whether a primitive or wrapper class can be assigned without 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

B: int, Integer

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

B: k2 int is a primary variable name and is reserved. It cannot be used as a variable 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[] args) { 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-

B: play-play-

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

C: Instantiates a new object

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

C: Integer

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.

C: It does not have a default value.

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 and exception at runtime.

C: The code does not compile Instance method being called on a primitive.

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.print(a + b); } } A: 4 B: 5 C: The code does not compile. D: The code compiles but throws an exception at runtime.

C: The code does not compile.

26. Of the types double, int, long, and short, how many could fill in the blank to have this code output 0 ? static _____ defaultValue; public static void main(String[] args) { System.out.println(defaultValue); } A: One B: Two C: Three D: Four

C: Three

35. How many of the String objects are eligible for garbage collection right before the end of the main method? public static void main(String[] args) { 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

C: Two

6. How many of the following methods compile ? public String convert (int value) { return value.toString(); } public String convert(Integer value) { return value.toString(); } public String convert(Object value) { return value.toString(); } A: None B: One C: Two D: Three

C: Two

15. How many instance initializers are in this code? 1: public class Bowling { 2: { System.out.print(); } 3: public Bowling () { 4: System.out.print(); 5: } 6: static { System.out.print(); } 7: { System.out.print(); } 8: } A: None B: One C: Two D: Three

C: Two 3 - 5 is a constructor

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.

C: aab

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

C: char and int

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

C: class 5MainSt {}

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

C: double double is larger than float. java automatically widens the type. java requires a suffix of f to be used to identify a float. Also by default java assigns pi as a double.

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.

C: int num = _9_99;

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() {}

C: public TennisBall() {}

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

D: None of the above

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.

D: None of the above.

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

D: None of the above: all orders are valid

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

D: String

2. What is the output of the following ? 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.

D: The code does not compile.

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

D: float f2 = 5.0; //p4 Java uses the suffix f to indicate a number is a float. Java automatically widens a type, allowing a float to be assigned to either a float or a double. This makes lines p1 and p3 both compile because a float is assigned to a double and a float. Since double is larger than float when it widens a float needs to have the suffix. So line p2 is fine since double is larger. p4 needs to have a suffix since it will be widened and considered to be a double without the suffix f.

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();

D: new TennisBall(); The new keyword is used to call a constructor. When using a new keyword you need to include the parenthesis. Option b is used to call a method.


Conjuntos de estudio relacionados

Macro - 21.1 Defining and Computing Unemployment Rate and Patterns in Unemployment

View Set

Physical and Political Features of Europe

View Set

Diseases of the Newborn 3-A, 3-B TORCH infections

View Set

Domain 3 & 4 Practice Calculations

View Set

Exam: 03.08 Electricity Module Exam

View Set

Combo with "Chapter 22: Physiologic and Behavioral Adaptations of the Newborn" and 5 others

View Set

Module 7 Study Questions CYBR 155

View Set