Java - Interview Questions and Answers - Yes / No

Q1.  Can we reduce the visibility of the overridden method ?

Ans. No

Q2.  Does Java provides default copy constructor ?

Ans. No

Q3.  Can constructors be synchronized in Java ?

Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed.

Q4.  can we create a null as a key for a map collection ?

Ans. Yes , for Hashtable. Hashtable implements Map interface.

Q5.  Can we override static methods ? Why ?

Ans. No. 

Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior. 

A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable.

Q6.  Can we reduce the visibility of the inherited or overridden method ?

Ans. No.

Q7.  Is runnable a Marker interface ?

Ans. No , it has run method declared.

Q8.  Can we declare interface methods as private ?

Ans. No. 

Q9.  Can we have multiple servlets in a web application and How can we do that ?

Ans. Yes by making entries in web.xml

Q10.  Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?


Ans. No, Compiler will create default constructor only if we don't specify any constructor.

Q11.  Can we overload constructors ?

Ans. Yes.

Q12.  Do we need to import java.lang.package ?

Ans. No, It is loaded by default by the JVM.

Q13.  Can finally block be used without catch ?

Ans. Yes but should follow "try" block then.

Q14.  Can we instantiate the object of derived class if parent constructor is protected ?

Ans. No

Q15.  Can we declare an abstract method private ?

Ans. No Abstract methods can only be declared protected or public.

Q16.  Are there any global variables in Java, which can be accessed by other part of your program?

Ans. No. Global variables are not allowed as it wont fit good with the concept of encapsulation.

Q17.  Can we call constructor explicitly ?

Ans. Yes.

Q18.  Does a class inherit the constructor of its super class?

Ans. No.

Q19.  Is it possible to compile and run a Java program without writing main( ) method?

Ans. Yes, it is possible by using a static block in the Java program.

Q20.  Can we call the garbage collector explicitly ?

Ans. Yes, We can call garbage collector of JVM to delete any unused variables and unreferenced objects from memory using gc( ) method. This gc( ) method appears in both Runtime and System classes of java.lang package.

Q21.  Does every class needs to have one non parameterized constructor ?

Ans. No. Every Class only needs to have one constructor - With parameters or without parameters. Compiler provides a default non parameterized constructor if no constructors is defined.  

Q22.  Can we use "this" within static method ? Why ?

Ans. No. Even though "this" would mean a reference to current object id the method gets called using object reference but "this" would mean an ambiguity if the same static method gets called using Class name.  

Q23.  Can try statements be nested?

Ans. Yes

Q24.  Will static block for Test Class execute in the following code ? 

class Test
{
  static
  {
     System.out.println("Executing Static Block.");
   }
   public final int param=20;
   
   public int getParam(){
  return param; 
   }
}

public class Demo 
{
   public static void main(String[] args) 
   {
      System.out.println(new Test().param);
    }
}


Ans. Yes. 

Q25.  Is JVM a overhead ? 

Ans. Yes and No. JVM is an extra layer that translates Byte Code into Machine Code. So Comparing to languages like C, Java provides an additional layer of translating the Source Code.

C++ Compiler - Source Code --> Machine Code
Java Compiler - Source Code --> Byte Code   ,  JVM - Byte Code --> Machine Code

Though it looks like an overhead but this additional translation allows Java to run Apps on all platforms as JVM provides the translation to the Machine code as per the underlying Operating System. 

Q26.  Is it legal to initialize List like this ?

LinkedList<Integer> l=new LinkedList<int>(); 

Ans. No, Generic parameters cannot be primitives.

Q27.  Can finally block throw an exception ?

Ans. Yes.

Q28.  Can we have try and catch blocks within finally ?

Ans. Yes

Q29.  Will this code compile fine ?

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));

Ans. Yes.

Q30.  Will this code work fine if BuggyBread2 doesn't implement Serializable ?

class BuggyBread1 extends BuggyBread2 implements Serializable{
private int x = 5;
public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
   objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Ans. Yes.

Q31.  Can we compose the Parent Class object like this ?

class BuggyBread1 extends BuggyBread2 {
private BuggyBread2 buggybread2; 
public static void main(String[] args){
buggybread2 = new BuggyBread2();
}
}

Ans. Yes.

Q32.  Will this code Work ? If not , Why ?

java.util.Calendar c = new java.util.Calendar();

Ans. No. It gives the error "Cannot Instantiate the type Calendar". Calendar is an abstract class and hence Calendar object should be instantiated using Calendar.getInstance().

Q33.  Can value be null in TreeMap ?

Ans. Yes.

Q34.  Can we overload main method in Java ?

Ans. Yes, but the overloaded main methods without single String[] argument doesn't get any special status by the JVM. They are just another methods that needs to be called explicitly.

Q35.  Is It Good to use Reflection in an application ? Why ?

Ans. no, It's like challenging the design of application. 

Q36.  Can we have a default method without a Body ?

Ans. No. Compiler will give error. 

Q37.  Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ?

Ans. No. Compilation error.

Q38.  Can we have a default method definition in the interface without specifying the keyword "default" ? 

Ans. No. Compiler complains that its an abstract method and hence shouldn't have the body.

Q39.  Can we use static method definitions in Interfaces ?

Ans. Yes, Effective Java 8.

Q40.  Can we extend an Enum ?

Ans. No. Enums are final by design.