Java Interview Qs Flashcards
Difference between JRE and JVM?
JRE is an implementation of the JVM - If you want to execute java you need the JRE Installed
Can you have multiple inheritance in java?
Only through interfaces.
Interface can extent multiple interfaces
write the main method?
Public static void main(String arg[])
//public and static so that Java can access it without initializing the class //pass runtime arguments
Java.lang(Object) imported by default?
yes
what are the access modifiers for class?
for class - public and default
public: access from anywhere/class in other packages
default: can only be accessed from classes in that package
what are access modifiers for members?
public: global access
private: only inside the class
protected: classes in the same packages and subclasses
default: classes in the same package
how can we optimize SQL?
- Specific columns to retrieve
- use limit
ex/ SELECT name, produce FROM products LIMIT 10 - Use “add index” to a table
- Don’t use select *
** Index - data structure improves the s[eed of operations
Insert and Update take longer than selects with indexed tables.(Need to insert/update index values as well)
ORM?
Object/Relational Mapping
Helps application achieve persistence. Keeping state- object live on beyond the scope
what is the final keyword?
- used as a keyword so class cannot be extended
- using final on a method in a parent class so the child class cannot override it
- used with variables so it can only be assigned once
what is the static keyword?
class level variables are global
methods can be static - access only static variables
what is finalize?
called by the garbage collector in the object class
what is a static block?
group of statements that gets executed when the class is loaded into memory by java class loader
what is a class loader?
use custom if you want a class at runtime or FTP server or another web service at runtime
ex/ bootstrap, extensions, system
loads bytecode into memory when we want to access any class
inheritance
- used for code reusability
- a subclass can inherit the contents of a superclass
polymorphism
a concept where an object behaves differently in different situations
compile time polymorphism
circle{ public void draw(); public void draw (int r); } overloading - different arguments same method name
runtime polymorphism
"is-a" relationship subclass method overrides the superclass method - different implementation, with the same name
Abstraction
the concept of hiding the internal details and describing things in simple terms(Encapsulation and inheritance)
Encapsulation
Hiding data with access modifiers so that it’s only visible to that class
Interfaces
- provide a way to achieve abstraction
- interface can implement a superclass
- another class can implement an interface but another interface extends an interface
- All attributes of interface are public, static, final
Abstract class vs. Interface
abstract classes can have method implementations but interfaces cannot mostly used as a base for subclass
Wrapper Classes
immutable/final
object representation of primitive types
byte,short,int,long,float, double, char, boolean
super();
access superclass constructor
this
reference to the current object
garbage collecting
the process of checking the heap for objects that are no longer referenced and deleting them
serialization
convert java object to a stream, the stream can be saved to a file
stream data -> object is deserialization
“InstanceofKeyword”
Checks if an object belongs to a class or not
Is string a type or class
String is an object, its a class
Pass by value
Java is passed by value
method parameter values are copied to another variable and then copied object is passed in
Pass by reference
reference to the actual parameter is passed to the method
what is stored on the stack?
LIFO - main method, reference variable, static varaibles