Chapter 1 - Java building blocks Flashcards

1
Q

How many public classes a file can have?

A
  1. Each file can contain only one public class

2. The file name must match the class name, including case, and have a .java extension.

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

What is the main method structure?

A

public static void main (String[ ] args) {

}

Where:

public is the access modifier. It declares this method’s level of exposure to potencial callers in the program.

static binds a method to its class, so it can be called by just the class name. An object does not need to be created to call the method.

void represents the return type. A method that returns no data.

main method: represented as an array of java.lang.String objects.

Valid sintax:

String[ ] args

String args [ ]

String… args

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

What are imports needed for?

A

Imports are needed to be able to reference an external class.

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

What are wildcards used for?

A

Wildcards (*) are use to import all the classes from the same package. Importing all the classes in a package vs just the class that is going to be used does not slow down the program. Listing each of the classes makes the code easy to read. Using the wildcard can shorten the import list.

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

Is java.lang package imported automatically?

A

Yes, Java.lang package is imported automatically, so no import statements need to be used for classes in the lang package. I.e. java.lang.System.

Below are examples of redundants imports:

Import java.lang.System
Import java.lang.*

Import java.util.Random
Import java.util.*

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

What are the classes import rules?

A
  • A wild card only matches class names.
  • Just one wildcard must be present and it should be at the end.
  • Just classes can be imported. Methods cannot be imported.

Examples of bad importers:

import java.nio.*;

import java.nio..;

import java.nio.files.Paths.*;

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

What is the code needed to compile a Java program from the command prompt?

A

To Compile:

javac package/Class.java

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

What is the code needed to run a Java program from command prompt?

A

java package.class

In Java you don’t need to pass the extension when running a program. Example:

java package.ClassB

Instead of java package.ClassB.class

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

What is the purpose of a constructor?

A

The purpose of the 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

How a constructor should be defined?

A
  • The name of the constructor should match the name of the class.
  • There is no return type in the constructors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a default constructor?

A

default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.

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

Where are instance initializers located at?

A

instance initializers blocks are outside a method.

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

How is the initialization order?

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
14
Q

What is a literal?

A

When a number is present in the code, it is called Literal.

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

Can underscores be included in numbers? Where they can go?

A

Yes, since Java 7, underscores can be included in numbers to make them easier to read.

  • Underscores can be added 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

What is a reference type?

A

A reference type refers to an object (instance of a class).

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

What a primitive type holds?

A

Primitive types hold their values in memory where the variable is allocated.

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

What a pointer is?

A

references do not hold the value of the object they refer to. Instead they point to an object by storing the memory address where the object is located at. This concept is referred to as a pointer.

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

Can a reference type be asigned a null?

A

Yes, Reference types can be assigned null, which means they do not currently refer to an object.

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

Can a primitive type be assigned a null?

A

No, Primitive types will give you a compiler error if you attempt to assign them null.

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

Can reference types call methods?

A

Yes, reference types can be used to call methods when they do not point to null.

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

Do primitives have methods declared on them?

A

Primitives do not have methods declared on them.

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

What is the purpose of a constructor?

A

The purpose of the 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
24
Q

How a constructor should be defined?

A
  • The name of the constructor should match the name of the class.
  • There is no return type in the constructors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is a default constructor?

A

default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.

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

Where are instance initializers located at?

A

instance initializers blocks are outside a method.

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

How is the initialization order?

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
28
Q

What is a literal?

A

When a number is present in the code, it is called Literal.

29
Q

Can underscores be included in numbers? Where they can go?

A

Yes, since Java 7, underscores can be included in numbers to make them easier to read.

  • Underscores can be added anywhere EXCEPT at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
30
Q

What is a reference type?

A

A reference type refers to an object (instance of a class).

31
Q

What a primitive type holds?

A

Primitive types hold their values in memory where the variable is allocated.

32
Q

What a pointer is?

A

references do not hold the value of the object they refer to. Instead they point to an object by storing the memory address where the object is located at. This concept is referred to as a pointer.

33
Q

Can a reference type be asigned a null?

A

Yes, Reference types can be assigned null, which means they do not currently refer to an object.

34
Q

Can a primitive type be assigned a null?

A

No, Primitive types will give you a compiler error if you attempt to assign them null.

35
Q

Can reference types call methods?

A

Yes, reference types can be used to call methods when they do not point to null.

36
Q

Do primitives have methods declared on them?

A

Primitives do not have methods declared on them.

37
Q

What are the identifiers definition rules?

A
  • The name must begin with a letter or the symbol $ or _
  • Subsequent characters may also be numbers.
  • Java reserved words cannot be used.
  • dots are nor allowed.
38
Q

What is the capitalization that needs to be used while defining variables and methods?

A

Method and variables names begin with lowercase letter followed by CamelCase.

39
Q

What is the capitalization that needs to be used while defining a class?

A

Class names begin with an uppercase letter followed by CamelCase. Don’t start any identifiers with $. The compiler uses this symbol for some files.

40
Q

Which types of variables does Java have?

A
Local variables
Instance and class variables
41
Q

Where a local variable should be defined and when does it need to be initialized?

A

Local variables are defined within a method.

Must be initialized before use. They do not have a default value

42
Q

Where instance variables are defined and when do they need to be initialized?

A

Instance and Class Variables are not local variables. Instance variables are also called fields.

A class variable can be recognized because it has the static keyword in its declaration.

Instance and class variables do not require to be initialized. As soon as they are declared, they are given a default value.

43
Q

What is the default value assigned to Java objects?

A

For objects java assigns NULL

44
Q

What are the default values assigned to primitives?

A

For primitives java assigns 0 or false

boolean: false
byte, short, int, long: 0
float, double: 0.0
char: ‘\u0000’ (NUL)

45
Q

What is the scope of a local variable?

A

Local variables - in scope from declaration to end of block.

46
Q

What is the scope of an instance variable?

A

Instance variables - in scope from declaration until object garbage collected.

47
Q

What is the scope of a class variable?

A

Class variables - in scope from declaration until program ends.

48
Q

What garbage collection refers to?

A

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

49
Q

When an object is no longer reacheable?

A
  • The object no longer has any references pointing to it.

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

50
Q

How many times can the finalize() method run?

A
  • finalize() : the call to this method could run zero or one time.
51
Q

In which line should be placed a chainned constructor call?

A
  1. When channing constructors, the chainned constructor line call should be the first inside the other constructor.
52
Q

Can chainned constructors be placed inside methods?

A

No, Chainned constructors cannot be placed inside a method. An error will be thrown.

53
Q

What are the 3 flavors of operators available in Java?

A

three flavors of operators are available in Java: unary, binary and ternary.

54
Q

Arithmetic operators should be applied to? And EXCEPT to?

A

All of the arithmetic operators may be applied to any Java primitives. EXCEPT boolean and String.

55
Q

Which aritmetic operators can be applied to String?

A

Only the addition operators + and += may be applied to String values, which result in String concatenation.

56
Q

If two values have different data types in an operation, what Java does?

A

If two values have different data types, Java will automatically promote one of the values to the larger of the two data types

57
Q

To which data type are promoted small data types such as byte, short and char while used with Java binary arithmetic operators?

A

Smaller data types, namely byte, short and char, are first promoted to int any time they’re used with Java binary arithmetic operator, even if neither of the operands is int. **important: unary operators are excluded from this rule. I.e: applying ++ to a short value results in a short value.

58
Q

Which data type will have a resulting value after all promotion has occur?

A

After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

59
Q

Can logical inversion (!) be applied to numeric values?

A

No, Logical inversion cannot be performed on a numeric value.

i.e. int x = !5;

60
Q

Can numerical negation be applied to booleans?

A

No, numerical negation cannot be applied to booleans.

i.e. boolean y = -true

61
Q

What is done by Java to go from larger numerical data types to smaller data types?

A

Casting can be used to avoid compiler issues everytime you are going from larger numerical data type to a smaller numerical data type, or converting from floating-point number to an integer value.

Examples:

int x = (int)1.0

Short y = (short)1921222

int z = (int)9f

long t = 192301398193810323L

62
Q

A rule for a compound assignment operator is?

A

The left-hand side of the compound operator can only be applied to a variable that is already defined and cannot be used to declare a new variable. For example:

int x = 2, z = 3;

x *= z

x should be defined first. If x was not already defined a compiler error will happen.

63
Q

Which are the 3 logical operators use in Java?

A

Logical operators are: &, |, and ^

& aka AND
| aka exclusive OR
^ aka inclusive OR

64
Q

When the logical operator & is true?

A

AND is only true if both operators are true.

65
Q

When the inclusive OR operator is false?

A

Inclusive OR is only false if both operands are false.

66
Q

When the exclusive OR operator is true?

A

Exclusive OR is only true if the operands are different.

67
Q

What are the 2 types of conditional operators?

A

Conditional operators are && and ||

68
Q

What is the main important characteristic of conditional operators && and || while examining an expression?

A

Conditional operators are nearly identical to the logical operators except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.