Cognizant Java interview questions and answers.

Java Interview Questions and Answers covered in this post:


    Java interview questions and answers.


        What is Heap memory and Stack Memory? What is the difference between Heap Memory Stack Memory?

    -          Heap Memory :

    o   Heap memory is used to store the Objects. 

    -          Stack Memory:

    o   Stack memory is used to store the local variables and function list.

     

    Heap Memory

    Stack Memory

    1.

    Heap memory stores the objects.

    Stack Memory stores local variables, reference variables and methods.

    2.

    Objects stores in a hierarchical manner.  

    Variables stores in a sequential manner.

    3.

    Objects can reside in heap memory until it points to reference variable.

    Variables will be removed once the function is executed.

    4.

    Access speed is slow.

    Access speed is high.

    5.

    If Heap memory gets full JVM immediately throw OutOfMemoryError.

    If Stack gets full JVM immediately throws StackOverflowError.

    6.

    The Garbage collections mechanism is used to reclaim the memory.

    There is no role of garbage collector because variables are automatically removed once the function is executed.

    7.

    Heap is a flexible memory.

    Stack is not flexible memory.

    8.

    -Xms and –Xmx JVM options can be used to increase or decrease the size of the Heap.

    -Xss option can be used to increase the size of the stack.



        Why java doesn't support Multiple Inheritance?

    -     To avoid an ambiguity situation java doesn’t support multiple inheritance.

    -    We can illustrate with an example. You have three classes. Class A, Class B and Class C.

    Class C extends Class A and Class B. Say suppose Class A and Class B has printHello() method. The compiler cannot decide whether which class printHello() method should inherit in class C. So avoiding this ambiguity situation java doesn’t support multiple inheritance.

    -     But indirectly java supports multiple inheritance through interfaces.


        Can we declare static method and static variables in abstract class?

    -          Yes. You can declare static method and static variables inside an abstract class.


     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in


        Java.lang.Object class have equals() method then why there is a need to override equals() method in user-defined class?

    -          Object class equals() method compares references not the actual content of an object.

    -          In short, if two references pointing to the same object then only it will return true otherwise it will return false irrespective of content. That’s why we have to override the equals() method in the user-defined class.

    -    Once we override the equals() method in the user-defined class then we can implement a logic to compare the actual content of the object.


        Can you override the static method?

    -          No. We cannot override the static method.

    -          Static method belongs to class not instance.

    -          Only instance method can be overridden cause each object has its member variables and methods.

    -          If you are trying to override the static method that will be considered as method shadowing.


        Can constructor declare exception with throws keyword?

    -          Yes we can declare an exception in the constructor definition with the throws keyword.

    -          We can declare both exceptions checked exceptions and unchecked exceptions in the constructor definition.

    -          For your reference, I have provided multiple scenarios in the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.FileNotFoundException;

    import java.io.IOException; 

    public class ExceptionThrowsInConstructor { 

          public ExceptionThrowsInConstructor() throws FileNotFoundException{      

          }     

          public ExceptionThrowsInConstructor(int num) throws IOException{           

          }     

          public ExceptionThrowsInConstructor(String str) throws ArrayIndexOutOfBoundsException, NullPointerException{           

          }    

    }

     

        What are Upcasting and Downcasting?

    -          Upcasting means casting the child object into the parent object.

    -          Implicit upcasting is possible.

    -          UpCasting is also called Widening.

    -          Downcasting means casting the parent object into a child object.

    -          Implicit downcasting is not possible.

    -          Downcasting is also known as Narrowing.

    -          Please refer to the below example for upcasting and downcasting.

    package simplifiedjava.crackedInterview; 

    public class Parent {     

          public void check(){

                System.out.println("Parent Class check() method");         

          }    

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public void check(){

                System.out.println("Child Class check() method");          

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class UpCastingDownCastingDemo { 

          public static void main(String[] args) {           

                Parent p = (Parent)new Child(); // Upcasting

                p.check();           

                Parent p1 = new Child();

                Child c = (Child)p1;    //downcasting

                p1.check();

          }

    }

    Output :

    Child Class check() method.

    Child Class check() method.

     

        How many types we can handle the exception?

    -          We can handle exceptions in two ways.

    1.       Using try and catch block.

    2.       Using throws keyword.

    -          Please refer to the below example to try-catch block.

    package simplifiedjava.crackedInterview; 

    public class Aeroplane {

          public void setLocation(String locationName) {

                try {

                      if(locationName.equals(null)) {

                            throw new NullPointerException();

                      }

                }catch(NullPointerException e) {

                      e.printStackTrace();

                }

          }    

    }

     -          Please refer to the below example for the throws keyword.

    package simplifiedjava.crackedInterview;

    public class Aeroplane { 

          public void setLocation(String locationName)throws NullPointerException {

                if(locationName.equals(null)) {

                      throw new NullPointerException();

                }          

          }    

    }


        Can you explain why NoClassDefFoundError occurred?

    -          Java Virtual Machine is not able to find a particular class at runtime which was available at compile which triggers NoClassDefFoundError.

    -          If a class was present during compile time but not available in java classpath during runtime.


        What is an AutoClosable resource?

    -          The resource is an object that must be closed after finishing the program.

    -          AutoClosable statement ensures that the resource is closed at the end of the statement execution.

    -          Please refer below example.

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.util.Scanner; 

    public class AutoClosableResourceDemo { 

          public static void main(String[] args) {

                Scanner scanner = null;

                try {

                    scanner = new Scanner(new File("Resume.doc"));

                    while (scanner.hasNext()) {

                        System.out.println(scanner.nextLine());

                    }

                } catch (FileNotFoundException e) {

                    e.printStackTrace();

                } finally {

                    if (scanner != null) {

                        scanner.close();

                    }

                }

          }

    }



    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here

    Upcoming questions in next post:

    1. What is JVM? What is the difference between JDK, JVM and JRE.

    2. Will the code compile If parent class method is not throwing any exception but child class method is throwing an checked exception.

    3. What is constructors. What are the types of constructor.

    4. Can we declare static method and static variables in abstract class.

    5. What is object cloning.

    6. What is the difference between StringBuilder and StringBuffer.

    7. Can we declare empty catch block.

    8. What is Thread-Local class. What is the purpose to have this class.

    9. What is the difference between Regular Inner Class and Annonymous Inner Class.19 May Diary

    10.Why Collection interface doesn't extends Clonable and Serializable interface. 


    Previous Post                                                                    Next Post



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     

    Post a Comment

    1 Comments