Top JAVA Interview Questions and Answers
Sr No. | Questions and Answers |
Que.1 | How can you differentiate between anonymous inner class and functional interface in lambda expression? |
Ans.1 | ● We are not able to create object of interface but we can create reference of interface. In This case we can create object of its implementation class or else we have to call the method of any class which will return us the object for that interface through its implementation class.
● If we have functional interface we implement it in any class and provide implementation. But only for one method we do not prefer that. ● In this situation we provide implementation by using anonymous inner class or lambda expression. ● For functional interface, lambda expression is more suitable as it will not generate any class file for it. ● Whereas anonymous inner class will always create a class file (something like, ClassName$1.class). ● But in case of interface is having more than one abstract method we can prefer anonymous inner class. |
Que.2 | How to make password comparison with mySQL database case sensitive when we use prepared statement? |
Ans.2 | ● If we are comparing any random values that are coming from any source like form console or html file and we want to check that values with database we fire sql query with the help of PreparedStatement interface.
● Ex. If we have Login.html file which is accepting login id and password. ● In this case we can add both the parameters which we have mentioned inside query as ? (question mark). ● The comparison is totally depend on which database you are using. ● It means if it is MySQL database it will be case insensitive and in Oracle databases it will case sensitive. ● In this situation to make fields like password case sensitive by mentioning that particular field as BINARY. So it will cover data into binary format and then compare it. So will be Strictly case sensitive. ● The part of code is given below: Ex. PreparedStatement ps=con.prepareStatement(“select * from userdata where id=? and pwd=BINARY ?”); ps.setInt(1, Integer.parseInt(request.getParameter(“id”))); ps.setString(2, request.getParameter(“pwd”)); ResultSet rs=ps.executeQuery(); |
Que.3 | Differentiate with example Lambda expression with parameter and without parameter. |
Ans.3 | ● File 1: package with.without.parameter;
● File 2: package with.without.parameter; ● File 3: package with.without.parameter; ● File 4: package with.without.parameter; |
- File 2: package with.without.parameter;
- File 3: package with.without.parameter;
- File 4: package with.without.parameter;
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Que.4 | How many types of design patterns we have in java? List all categories. | ||||
Ans.4 | We basically have three types of design patterns, that are listed below:
1) Creational: ● Singleton ● Factory ● Abstract Factory ● Builder ● Prototype 2) Types: ● Adaptor ● Composite ● Proxy ● Fly Weight ● Facade ● Bridge ● Decorator 3) Behavioural: ● Template method ● Mediator ● Chain of responsibility ● Observer ● Strategy ● Command ● State ● Visitors ● Iterator ● Interpreter ● memento |
||||
Que.5 | Java-8 is having default method in interface. How it is working? | ||||
Ans.5 | Interface is having all the methods public and abstract by default, so we have to give an implementation of every method specified in interface. Then Java-8 introduced us default methods in interface. The following conditions you need to follow to make it,
● Java -8 should be installed in your system ● You must specify ‘default’ keyword to specify that particular method is default ● You can write implementation code inside that method ● It is used when we have common implementation but in case we want to change it we can override the method ● Ex. package name is “defaultmethod” File 1: DefaultMethodInterface.java File 2: DefaltMethodInInterface.java
|
Que.7 | When to use default method in interface and when static? |
Ans.7 | ● Eventually you can any one of them or both whenever you have common implementation code.
● When you have common implementation but in certain situation you want to change some part of it or else the complete implementation of the method and just want to follow that structure then default methods are advisable. ● When you have common implementation and you not at all want to change its implementation part then static methods are advisable. ● Default methods can be overridden by the programmer and static methods are not as per the polymorphic nature of methods. |
Que.8 | What is the purpose of making object reference final?
Ex. final MyClass obj=new MyClass(); |
Ans.8 | ● We have two different data types in Java Primitive and Non-primitive. When we make any primitive final its value will be constant and should not change.
● Ex. final double PI=3.14; ● Whereas when we create object if we make object reference final then that reference should not be reassigned to another object. ● Ex. Statement1: MyClass obj=new MyClass(); Statement2: obj=new MyClass(); ● In above example after writing first statement same object reference can not be assigned to another object. Second statement will flag an error. ● |
Que.9 | Explain with example jdbc batch. |
Ans.9 | ● When we have a group of queries to execute then instead of executing each and every query one by one we create a batch (group) of queries and execute it to improve the all over performance. This process is called batch processing.
● The methods for batch processing has been provided by java.sql.Statement and java.sql.PreparedStatement interfaces. ● Ex. DAO class for both examples ● Ex. 1: Batch processing by Statement interface. ● Output After above code in Database: ● Ex. 2: Batch processing by PreparedStatement interface. Program Section 1: Program Section 2: Program Section 3: ● Output after above code on the console: ● Output All over after execution above both the programs for statement and PreparedStatement in database:
|
Que.10 | Can we override static method? Prove your answer with examples. |
Ans.10 | ● No, Static methods are not overridden. The following program will prove that instance method override is possible but not static method.
● File1:The following method shows instance method of super class ● File2:The following method shows instance method of sub class ● File3:The following code shows upcasting and method calling from main Method ● File4:The above code will give the output ● File5:Now if we make super class method static ● File6: And sub class method is still instance then we get compilation error as shown below ● Here it shows the compilation error “This instance method cannot override the static method from FirstClass(Super class name)”. So are not able to execute the programs after that. So here it proves that static methods are not overridden. ● File7:Also if we make both super and sub class methods static then we will not get any compilation error, but the methods are not overridden. ● File8:In this case main class will generate warning while calling method by using object but not statically ● File9: So we will be able to run program but we will not able to get output of sub class method. Output is given below |
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Call the Trainer and Book your free demo Class for JAVA now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.