one Flashcards
Which of the following are valid Java identifiers? (Choose all that apply) A. A$B B. _helloWorld C. true D. java.lang E. Public F. 1980_s
A, B, E. Option A is valid because you can use the dollar sign in identifiers. Option B is
valid because you can use an underscore in identifiers. Option C is not a valid identifier
because true is a Java reserved word. Option D is not valid because the dot (.) is not
allowed in identifiers. Option E is valid because Java is case sensitive, so Public is not
a reserved word and therefore a valid identifier. Option F is not valid because the first
character is not a letter, $, or _.
What is the output of the following program?
1: public class WaterBottle {
2: private String brand;
3: private boolean empty;
4: public static void main(String[] args) {
5: WaterBottle wb = new WaterBottle();
6: System.out.print(“Empty = “ + wb.empty);
7: System.out.print(“, Brand = “ + wb.brand);
8: } }
A. Line 6 generates a compiler error.
B. Line 7 generates a compiler error.
C. There is no output.
D. Empty = false, Brand = null
E. Empty = false, Brand =
F. Empty = null, Brand = null
D. Boolean fields initialize to false and references initialize to null, so empty is false
and brand is null. Brand = null is output.
Which of the following are true? (Choose all that apply) 4: short numPets = 5; 5: int numGrains = 5.6; 6: String name = "Scruffy"; 7: numPets.length(); 8: numGrains.length(); 9: name.length(); A. Line 4 generates a compiler error. B. Line 5 generates a compiler error. C. Line 6 generates a compiler error. D. Line 7 generates a compiler error. E. Line 8 generates a compiler error. F. Line 9 generates a compiler error. G. The code compiles as is.
B, D, E. Option A (line 4) compiles because short is an integral type. Option B (line
5) generates a compiler error because int is an integral type, but 5.6 is a floating-point
type. Option C (line 6) compiles because it is assigned a String. Options D and E (lines
7 and 8) do not compile because short and int are primitives. Primitives do not allow
methods to be called on them. Option F (line 9) compiles because length() is defined
on String.
Given the following class, which of the following is true? (Choose all that apply) 1: public class Snake { 2: 3: public void shed(boolean time) { 4: 5: if (time) { 6: 7: } 8: System.out.println(result); 9: 10: } 11: } A. If String result = "done"; is inserted on line 2, the code will compile. B. If String result = "done"; is inserted on line 4, the code will compile. C. If String result = "done"; is inserted on line 6, the code will compile. D. If String result = "done"; is inserted on line 9, the code will compile. E. None of the above changes will make the code compile.
A, B. Adding the variable at line 2 makes result an instance variable. Since instance
variables are in scope for the entire life of the object, option A is correct. Option B is
correct because adding the variable at line 4 makes result a local variable with a scope
of the whole method. Adding the variable at line 6 makes result a local variable with
a scope of lines 6–7. Since it is out of scope on line 8, the println does not compile and
option C is incorrect. Adding the variable at line 9 makes result a local variable with
a scope of lines 9 and 10. Since line 8 is before the declaration, it does not compile and
option D is incorrect. Finally, option E is incorrect because the code can be made to
compile.
Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile? (Choose all that apply) package aquarium; public class Tank { } package aquarium.jellies; public class Jelly { } package visitor; INSERT IMPORTS HERE public class AquariumVisitor { public void admire(Jelly jelly) { } } A. import aquarium.*; B. import aquarium.*.Jelly; C. import aquarium.jellies.Jelly; D. import aquarium.jellies.*; E. import aquarium.jellies.Jelly.*; F. None of these can make the code compile.
C, D. Option C is correct because it imports Jelly by classname. Option D is cor-rect because it imports all the classes in the jellies package, which includes Jelly. Option A is incorrect because it only imports classes in the aquarium package—Tank in this case—and not those in lower-level packages. Option B is incorrect because you cannot use wildcards anyplace other than the end of an import statement. Option E is incorrect because you cannot import parts of a class with a regular import statement. Option F is incorrect because options C and D do make the code compile.
Given the following classes, what is the maximum number of imports that can be removed and have the code still compile? package aquarium; public class Water { } package aquarium; import java.lang.*; import java.lang.System; import aquarium.Water; import aquarium.*; public class Tank { public void print(Water water) { System.out.println(water); } } A. 0 B. 1 C. 2 D. 3 E. 4 F. Does not compile.
E. The first two imports can be removed because java.lang is automatically imported.
The second two imports can be removed because Tank and Water are in the same pack-age, making the correct answer E. If Tank and Water were in different packages, one of
these two imports could be removed. In that case, the answer would be option D.
Given the following classes, which of the following snippets can be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply) package aquarium; public class Water { boolean salty = false; } package aquarium.jellies; public class Water { boolean salty = true; } package employee; INSERT IMPORTS HERE public class WaterFiller { Water water; } A. import aquarium.*; B. import aquarium.Water; import aquarium.jellies.*; C. import aquarium.*; import aquarium.jellies.Water; D. import aquarium.*; import aquarium.jellies.*; E. import aquarium.Water; import aquarium.jellies.Water; F. None of these imports can make the code compile.
A, B, C. Option A is correct because it imports all the classes in the aquarium package
including aquarium.Water. Options B and C are correct because they import Water by
classname. Since importing by classname takes precedence over wildcards, these com-pile. Option D is incorrect because Java doesn’t know which of the two wildcard Water classes to use. Option E is incorrect because you cannot specify the same classname in
two imports.
Given the following class, which of the following calls print out Blue Jay? (Choose all that
apply)
public class BirdDisplay {
public static void main(String[] name) {
System.out.println(name[1]);
} }
A. java BirdDisplay Sparrow Blue Jay
B. java BirdDisplay Sparrow “Blue Jay”
C. java BirdDisplay Blue Jay Sparrow
D. java BirdDisplay “Blue Jay” Sparrow
E. java BirdDisplay.class Sparrow “Blue Jay”
F. java BirdDisplay.class “Blue Jay” Sparrow
G. Does not compile
B. Option B is correct because arrays start counting from zero and strings with spaces
must be in quotes. Option A is incorrect because it outputs Blue. C is incorrect because
it outputs Jay. Option D is incorrect because it outputs Sparrow. Options E and F are
incorrect because they output Error: Could not find or load main class Bird-Display.class.
Which of the following legally fill in the blank so you can run the main() method from the command line? (Choose all that apply) public static void main( ) A. String[] _names B. String[] 123 C. String abc[] D. String _Names[] E. String... $n F. String names G. None of the above.
A, C, D, E. Option A is correct because it is the traditional main() method signature
and variables may begin with underscores. Options C and D are correct because the
array operator may appear after the variable name. Option E is correct because
varargs are allowed in place of an array. Option B is incorrect because variables are
not allowed to begin with a digit. Option F is incorrect because the argument must be
an array or varargs. Option F is a perfectly good method. However, it is not one that
can be run from the command line because it has the wrong parameter type.
Which of the following are legal entry point methods that can be run from the command
line? (Choose all that apply)
A. private static void main(String[] args)
B. public static final main(String[] args)
C. public void main(String[] args)
D. public static void test(String[] args)
E. public static void main(String[] args)
F. public static main(String[] args)
G. None of the above.
E. Option E is the canonical main() method signature. You need to memorize it.
Option A is incorrect because the main() method must be public. Options B and F
are incorrect because the main() method must have a void return type. Option C is
incorrect because the main() method must be static. Option D is incorrect because the
main() method must be named main.
Which of the following are true? (Choose all that apply)
A. An instance variable of type double defaults to null.
B. An instance variable of type int defaults to null.
C. An instance variable of type String defaults to null.
D. An instance variable of type double defaults to 0.0.
E. An instance variable of type int defaults to 0.0.
F. An instance variable of type String defaults to 0.0.
G. None of the above.
C, D. Option C is correct because all non-primitive values default to null. Option D is
correct because float and double primitives default to 0.0. Options B and E are incor-rect because int primitives default to 0.
Which of the following are true? (Choose all that apply)
A. A local variable of type boolean defaults to null.
B. A local variable of type float defaults to 0.
C. A local variable of type Object defaults to null.
D. A local variable of type boolean defaults to false.
E. A local variable of type boolean defaults to true.
F. A local variable of type float defaults to 0.0.
G. None of the above.
G. Option G is correct because local variables do not get assigned default values. The
code fails to compile if a local variable is not explicitly initialized. If this question
were about instance variables, options D and F would be correct. A boolean primitive
defaults to false and a float primitive defaults to 0.0.
Which of the following are true? (Choose all that apply)
A. An instance variable of type boolean defaults to false.
B. An instance variable of type boolean defaults to true.
C. An instance variable of type boolean defaults to null.
D. An instance variable of type int defaults to 0.
E. An instance variable of type int defaults to 0.0.
F. An instance variable of type int defaults to null.
G. None of the above.
A, D. Options A and D are correct because boolean primitives default to false and
int primitives default to 0.
Given the following class in the file /my/directory/named/A/Bird.java: INSERT CODE HERE public class Bird { } Which of the following replaces INSERT CODE HERE if we compile from /my/directory? (Choose all that apply) A. package my.directory.named.a; B. package my.directory.named.A; C. package named.a; D. package named.A; E. package a; F. package A; G. Does not compile.
D. The package name represents any folders underneath the current path, which is
named.A in this case. Option B is incorrect because package names are case sensitive, (I need to check this)
just like variable names and other identifiers.
Which of the following lines of code compile? (Choose all that apply) A. int i1 = 1_234; B. double d1 = 1_234_.0; C. double d2 = 1_234._0; D. double d3 = 1_234.0_; E. double d4 = 1_234.0; F. None of the above.
A, E. Underscores are allowed as long as they are directly between two other digits.
This means options A and E are correct. Options B and C are incorrect because the
underscore is adjacent to the decimal point. Option D is incorrect because the under-score is the last character.