Java Flashcards

Understanding the Java Programming Language

1
Q

What is Java?

A

Java is an Object Oriented Program known for it’s “run once, write anywhere” because of the JVM. Garbage Collection, Secure, Simpler, Portable.

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

What is JRE

A

Java Runtime Environment - This contains the libraries that for the applications

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

What is JDK

A

Java Development Kit - This contains the compiler and is where applications are developed turns the code to bytecode

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

What is JVM

A

Java Virtual Machine - runs the bytecode on for the specific platform.

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

What’s the difference between an object and a class

A

An object is an instantiation of the class. Class is a blueprint for an object.

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

Object: some of the methods for the object class are equals, getClass, hashCode, toString, thread stuff.

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

Where are Strings stored?

A

Primarily the String Pool.

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

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

A

Stack and the object is in the heap.

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

What are annotations?

A

Annotations are a type of metadata that signal to the compiler information about the program
Java has a few built-in annotations you should be familiar with:
@Override - declares the method must override an inherited method (otherwise, a compilation error occurs)
@Deprecated - marks a method as obsolete (compilation warning if used anywhere)
@SuppressWarnings - instructs compiler to suppress compilation warnings
@FunctionalInterface - designates an interface to be a functional interface (covered in another module)

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

What is a POJO vs a bean?

A

Plain Old Java Object, has no special restrictions outside those enforced by Java.
A bean is a type of POJO which is Serialized, has private fields with getters, setters, or both, a
no-arg constructor, and fields which are only accessible by the constructor or getters/setters.

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

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

A

No, when there are no reference variables pointing to that object.

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

Why are strings immutable in java?

A

To save space in the heap.

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

How would you make your own objects immutable?

A

The use of final and getters and setters. Private would be used in this case.

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

What are the different variable scopes in Java?

A

Class, or static, scope
Instance, or object, scope
Method scope
Block scope

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

Class, or static, scope?

A

Class scoped variables reside on the class definition itself. This means that when objects update a class-scoped variable, the change is reflected across all instances of the class. Class scope is declared with the static keyword. Methods can also be declared as class scope. However, static methods cannot invoke instance methods or variables (think about it: which specific object would they reference?). Static methods and variables should be referenced through the class directly, not through an object. For example: MyClass.myStaticMethod() or MyClass.myStaticVariable.

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

Instance, or object, scope

A

Instance scope means that the variable is attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class.

17
Q

Method scope

A

Method scope is the scope of a variable declared within a method block, whether static or instance. Method-scoped variables are only available within the method they are declared; they do not exist after the method finishes execution (the stack frame is popped from the stack and removed from memory after execution).

18
Q

Block scope

A

Block scoped variables only exist within the specific control flow block, of which there are several in Java: for, while, and do-while loops, if/else-if/else blocks, switch cases, or even just regular blocks of code declared via curly braces ({}). After the block ends, variables declared within it are no longer available.

19
Q

What are the access modifiers in Java?

A

Access modifiers are keywords which define the ability of other code to access the given entity. Modifiers can be placed on classes, interfaces, enums, and class members. The access modifiers are listed below:

Modifier Access Level
public Available anywhere
protected Within the same package, and this class' sub-classes
default Within the same package
private Only within the same class
20
Q

What are the non-access modifiers in Java?

A
static - denotes "class" scope, meaning the member resides on the class itself, not object instances.
static variables can be accessed through the class, e.g. MyClass.staticVariable static methods can be called directly without needing an instance of the class, e.g. MyClass.someMethod()

final- when applied to a variable, it means the variable cannot be re-assigned when applied to a class, it means the class cannot be extended when applied to a method, it means the method cannot be overriden

abstract - when applied to a class, the class cannot be instantiated directly (instead, it should be inherited)
when applied to a method, only the method signature is defined, not the implementation. Also, the class where the method resides must also be abstract. Concrete subclasses must implement the abstract method.
synchronized - relevant to threads and preventing deadlock phenomena (discussed in a separate module)

transient - marks a variables as non-serializable, meaning it will not be persisted when written to a byte stream (discussed in another module)

volatile - marks a variable to never be cached thread-locally. Obscure, rarely-used keyword.

strictfp - restricts floating point calculations for portability. Obscure, rarely-used keyword.

21
Q

What is the difference between static and final variables?

A
static - denotes "class" scope, meaning the member resides on the class itself, not object instances.
static variables can be accessed through the class, e.g. MyClass.staticVariable static methods can be called directly without needing an instance of the class, e.g. MyClass.someMethod()
final
when applied to a variable, it means the variable cannot be re-assigned      when applied to a class, it means the class cannot be extended                   when applied to a method, it means the method cannot be overriden
22
Q

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

A
boolean     false
    char         “” or \u0000
    short         0
    byte         0
    int         0
    long         0L
    float         0.0f
    double        0.0d
23
Q

What is a wrapper class? List them

A

Wrapper classes are classes that let you treat primitives as Objects.

24
Q

What is autoboxing / unboxing?

A

Turning primitives to wrapper classes and reverse.

25
Q

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

A

Pass by value - This means that a copy of the value will be passed to a method. But the trick is that passing a copy of the value also changes the real value of the object.

26
Q

What makes a class immutable?

A

An object is considered immutable if its state cannot change after it is constructed

 The class must be declared as final (So that child classes can’t be created)
 Data members in the class must be declared as private (So that direct access is not allowed)
 Data members in the class must be declared as final (So that we can’t change the value of it after object creation)
 A parametrized constructor should initialize all the fields performing a deep copy (So that data members can’t be modified with object reference)
 Deep Copy of objects should be performed in the getter methods (To return a copy rather than returning the actual object reference)
 No setters (To not have the option to change the value of the instance variable)
27
Q

If two objects are equal, do they have the same hashcode? If not equal?

A
Yes, if they do not equal the hashCode can be the same but not recommended.
    The hashCode() method returns a hash code - a number that puts instances of a class into a finite number of categories. There are a few rules that the method follows:
You are expected to override hashCode() if you override equals()
The result of hashCode() should not change in a program
if .equals() returns true, the hash codes should be equal
if .equals() returns false, the hash codes do not have to be distinct. However, doing so will help the performance of hash table
28
Q

What data types are supported in switch statements?

A

byte, char, short, int, String, and the wrappers of the primitives

29
Q

How to pass multiple values with a single parameter into a method?

A

Parse
List
Array
this needs more

30
Q

What are static imports?

A

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification

31
Q

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

A

== checks hashcode and .equals() checks the actual value.

32
Q

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

A

forEach is specific to Collection API

Enhanced for loop can be used anywhere.

33
Q

What are the 3 usages of “super” keyword?

A

Variables, methods, and constructors

34
Q

How would you perform constructor chaining?

A

Calling constructor in other constructors

With the “this” keyword

35
Q

What happens if you don’t define a constructor for a class? Can you still instantiate it?

A

A default constructor will be “injected” into the class by the compiler. Instantiation still occurs.

36
Q

What is a constructor

A

When we use the new keyword in order to create an object, the JVM is invoking a special class member called a constructor. A constructor declares how an object is to be instantiated and initialized from the class “blueprint”. A constructor is declared like a method, except its method signature does not contain a return type, and a constructor always has the same name as the class. The new object returned from the constructor is always of the class in which the constructor is declared.

37
Q

What is the “this” keyword?

A

“This” keyword refers to the object which is being instantiated - it is used to initialize instance variables, or - to call other constructors (within the same class) (this is called constructor chaining).

38
Q

What is the “super” keyword?

A

the super keyword, which references the “super”, or parent, class. When invoked as a method (super()), the parent class constructor will be called. A super() call (or a this() call) must be the first line of any constructor. If not explicitly provided, the compiler will inject it on the first line