Q.1 What are good software practices for developing scalable ,testable and maintainable software?
1- Requirement understanding and resolving ambiguities with business
2- follow sonalrlint for code coverage and TDD approch
3-Automate all non-productive manual tasks related to development (Devops)
4-keep refactoring follow DRY(don't repeat yourself)
5- add automated test casuse
6-use profiling tool
7-pair programming
8-use CI /CD
9-avoid last minute surprise(follow agile)
what is the difference between 32 bit and 64 bit versions in java ?
1. Native data type sizes and memory-address spaces.
1. 64-bits JVMs have benefit of allocating more memory than the 32-bits ones.
Definition
of Realization
Realization
UML diagram notation
Question2 ➽Why Java doesn’t support multiple inheritance?
https://javacmlearning.blogspot.com/2019/09/multiple-inheritance-in-java.html
Question3 ➽ what is difference between method overloading,method overriding and hiding of methods and variable?
This is the difference between overrides and hiding,
Question 4 ➽What is difference between path and classpath variables?
Question 9➽What is multi-catch block in java?
Question 10 ➽What is Java Reflection API? Why it’s
so important to have?
https://javacmlearning.blogspot.com/2019/09/use-of-reflection-api-in-java.html
1- Requirement understanding and resolving ambiguities with business
2- follow sonalrlint for code coverage and TDD approch
3-Automate all non-productive manual tasks related to development (Devops)
4-keep refactoring follow DRY(don't repeat yourself)
5- add automated test casuse
6-use profiling tool
7-pair programming
8-use CI /CD
9-avoid last minute surprise(follow agile)
what is the difference between 32 bit and 64 bit versions in java ?
1. Native data type sizes and memory-address spaces.
1. 64-bits JVMs have benefit of allocating more memory than the 32-bits ones.
2. 64-bits JVMs use native datatypes, which are good in terms of performance but on the other hand also needs larger space.
3. For JVMs which the Garbage Collector (GC) freezes the machine, the 64-bits versions may be slower as the GC must check bigger heaps/unsused objects and it takes more time.
Oops concept-
1-Polymorphism
2- inheritance
3- abstraction
4-encapsulation
5-Association
Association
Association
Association is
a relationship between two
classes. In this relationship the object of one instance perform an action on
behalf of the other class. The typical behavior can be invoking the method of
other class and using the member of the other class.
https://javacmlearning.blogspot.com/2019/09/association-in-java.html
Aggregation
https://javacmlearning.blogspot.com/2019/09/aggregation-in-java.html
Aggregation
https://javacmlearning.blogspot.com/2019/09/aggregation-in-java.html
Composition
Composition is a special type of aggregation
relationship with a difference that its the compulsion for the OtherClassobject (in previous
example) to exist for the existence of MyMainClass.
Class A owns Class B.
Life or existance of the composite object is dependent on the existance of container
object, Existence of composite object is not meaningful without its container
object.
public
class MyMainClass{
OtherClass
otherClassObj = new OtherClass();
}
Example:
- Order
consists of LineItems / Order owns LineItems!!
- Body
consists of Arm, Head, Legs.
- BankAccount
consists of Balance and TransactionHistory.
·
A composition is used
where each part
may belong to only one whole at a time.
·
Eg. A
line item is part of an order so A line item cannot exist without an order.
Compositions are represented by the line with
filled diamond.
Generalization
·
Generalization is often
equalized with "Inheritance" But there is a sharp and meaningful
distinction between the two.
·
Generalization describes the
relationship, Inheritance is the programming implementation of generalization.
·
Generalization is used to represent the object
oriented programming concept of Inheritance.
Definition
of Realization
- A realizationis a semantic
relationship between classifiers in which one classifier specifies a
contract that another classifier guarantees to carry out.
Realization
UML diagram notation
Note that you can represent
realization in two ways:
1.
In the canonical form (using
the interface stereotype and the dashed directed line with a large open
arrowhead) and open arrowhead pointing to the classifier that specifies the
contract.
In an elided form (using the interface lollipop notation).
Relations:
reference
https://www.codingame.com/playgrounds/503/design-patterns/uml-basics
Questions for cracking Java interviews
Question1 ➽ what are new features added in java8?
https://javacmlearning.blogspot.com/2019/09/questions-for-cracking-java-interviews.html
https://javacmlearning.blogspot.com/2019/09/questions-for-cracking-java-interviews.html
Question2 ➽Why Java doesn’t support multiple inheritance?
https://javacmlearning.blogspot.com/2019/09/multiple-inheritance-in-java.html
Question3 ➽ what is difference between method overloading,method overriding and hiding of methods and variable?
This is the difference between overrides and hiding,
- If both method in parent class and child class are an instance method, it called overrides.
- If both method in parent class and child class are static method, it called hiding.
- One method cant be static in parent and as an instance in the child. and visa versa
PATH is an environment variable used by operating system to
locate the executables. That’s why when we install Java or want any executable
to be found by OS, we need to add the directory location in the PATH variable.
Classpath is specific to
java and used by java executables to locate class files. We can provide the
classpath location while running java application and it can be a directory,
ZIP files, JAR files etc.
Question 5 ➽what is the use of main method in java?
main() method is the entry point of any standalone java
application. The syntax of main method is
public static void main(String
args[])
.
main method is public
and static so that java can access it without initializing the class. The input
parameter is an array of String through which we can pass runtime arguments to
the java program.
Question 6➽what is static import?
If we have to use any static variable or method from other
class, usually we import the class and then use the method/variable with class
name.
mport java.lang.Math;
//inside
class
double test = Math.PI * 5;
Question 7➽ what is order of calling constructors in case of inheritance?
it is from base class or parent class to child class or derived class
case 1- Single inheritance
class Parent { Parent(){ System.out.println("Parent()..."); } } class Child extends Parent { Child(){ System.out.println("Child()..."); } } public class TestConstructorCallOrder { public static void main(String[] args) { //Create object of Child class object System.out.println("Constructor call order..."); new Child(); } }
OUTPUT: Constructor call order… Parent()… Child()…
case 2- Multilevel inheritance
class Design { Design(){ System.out.println("Design()..."); } } class Coding extends Design { Coding(){ System.out.println("coding()..."); } } class Testing extends Coding { Testing() { System.out.println("Testing()..."); } } public class TestConstructorCallOrder { public static void main(String[] args) { //Create object of bottom most class object System.out.println("Constructor call order..."); new Testing(); } }
OUTPUT: Constructor call order… Design()… coding()… Testing()…
Question 8➽ What is try-with-resources in java?
try-with-resources statement for automatic resource management. A resource is an object that must be closed once your program is done using it, like a File resource or JDBC resource for database connection or a Socket connection resource. Before Java 7, there was no auto resource management and we should explicitly close the resource once our work is done with it. Usually, it was done in the finally block of a try-catch statement. This approach used to cause memory leaks and performance hit when we forgot to close the resource.Before Java 7:try{//open resources like File, Database connection, Sockets etc} catch (FileNotFoundException e) {// Exception handling like FileNotFoundException, IOException etc}finally{// close resources}Java 7 implementation:try(// open resources here){// use resources} catch (FileNotFoundException e) {// exception handling}// resources are closed as soon as try-catch block is executed.Let’s write a simple program to read a file and print the first line using Java 6 and older versions and Java 7try-with-resources implementation.
Benefits of using try with resources
·
More readable code and easy to write.
·
Automatic resource management.
·
Number of lines of code is reduced.
·
No need of finally block just to close the resources.
·
We can open multiple resources in try-with-resources statement
separated by a semicolon. For example, we can write following code:
try (BufferedReader br =
new BufferedReader(new FileReader(
"C:\\journaldev.txt"));
java.io.BufferedWriter
writer =
java.nio.file.Files.newBufferedWriter(FileSystems.getDefault().getPath("C:\\journaldev.txt"),
Charset.defaultCharset())) {
System.out.println(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
·
When multiple resources are opened in try-with-resources, it
closes them in the reverse order to avoid any dependency issue. You can extend
my resource program to prove that.
Question 9➽What is multi-catch block in java?
Java 7 one of the
improvement was multi-catch block where we can catch multiple exceptions in a
single catch block. This makes are code shorter and cleaner when every catch
block has similar code.
If a catch block handles
multiple exception, you can separate them using a pipe (|) and in this case
exception parameter (ex) is final, so you can’t change it.
In Java 7, catch block
has been improved to handle multiple exceptions in a single catch block. If you
are catching multiple exceptions and they have similar code, then using this
feature will reduce code duplication. Let’s understand this with an example.
Before Java 7:
catch (IOException ex) {
logger.error(ex);
throw new MyException(ex.getMessage());
catch (SQLException ex) {
logger.error(ex);
throw new MyException(ex.getMessage());
}catch (Exception ex) {
logger.error(ex);
throw new MyException(ex.getMessage());
}
In Java 7, we can catch all these exceptions in a single catch block as:
catch(IOException
| SQLException | Exception ex){
logger.error(ex);
throw new MyException(ex.getMessage());
}
If a catch block handles multiple exception, you can separate them using
a pipe (|) and in this case exception parameter (ex) is final, so you can’t
change it. The byte code generated by this feature is smaller and reduce code
redundancy.
Another improvement is
done in Compiler analysis of rethrown exceptions. This feature allows you to
specify more specific exception types in the throws clause of a method
declaration.
Let’s see this with a
small example:
public class Java7MultipleExceptions
{
public static void main(String[] args) {
try{
rethrow("abc");
}catch(FirstException
| SecondException | ThirdException e){
//below
assignment will throw compile time exception since e is final
//e
= new Exception();
System.out.println(e.getMessage());
}
}
static void rethrow(String s)
throws FirstException,
SecondException,
ThirdException
{
try {
if (s.equals("First"))
throw new FirstException("First");
else if (s.equals("Second"))
throw new SecondException("Second");
else
throw new ThirdException("Third");
}
catch (Exception e) {
//below
assignment disables the improved rethrow exception type checking feature of
Java 7
//
e=new ThirdException();
throw e;
}
}
static class FirstException extends Exception {
public FirstException(String
msg) {
super(msg);
}
}
static class SecondException
extends Exception {
public SecondException(String
msg) {
super(msg);
}
}
static class ThirdException extends Exception {
public ThirdException(String
msg) {
super(msg);
}
}
}
As you can see that in
rethrow
method, catch block is catching Exception but it’s not part of
throws clause. Java 7 compiler analyze the complete try block to check what
types of exceptions are thrown and then rethrown from the catch block.
Note that this analysis
is disabled if you change the catch block argument.
Question 10 ➽What is Java Reflection API? Why it’s
so important to have?
https://javacmlearning.blogspot.com/2019/09/use-of-reflection-api-in-java.htmlQuestion 11 ➽ when to use linkedlist over arraylist in java?
LinkedList
is faster in add and remove, but slower in get. In brief, LinkedList
should be preferred if:- there are no large number of random access of element
- there are a large number of add/remove operations
=== ArrayList ===
- add(E e)
- add at the end of ArrayList
- require memory resizing cost.
- O(n) worst, O(1) amortized
- add(int index, E element)
- add to a specific index position
- require shifting & possible memory resizing cost
- O(n)
- remove(int index)
- remove a specified element
- require shifting & possible memory resizing cost
- O(n)
- remove(Object o)
- remove the first occurrence of the specified element from this list
- need to search the element first, and then shifting & possible memory resizing cost
- O(n)
=== LinkedList ===
- add(E e)
- add to the end of the list
- O(1)
- add(int index, E element)
- insert at specified position
- need to find the position first
- O(n)
- remove()
- remove first element of the list
- O(1)
- remove(int index)
- remove element with specified index
- need to find the element first
- O(n)
- remove(Object o)
- remove the first occurrence of the specified element
- need to find the element first
- O(n)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.