Sunday, February 14, 2021

SQL Query

SQL JOIN

A JOIN is used to combine rows from two or more tables, based on a related column between them.

  • (INNER) JOIN: Returns records that have matching(intersection) values in both tables.
  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
  • SELF JOIN:A self JOIN is a regular join, but the table is joined with itself.
        eg: SELECT * FROM customer c1 JOIN customer c2 ON c1.Id=c2.Id;'
  • Equi Joins: EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables. An equal sign (=) is used as comparison operator in the where clause to refer equality.

    You may also perform EQUI JOIN by using JOIN keyword followed by ON keyword and then specifying names of the columns along with their associated tables to check equality.

        eg: select * from customer c1 join order o1 on c1.id=o1.id;

SELECT SQL Query:  SELECT s_id, name, age FROM student;

2.  SELECT * FROM student WHERE name = 'Abhi';

3. write a query to display all students name whose name is of 6 characters length.

Select * from student where length(firstName)=6;

4. write a query from copy one table data to another table

Insert into employee(id,name,depart) select id,name,depart from student;

5. Crud operations:

 a. SELECT * FROM EMPLOYEE;

 b. INSERT INTO EMPLOYEE(id, name, city) VALUES(1,'PANKAJ','KALAYAN';

 c. DELETE FROM employee WHERE id=1;

 d. UPDATE employee SET name='PANKAJ' WHERE id=1;

 e. CREATE TABLE employee(id INT NOT NULL PRIMARY KEY, name CHAR(30), city CHAR(30));

f. SEVENTH SALARY:

SELECT * FROM employee ORDER BY salary DESC LIMIT 6,1;

g. IN CLAUSE: SELECT * FROM employee WHERE salary in(10000,20000);

h. Whole List : SELECT * FROM marksheet WHERE  physics>33 and maths>30 and chemistry>33 ORDER BY (physics+chemistry+maths) DESC LIMIT 1,10;

i. NOT MATCH: SELECT * FROM employee e WHERE NOT EXISTS(SELECT * FROM order 0 where e.id=o.id);


Group By:- Group by clause is used to group the results of a SELECT query based on one or more columns. It is also used with SQL functions to group the result from one or more tables.

NOTE:- You must remember that Group By clause will always come at the end of the SQL query, just like the Order by clause.


Wednesday, February 10, 2021

Collection Framework

 Q. What are the difference between ArrayList and LinkedList?

Ans: There are following difference between ArrayList and LinkedList:

ArrayListLinkedList
1. ArrayList internally uses a dynamic array to store the elements.1. LinkedList internally uses doubly linked list to store the elements
4. ArrayList is not efficient for manipulation because a lot of shifting is required.4. LinkedList is efficient for manipulation.
5. If you do not frequently add elements at start and middle of the array and want fast random access then use Arraylist5. if you frequently insert element in a list and want sequential access then use Linkedlist
6. ArrayList does Random Access with the help of Binary Search
6. LinkedList does sequential access
2. By default new element is added at the end of array.2. By default element is added as a last node.

3. Adding or Deleting a new element at start or middle of the ArrayList is very slow because all the later elements copied forward or backward.3. Simply create a new node and change pointers

Wednesday, February 3, 2021

Java 8

 Feature of Java 8

1. Stream: is a sequence of elements supporting sequential and parallel aggregate operations.

1. Lambda Expression:is nothing but anonymous function and it applicable with functional interface(Lambda expression use with functional interface without functional interface no use of Lambda expression).

The main objective of Lambda Expression:

To bring benefits of functional programming into java

Note: only single argument method have paranthess is optional. but zero arg, and two, three and etc. arg then paranthess should be manadatory

Note: In Lambda Expressiong, Without curly braces we can not return keyword. Compiler will consider returned value automatically. 

Once we write Lambda Expressions then I will discuss Functional Interface

2. Functional Interface: If any interface which contains single abstract method. it called functional Interface.

Within curly braces if we want to return some value compulsory we should use return statement.

Anonymous Inner class !=Lambda Expressions

If anonymous inner class implements an interface that contain single abstract method then only we can replace that anonymous inner class with Lambda expressions


Predicate:-Take some input and perform some conditional check and returns boolean value.

Function:-Take some input and perform some operation and return the result which is need not be boolean type.

Consumer:-Accept some input and perform required operation and not required to return anything.

Supplier:-just supply my required objects and it won't take and input.


Hibernate

Q. What is ORM ?
Ans: ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.
Q. What is Hibernate?
Ans:Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables.

Q.Why do you need ORM tools like hibernate?

The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:

  • Improved productivity
    • High-level object-oriented API
    • Less Java code to write
    • No SQL to write
  • Improved performance
    • Sophisticated caching
    • Lazy loading
    • Eager loading
  • Improved maintainability
    • A lot less code to write
  • Improved portability
    • ORM framework generates database-specific SQL for you

Q. What are the differences between First level cache and second level caches?

Ans: There are following differences between First level cache and second level caches

First level cacheSecond level cache
It is associated with session objectSecond level cache is associated with session factory object
By default hibernate uses first level cache on pre-transaction basisSecond level cache will have to enable by developer when you need.
Hibernate uses this cache mainly to reduce the number of SQL queriesHibernate uses second level cache  mainly to reduce database traffic


16.What’s the difference between load() and get()? 

load() vs. get() :-

load() get() 
Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get() methods. 
load() method will throw an exception if the unique id is not found in the database. get() method will return null if the unique id is not found in the database. 
load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.  get() will hit the database immediately. 

Q. What is HQL?

Ans: Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't depends on the table of the database. Instead of table name, we use class name in HQL. So it is database independent query language.

Advantage of HQL

There are many advantages of HQL. They are as follows:

  • database independent
  • supports polymorphic queries
  • easy to learn for Java Programmer

Q. What is Criteria?

Ans: The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.

Advantage of HCQL

The HCQL provides methods to add criteria, so it is easy for the java programmer to add criteria. The java programmer is able to add many criteria on a query.

Q. What is the difference between HQL and SQL?

Ans: HQL is database independent when you switch your application from one database to another database you do not need to change HQL queries. whereas SQL are database dependent and you need to change it when you change database. 

Q. What is Eager fatching?

Ans: It is just like outer join which return data of parent object as well as child object .

OR

It fetch all data from both table whether they are matched or not. 

It is enable by fetch=join. In case of Eager fatching one query is fired.

Q. What is Lazy Fatching?

Ans: Lazy fatching is done by on demand. It fatch data either of parent object or child object but on demand. It is enable by lazy=true. In case of lazy fatching one query is fired but if we demand for child object like getBids() method then second query will be fired.

Q. What is Batch Fatching?

Ans: Batch fetching get the data in form of batch or collection. by specifying list of primary key or foriegn key.

Q. Who convert HQL into SQL?

Ans: Dilect convert HQL into SQL

Q. Who convert crieteria into SQL?

Ans: Dilect convert criteria into SQL.

Q. Which Dilect did you use in your application?

Ans: We used Mysql dilect in our application.

Q. How did you make DCP in hibernate your application?

Ans: Session factory itself a DCP.

Q. Session factory is mutable or immutable?

Ans: Session factory is immutable

Q. What is transient, persistent and detached object?.

Ans: 

Transient: Till the time object is newly created and not stored in the database. It is called transient.


Tuesday, February 2, 2021

Spring Boot

 

1) What is Spring Boot?

Spring Boot is a Spring module which provides RAD (Rapid Application Development) feature to Spring framework.

It is used to create stand alone spring based application that you can just run because it needs very little spring configuration.

2) Spring Vs Spring Boot?

SpringSpring Boot
String is a web application framework based on JavaSpring Boot is a Spring module
Spring Provides tools and libraries to create customized web applicationsSpring Boot is Used to create a Spring application project which can just run/ execute
Spring is more complex than Spring BootSpring Boot is less complex than the Spring framework



Q. What embedded containers are supported by Spring Boot? 

Ans:-Spring Boot supports three embedded containers:

  • Tomcat (used by default)
  • Undertow
  • Jetty

Q. What is Thymeleaf in Spring Boot and how to use thymeleaf?

Ans:- It is a Java-based server-side template engine that offers elegant and natural templates for a web application. we can integrate well with Spring Framework and HTML5 Java web applications. To use Thymeleaf, you need to add the following code in the pom.xml file:

1
2
3
4
<dependency>   
<groupId>org.springframework.boot</groupId>   
<artifactId>spring-boot-starter-thymeleaf</artifactId>   
</dependency>   

Q. Can you name and briefly explain all the spring boot components/features?

Ans:The main features of Spring Boot are:-
1. Starter dependency
2. Auto-Configuration
3. Spring Initializer
4. Spring Actuator
5. Spring CLI

Advantages of Spring Boot:

  • It reduces development and testing time. 
  • It uses JavaConfig instead of XML.
  • It provides an opinionated development approach.
  • It offers starter projects or defaults for agile development.
  • No separate web server is required; hence there is no need to boot up Glassfish, Tomcat, or any other server.

  1.  It is very easy to develop Spring Based applications with Java or Groovy.
  2. It reduces lots of development time and increases productivity.
  3. It avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  4.  It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
  5.  It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort
  6.  It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  7.  It provides CLI (Command Line Interface) tool to develop and test Spring Boot(Java or Groovy) Applications from command prompt very easily and quickly.
  8.  It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle
  9.  It provides lots of plugins to work with embedded and in-memory Databases very easily.


3. Why we need Spring Boot?

  1. To ease the Java-based applications Development, Unit Test and Integration Test Process.
  2. To reduce Development, Unit Test and Integration Test time by providing some defaults.
  3. To increase Productivity.

What are the features of Spring Boot?

  • Web Development
  • SpringApplication
  • Application events and listeners
  • Admin features

Monday, February 1, 2021

Polymorphism

Q. What is  OOP?

Ans: OOP(Object oriented programming) represent a programming methodology based on objects instead of just procedure and functions.

Q. What is polymorphism?

Ans: Polymorphism is the ability of an object to behave differently in different states.

  The most common use of polymorphism in oop occurs when an object of parent class hold the reference of child class object. 

Q. What is method Overloading and Overriding?

Ans: Overloading:-If a class has multiple method having same name but different in parameters list, it is known as method Overloading.

Note:There are two ways to overload the method.

          1. By changing number of argument

          2. By changing the data type.

Remember: Overloading is not possible by changing return type of method. it will cause compilation error.

Overriding: If child class have same method as declared in the parent class. it is known as method overriding in java.

Q. can you write return statement in finally block?

Ans: Yes, but you will get warning message at compile time.

Q. What are the difference between static polymorphism and dynamic polymorphism?

Ans: Static polymorphism is achieved by method overloading. Static polymorphism is a process in which a call to a overloaded method is resolved at compile time whereas Dynamic polymorphism is a process in which a call to an overriden method is resolved at runtime.

Overloading vs Overriding in Java

-----------------------------------------------------------------------------------
OverloadingOverriding 
1. More than one method with same name, different prototype in name scope is called method overloading1. More than one method with same name, same prototype in different scope is called method overriding
2. In case of method overloading, parameter must be different.2. In case of method overriding, parameter must be same.
3. Method overloading is performed within class3. Method overriding occurs in two classes.
4. In case of method overloading, return type can be same or different.4. In case of method overriding, return type must be same.
Q. What is Upcasting and Downcasting in java?
Ans: Upcasting: When Parent object holds the reference on a child then it is called upcasting. upcasting is  done automatically.
         Downcasting: vice versa but downcasting must be manually done by the programmer

Q. What is the senario of inheritance?
Ans: It is a parent child relationship when parent class need to provide some default behaviour and child class need to provide some specilized behavior.

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...