Shared Flashcard Set

Details

SCJP 6 Chapter 3
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 3 - Assignments
27
Computer Science
Professional
02/23/2011

Additional Computer Science Flashcards

 


 

Cards

Term
What is the output?

static void a(int b)
{
b = 7;
}

public static void main()
{
int c = 5;
b(c);
System.out.println(c);
}
Definition
5
Term
Which is preferred?

int[] x;

or

int x[];
Definition
int[] x;
Term
What is the output?

static void main()
{
int x[4] = new int[4];
x[3] = 1;
x[0] = 1;
x[2] = 7;
x[1] = 5;

for (int i=0; i<3; i++)
{
System.out.println(" " + x[i]);
}
}
Definition
Does not compile. int x[4] is not legal.
Term
Which of the following will compile?

1) String random[][] = new String[9][]();
2) Integer [] ints = new Integer [] {7,8,9};
3) Integer [] ints2 = new Integer {7,8,9};
4) Integer [] ints3 = {7,8,9};
5) int [] bacon [] = new int [3][]{{4,7,2},{}};

6) int $[];
char[] $$;
$ = $$;

7) Car [] cars;
Nissan nissans[][];
cars = nissans;

8) New String[3] {“Dog”, null, “Bacon”};
Definition
only 2,4
1 – illegal ();
3 – missing [];
5 – cannot declare size when creating anonymous array
6 – cannot assign char array to an int array
7 – type Nissan can be assigned to type car, but arrays must be of the same dimension.
8 – Not declare size when constructing anonymous array

Pg(231-234)
Term
In what order will the following happen?

When the first line of the static main method creates an instance of the Child class:

1) Parent’s Static Initialization blocks
2) Child’s Static Initialization blocks
3) Parent’s Initialization blocks
4) Childs Initialization blocks
5) Parent’s Constructor(s)
6) Child’s Constructors(s)
Definition
Answer:
1,2,3,5,4,6

Pg(234-237)
Term
In what order will the following happen?
*What special case will cause a different order?

Typically when the first line of the static main method creates an instance of the parent class:

1) Parent’s Static Initialization blocks
2) Child’s Static Initialization blocks
3) Parent’s Initialization blocks
4) Childs Initialization blocks
5) Parent’s Constructor(s)
6) Child’s Constructors(s)
Definition
Answer:
1,3,5,
*If the main method is imbedded in the child object then the order would be:
1,2,3,5


pg(234-237)
Term
What are the two primary purposes of Java wrapper classes?
Definition
1) To provide a mechanism to wrap values in an object so that the values can be used for activities reserved for Objects such as the Collections API.
2) To provide an assortment of utility functions for primitives.
Term
What is the heap?
Definition
The part of memory where Java objects live, and the one and only part of memory that is in any way involved in the garbage collection process
(p. 255)
Term
When does the garbage collector run?
Definition
The JVM decides when to run the garbage collector. One can request that it run through the code, but there are no guarantees that it will do so.
(p. 256)
Term
When is an object eligible for garbage collection?
Definition
When no live thread can access it.
(p. 256)
Term
What are the three ways one can remove a reference to an object, thereby making it eligible for garbage collection?
Definition
1. Set the reference to null.
2. Set the reference variable to refer to another object
3. Isolate a reference (make them an island, so that even if two objects reference one another, those objects themselves cannot be reached by any thread).
(p. 257-259)
Term
What always runs just before an object is deleted by the garbage collector?
Definition
The finalize() method (inherited from Object)
(p. 263)
Term
Can finalize() result in saving an object from deletion?
Definition
Yes - in that method, one could write code that passes a reference to the object in question back to another object, effectively uneligibilizing the object for garbage collection.
(p. 263)
Term
T/F: The finalize() method can be called more than once
Definition
F: The finalize method is called only once (at most) by the garbage collector, and might never be called at all if the object is never eligible for deletion or the garbage collector never runs.
(p. 263)
Term
Default value for Boolean variables is True. (T/F?)
Definition
False
Term
Float initializes to 0.0F. (T/F)
Definition
False. 0.0
Term
class ArrayExample {
public static void main(String[] args) {
int x = -1;
int y = 0;
int z = 1;
int[] myArray = new int[3]
myArray[y] = -1;
myArray[x] = 0;
myArray[z] = 1;
System.out.println(myArray[2][0]);
} }

What will the following output?
a. -1
b. 0
c. Compilation Error
d. Runtime Error
e. ArrayIndexOutOfBoundsException
Definition
b. Runtime exception. x is a negative number. (pg 225)
Term
class ArrayExample {
public static void main(String[] args) {
int x = -1;
int[] tempArray = {{1,2,4,4}, {x,2}, {3,x}};
System.out.println(tempArray[1][0]);
}}

T/F - This will compile without errors.
Definition
True - This will compile correctly, and output -1. (pg 226)
Term
How many objects are created on the heap with the following code?

int[][] tempArray = {{1,9,2,2}, {3,7}, {4,4}};
Definition
Four objects are created on the heap. (pg 228)
Term
class ArrayExample {
public static void main(String[] args) {
int x = 0;
int[] tempArray;
tempArray = new int[3] {4,x,2};
System.out.println(tempArray[2]);
}}

T/F - The following code will compile correctly and output 0.
Definition
False - You cannot specify a size for an anonymous array creation syntax. (pg 230)
Term
What is the output of:

class AddBoxing {
static void go(Integer x) { System.out.println("Integer"); }
static void go(long x) { System.out.println("long"); }

public static void main(String[] args) {
int i=5;
go(i);
}
}
Definition
long
Term
What is the output of:

class AddVarArgs {
static void go(int x, int y) { System.out.println("int,int"); }
static void go(byte... x) { System.out.println("byte..."); }

public static void main(String[] args) {
byte b=5;
go(b,b);
}
}
Definition
int, int
Term
Can you widen and then box?
Definition
No
Term
Can you box and then widen?
Definition
yes
Term
Can you widen var-args?
Definition
yes
Term
What does the following Octal numbers equal in decimal:

A. int x = 011
B. int y = 010
Definition
A. 9
B 8
Term
will this code compile?

byte b =3;
b = (byte) (b+7);
Definition
Will not compile without the cast, since b + 7 results in an int.
Supporting users have an ad free experience!