Core Java
What happens if a class isn't found by any classloader?
A ClasssNotFoundException is thrown.
What is Object Oriented Programming?
A Style of programming that represents a program as a system of objects and enables code-reuse.
What is a class?
A blueprint that defines an objects variables and methods.
What are java methods?
A collection of statements that perform specific tasks and return the result to the caller. A Java method can perform specific tasks without returning anything. Methods in Java allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is different from languages like C, C++, and Python.
What is a VM Stack frame?
A frame is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Contains local variables
finalize
A method that can be called just before an object is garbage collected to perform clean up processing.
char
A primitive data type that represents an ASCII character.
boolean
A primitive data type that represents either true or false. Stored on the stack.
float
A primitive data type that represents numbers with a decimal point. It cannot hold numbers as large or as precise as a double, but it takes up less memory. 32 bit
What are the differences between abstract classes and interfaces?
Abstract classes can have abstract and non-abstract methods. Abstract class can have final, non-final, static and non-static variables while interfaces have only static and final variables.
What is abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it.
Explain linking.
After a class is loaded. the bytecode verifier will verify whether the bytecode is proper or not. Once verified the JVM allocates memory for the class variables and initializes them to their default values. During preparation static variables are initialized to their default values.
What is an error?
An error results from a scarcity in system resources. You cannot recover from an error; the program will stop. System causes errors. unchecked OutOfMemoryError
What is the difference between an error and an exception?
An error results from a scarcity in system resources. You cannot recover from an error; the program will stop. You can recover from an exception. System causes errors, code causes exceptions. An error is unchecked while an exception can be checked or unchecked. Errors occur during runtime, exceptions can occur at compile or runtime
What is an exception?
An exception is an unexpected event that arises during the execution of a program and disturbs the normal flow of the program's instructions. Exceptions are caught by handlers positioned along the thread's method invocation stack.
What is an immutable class?
An immutable class means that once an object has been created, we cannot change its content.
What is the Native Method stack?
An implementation of the Java Virtual Machine may use conventional stacks, colloquially called "C stacks," to support native methods (methods written in a language other than the Java programming language). Native method stacks may also be used by the implementation of an interpreter for the Java Virtual Machine's instruction set in a language such as C. Java Virtual Machine implementations that cannot load native methods and that do not themselves rely on conventional stacks need not supply native method stacks.
What is an interface?
An interface is a class that only includes method signatures. The methods are not implemented in the interface. Another class must be created to supply the implementation. Interfaces are by default public, final and static. Many interfaces can be implemented. Since java 8 interfaces can have default and static methods. have only static and final variables. can only extend another java interface members are public
What is an object?
An object is an instance of a class
What are the steps when executing a program?
As soon as we run the program, it loads all the Runtime classes into the Heap space. When the main() method is found at line 1, Java Runtime creates stack memory to be used by main() method thread. We are creating primitive local variable at line 2, so it's created and stored in the stack memory of main() method. Since we are creating an Object in the 3rd line, it's created in heap memory and stack memory contains the reference for it. A similar process occurs when we create Memory object in the 4th line. Now when we call the foo() method in the 5th line, a block in the top of the stack is created to be used by the foo() method. Since Java is pass-by-value, a new reference to Object is created in the foo() stack block in the 6th line. A string is created in the 7th line, it goes in the String Pool in the heap space and a reference is created in the foo() stack space for it. foo() method is terminated in the 8th line, at this time memory block allocated for foo() in stack becomes free. In line 9, main() method terminates and the stack memory created for main() method is destroyed. Also, the program ends at this line, hence Java Runtime frees all the memory and ends the execution of the program.
What tools does the JDK have?
Contains tools for developing, debugging, and monitoring java applications.
What is a local variable?
Declared within a method. Destroyed after exiting block or return.
enum
Declares an enumerated (unchangeable) type. enum Level { LOW, MEDIUM, HIGH } Level myVar = Level.MEDIUM;
What is exception handling?
Exception Handling is a mechanism to handle runtime errors. It is mainly used to handle checked exceptions.
What is the difference between final, finally, finalize?
Explain.
exports
Exports a package with a module. Introduced in Java 9.
What are the differences between Heap and Stack in the JVM runtime memory model?
Heap memory is used by all part of the application. (Public, viewable to all threads) Objects are stored in the heap and the reference to sit stored in the stack. Stack memory is short lived, while heap memory lives while app is executing.
What does the extension class loader do?
If the class isn't found by Bootstrap, then the extension class loader will search for the class in the jre\lib\ext folder.
What does the application class loader do?
If the class isn't found by the extension class loader the application class loader will search all the jar files and classes in the CLASSPATH environment variable.
What is inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class. It represents IS-A relationship. It is used for Code Reusability and Method Overriding.
What occurs in Initialization?
Initialize class variables with the routine specified by the programmer. Initialize its super classes if it is not already initialized. All static variables will be assigned with the original values, and the static blocks will be executed.
What is an instance variable?
Instance variables are variables within a class but outside any method, constructor or block. Receive default value. These variables are created and destroyed when an object is created and destroyed. Each object has own copy. Stored in the heap along with the object's data.
How is multiple inheritance achieved in java?
It is achieved being able to implement multiple interfaces.
What is a checked exception?
It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation. Error outside of the program itself. IOExceptions, FileNotFoundException
What is resolution in linking?
JVM locates classes, interfaces, fields, and methods referenced in the constant pool (symbol table) and determines the concrete values from their symbolic reference.
How many keywords does Java have?
Java has 53 keywords.
Is Java pass by value or pass by reference?
Java is pass by value.
Why is java portable?
Java is portable because any java program can run on any device containing the JRE. Java programs do not have to be customized to run on every device. The JRE is what has to be customized.
Are java programs platform dependent or independent?
Java programs are platform independent.
What happens after a class is loaded in the JVM?
Linking is performed.
What is the constant string pool?
Located in a dedicated location within the Heap. All strings defined in the program will be stored in the string pool. When a string object is created the string is stored in the constant string pool.
What are the five JVM memory areas?
Method Area, Heap, Stacks, and the Proces Counter register, and Native Memory.
What are the differences between method overloading and overriding?
Method overloading implements compile-time polymorphism. Method overriding implements runtime polymorphism. Overloading methods have different method signatures while overriding methods have the same method signatures. Overriding is when a sub-class overrides, or changes method inherited from the parent class.
goto
Not in use, and has no function
const
Not in use, use final.
What are the differences between primitive and reference variables?
Primitive variables are created in the stack, while reference variables are ' Primitive Data types are predefined. Reference types are not (except String). Reference types can be null, primitives cannot. Reference types can be used to call methods, primitives cannot. Reference types have the same size, primitive have different types.
What are the four access modifiers?
Public, Private, Protected, Default
What are reference data types?
Reference variables store the location of an object in memory. Examples include: Class, Arrays, Annotations, Interfaces, and Enumerations
default
Specifies the default block in the switch statement.
What is the Virtual Machine (VM) Stacks?
Stores local variables and partial results, and plays a part in method invocation and return. The VM stores frames, and Frames are pushed and popped as they are needed and not needed. Each method has its own VM stack.
What are some examples of immutable classes?
Strings, wrapper classes.
What is encapsulation?
Surrounding something, not just to keep the contents together, but also to protect those contents. Restricts access to the inner workings of a class or any objects based on that class; this is referred to as information hiding or data hiding.
What does a .java file contain?
The .java files contain the source code.
What does the Bootstrap class loader do?
The Bootstrap class loader will try to find the class being loaded. It scans the rt.jar file in the JRE lib folder.
What is the JVM memory Heap?
The JVM Heap is public, so it is shared among all threads. The Heap stores objects
How many class loaders does the JVM have?
The JVM has Bootstrap, extension and application.
What is the Java Virtual Machine?
The JVM is a virtual machine that runs Java bytecode. It is a runtime environment where bytecode can be run.
Is the JVM platform dependent or independent?
The JVM is platform dependent.
Is the JVM platform independent or platform dependent?
The JVM is platform dependent.
Why is the JVM virtual?
The JVM is virtual because it provides a machine that does not depend on the underlying operating system and the machine hardware architecture.
What translates bytecode into machine code?
The JVM translates bytecode into machine code.
What is the Java Development Kit (JDK) in Java?
The Java Development Kit is a core java package that consists of the JRE and development tools.
What is the Java Runtime Environment (JRE)?
The Java runtime Environment is a software package that bundles libraries and the Java Virtual Machine.
What is the Program Counter (PC) Register?
The PC register can support multiple threads of execution at once. Each thread has its own PC register. While a current method is being run, the PC register contains the address of the current JVM instruction being executed. Native methods are excluded, their behavior is undefined.
What is the class loader?
The class loader is a subsystem for loading classes.
What are the three primary functions of the class loader.
The class loader will load, link and initialize classes.
What is an unchecked exception?
The compiler does not require that your code handle the exception, and if it occurs, it is detected at runtime and may terminate your program. Error in logic. ArrayIndexOutOfBounds, Divide By 0
What is the run-time constant pool?
The constant pool contains the names of classes that are referenced, initial values of strings and numeric constants, and other miscellaneous data crucial to proper execution. Used to keep track of class and its members. (Using symbols)
What is a fully checked exception?
The current class and its child classes have been checked.
What is a partially checked exception?
The current class is checked, and its child classes may or may not be checked.
final
The final keyword can apply to classes, methods and variables. final methods cannot be extended. final variables cannot be changed. final methods cannot be overridden by child classes.
What does the java complier do?
The java compiler takes the source code and converts into bytecode. .java files are compiled into .class files
Describe the keyword: default
The keyword default is an access modifier. default allows members to be accessible within the package
Describe the keyword: private
The keyword private is an access modifier. private allows members to be accessible within the class.
Describe the keyword: protected
The keyword protected is an access modifier. protected allows members to be accessible within the class and its child classes.
Describe the keyword: public
The keyword public is used as an access modifier. public allows members to be accessible from anywhere.
What does the Method Area store?
The method area stores class structures like metadata, the constant runtime pool, and the code for methods including fields, and constructors. The method area is public, so all threads share the Method Area.
Where is the run-time constant pool stored?
The run-time constant pool is stored in the method area.
What is abstract class?
These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body. Only one abstract class can be inherited or extended. Can have access modifiers. Can contain regular and abstract methods. Abstract class can have final, non-final, static and non-static variables can extend other java classes or implement interfaces
else
Used in conditional statements.
finally
Used with exceptions. After the try catch block has executed, the finally block will execute even if an exception has occurred. Cleans up all resources used by the try-catch block
do
Used with while loop to create a do-while loop. A do-while loop ensures our loop iterates at least once.
What are the three major steps in Linking?
Verification, Preparation and Resolution(optional)
What is Polymorphism?
When two different objects respond to the exact same message in different ways. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Another is overloading, where methods have different number of parameters while sharing the same name.
What is compile-time polymorphism?
Whenever an object is bound with its functionality at the compile time, this is known as the compile-time polymorphism. At compile-time, java knows which method to call by checking the method signatures. So, this is called compile-time polymorphism or static or early binding. Compile-time polymorphism is achieved through method overloading.
How can you create an immutable class?
You must declare your class as final so that child classes can't be created. Data fields must be declared as private, so that direct access is not allowed. Data members must be declared as final, so they can't be changed after object creation. A parameterized constructor must initialize fields using deep copy, so that data can't be modified. Deep copy must also be performed when returning a value. Don't use setters.
What is the most basic thing you need to run a java program?
You need the Java Runtime Environment.
assert
assert is used for debugging You expect a value returned so you check by asserting that the expected value is returned. You can assert an expected value or test for a wrong value.
break
breaks out of a loop, or from a switch case
What are primitive data types?
byte, short, int, long, float, double, Boolean, char They are initialized by default: byte, short, int, long, char = 0 double, float = 0.0 Boolean = false
catch
catches the exception in a try-catch block
class
defines a class
extends
extends creates the inheritance relationship. used only once per class //example public class Manager extends Employee { }
for
for loop
byte
holds values -128 -> 127 8 bits primitive
What is runtime polymorphism?
is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
What is linking?
it is the process of taking a class or interface and combining it into the run-time state of the JVM so that it can be executed.
case
marks a block of code in switch statements switch (value) { case 'A': System.out.println("Hello World"); break; case 'B': System.out.println("Goodbye World"); break; default: System.out.println("Meh"); }
double
primitive Stores floating point numbers. 64 bit
continue
skip to the next iteration of the loop