Chapter 2 Flashcards

1
Q

What is a constructor in java

A

this is a special type of method that creates a new object based on the parent Class.

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

What is the correct syntax of a constructor within in a class

A
public name () {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does a normal method have that a constructor does not have

A

A return type

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

What do we know of the name of a constructor class

A

The name matches the name of the parent class.

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

what is between the braces in java { }

A

a Code block

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

Name four types of code blocks

A

A Class definition
A Method declaration
A Inner Block
A instance initializer

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

What is the correct order of initialization in a class in regards to running the code

A

Fields and instance initializer first in order of appearnce in the code (top to bot) Followed by the constructor.

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

is a “String” variable a primitive type?

A

No, A string is a object, not a primitive type.

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

What are correct values for a boolean

A

True or False

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

What is the correct value range for a byte

A

-128 to 127

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

what is the difference between a int and a float

A

The same range as the fields of a int, but with up to 7 decimal digits

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

what is the difference between a long and a double

A

The same range as the fields of a long, but with up to 16 decimal digits

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

what do all possible values of the ‘char’ type have in commen.

A

They are all members of the Unicode Ascii values

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

What is a easy way to calculate the total range of a “byte”

A

A byte is 8 bits. A bit can have 2 values : 1 or 0. So a byte can have a range of 2^8 different combinations of bits.

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

How are the maximum number of bits used by the Java engine.

A

Java uses the bits to figure out how much memeory is needed for a Var. For example Java allocates 32 bits if you write (int num;) Memory is wasted if the value of num never exceeds a “short” wich is halve the size.

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

What happens when we you declare a value that fits a long and does not fit a int. But without including a capitol L at the end of the value.

A

java will throw a error because any number in java code is considered a int if not declared as a long by adding the L like : 3123456789L

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

Where can you NOT use a underscore inside a number decleration.

A

Not at the start 001.0
Not at the end 001.0

Not next to a decimal. 001_.0

This is fine : 0_01.0 or 001_0

18
Q

will this compile :
String reference = “hello”
int len = reference.length();
int bad = len.length();

A

It will not compile.
len is a primitive data type that we can not use to call methods. The method length can be called on the string because a string is not a primitive.

19
Q

what is the difference between declaring a variable and initializing a variable.

A

A declared variable is not used yet, the java problem is simply aware of the variable and it can be initialized. Initializing the variable is the first time a value is assigned to this.

20
Q

What is the correct syntax to declare and initialize a variable

A

=
String zooName = “Arits”;
or
int numberAnimals = 100;

21
Q

what is a java identifier

A
this is the name of a 
variable
method
class
interface
package
22
Q

what does a java identifier have to begin with

A

a letter : example
a $ symbol $example
a _ symbol _example

23
Q

what are the special rules regarding numbers in identifier

A

they can be used in a identifier but can not be used as the first character.

not valid : 1example
valid example : example2

24
Q

Can a single _ (underscore) be used as a identifier

A

no, this is not allowed in versions after java 9

25
Q

can you use java reserved words as identifiers?

A

No you can not

Since java is case sensitive you could use “CLASS” but you can’t use “class”
Don’t do this, even if you can

26
Q

what are the allowed characters to be used in a identifier

A

the only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘

27
Q

what are the common conventions around camelCasing in identifiers

A

method and variables are writtin in camelCase with the first letter being lowercase

likeThisExample

class and interface names are written in camelCase with the first letter being uppercase

LikeThisExample

28
Q
how many variables are declared and initialized in this statement
{
String s1, s2;
String s3 = "yes" , s4 = "no";
int i1, i2, i3 = 0; 
}
A

4 are declared
s1, s2, i1, i2

3 are initialized
s3, s4, i3

29
Q
which of the following are legal declarations
1 boolean b1, b2;
2 String s1 = "1", s2;
3 double d1, double d2; 
4 int i1; int i2;
5 int i3; i4;
A

1 : legal
2 : legal
3 : NOT LEGAL - you can’t declare 2 var types in the same statement even if they are the same types.
double d1, d2; would be legal
4 : legal
5 : NOT LEGAL - this line has 2 statements, and one does not have a var type.
int i3; int i4; would be legal

30
Q

When must a variable be initialized

A

BEFORE using it.

31
Q

where can you use a local variable

A

inside of a constructor, method or initializer block

32
Q

where are instance variables used

A

this variable is part of one object.

33
Q

where are class variables used

A

This variable can be used by all instances of a class. It can also be used outside of the class without a active instance of the class.

34
Q
does this compile
public class VarKeyWord {
   var tricky = "Hello";
}
A

NO, that will not compile

tricky is a instance variable and the var keyword can only be used for local variables.

35
Q

what happens when you use the var keyword as a variable type when declaring and initializing a variable.

A

This can only be done for local variables and it tells java to infer the type of the variable based on the first value.

36
Q

Can a local variable that has been initilized using var change its type

A

no, it cannot.

37
Q

does this compile
{
var question;
quesiton = 1;

}

A
No.
To declare a var variable it has to be initialized in the same statement. 
this would compile: 
var question =  1;
or 
var question
= 1;
38
Q

why does this not compile

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

A

because the var keyword can not be used for method parameters. It can only be used for a local variable

39
Q
does this compile
public class var {
  public var() {
  }
}
A
Does not compile
var is not a reserved word, but it is a reserved type. 
So it can not be used to to define a type such as 
class
interface 
enum
40
Q

when does a local variable go out of scope

A

At the end of the code block where the variable has been declared.

41
Q

when does a instance variable go out of scope

A

when the parent object is eligible for garbage collection

42
Q

when does a class variable go out of scope

A

in scope from declaration until program ends