1 Class structure Flashcards

1
Q

What is the sequence to compile and run a java program from command line?

A
  • open the command prompt
  • navigate to the directory where the .java file with main method is located
  • type & enter: javac filename.java –> the file will get compiled and a .class file will be generated
  • type & enter: java filename –> program should start running

Example:
$ javac Zoo.java
$ java Zoo (* NOT Zoo.java)

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

How can the arguments be written in the main method?

A

In practice, you can write String[] args, String args[] or String…
args; the compiler accepts any of these.

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

What is typed at the command line when arguments need to be passed into main?

A

compile as normal
add the parameters at the end of the run line
$ javac Zoo.java
$ java Zoo Bronx Zoo (Bronx and Zoo are parameters)

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

What java elements are necessary to compile? to run code?

A

you need to have a JDK to compile because it includes a compiler.

You do not
need to have a JDK to run the code—a JRE is enough

Java class files run on the JVM and
therefore run on any machine with Java rather than just the machine or operating system
they happened to have been compiled on.

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

Which package is automatically imported to all java classes and doesn’t show at the top?

A

java.lang.

It is automatically imported. You can still type this package in an import statement, but you don’t have to.

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

What are the rules for wildcards?

A
has to replace a class name
can only have one wildcard in the statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a code block that is outside a method?

A

instance initializers

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

What must happen first before an object reference (non primitive variable) can be referred to?

A

You can’t refer to a object’s reference before it has
been initialized:
{ System.out.println(name); } // DOES NOT COMPILE
private String name = “Fluffy”;

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

What is the order of initialization?

A

Fields and blocks are run first in order, setting

Then the constructor runs.

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

What are the Java primitive types and their sizes?

A
boolean  true or false true
byte     8-bit integral value 123
short   16-bit integral value 123
int        32-bit integral value 123
long     64-bit integral value 123L
float     32-bit floating-point value 123.45f
double 64-bit floating-point value 123.456
char     16-bit Unicode value 'a'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a long value written?

A

the integers followed by “l” or “L” Uppercase L is preferred.

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

Can a primitive be assigned “null”?

A

No, only object references can be assigned null

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

Is this a legal declaration?

int num, String value;

A

int num, String value; // DOES NOT COMPILE

You can’t declare two different types on same line

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

What are the rules for identifiers?

A

The name must begin with a letter or the symbol $ or _
Subsequent characters may also be numbers
You cannot use the same name as a Java reserved word.
Remember that Java is case sensitive, so you can use versions of the keywords that only
differ in case. Please don’t, though

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

What is a local variable?

A

A local variable is a variable defi ned within a method

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

Do local variables have a default value?

A

No, local variables have to be initialized.

17
Q

What happens if you try to run code that has an uninitialized local variable?

A

It depends if you try to use it. It won’t compile if you try to use a local variable that hasn’t been initialized. If you declare it but don’t use it, then it will just be ignored.

18
Q

Does this compile?

4: public int notValid() {
5: int y = 10;
6: int x;
7: int reply = x + y;
8: return reply;
9: }

A

No, the local variable X is not initialized at the time it is used. The code won’t compile.

19
Q

What does finalize() do?

A

finalize() may or may not run. It is only called when the object is eligible for garbage collection so don’t add it while there is still a variable referring to the object (like a static variable that stays in scope until the program ends.

The finalize() method will run once for each object if/when it is first garbage collected. finalize() call could run zero or one time. It will NEVER run a second time

20
Q

Can a local variable be used outside the method?

A

No.

21
Q

How are local variables affected by code blocks (Braces { } )?

A

Remember that blocks can contain other blocks. These smaller contained blocks can reference
variables defined in the larger scoped blocks, but not vice versa.

22
Q

What is the order of variable initialization?

A

Fields and instance initializer blocks are run in the order in which they appear in
the file.

The constructor runs after all fields and instance initializer blocks have run.

23
Q

What are the default initialization values by type?

A

boolean false
byte, short, int, long 0 (in the type’s bit-length)
float, double 0.0 (in the type’s bit-length)
char ‘\u0000’ (NUL)

All object references (everything else) null

24
Q

What is the order of initialization?

A
  1. If there is a superclass, initialize it first (we’ll cover this rule in the next chapter. For
    now, just say “no superclass” and go on to the next rule.)
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.
25
Q

What are the JavaBeans naming conventions (getters & setters)?

A
Properties are private. private int numEggs;
Getter methods begin with is if the
property is a boolean.
public boolean isHappy() {
return happy;
}
Getter methods begin with get if the
property is not a boolean.
public int getNumEggs() {
return numEggs;
}
Setter methods begin with set. public void setHappy(boolean happy) {
this.happy = happy;
}
The method name must have a prefix
of set/get/is, followed by the first
letter of the property in uppercase, followed
by the rest of the property name.
public void setNumEggs(int num) {
numEggs = num;
}
26
Q

What does a main method look like that takes parameters?

A

? ask Terril

27
Q

How can you write a main method that takes parameters?

A
public class Zoo {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
} }
28
Q

When the main method takes parameters, how can the info be supplied?

A

In the command line, or ? other ways

29
Q

What does the wildcard import?

A

It only imports classes in the indicated package. It doesn’t import child packages, fields or methods.

30
Q

What package is automatically imported (you don’t see the import)?

A

java.lang

31
Q

import java.nio.file.Files;
import java.nio.file.Paths;

Explain why these imports don’t work
1) import java.nio.*;

2) import java.nio..;
3) import java.nio.files.Paths.*;

A
import java.nio.*; // NO GOOD – a wildcard only matches
//class names, not "file.*Files"
import java.nio.*.*; // NO GOOD – you can only have one wildcard
//and it must be at the end
import java.nio.files.Paths.*; // NO GOOD – you cannot import methods
//only class names