chapter 1_JavaBuildingBlocks Flashcards

1
Q
  1. What are the primary elements in Java Class?
A

a. methods (functions)

b. fields, (variables)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. what are the 3 types of comments?
A

a. A single-line comment -> //
b. A multiple-line comment
- > /* Multiple
* line comment
* /
c. Javadoc comment
- > * Javadoc multiple-line comment
* @author Jeanne and Scott
* /

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. what command need to run java
A
  • javac .java

- java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. what java not import
A
  • It doesn’t import child packages, fi elds, or methods; it imports only classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. what is special package that java automatically import
A
  • java.lang.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. how many import that redundant?

1: import java.lang.System;
2: import java.lang.;
3: import java.util.Random;
4: import java.util.
;
5: public class ImportExample {
6: public static void main(String[] args) {
7: Random r = new Random();
8: System.out.println(r.nextInt(10));
9: }
10: }

A
  • three of the imports are redundant. Lines 1 and 2 are redundant because everything in java, Line 4 is also redundant in this example because Random is already imported from java.util.Random.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What is constructor look like? can you give some example?
A
public class Chick {
public Chick() {
System.out.println("in constructor");
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What is instance look like? can you give some example?
A

Random r = new Random();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. what is read and write variables is known as?
A

Reading a variable is known as getting it. Writing to a variable is known as setting it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is ‘instace initializers’?
A

Code blocks that appear outside a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Explain order of 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. how many built-in data types or Java primitive types, and state it.
A

Eight, which are boolean, byte, short, int, long, float, double and char

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Can you state all keyword and type contains in Java primitive type
A

a. boolean - true or false
b. byte - 8-bit integral value
c. short - 16-bit integral value
d. int - 32-bit integral value
e. long - 64-bit integral value
f. float - 32-bit floating-point value
g. double - 64-bit floating-point value
h. char - 16-bit Unicode value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. how many base can write in java, and how to write it
A

a. base 10 - System.out.println(56); // 56
b. binary - System.out.println(0b11); // 3
c. octal - System.out.println(017); // 15
d. hexadecimal - System.out.println(0x1F); // 31

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. explain about numeric literals
A

can have underscores in numbers to make them easier to read, You can add
underscores anywhere except at the beginning of a literal, the end of a literal, right before a
decimal point, or right after a decimal point.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. How many ways value can be assigned to reference?
A

Two

a. A reference can be assigned to another object of the same type.
b. A reference can be assigned to a new object using the new keyword.

17
Q
  1. state few key difference between primitives and reference type.
A

a. Reference types can assigned to null, while primitive will give compiler error if assigned to null.
b. Reference types can used to call method when not point to null. While primitives do not have method.
c. all the primitive types have lowercase type names

18
Q
18. Fiure out which of the following are legal declarations.
boolean b1, b2;
String s1 = "1", s2;
double d1, double d2;
int i1; int i2;
int i3; i4;
A
  • The fi rst statement is legal. It declares two variables without initializing them.
  • The second statement is also legal. It declares two variables and initializes only one of them.
  • The third statement is not legal. Java does not allow you to declare two different types in the same statement.
  • The fourth statement is legal. Although int does appear twice, each one is in a separate statement.
  • The fi fth statement is not legal. Again, we have two completely different statements on the same line. The second one is not a valid declaration because it omits the type.
19
Q
  1. What is rules about identifier names.
A

a. The name must begin with a letter or the symbol $ or _.
b. Subsequent characters may also be numbers.
c. cannot use the same name as a Java reserved word, but you can use versions of the keywords that only differ in case.

20
Q
  1. can you explain about local variables.
A

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

21
Q
  1. explain about Instance and Class variable.
A

a. variables that are not local variables
b. Class variables are shared across multiple objects. We can tell its as class variable because it has static keyword
c. not require to declare, it will give default value

22
Q
  1. state the Default initialization value by type.
A

Variable type - Default initialization value

a. boolean - false
b. byte, short, int, long - 0 (in the type’s bit-length)
c. float, double - 0.0 (in the type’s bit-length)
d. char - ‘\u0000’ (NUL)
e. All object references (everything else) - null

23
Q
  1. state the rules on variables scope.
A

a. Local variables—in scope from declaration to end of block
b. Instance variables—in scope from declaration until object garbage collected
c. Class variables—in scope from declaration until program ends

24
Q
  1. whats the functions of garbage collector?
A

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

25
Q
  1. Where Java Object are stored?
A

All Java objects are stored in your program memory’s heap.

26
Q
  1. Can you explained about Heap?
A
  • also referred as free store, represented large pool of unused memory allocated to your Java application
  • may be quite large, but its limit to size, depend on environment
  • will run out of memory, if program keeps initiating object and leave them on heap.
27
Q
  1. what is System.gc() is used for?
A

might be a good time for Java to kick off a garbage collection run. Java is free to ignore the request.

28
Q
  1. What situation is the object no longer reachable?
A

■ The object no longer has any references pointing to it.

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

29
Q
  1. figure out when each object fi rst becomes eligible for garbage collection:
    1: public class Scope {
    2: public static void main(String[] args) {
    3: String one, two;
    4: one = new String(“a”);
    5: two = new String(“b”);
    6: one = two;
    7: String three = one;
    8: one = null;
    9: } }
A

a. Draw a box with the string “a” in it and draw an arrow from the word one to that box. Line 5 is similar. Draw another box with the string “b” in it this time and an arrow from the word two
b. On line 6, the variable one changes to point to “b”. Either erase or cross out the arrow from one and draw a new arrow from one to “b”. On line 7, we have a new variable, so write the word three and draw an arrow from three to “b”.

c. Finally, cross out the line between one and “b” since line 8 sets this variable to null.
d. On line 6, we got rid of the only arrow pointing to “a”, making that object eligible for garbage collection.

e. “b” has arrows pointing to it until it goes out of scope. This means “b” doesn’t go out of scope until the end of the method on line 9.

30
Q
  1. can u explained to me about finalize()
A
  • This method gets called if the garbage collector tries to collect the object.
  • 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.