Java Language Concepts Flashcards

1
Q

What is the purpose of the main method of a class?

A

The main method is like the entry point into the application.

It is the first method to be executed in an application

You can call other methods from the main method to execute them.

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

What is a ‘method signature’?

A

A method signature consists of the method name and a parameter list.

methodName(int x, int y)

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

What is a method?

A

a method is a block of code that performs certain actions when it is called.

You can pass data, known as parameters, into a method.

Methods are also known as functions.

The major benefit of methods is the ability to reuse code.

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

What are the rules for writing a construct?

A

1) Constructors of a class must have the same name as the class name in which it resides.
2) A constructor in Java cannot be abstract, final, static or synchronized.
3) Access modifiers can be used in a constructor declaration to control its access.

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

How are constructors different from methods in Java?

A

Constructors must have the same name as the class.

Constructors do not have a ‘return type’ or return any value.

Constructors are called only once at the time of object creation, methods can only be called any number of times.

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

What are the (3) different types of constructors?

A

Default Constructor.

No Arg Constructor.

Parameterized Constructor.

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

What is a default constructor?

A

A default constructor inserted by Java compiler during compilation, with no parameters (arguments) or code in the body.

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

What is a No-Arg constructor?

A

A No-Arg constructor is explicitly coded with no parameters (arguments) but the body can contain code.

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

What is a parameterized constructor?

A

A parameterized constructor is explicitly coded with parameters (arguments). There is no limit on the number of parameters.

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

What are constructors?

A

Constructors are blocks of code within a class that initialize newly created objects.

Can be used to assign values to the class variables at the time of object creation.

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

What is a return type and how does it work?

A

You declare a method’s ‘return type’ in its method declaration. Within the body of the method, you use the ‘return’ statement to return the value.

The data type of the return value must match the method’s declared return type.

Any modified declared ‘void’ doesn’t return a value and it does not need to contain a reutrn statement.

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

What are operators?

A

Operators are used to perform operations on variables and values.

The value is called an operand, while the operation (to be performed by the two operators) is defined by an operator.

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

What are the 4 categories of operators?

A

Arithmetic operators: used to perform common mathematical operations.

Assignment operators: used to assign values to variables.

Comparison operators: used to compare two values.

Logical operators: Determine the logic between variables or values.

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

What are Reference Data Types?

A

When declaring a variable as a string, the variable name is the reference, the values in quotes is the object that Java then stores in a ‘constant pool’.

Any other string variables with the same values in quotes will refer to the same object in the ‘constant pool’.

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

What is the purpose of “this” keyword in a method?

A

When placed in front of a variable within a method, “this” keyword refers to the globally declared member/class variable.

Used for distinguishing between a local variable and global variable of the same name.

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

What are the 8 primitive data types?

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char
17
Q

What are the attributes in a class?

A
  1. Class attributes are also known as variables within a class.
  2. Variables are the attributes of a class.
18
Q

What are global variables?

A

Variables that are defined outside of the “main method” in a class.

If the main method is static, the global variables will need to be declared as static.

Also known as “member variables”.

19
Q

What are local variables?

A

Variables that are declared within a method.

Local variables are only accessible to the method in which they were declared.

20
Q

What are getters and setters?

A

Getters are methods that are used to get the value of a member variable (attribute) within a class.

Setters are methods that are used to set the value of a member variable(attribute) within a class.

21
Q

What are the two main characteristics of objects?

A

State of the object –> determined by the class variables

Behavior of the object –> determined by the methods of a class.

22
Q

How can you modify/override attribute values?

A

Declare the object name followed by (dot) and the assignment operator (=), then the new value

myObj.x = 40

Difference between override and modify is whether an attribute/variable already has an existing value.

23
Q

How can you access attributes within a class?

A

You can access attributes by creating an object of the class by using the dot syntax (.)

Syntax example:
MyClass myObj = new MyClass();
system.out.println(myObj.x);
24
Q

How do you create an object?

A

An object is created from a class

To create an object, specify the class name followed by the object name and use the keyword “new” followed by the class name and parenthesis.

Syntax example:
MyClass myObj = new MyClass();
25
Q

How can you prevent existing attributes from being overwridden or modified?

A

Declare the attribute “final” using the “final” keyword.

The “final” keyword is called a modifier.

26
Q

What is encapsulation?

A

The purpose of encapsulation is to make sure that “sensitive” data is hidden from users.

Encapsulation prevents class variables/attributes from being called directly from other classes.

27
Q

How is encapsulation achieved?

A

1) Declare class variables(attributes) as private.

2) Provide public getter/setter methods to access and update the value of a private variable.

28
Q

What are the 6 components of a method declaration?

A

1) Modifier: Defines access type of the method.
2) Return type: The data type of a value that is returned.
3) Method name.
4) Parameter list: Comma separated list of the input parameters, proceded with their data type, within enclosed parenthesis.
5) Exception list: Any exceptions you expect the method can throw.
6) Method body: The code that is enclosed between braces that perform your intended operations when executed.

29
Q

What are the 5 types of non-access modifiers?

A

Static modifier: can be applied to class methods and variables

Final modifier: Used for finalizing implementation of classes, methods, and variables.

Abstract modifier: Used for creating abstract classes and methods.

Synchronized and volatile modifiers used for threads.

30
Q

What is the final modifier used for?

A

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object.

A final method cannot be overriden by any subclasses. The main intention of making a method final would be that the content of the method should not be changed by any outsider.

The main purpose of using a class being declared as final is to prevent the class from being subclassed.

31
Q

What is a static modifier used for?

A
A static modifier is used to create variables that will exist independently of any instances created for the class
- Static variables are also known as class variables
- Local variables cannot b declared static.
Used to create methods that will exist independently of any instances created for the class.
  • Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.