Tactical Java Interview Questions - ayushmathur94/DirectQuesAns_Prep GitHub Wiki
Q1. What is difference between == and .equals() method in Java with an example ?
public static void main(String[] args) { String s1 = new String("HELLO"); String s2 = new String("HELLO"); System.out.println(s1==s2); // false coz different addresses in memory System.out.println(s1.equals(s2)); // true coz comparing only values }
Q2. Method overloading and Method Overriding with runtime polymorphism example.
Q3. Difference between String , String Builder and String Buffer with an example ?
Q.4. What is difference between abstract class and interface ?
Abstract Class | Interface | |
---|---|---|
Abstract class has a default constructor and it is called whenever the concrete class is instantiated | Interface doesn't have any constructor and it can't be instantiated | |
Abstract class consists of abstract methods as well as non-abstract methods. | Interface only contains abstract methods. | |
Classes which implement the abstract classes should provide the implementation only for abstract methods. | Classes which implement the interface should provide implementation for all the methods. | |
Abstract class contains instance variables. | Interface consist only of constants. |
Q Give a real world example of abstraction ?
A car owner knows how to drive it. He knows about various components of car and how to use them.
For example, a car owner knows that the accelerator pedal is used to increase the speed of car, and pressing the brake pedal stops it.
To perform these simple actions, you only need to know how to use these components but not need to know how they function.
Q5. output of the following program :
public class Test{ public static void main(String args[]){ System.out.println(10+20+"Javatpoint"); // result 10+20= 30 + Javatpoint => 30Javatpoint System.out.println("Javatpoint" + 10 + 20); // result Javatpoint10 + 20 => Javatpoint1020 System.out.println(10*20 + "Javatpoint"); // result 10*20 = 200 + Javatpoint => 200Javatpoint } }
Q6. Output of the following program :
public class operator{ public static void main(String args[]){ int a = 4; System.out.println(a++); // will return value first and then increments a so value will be 4 } __________________________________________________________________ public class operator { public static void main(String args[]){ int a = 4; System.out.println(++a); // ++ operator is prefix here so it will increment the value and then return the resulted value as 5 } } ______________________________________________________________ public class operator { public static void main(String args[]){ int a = 4; System.out.println(a--); // will return value first and then decrements a , so value will be 4 } __________________________________________________________________ public class operator { public static void main(String args[]){ int a = 4; System.out.println(--a); // -- operator is prefix here so it will decrement the value and then return the resulted value as 3 } } _______________________________________________________________________
Q7.What are constructors and how many types of constructors are used in Java ?
Q8. Write a Java program to print the Fabonacci series using both recursion and iteration up to a given number ?
Q9. Check whether given number is prime or not ?
Q10. Write a program to check whether the given string is palindrome or not ?
Q11. Write a program to find the factorial of a number ?
Q12. Difference between throw and throws keywords ?
Q13. What is the output of the following program ?
public class Main{ public static void main(String[] args){ try{ throw 90; }catch(int e){ // incompatible types int can not be converted to thorwables , in java throwable objects can only be thrown complier will show error System.out.println("Caught the exception" + e); } }
Q What is explain internal working of a hash map ? What happens at map.put(k,v) , map.get(k) and map.put(k) ?