Chapter 1 Java Building Blocks Flashcards

Java Basics Working with Java Data Types

1
Q

What is an object?

A

An object is a runtime instance of a class in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two primary elements in Java classes?

A

Methods and fields. Together these are called the members of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

[Blank] holds the state of the program.

A

Variables (Fields)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

[Blank] operates on the state of the program.

A

Methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Java calls a word with special meaning a [Blank].

A

Keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Public classes in Java means?

A

The class can be used by other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A method is

A

an operation that can be called again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Return type void means

A

that no value at all is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Methods that require information be supplied to it from the calling method is called a [Blank]

A

parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The full declaration of a method is called [Blank]

A

method signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the the three type of comments?

A
Single line comments: 
//

Multiple line comment:
/*
*
*/

Javadoc comment
/**
 *
*
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a main() method?

A

A Java program begins execution with its main() method. It is the gateway between the startup of a Java process, which is managed by the JVM (Java Virtual Machine).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The file must have what extension to be able to be compiled as Java code?

A

.java

The name of the file must also match the name of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The compiler turns a .java file into what type of file?

A

.class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is bytecode?

A

Bytecode is a class file which consists of instructions that the JVM knows how to execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is method access modifier?

A

It declares the method’s level of exposure to potential callers in the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

The keyword [Blank] binds a method to its class so it can be called by just the class name.

A

static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

[Blank] is a fixed size list of items that are all the same type.

A

Arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Command line arguments are treated as [Blank] objects, even if they represent another data type.

A

String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

You need to have [Blank] to compile because it includes a compiler.

A

JDK (Java Development Kit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

You need [Blank] to run the code.

A

JRE (Java Run-time Environment)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Java class runs on [Blank] and therefore runs on any machine with Java rather than just the machine or operating system they happened to have been compiled on.

A

JVM (Java Virtual Machine)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Java code is organized into folders called [Blank]

A

packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does import statement do?

A

Import statement tell Java which packages to look in for classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Wildcard (*) imports

A

all packages that matches classes in the package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What package is automatically imported?

A

java.lang

package contains the classes that are fundamental to the design of the Java programming language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

[Blank] keyword is needed to create an instance of a class.

A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

[Blank] is a special type of method that creates a new object.

A

Constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Two key points to note about a constructor:

A

Name of constructor must match the name of the class.

There are no return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the purpose of a constructor.

A

Initialize fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Are you required to code a constructor?

A

No, the compiler will supply a “do nothing” default constructor for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Codes between braces are call a

A

code block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Instance initializers are

A

code blocks that appears outside a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What are the two type of data?

A

Primitive type

Reference type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are the 8 primitive data type?

A
Byte
Short
Int
Float
Double
Long
Boolean 
Char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Boolean Type

A

True and False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Byte Type and Size

A

Integral Value and 8-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Short Type and Size

A

Integral Value and 16-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Int Type and Size

A

Integral Value and 32-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Long Type and Size

A

Integral Value and 64-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Float Type and Size

A

Floating Point Value and 32-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Double Type and Size

A

Floating Point Value and 64-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Char Type and Size

A

Unicode Value and 16-bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Reference types refer to the [Blank]

A

Object (An instance of a class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

A reference “points” to an object by storing the [Blank] where the object is located/

A

memory address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

A value can be assigned to the reference in which two way?

A
  1. A reference can be assigned to another object of the same type.
  2. A reference can be assigned to a new object using the new keyword.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Can a reference type be assigned to null?

A

Yes, it means it does not currently refer to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

Can a primitive type by assigned to null?

A

No, it will give you a compiler error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Primitive type naming convention

A

lowercase type names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Classes naming convention

A

Class names begin with an uppercase letter followed by CamelCase. Don’t start any
identifiers with $. The compiler uses this symbol for some files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

A [Blank] is a name for a piece of memory that stores data.

A

variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

When you declare a variable, you need to

A

state the variable type along with giving it a name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

Can you declare and initialize a variable in the same statement?

A

Yes, as long as it is all the same type. You can also initialize any or all of those value inline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

3 rules of identifier names.

A
  1. The name must begin with a letter or the symbol $ or _.
  2. Subsequent characters may also be numbers.
  3. You cannot use the same name as a Java reserved word.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is a reserved word?

A

A reserved word is a keyword that Java has reserved so that you are not allowed to use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Name some reserved words:

A
abstract            assert             boolean 
break                byte                case 
catch                 char                class 
const*                continue        default 
do                      double           else
enum                 extends          false 
final                    finally             float                    
for                      goto*              if 
implements       import            instanceof 
int                       interface        long
native                 new                null 
package            private            protected 
public                 return             short 
static                  strictfp            super 
switch                synchronized  this
throw                  throws            transient 
true                     try                   void 
volatile                while
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

Method and variables naming convention:

A

Method and variables names begin with a lowercase letter followed by CamelCase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

Java supports which character set:

A

Unicode: which has more than 45,000 characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

local variable

A

is a variable defined within a method. Local variables must be initialized
before use. They do not have a default value and contain garbage data until initialized. The
compiler will not let you read an uninitialized value.

60
Q

Variables that are not local variables are known as

A

Instance variables or class variables

61
Q

Instance variables are also called [Blank]

A

fields

62
Q

[Blank] variables are shared across multiple objects

A

Class

63
Q

Variables are class variables if they have the keyword [Blank] before it.

A

Static

64
Q

Does instance and class variables needs initialization

A

No, as soon as you declare these variables, they are given a default value.

65
Q

Boolean default initialization value

A

false

66
Q

byte, short, int, long default initialization value

A

0 (in the type’s bit-length)

67
Q

float, double default initialization value

A

0.0 (in the type’s bit-length)

68
Q

char default initialization value

A

‘\u0000’ (NUL)

69
Q

All object references (everything else) default initialization value

A

null

70
Q

Methods in scope local

A

Cannot be used outside the method

71
Q

Scope

A

Local Variables: in scope from declaration to end of block
instance Variables: in scope from declaration until object garbage collected
Class Variables: in scope from declaration until program ends

72
Q

Is package declaration required and where does it go in the file. How do you declare a package?

A

No, first line in the file, package abc;

73
Q

Is import declaration required and where does it go in the file. How do you declare an import?

A

No, immediately after a package, import java.util.*;

74
Q

Is class declaration required and where does it go in the file. How do you declare a class?

A

Yes, Immediately after the import, public class c;

75
Q

Is field declaration required and where does it go in the file. How do you declare a field?

A

No, anywhere inside a class, int value;

76
Q

Is method declaration required and where does it go in the file. How do you declare a method?

A

No, anywhere inside a class, void method();

77
Q

How does Java destroy objects?

A

Java automatically take care of it for you as it provides a garbage collector to automatically look for objects that aren’t needed anymore.

78
Q

Where in memory are Java objects stored?

A

Heap

79
Q

Is System.gc() guaranteed to run?

A

No. System.gc() simplys suggests that now might be a good time for Java to kick off a garbage collection run. Java is free to ignore the request.

80
Q

An object is no longer reachable when

A
  1. The object no longer has any references pointing to it.

2. All references to the object have gone out of scope.

81
Q

The reference is

A

a variable that has a name and can be used to access the contents of an object.

82
Q

[Blank] can be assigned to another reference, passed to a method, or returned from a method.

A

A reference

83
Q

[Blank] are the same size, no matter what their type is.

A

All references

84
Q

[Blank] sits on the heap and does not have a name. Therefore, you have no way to access an object except through a reference.

A

An object

85
Q

[Blank] cannot be assigned to another object, nor can an object be passed to a method or returned from a method. It is the object that gets garbage collected, not its reference.

A

An object

86
Q

This method gets called if the garbage collector tries to collect the object.

A

finalize()

If the garbage collector doesn’t run, the method doesn’t get called. If the garbage collector fails to collect the object and tries to run it again later, the method doesn’t get called a second time.

87
Q

[Blank] is only run when the object is eligible for garbage collection.

A

finalize()

88
Q

Benefits of Java

A
Object Oriented
Encapsulation
Platform Independent
Robust
Simple
89
Q

What is object oriented?

A

All code is defined in classes and most of those classes can be instantiated into objects.

90
Q

Encapsulation

A

Java supports access modifiers to protect data from unintended access and modification.

91
Q

Platform Independent

A

Java is an interpreted language because it gets compiled to bytecode. A key benefit is that Java code gets compiled once rather than needing to be recompiled for different operating systems.

92
Q

How is Java Robust?

A

One of the major advantages of Java over C++ is that it prevents memory leaks. Java manages memory on its own and does garbage collection automatically.

93
Q

How is is Java Simple?

A

Java was intended to be simpler than C++. In addition to eliminating pointers, it got rid of operator overloading. In C++, you could write a + b and have it mean almost anything.

94
Q

How is Java Secure?

A

Java code runs inside the JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.

95
Q

How do you reference classes from other packages?

A

You use import statements

96
Q

A wildcard ending an import statement means

A

you want to import all classes in that package.

97
Q

Constructors create [Blank]

A

Java Objects

98
Q

When an object is instantiated, [Blank] and [Blank] of codes are initialized first. Then the constructor is run.

A

fields and blocks

99
Q

[Blank] are the basic building blocks of Java

A

Primitive types

100
Q

Declaring a variable involves stating the [blank] type and giving the variable a name,

A

data

101
Q

[Blank] variables must be specifically initialized.

A

Local

102
Q

[Blank] maybe contain letters, numbers, $, or _.

A

Identifiers

103
Q

[Blank] may not begin with numbers.

A

Identifiers

104
Q

[Blank]

A

refers to that portion of code where a variable can be accessed.

105
Q

Depending on scope in Java, what are the three kind of variables?

A

Instance, class and local.

106
Q

[Blank] are nonstatic fields in your class

A

Instance variables

107
Q

[Blank] are the static field within a class

A

Class variables

108
Q

[Blank] are declared within a method.

A

Local variables

109
Q

Orders within file: Class declaration, fields, import statement, methods, package statement

A
package statement
import statement
class
fields / methods
110
Q

Garbage collectors are responsible for

A

removing objects from memory when they can never be used again.

111
Q

An object becomes eligible for garbage collection when there are

A

no more references to it or its references have all gone out of scope.

112
Q

Java code is [Blank], meaning all code is defined in class

A

object oriented

113
Q

[Blank] allows classes to encapsulate data.

A

Access modifiers

114
Q

Java is [Blank], compiling to bytecode.

A

platform independent

115
Q

Java is secure because it runs in a [Blank]

A

virtual environment

116
Q

Java is robust and simple because it does not

A

provide pointers or operator overloading

117
Q

Accessing arguments that wasn’t passed in will cause the code to

A

throw an exception

118
Q

A constructor has the same name as the

A

class

119
Q

Can multiple variables be declared and initialized in the same statement?

A

As long as they share the same type

120
Q

[Blank] variables require an explicit initialization

A

Local Variables

121
Q

Local variables go out of scope when

A

the block they are declared in ends.

122
Q

Instance variables go out of scope when

A

the object is garbage collected.

123
Q

Class variables go out of scope when

A

the program stops running.

124
Q
Which of the following are valid Java identifiers?
A. A$B
B. _helloWorld
C. true
D. java.lang
E. Public
F. 1980_s
A

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 _.

125
Q

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

A

D. Boolean fields initialize to false and references initialize to null, so empty is false
and brand is null. Brand = null is output.

126
Q
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.
A

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.

127
Q
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

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.

128
Q
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.
A
C, D. Option C is correct because it imports Jelly by classname. Option D is correct
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.
129
Q
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.
A

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 package,
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.

130
Q
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

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 compile.
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.

131
Q
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.
A

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.

132
Q
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

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.

133
Q

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.

A

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.

134
Q

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.

A

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 incorrect
because int primitives default to 0.

135
Q

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.

A

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.

136
Q

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

A, D. Options A and D are correct because boolean primitives default to false and
int primitives default to 0.

137
Q
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.
A

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,
just like variable names and other identifiers.

138
Q
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

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 underscore
is the last character.

139
Q
Given the following class, which of the following lines of code can replace INSERT CODE
HERE to make the code compile? (Choose all that apply)
public class Price {
public void admission() {
INSERT CODE HERE
System.out.println(amount);
} }
A. int amount = 9L;
B. int amount = 0b101;
C. int amount = 0xE;
D. double amount = 0xE;
E. double amount = 1_2_.0_0;
F. int amount = 1_2_;
G. None of the above.
A

B, C, D. 0b is the prefix for a binary value and is correct. 0x is the prefix for a hexadecimal
value. This value can be assigned to many primitive types, including int and
double, making options C and D correct. Option A is incorrect because 9L is a long
value. long amount = 9L would be allowed. Option E is incorrect because the underscore
is immediately before the decimal. Option F is incorrect because the underscore is
the very last character.

140
Q
Which of the following are true? (Choose all that apply)
public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
} }
A. Bunny is a class.
B. bun is a class.
C. main is a class.
D. Bunny is a reference to an object.
E. bun is a reference to an object.
F. main is a reference to an object.
G. None of the above.
A
A, E. Bunny is a class, which can be seen from the declaration: public class Bunny. bun
is a reference to an object. main() is a method.
141
Q
Which represent the order in which the following statements can be assembled into a program
that will compile successfully? (Choose all that apply)
A: class Rabbit {}
B: import java.util.*;
C: package animals;
A. A, B, C
B. B, C, A
C. C, B, A
D. B, A
E. C, A
F. A, C
G. A, B
A
C, D, E. package and import are both optional. If both are present, the order must
be package, then import, then class. Option A is incorrect because class is before
package and import. Option B is incorrect because import is before package. Option
F is incorrect because class is before package. Option G is incorrect because class is
before import.
142
Q

Suppose we have a class named Rabbit. Which of the following statements are true?
(Choose all that apply)
1: public class Rabbit {
2: public static void main(String[] args) {
3: Rabbit one = new Rabbit();
4: Rabbit two = new Rabbit();
5: Rabbit three = one;
6: one = null;
7: Rabbit four = one;
8: three = null;
9: two = null;
10: two = new Rabbit();
11: System.gc();
12: } }
A. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 6.
B. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 8.
C. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 12.
D. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 9.
E. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 11.
F. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 12.

A

B, D. The Rabbit object from line 3 has two references to it: one and three. The references
are nulled out on lines 6 and 8, respectively. Option B is correct because this
makes the object eligible for garbage collection after line 8. Line 7 sets the reference
four to the now null one, which means it has no effect on garbage collection. The Rabbit
object from line 4 only has a single reference to it: two. Option D is correct because
this single reference becomes null on line 9. The Rabbit object declared on line 10
becomes eligible for garbage collection at the end of the method on line 12. Calling
System.gc() has no effect on eligibility for garbage collection.

143
Q

What is true about the following code? (Choose all that apply)
public class Bear {
protected void finalize() {
System.out.println(“Roar!”);
}
public static void main(String[] args) {
Bear bear = new Bear();
bear = null;
System.gc();
} }
A. finalize() is guaranteed to be called.
B. finalize() might or might not be called
C. finalize() is guaranteed not to be called.
D. Garbage collection is guaranteed to run.
E. Garbage collection might or might not run.
F. Garbage collection is guaranteed not to run.
G. The code does not compile.

A

B, E. Calling System.gc() suggests that Java might wish to run the garbage collector.
Java is free to ignore the request, making option E correct. finalize() runs if an object
attempts to be garbage collected, making option B correct.

144
Q
What does the following code output?
1: public class Salmon {
2: int count;
3: public void Salmon() {
4: count = 4;
5: }
6: public static void main(String[] args) {
7: Salmon s = new Salmon();
8: System.out.println(s.count);
9: } }
A. 0
B. 4
C. Compilation fails on line 3.
D. Compilation fails on line 4.
E. Compilation fails on line 7.
F. Compilation fails on line 8.
A

A. While the code on line 3 does compile, it is not a constructor because it has a return
type. It is a method that happens to have the same name as the class. When the code
runs, the default constructor is called and count has the default value (0) for an int.

145
Q

Which of the following are true statements? (Choose all that apply)
A. Java allows operator overloading.
B. Java code compiled on Windows can run on Linux.
C. Java has pointers to specific locations in memory.
D. Java is a procedural language.
E. Java is an object-oriented language.
F. Java is a functional programming language.

A

B, E. C++ has operator overloading and pointers. Java made a point of not having
either. Java does have references to objects, but these are pointing to an object that can
move around in memory. Option B is correct because Java is platform independent.
Option E is correct because Java is object oriented. While it does support some parts of
functional programming, these occur within a class.

146
Q
Which of the following are true? (Choose all that apply)
A. javac compiles a .class file into a .java file.
B. javac compiles a .java file into a .bytecode file.
C. javac compiles a .java file into a .class file.
D. Java takes the name of the class as a parameter.
E. Java takes the name of the .bytecode file as a parameter.
F. Java takes the name of the .class file as a parameter.
A
C, D. Java puts source code in .java files and bytecode in .class files. It does not use
a .bytecode file. When running a Java program, you pass just the name of the class
without the .class extension.