Top Advanced Java Interview Questions and Answers

    11 Votes

21) Can you explain about reflection in Java ?

If a programmer wants to access entities or invoke methods in a program dynamically, i.e. if the programmer is unaware of the methods and variables that needs to be invoked at runtime but unaware of it while coding, we can use reflection. For example
Method method = ABC.getClass().getMethod("doSomething", null);
method.invoke(ABC, null);

22) Can you explain about java.lang.class ?

When JVM creates an instance of a class, it creates an object "java.lang.Class object" which describes the type of the object. This class object is shared by all the objects of a class. If you want to access the class object of an instance, use getClass() method of the object. This method is inherited from java.lang.Object

Ex: Created two instances class called Programmer
Programmer A = new Programmer();
Programmer B = new Programmer();
// For check Instances
if(A.getClass() == B.getClass())
{
System.out.println("A and B are instances of same class");
}else{
System.out.println("A and B are instances of different class");
}


23) Can you explain about Singleton class in Java ?


Singleton class is used to control no of object created for a class, limiting the number to one. But if the situation changes in future, it allows to create more objects without affecting existing clients.


24) Can you explain about Static class in Java ?


A class can be made static provided that the class is a nested class. A nested class is class which is defined inside a class. But top class can't me made static. Example :

public class Test
{
static class StaticInnerClass
{
public static void innerMethod()
{ System.out.println("Static Inner Class!"); }
}
public static void main(String args[])
{
Test.StaticInnerClass.innerMethod();
}
}


25) Can you explain about volatile Keywords in Java ?

  • Volatile keyword is used to indicate the threads using a common variable that, the variable which is declared as Volatile can be updated by multiple threads. So threads should not cache the threads locally and in turn should get the value for the variable from main memory. 
  • If a variable is declared as volatile, it won't be serialized.


26) What are the advantages of organizing classes and interfaces into a package ?

  • Determination of a category of a file is simplified.
  • Name space collision is avoided.
  • Access restriction can be applied with the use of packages.
  • Packages provide reusability of code


27) Can you explain about Java naming convention ? 


Common Naming conventions as below :

  • package names always start with lowercase characters. Ex: java.util
  • Class names always begin with a capital letter and followed next word start with a capital letter. Ex: GregorianCalendar
  • Java Naming convention specifies that instances and other variables must start with lowercase followed next word should be capital letter. Ex : MyClass myClass = new MyClass();
  • Constant variables are declared using “static final” modifiers. And such variables must contain only UpperCase charachters and multiple words must be seperated using ‘_’. Ex: static final char END_OF_FILE = 'e';
  • Methods in Java also follow the same like Objects and variables. For example
void myMethod(){
String strVal = "ABCD";
}

28) How to call a garbage collector in java?

System.gc() or Runtime.getRuntime().gc().


29) What are the new features available in Java 1.7 ?

  • Strings in switch Statement 
  • Type Inference for Generic Instance Creation 
  • Multiple Exception Handling 
  • Support for Dynamic Languages 
  • Try with Resources 
  • Java nio Package 
  • Binary Literals, underscore in literals 
  • Diamond Syntax 
  • Automatic null Handling


30) What are the advantage of Inheritance in Java ?

  • Re-usability : Inheritance helps derived class to use methods of base class without rewriting them
  • Extensibility : Extending the base class logic as per business logic of the derived class
  • Data hiding : Allows base class to keep some private data which can't be altered by the derived class
Page 3 of 6

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Java Interview Questions
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers