Basics Flashcards

1
Q

What is Java?

A

Java is a high level, object oriented programming language. Java is considered highly robust, secure, simple, and high performing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What is the JRE?
A

The JRE is the Java Runtime Environment, which is part of the JDK and is responsible for running a java program. It contains the class libraries and other resources a specific java program needs to run. Contains the JVM and is inside the JDK

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

What is the JDK?

A

The Java Development Kit, it contains the compiler and developer tools needed to develop a java application. Contains the JRE and JVM

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

What is the JVM?

A

JVM stands for JVM, it is designed to execute the bytecode in .clss files which runs the java application. Lives inside the JDE and JDK

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

What is the difference between an object and a class?

A

An object is an instance of a java class, it has its own identity, behavior, and state whereas a class is a user designed blueprint by which an object is created. It has a set of properties or methods that define its specifications

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

What is the root class from which every class extends?

A

The object class is the root of the class hierarchy

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

What are the primitive data types in Java?

A

boolean, char, double, float, long, int, short, byte

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

Where are Strings stored?

A

String is a class and strings are treated as objects, so they’re located in the heap. Specifically in the String Constant Pool

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

Explain Stack vs Heap

A

Stack is used to store method invocations and local variables, works based on Last in First Out; Heap is where all objects are stored, created on startup

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

Are variable references stored on the stack or heap? What about the objects they refer to?

A

Variable references can be stored in the stack, all java objects are stored within the heap

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

What is a stack frame and when are they created?

A

A stack frame contains the state of one java method invocation. When a function is called, a stack frame is created in the stack segment

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

What are annotations?

A

They are used to provide supplementary information about a program, they do not change the actions of a program, but they help associate metadata to the program elements (@Override)

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

What is a POJO vs a Bean?

A
  • A POJO is a Plain Old Java Object, it is a java object that does not have any special restrictions and is used for increasing readability and reusability in a program
  • A Bean is a special POJO with restrictions such as private fields that require getters and setters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you force garbage collection in Java? When is an object eligible for garbage collection?

A

By either setting a variable to null, redirecting a variable’s reference, or calling the finalize() method in the object class.
- Objects that no longer have a variable reference are eligible for garbage collection

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

Why are strings immutable in java? How would you make your own object immutable?

A

Strings are immutable because string objects are cached in the string pool and since all the strings in the pool are shared between multiple clients, there is a risk that a change would effect other clients
- In order to make an object immutable, you would need to add the final keyword to ensure there are no changes

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

What is the difference between String, StringBuilder, and StringBuffer?

A
  • A string is a n object representation of char values which is immutable, internally, strings are a special type of char array
  • StringBuilder and StringBuffer both create mutable strings objects, but the difference between the two is that StringBuffer is thread safe, whereas StringBuilder is not
17
Q

What are the different available scopes in Java?

A
  • Local - Declared inside the body of the method and not shared with any other methods
  • Instance - Declared inside the class, but outside the methods. This is shared among each method but not among each instance
  • Static - Declared when the static keyword is used and can be shared among all classes
18
Q

What are some access modifiers in Java? Explain them

A
  • Private - We can only access private members within the same class
  • Public - We can access public members from anywhere
  • Protected - We can access the protected members within the same package and within child classes.
  • Default - We can access these members only within the same package. If no access modifier is given, they are set to default
19
Q

What are the non-access modifiers in Java?

A
  • Static - Used to create variables/methods that will exist independently of any instances of the class
  • Final - Used to declare that the variable/method with this keyword cannot be changed or overriden
  • Abstract - Used to declare that a class is abstract and allows a method to be declared without implementation
  • Synchronization - Used to indicate that a method can only be accessed by one thread at a time
20
Q

What is the difference between static and final variables?

A

Static is used to define that a class member can be used independently of any other object in the class

Final is used to declare that the class member cannot be overridden

21
Q

What are the default values for all data types in Java?

A
char - \u000 or null
double - 0.0d
float - 0.0f
long - 0
int - 0
short - 0
byte - 0
boolean - false
22
Q

What is a wrapper class? List them

A

The wrapper class in java provides the mechanism to convert primitive data types to objects and vice versa

Boolean, Character, Byte, Short, Integer, Long, Float, Double

23
Q

What is Autoboxing/Unboxing?

A

Autoboxing is the automatic conversion of primitive data types into its corresponding object wrapper class, whereas Unboxing is the reverse

24
Q

Is Java pass-by-value or pass-by-reference?

A

Java is pass-by-value; confusingly, a reference in java is a reference to a value, so all arguments are passed by value

25
Q

What makes a class immutable?

A

The final keyword when declaring a class makes it immutable which prevents it from being extended by another class

26
Q

If two objects are equal, do they have the same hashcode? What about if they’re not equal?

A

If two objects are equal, they must have the same hashcode, but just because they have the same hashcode, that does not mean they are equal

27
Q

What data types are supported in switch statements?

A

int, byte, short, char

28
Q

List all non-access modifiers

A

static, final, abstract, synchronized, transient, volatile, strictfp

29
Q

How do you pass multiple values with a single parameter into a method

A

You would need to pass all your values into an array structure or collection and pass an array or collection as the parameter

30
Q

What is a static block?

A

A way to statically initialize a class, a static block is a set of instructions that is run only once when a class is loaded into memory

31
Q

What are static imports?

A

Static import allows us to statically import a class, allowing us to access a method of a class directly without having to a class name

32
Q

What methods are available in the object class?

A
clone()
equals()
finalize()
hashCode()
toString()
33
Q

What is the difference between == and .equals()?

A

The == operator compares the reference or memory location, not the value of the objects being compared, whereas the .equals() method compares the values of the objects

34
Q

What is an enhanced for loop and what is a forEach() loop?

A
An enhanced for loop is a simpler way of iterating through all the elements of a collection or array.
The forEach() method however can be used on classes that extend the iterable interface
35
Q

What are the 3 usages of the “super” keyword?

A
  • Used with variables in order to reference data members that have the same name in both the parent and child class, super means you’re referring to the parent class
  • You can use it to invoke the parent class methods
  • You can use it to invoke the parent class constructors
36
Q

What is the first line of any constructor?

A

The first line must be a call on another constructor in the same class using “this” or a call on the superclass constructor using “super”

37
Q

How would you perform constructor chaining?

A
By using "this" keyword for chaining constructors in the same class
By using "super" keyword for chaining constructors from the parent class
38
Q

What happens if you don’t define a constructor for a class?

A

A default constructor will be applied by java