Java Building Blocks Flashcards

1
Q

Is Java the coolest?

A

Yes. Yes it is.

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

What is a method?

A

A method is a block of code with a unique signature that defines what an object does.

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

What does void mean in a method signature?

A

It means that the method returns no value to whatever called it.

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

What is a parameter in the context of a method?

A

A parameter is information supplied to the method so that it can perform necessary actions.

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

What are the minimum components of a method signature that are required for code to compile (ignoring rules about constructors)?

A

A method must have a return type, a valid identifier, parenthesis for parameters, and a code block. Example:

void a() { }

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

How many public classes can there be in a single Java file?

A

1, though there can be other classes within a file so long as only one is declared public.

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

What method is required to begin execution of a Java program?

A

The main method.

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

When you compile a *.java file using the javac command, what is the result?

A

A file of bytecode with the same name as the *.java file with a file extension of .class.

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

What is the correct method signature of a main method?

A

It must be declared public and static. Its return type must be void. The method name must be “main” without caps, etc. It must accept an array or varargs of Strings. Example:

public static void main(String[] args) { }
public static void main(String… args) { }

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

When passing arguments via command line to a Java main method, what is the delimiter between arguments?

A

The space character.

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

If you want an argument to contain a space, how do you pass it to a Java main method?

A

You enclose the argument in double quotes. Example:

java ProgToRun arg “arg with space”

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

When passing arguments into java via the command line, what type of object or primitive is initially created from the argument?

A

A String. This is true regardless of what is typed in as an argument. Thus 2 is parsed as a string when input as an argument to java.

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

When writing a Java class, what imports do you get by default?

A

All the items in java.lang. Effectively this:

java.lang.*;

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

What are the minimum requirements for an import statement?

A
The import keyword, a valid package and class 
OR 
valid package and wildcard, and a semicolon.  Example:

import java.lang.String;
import java.lang.*;

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

What does the wildcard (*) do in an import statement?

A

It tells the compiler that it may use ALL classes in a particular package.

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

Does the import wildcard (*) allow the compiler access to classes in sub-packages?

A

No.

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

When using the import wildcard (*) does the compiler add all the classes from the package when compiling the code?

A

No. The compiler may have access to all the classes in the package, but it only uses the ones it actually needs. Thus using the wildcard (*) does not negatively impact the compiler.

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

Can you use import wildcards (*) when packages contain classes of the same name?

A

No. This is compile error. You must explicitly import classes where class names collide.

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

Are import statements required?

A

No. A class might only use classes in java.lang.*. Additionally, you can explicitly declare all variables with a fully qualified package, like this:

public class A { 
public com.cerner.OurClass ourClass = new com.cerner.OurClass(); 
public static void main(String... args) { java.lang.System.out.println("This compiles!"); }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the minimum requirements for declaring a package?

A

The package keyword, a valid Java identifier, and a semicolon. Like this:

package a;

21
Q

Can you declare a package with any valid identifier and have the code compile?

A

Yes. However, the package identifier must correspond with the name of the folder where the code is located.

22
Q

What is the appropriate syntax for the javac command when compiling code in packages?

A

java packageA.ClassNameA

Additionally, the command must be run from folder that contains the folder named for the package being used in compilation.

23
Q

How do you create an object in Java code?

A

Declare the class, assign a valid identifier, use the assignment operator, use the keyword new, use the desired constructor. Like this:

CoolThing thing = new CoolThing();
WithParam otherThing = new WithParam(String param);
24
Q

What is a constructor?

A

A constructor is a specialized method that is design to help correctly create a new instance of an object (initialize fields).

25
Q

What are the minimum requirements for a constructor to correctly compile?

A

No return type and a name identical to the class name, including capitalization, parenthesis to accept parameters, and a code block. This is right:

public CoolThing() { }

This is a method but not a constructor:

public void CoolThing() { }

26
Q

Do you have to write a constructor for all classes?

A

No.

27
Q

What is the constructor called that is supplied by the compiler if no constructor has been explicitly provided?

A

The default constructor.

28
Q

Can you declare and initialize a variable on one line?

A

Yes.

29
Q

What is an instance initializer block?

A

It is a code block not directly following a class, method, or branching logic.

30
Q

What is the order of initialization?

A
  1. Fields and instance initializers in the order they appear in the file.
  2. The constructor.
31
Q

Does order matter with fields and instance initializers?

A

Yes. Example:

{ System.out.println(name); } // DOES NOT COMPILE
private String name = “Fluffy”;

32
Q

Assuming you have packages and imports, in what order must those items be declared in relationship to the class declaration?

A

The package declaration must be the first line in the file. Import statements must come immediately after the declaration. The class declaration comes immediately after the imports. Comments are excepted.

33
Q

What is Java garbage collection?

A

The process of automatically freeing memory on the heap by deleting objects that are no longer reachable by a program.

34
Q

Is System.gc() guaranteed to run?

A

No. When you call System.gc() you are only RECOMMENDING to the JVM that it should call garbage collection.

35
Q

When is an object no longer reachable by a program?

A

When the object no longer has any references pointing to it.
OR
All references to the object have gone out of scope.

36
Q

What is the finalize() method?

A

The finalize() method is the method that is called when the garbage collector tries to collect the object.

37
Q

If the garbage collector doesn’t run, is finalize called?

A

No.

38
Q

If the garbage collector fails to collect an object and tries again later, is the finalize() method called again?

A

No. The finalize is only ever called once.

39
Q

What are the two things grouped together to make an object? What do we call these items in Java?

A

Data and procedures/functions

Fields and methods

40
Q

What are the three reasons we group data and procedures in OOP?

A
  1. Ease of communication
  2. Ease of reuse
  3. Ease of modification
41
Q

As an OOP, what is considered the basic building block of a Java program?

A

A Class

42
Q

What is a class in Java?

A

A class is a template for defining an object. A class contains the kinds of primitive and reference types and object will have, as well logic that sets, creates, and\or modifies that data.

43
Q

How do you use a class?

A

You create an object from the class.

44
Q

What is an object?

A

An object is a runtime instance of a class, stored in memory. It is also a reference type.

45
Q

What do all the various objects created from all the various classes at some instant in time represent?

A

The current state of a program.

46
Q

What is the state of a program?

A

It is all the objects and their data, created from all the various classes at some instant in time.

47
Q

What are the members of a class?

A

Class members are all the fields and methods.

48
Q

What are all the fields and methods of a class called collectively?

A

Class members or simply members.

49
Q

What does the simplest Java class that will compile contain?

A

A public class declaration with a legal identifier as the class and an empty code block. Example:

public class A { }