.1. What is JVM?
Java Virtual Machine, which provides JRE in which byte codes can be executed.
2. What is JRE?
Java run time environment, which is implemented by JVM. This has minimum requirements for executing java byte code. Like supporting files, core classes etc.
3. What is JDK?
It is Java development kit, Which has JRE and developing tools.
4. What is JIT?
JIT is just in time compiler, which compiles the code at run time. Compiler is the one which translates programming code to object code. Machine understandable code
5. What is an object?
Object is the one which has state and behaviour
6. What is a class?
Class is a blueprint that describes the behaviour that object of its type supports
7. What is a method?
Collection of statements grouped together to perform an operation
8. Inheritance: When one class acquires the properties of another class, it is known as inheritance. It uses extends keyword. In java multiple inheritance is not possible. Once class and extend only one class
9. Encapsulation: Process of hiding irrelevant information to other part of the program in known as encapsulation
10. Abstraction: Process of proving relevant information to other part of the program is known as Abstraction
11. Polymorphism: Object taking different forms based on substances.
Method Overloading: When same method name is used more than one time in same class but with different argument it is knows as method Overloading
Method Overriding: Same method name with same arguments in base and subclass is known as method overriding
12. Can Main() be overloaded?
13. Static Method and Variables
- Static variables value is same for all instances of same class
- Static method belongs to the class and can be invoked without creating instance of the class
- Static method cannot have non-static data member.
- This and super cannot be used with static context
14. Class Modifiers
- A class with no modifier is known as Default Modifier
- Package level access, can be seen only by classes within same package
- Gives access to all classes from all packages.
- But if we need to import public class from another package, we still need to import it to use it.
- Class level access, Can be used in a class where private member is declared.
- When extended we wont be able to inherit private member variable
- We can see private method/private member in subclass with the same name as Super class. But that don’t mean it is been overridden. That is just a method declared in sub-class.
- Protected member can be accessed by subclass even if the subclass is in different package (Through Inheritance)
- The difference between default and protected is, Default can be accessed in subclass but only within same package. But protected can be accessed by subclass even in different package.
- Final key word can be applied for variables, methods and classes
- Variable marked as final cannot change its value.
- Final methods cannot be overridden
- Final class cannot be extended.
- When a final variable is not initialized a value at run time is known as blank final variable
- We can declare final method in subclass with same name but with different arguments
- We can initialize blank final variable using constructor
- Abstract class can never be instantiated, it can only be extended
- Abstract class can have both abstract and non-abstract methods
- If one method is declared as abstract, the class should be declared as abstract
- One abstract class can extend another abstract class but doesn’t necessarily implement all methods
- A class cant be marked both as abstract and final. Why?
- Final classes cannot be extended, But abstract class can only be Inherited
- Methods cannot be both Final and private in abstract class, as both cannot be subclassed.
- Interfaces are similar to class.
- Can have abstract methods, default methods and static methods.
- One interface can extend another interface and a class can implement interface not extend it
- Interface doesn’t have constructors
- All the methods in interfaces are abstract
16. Difference between Abstract class and Interface.
|Can have constructor||Cannot have constructor|
|Can have fields that are non-static, final||All the fields are public static final|
|Can have methods with access specifiers like public, protected and private||All methods are public|
As from Java 8, Interfaces can also have static and default methods which is not mandatory to be implemented by implementing class.
17. Stack Memory vs Heap Memory:
|Stack Memory||Heap Memory|
|Stack memory is static memory, allocated during compile time||Heap Memory is dynamic memory allocated during Run time|
|Directly stored in memory so access to this memory is faster||Accessing this memory is slower, and heap size is limited by size of virtual memory|
|Local variables are stored in Stack||Objects are stored in Heap|
- Enum is a datatype that has contant values.
- We should use Enum when a variable needs to use one value from set of values
- Increases compile time checking and will help us to parse only valid constants
- Enum can have constructors but cannot invoke constructor directly. It is invoked automatically
- Enum can be declared as separate class or class member
- They should not be declared within a method
- Assigning one object to another or one datatype to another is known as casting
- With datatypes, Byte, Short, int, long, float and double.
- Byte can be converted to short, int, long, float, double
- Short can be converted to int, long, float, double
- Int can be converted to long, float, double
- Long can be converted to float and double
- Float to double
- When a subclass object is assigned to superClass object is it known as Upcasting
- This is done automatically
Subclass s1 = new Subclass();
Superclass super1 = (Superclass) s1;
another way: Superclass super1 = new Subclass();
- When Superclass object is assigned to subclass it is known as Downcasting
- This has to be done manually
- Advantage is, it is used to get derived type using a reference to a base type
Superclass s1 = new Subclass();
Subclass sub1 = (Subclass) s1;
- String and String.valueOf(): (Casting)
- This works when the object really is a string
- Object o = “String”
String str = (String) o;
- Irrespective of object, this converts to string.
- Object o = 20;
String s1 = String.valueof(o)
- Can a constructor be private?
- Can a Constructor be static or final or abstract. Why?
- Static – Belongs to a class and not a object. When we create an object, Constructor gets initialized
- Abstract –means method cannot have a body and can be used in subclass. But constructor are called when creating object (new keyword) so it needs to have body
- Final– A constructor internally is final, so we cannot declare constructor final again. Also if we make constructor final, we wont be able to create object of the class
- A constructor can be private and can be overloaded|
- Why is string non-primitive type?
- String is an object, array of characters
- Int, short, long, char, float and double all have fixed length memory. But string does not have different length
- Post Increment and Pre Increment:
- Will increment the value of i in the first iteration but returns the original value of I.
- Will increment the value of I and returns the incremented value
- Bitwise Operators: (Example here)
- Division Operation
- 3 >> 6 : Output is: 0
- ‘-’ or ‘+’ sign is noted
- Multiplication Operation
- 2 << 5: this works as 2^5 –> Output is 64 (starts from 2^0+2^1 etc)
- This is unsigned right shift
- Irrespective of sign it will always returns as positive
- Shifts 0 to the left most position irrespective of sign