Java 8 Associate Flashcards

1
Q

Which of the following are valid Java identifiers?

  1. A$B
  2. _helloWorld
  3. true
  4. java.lang
  5. Public
  6. 1980_s
A

_helloWorld

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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) { } }

import aquarium.;
import aquarium.
.Jelly;
import aquarium.jellies.Jelly;
import aquarium.jellies.;
import aquarium.jellies.Jelly.
;
None of these can make the code compile.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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;
}

import aquarium.;
import aquarium.Water;import aquarium.jellies.
;
import aquarium.;import aquarium.jellies.Water;
import aquarium.
;import aquarium.jellies.*;
import aquarium.Water;import aquarium.jellies.Water;
None of these imports can make the code compile.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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]);
} }

java BirdDisplay Sparrow Blue Jay
java BirdDisplay Sparrow “Blue Jay”
java BirdDisplay Blue Jay Sparrow
java BirdDisplay “Blue Jay” Sparrow
java BirdDisplay.class Sparrow “Blue Jay”
java BirdDisplay.class “Blue Jay” Sparrow
Does not compile.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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()

String[] _names
String[] 123
String abc[]
String _Names[]
String… $n
String names
None of the above.

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

Which of the following are legal entry point methods that can be run from the command line? (Choose all that apply)
private static void main(String[] args)
public static final main(String[] args)
public void main(String[] args)
public static void test(String[] args)
public static void main(String[] args)
public static main(String[] args)
None of the above.

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

Which of the following are true? (Choose all that apply)

An instance variable of type double defaults to null.
An instance variable of type int defaults to null.
An instance variable of type String defaults to null.
An instance variable of type double defaults to 0.0.
An instance variable of type int defaults to 0.0.
An instance variable of type String defaults to 0.0.
None of the above.

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

Which of the following are true? (Choose all that apply)

A local variable of type boolean defaults to null.
A local variable of type float defaults to 0.
A local variable of type Object defaults to null.
A local variable of type boolean defaults to false.
A local variable of type boolean defaults to true.
A local variable of type float defaults to 0.0.
None of the above.

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

Which of the following are true? (Choose all that apply)

An instance variable of type boolean defaults to false.
An instance variable of type boolean defaults to true.
An instance variable of type boolean defaults to null.
An instance variable of type int defaults to 0.
An instance variable of type int defaults to 0.0.
An instance variable of type int defaults to null.
None of the above.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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)

package my.directory.named.a;
package my.directory.named.A;
package named.a;
package named.A;
package a;
package A;
Does not compile.

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

Which of the following lines of code compile? (Choose all that apply)

int i1 = 1_234;
double d1 = 1234.0;
double d2 = 1_234._0;
double d3 = 1234.0;
double d4 = 1_234.0;
None of the above.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
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);
} }

int amount = 9L;
int amount = 0b101;
int amount = 0xE;
double amount = 0xE;
double amount = 12.0_0;
int amount = 12;
None of the above.

A
17
Q

Which of the following are true? (Choose all that apply)

public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
} }

Bunny is a class.
bun is a class.
main is a class.
Bunny is a reference to an object.
bun is a reference to an object.
main is a reference to an object.
None of the above.

A
18
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, B, C
B, C, A
C, B, A
B, A
C, A
A, C
A, B

A
19
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: } }

The Rabbit object from line 3 is first eligible for garbage collection immediately following line 6.
The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.
The Rabbit object from line 3 is first eligible for garbage collection immediately following line 12.
The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.
The Rabbit object from line 4 is first eligible for garbage collection immediately following line 11.
The Rabbit object from line 4 is first eligible for garbage collection immediately following line 12.

A
20
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();
} }

finalize() is guaranteed to be called.
finalize() might or might not be called
finalize() is guaranteed not to be called.
Garbage collection is guaranteed to run.
Garbage collection might or might not run.
Garbage collection is guaranteed not to run.
The code does not compile.

A
21
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: } }

0
4
Compilation fails on line 3.
Compilation fails on line 4.
Compilation fails on line 7.
Compilation fails on line 8.

A
22
Q

Which of the following are true statements? (Choose all that apply)

Java allows operator overloading.
Java code compiled on Windows can run on Linux.
Java has pointers to specific locations in memory.
Java is a procedural language.
Java is an object-oriented language.
Java is a functional programming language.

A
23
Q

Which of the following are true? (Choose all that apply)

javac compiles a .class file into a .java file.
javac compiles a .java file into a .bytecode file.
javac compiles a .java file into a .class file.
Java takes the name of the class as a parameter.
Java takes the name of the .bytecode file as a parameter.
Java takes the name of the .class file as a parameter.

A