Top Core Java Interview Question - Genrehow - A Technology Blog

Monday, July 2, 2018

Top Core Java Interview Question

This post is basically to deliver you best and top core java question which is basically asked during recruitment by the various company. Hope freshers find it useful and it will be helpful for them.

The reader is encouraged to try the programming questions himself/herself before checking the answers.
Java Interview Question-1. What is output of following program?
byte[] a = {1,2,3};
byte [] b = (byte[]) a.clone(); print(a==b)

Answer: False

Java Interview Question-2. Can you have a virtual function in java?
Answer: Yes, all non-static functions are virtual

Java Interview Question-3. Can a lock be acquired on a class?
Answer: Yes, by using the synchronized static method

Java Interview Question-4. What pattern will you use to improve the following code if
(object instanceof a) dosomethiga if (object instanceof b) dosomethigb

Answer: We can use visitor pattern
Java Interview Question-5. How to provide secure web services?
Answer: We can use HTTPS protocol
Java Interview Question-6. What is WSDL used for?
Answer: wsdl is used to describe the structure of client request and server response
Java Interview Question-7. Is java call by value or call by reference
Answer: Java uses reference for object, value for primitive
Java Interview Question-8. What is the output of the below code and explain?
public static void main(String[] args) {
Employee e = new Employee();
e.setEname(“PPPP”);
int i = 99;
testO(e, i);
System.out.println(e.getEname()); System.out.println(i); 99
}
public static void testO(Employee e, int i) {
e.setEname(“AAAA”);
i = 100;
}

Answer: AAAA (Call by reference), 99(Call by value)

Java Interview Question-9. What is the difference between Web Server and Application Server?
Answer: Web Server can host web applications only. An application server can host both web and non-web applications supporting protocols other than HTTP e.g. RMI, RPC, etc.

Java Interview Question-10. Write a sample code to print the below pattern where n is number of rows.
*
* *
* * *
* * * *
Answer:
for(int i=1; i<=n; i++) {
for(j=0; j<i;j++) {
System.out.print(*);
}
System.out.print(“\n”);
}
Java Interview Question-11. Write the code for above pattern using java recursion
Answer: public static void printStar(int n) {
if(n <= 1) { System.out.print(“*”);}
else {
printStar(n-1);
for(int i=0;i< n;i++) {
System.out.print(“*”);
}
}
System.out.print(“\n”);
}
Java Interview Question-12. What is transient variable?
Answer: Transient variable can’t be serialized.
Java Interview Question-13. Is Iterator a Class or Interface? What is its use?
Answer: Iterator is an interface to iterate through the elements of a collection.
Java Interview Question-14. What is similarities/difference between an Abstract class and Interface?
Answer: Differences are as following:
• A class can implement as many interfaces as it wants but it can extend only one
abstract class
• Interfaces can define public methods and constants only. The abstract class can have
protected, static, etc. methods
Similarities:
• We cannot instantiate either an abstract class or interface
Java Interview Question-15. Describe the principles of OOPS.
Answer: Encapsulation, Inheritance, and Polymorphism are 3 main principles of OOPS
Encapsulation provides a way to hide data and methods.
Inheritance provides a way to reuse existing parent classes by its child classes.
Polymorphism provides a way to overload and override existing methods
Java Interview Question-16. What all access specifiers are available in Java?
Answer: Following access specifiers are available in Java
• Public
• Protected
• Private
• Default

No comments: