Table of Contents
- Question 1: What will be the output of below program:
- Question 2: Guess the output of below program:
- Question 3: Guess the output of below program:
- Question 4: Will below program compile?
- Question 5: Will below program compile:
- Question 6: Guess the output of below code:
- Question 7: Predict the output of below code:
- Question 8: Will below code compile:
- Question 9: Will below program compile:
- Question 10: Can you override static and private method in java.
In this tutorial, we will see top 10 Java interview questions. If you can solve these questions, it will help you to understand java programming better.You can also go through top 50 core java interview questions and answers.
Question 1: What will be the output of below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.arpit.java2blog; import java.util.HashMap; public class HashMapMain { public static void main(String[] args) { HashMap<Country,String> countryCapitalMap=new HashMap<>(); Country indiaCountry= new Country("India",10000); countryCapitalMap.put(indiaCountry, "Delhi"); indiaCountry.name="Dummy"; System.out.println("Capital of India is: "+countryCapitalMap.get(indiaCountry)); } } class Country { String name; long population; Country(String name,long population) { this.name=name; this.population=population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + (int) (population ^ (population >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Country other = (Country) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (population != other.population) return false; return true; } } |
Question 2: Guess the output of below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.arpit.java2blog; import java.util.HashSet; public class Country { public String name; @Override public int hashCode() { return 31; } @Override public boolean equals(Object obj) { return true; } public static void main(String args[]) { Country countryOne = new Country(); Country countryTwo = new Country(); countryOne.name = "India"; countryTwo.name = "Nepal"; HashSet<Country> countrySet = new HashSet<>(); countrySet.add(countryOne); countrySet.add(countryTwo); System.out.println(countrySet.size()); } } |
Question 3: Guess the output of below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class MethodOverloadingExample { public void methodTest(Object object) { System.out.println("Calling object method"); } public void methodTest(String object) { System.out.println("Calling String method"); } public static void main(String args[]) { MethodOverloadingExample moe=new MethodOverloadingExample(); moe.methodTest(null); } } |
Question 4: Will below program compile?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.arpit.java2blog; public class MethodOverrdingTestMain { public static void main(String[] args) { A a= new B(); a.method2(); } } class A{ public void method1() { System.out.println("Method1 in class A"); } } class B extends A{ public void method1() { System.out.println("Method1 in class B"); } public void method2() { System.out.println("Method2 in class B"); } } |
Question 5: Will below program compile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; public class BaseClass { public A m1() { System.out.println("In BaseClass method"); return new A(); } public static void main(String args[]) { BaseClass b=new SubClass(); b.m1(); } } class SubClass extends BaseClass { public B m1() { System.out.println("In SubClass method"); return new B(); } } class A { } class B extends A { } |
Question 6: Guess the output of below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.arpit.java2blog; public class StringMain { public static void main(String[] args) { String str1="Java2blog"; String str2=new String("Java2blog"); System.out.println(str1==str2); System.out.println(str1.equals(str2)); } } |
Question 7: Predict the output of below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class ExceptionTest { public static void main(String[] args) { System.out.println(exceptionTest()); } public static int exceptionTest() { int i=6; try{ throw new NullPointerException(); } catch(NullPointerException e) { i=10; throw e; } finally { i=20; System.out.println("In finally block"); return i; } } } |
Question 8: Will below code compile:
1 2 3 4 5 6 7 8 9 10 11 |
package com.arpit.java2blog; public class NullCheckMain { public static void main(String[] args) { Integer i = new Integer(null); String s = new String(null); } } |
Question 9: Will below program compile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.arpit.java2blog; interface Decorable { String color="Blue"; } public class Room implements Decorable{ public static void main(String[] args) { System.out.println("Decorating room with Color: "+Room.color); } } |
Question 10: Can you override static and private method in java.
That’s all about top 10 Java tricky interview questions.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.