Obj 3: Utilizing Java Object-Oriented Approach Flashcards

1
Q

what is an object ?

A

An object is a runtime instance of a class
in memory.

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

what is a reference ?

A

A reference is a variable that points to an object

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

what are the members of a class ?

A

Java classes have two primary elements: methods (often called functions or procedures) and fields (known as variables

Variables hold the state of the program, and methods operate on that state. If the change is important to remember, a variable stores that change

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

what is a method signature ?

A

The method name and parameter types are called the method signature

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

what does the main() method ?

A

The main() method lets the JVM call our code. The simplest possible class with a main() method looks like this:
public class Zoo {
public static void main(String[] args) {
System.out.println(“Hello World”);
} }

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

what are packages ?

A

Java puts classes in packages. These are logical groupings for classes.

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

import java.util.*
does it import child packages, fields or methods ?

A

The * is a wildcard that matches all classes in the package
The import statement doesn’t bring in
child packages, fields, or methods; it imports only classes directly under the package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
import java.util.Date;
 import java.sql.Date;

does it compile ?

A

No, Because there can’t be two defaults, the compiler tells you the imports are
ambiguous.

do this instead
~~~
public class Conflicts {
java.util.Date date;
java.sql.Date sqlDate;
}
~~~

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

what is reference type ?

A

A reference type refers to an object (an instance of a class). Unlike primitive types that hold their values in the memory where the variable is allocated, references do not hold the value of the object they refer to. Instead, a reference “points” to an object by storing the memory address where the object is located, a concept referred to as a pointer.

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

Park p = new Park();

A

First you declare the type that you’ll be creating (Park) and give the variable a name (p)

This gives Java a place to store a reference to the object. Then you write new Park() to
actually create the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
public class Chick {
   public Chick() {
      System.out.println("in constructor");
   }

 public class Chick {
   public void Chick() { }  // NOT A CONSTRUCTOR
 }
 }
A

There are two key points to note about the constructor: the name of the constructor
matches the name of the class, and there’s no return type.

The purpose of a constructor is to initialize fields, although you can put any code in there.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
String str = new String("hello !");
String str_ = str;

does it compile ?

A

A value is assigned to a reference in one of two ways:
- A reference can be assigned to another object of the same or compatible type.
- A reference can be assigned to a new object using the new keyword.

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

what is the 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

which one is decalaring a variable or initializing a variable
~~~
String str;
String str_ = “hello!”;
~~~

A

A variable is a name for a piece of memory that stores data. When you declare a variable, you need to state the variable type along with giving it a name. Giving a variable a value is called initializing a variable. To initialize a variable, you just type the variable name followed by an equal sign, followed by the desired value.

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

What is local variable ?

A

A local variable is a variable defined within a constructor, method, or initializer block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
final int y = 10;
 int x = 20;
 y = x + 10;

does it compile ?

A

No, The final keyword can be applied to local variables and is equivalent to declaring constants in other languages

15
Q
final int[] favoriteNumbers = new int[10];
 favoriteNumbers[0] = 10;
favoriteNumbers[1] = 20;
 favoriteNumbers = null; 

does it compile ?

A

No, we can modify the content, or data, in the array. The code does not compile when we try to change the value of the reference

16
Q
public int do() {
   int y = 10;
    int x;
    int reply = x + y;  
    return reply;
 }

Does it compile ?

A

No, Local variables do not have a default value and must be initialized before use.
the compiler will report an error if you try to read an uninitialized value

17
Q
public void findAnswer(boolean check) {
   int answer;
   int otherAnswer;
   int onlyOneBranch;
   if (check) {
      onlyOneBranch = 1;
      answer = 1;
  } else {
     answer = 2;
  }
   System.out.println(answer);
   System.out.println(onlyOneBranch); 
	 }

Does it compile ?

A

No, The onlyOneBranch variable is initialized only if check happens to be true. The compiler knows there is the possibility for check to be false, resulting in uninitialized code, and gives a compiler error.

On the exam, be wary of any local variable that is declared but not initialized in a single line. This is a common place on the exam that could result in a “Does not compile” answer. Be sure to check to make sure it’s initialized before it’s used on the exam.

18
Q
public class Car {
   private static String color;
   private String brand;
   public static void main(String[] args) {
     System.out.println(color);
     System.out.println(brand);
}

which one is the class variable and the instance variable ?

does it compile ?

A

no, compiler throws error “non-static variable brand cannot be referenced from a static context”
you should initialize instance variables
color -> class variable
brand -> instance variable

An instance variable, often called a field, is a value defined within a specific instance of
an object

a class variable is one that is defined on the class level and shared among all instances of the class. It can even be publicly accessible to classes outside the class and doesn’t require an instance to use

19
Q
public class VarKeyword {
   var tricky = "Hello"; 
 }

Does it compile ?

A

No, Local variable type inference works with local variables and not instance variables

20
Q
public void reassignment() {  
    var number = 7;
    number = 4;
    number = "five";  
 }

Does it compile ?

A

No, When you type var, you are instructing the compiler to determine the type for you. The compiler looks at the code on the line of the declaration and uses it to infer the type.

In Java, var is still a specific type defined at compile time. It does not change type at runtime.

21
Q
public void doesThisCompile(boolean check) {
      var question;
      question = 1;
      var answer;
      if (check) {
        answer = 2;
     } else {
       answer = 3;
     }
    System.out.println(answer);
 }

does this compile ?

A

No, Since question and answer are not assigned values on the lines where they are defined, the compiler does not know what to make of them

22
Q

~~~
public void twoTypes() {
int a, var b = 3;
var n = null;
}
```
Does this compile ?

A

No, All the types declared on a single line must be the same type and share the same declaration.

The compiler is being asked to infer the type of null. This could be any reference type. The only choice the compiler could make is Object. However, that is almost certainly not what the author of the code intended. The designers of Java decided it
would be better not to allow var for null than to have to guess at intent.

23
Q
var n = "Hello!";
n = null;

does this compile ?

A

yes, While a var cannot be initialized with a null value without a type, it can
be reassigned a null value after it is declared, provided that the under
lying data type is a reference type.

24
Q
public int addition(var a, var b) {  
   return a + b;
 }

does this compile ?

A

No, a and b are method parameters. These are not local variables. Be on the lookout for var used with constructors, method parameters, or instance variables. Using var in one of these places is a good exam trick to see if you are paying attention. Remember that var is only used for local variable type inference

25
Q
package var;
 public class Var {
   public void var() {
      var var = "var";
   }
   public void Var() {
      Var var = new Var();
   }
 }

does this compile ?

A

yes
var is not a reserved word and allowed to
be used as an identifier

this code does compile. Java is case sensitive, so Var doesn’t introduce
any conflicts as a class name. Naming a local variable var is legal.

26
Q
public void eatIfHungry(boolean hungry) {
   if (hungry) {
        int bitesOfCheese = 1;
   } 
    System.out.println(bitesOfCheese);  
 }

does it compile ?

A

No, The variable hungry has a scope of the entire method, while the variable bitesOfCheese
has a smaller scope. It is only available for use in the if statement because it is declared inside
of it. When you see a set of braces ({}) in the code, it means you have entered a new block of
code. Each block of code has its own scope

Local variables can never have a scope larger than the method they are defined in. However,
they can have a smaller scope.

27
Q
 public class Mouse {
     final static int MAX_LENGTH = 5;
     int length;
     public void grow(int inches) {
        if (length < MAX_LENGTH) {
           int newSize = length + inches;
           length = newSize;
       }
     }
 }

does this compile ?

A

Yes, instance variables are available as soon as they are defined and last for the entire lifetime of the object itself. The class, aka static, variables go into scope when declared like the other variable types. However, they stay in scope for the entire life of the program.

Local variables: In scope from declaration to the end of the block
Method parameters: In scope for the duration of the method
Instance variables: In scope from declaration until the object is eligible for garbage collection
Class variables: In scope from declaration until the program ends

28
Q

what is garbage collection ?

A

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

Garbage collection is responsible for removing
objects from memory when they can never be used again. An object becomes eligible for
garbage collection when there are no more references to it or its references have all gone
out of scope.

29
Q

What is the built-in method that runs the garbage colection where you can suggest ? is it guranteed to do anything ?

A

System.gc();
Java is free to ignore you. This method is not guaranteed to
do anything.

30
Q

When an object is no longer reachable ?

A

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

31
Q

Objects vs References

A

Do not confuse a reference with the object that it refers to; they are two different entities.
The reference is a variable that has a name and can be used to access the contents of an
object. A reference can be assigned to another reference, passed to a method, or returned
from a method. All references are the same size, no matter what their type is.

An object sits on the heap and does not have a name. Therefore, you have no way to access
an object except through a reference. Objects come in all different shapes and sizes and
consume varying amounts of memory. An object cannot be assigned to another object, and
an object cannot be passed to a method or returned from a method. It is the object that gets
garbage collected, not its reference

When you are asked a question about garbage collection on the exam, we recommend that you draw what’s going on.

32
Q
public void openZoo(Number time) {
   if(time instanceof String) 
      System.out.print(time);
 }

Does this compile ?

A

No, Number cannot possibly hold a String value, so the following causes a compilation error.
If the compiler can determine that a variable cannot possibly be cast to a specific class, it reports an error.

33
Q
 System.out.print(null instanceof Object);  // false
 Object noObjectHere = null;
 System.out.print(noObjectHere instanceof String);  // false
 System.out.print(null instanceof null);  // DOES NOT COMPILE
A

you should know that calling instanceof on the null literal or a null reference always returns false

This example does not compile, since null is used on the right side of the instanceof operator