Tuesday, October 9, 2018

Core Java Interview Question

Core Java Interview Question


Q1. Explain public static void main(String args[]).

  • public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
  • static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.
  • void : It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[] : It is the parameter passed to the main method
Q2. Differentiate between JDK, JRE and JVM.
  • JVM stands for Java Virtual Machine which provides runtime environment for Java Byte Codes to be executed.
  • JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime.
  • JDK (Java Development Kit) consists of JRE along with the development tools required to write and execute a program.
Q3  What is JVM and is it platform independent?

 JVM is responsible for converting byte code into machine readable code. JVM is not platform independent, thats why you have different JVM for different operating systems. We can customize JVM with Java Options, such as allocating minimum and maximum memory to JVM. It’s called virtual because it provides an interface that doesn’t depend on the underlying OS. 

Q4  Name some OOPS Concepts in Java? 

Java is based on Object Oriented Programming Concepts, following are some of the OOPS concepts implemented in java programming.
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Association
  • Aggregation
  • Composition
Q5 What is String in Java? String is a data type? 
 String is a Class in java and defined in java.lang package. It’s not a primitive data type like int and long. String class represents character Strings. String is used in almost all the Java applications and there are some interesting facts we should know about String. String in immutable and final in Java and JVM uses String Pool to store all the String objects.
Some other interesting things about String is the way we can instantiate a String object using double quotes and overloading of “+” operator for concatenation.

Q6 What are different ways to create String Object?
We can create String object using new operator like any normal java class or we can use double quotes to create a String object. There are several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilder.

String str = new String("abc");
String str1 = "abc";



When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.




 Q7 Why Char array is preferred over String for storing password?


String is immutable in java and stored in String pool. Once it’s created it stays in the pool until unless garbage collected, so even though we are done with password it’s available in memory for longer duration and there is no way to avoid it. It’s a security risk because anyone having access to memory dump can find the password as clear text.
If we use char array to store password, we can set it to blank once we are done with it. So we can control for how long it’s available in memory that avoids the security threat with String.

Q8 Is String thread-safe in Java? Why?

Yes, String is thread-safe because it's Immutable. All Immutable objects are thread-safe because once they are created, they can't be modified, hence no issue with respect to multiple threads accessing them.

Q9 Can we use String in switch case in Java?

Yes, after JDK 7 release, you can use the String in the switch case in Java. Earlier it wasn't possible but now it's possible.

Q10 When you execute String str = new String("abcd")? how many String objects are created? 
 
This is another tricky Java interview question as many Java developer will answer just one but that's not true. There are two String objects are created here, the first String object is created by String literal "abcd" and the second one is created by new String()
 
Q11 Difference between str1 == str2 and str1.equals(str2)?
 
The key difference is that first one is using == operator which does reference based comparison while the second one is using equals() method which does a content-based comparison. Since String class overrides equals() to perform character-based comparison first will return true only if both str1 and str2 points to the same object but the second one will return true if str1 and str2 have same content even if they are a different object
 

0 comments:

Post a Comment