After reading these tricky Core Java questions, you can easily attempt the objective type and multiple choice type questions on Core Java.
There is the list of core Java interview questions. If there is any core Java interview question that has been asked to you, kindly post it in the ask question section. The answers to the Core Java interview questions are short and to the point. Copyright Abuse. Enter company name. Forgot Password. Java Basic Interview Questions 1. Why is Java a platform independent language? Why is Java not a pure object oriented language?
Why does Java not make use of pointers? What do you understand by an instance variable and a local variable? What do you mean by data encapsulation? Tell us something about JIT compiler. How is an infinite loop declared in Java? Briefly explain the concept of constructor overloading Comment on method overloading and overriding by citing relevant examples.
A single try block and multiple catch blocks can co-exist in a Java Program. Explain the use of final keyword in variable, method and class. Do final, finally and finalize keywords have the same function? When can you use super keyword? Can the static methods be overloaded?
Can the static methods be overridden? What is the main objective of garbage collection? What part of memory - Stack or Heap - is cleaned in garbage collection process? Java Intermediate Interview Questions Apart from the security aspect, what are the reasons behind making strings immutable in Java?
Using relevant properties highlight the differences between interfaces and abstract classes. In Java, static as well as private method overriding is possible. Comment on the statement. What makes a HashSet different from a TreeSet? Why is the character array preferred over string for storing confidential information? What is the importance of reflection in Java?
What are the different ways of threads usage? What are the differences between constructor and method of a class in Java? Which among String or String Buffer should be preferred when there are lot of updates required to be done in the data?
How to not allow serialization of attributes of a class in Java? What happens if the static modifier is not included in the main method signature in Java? What happens if there are multiple main methods inside one class in Java? What do you understand by Object Cloning and how do you achieve it in Java? How does an exception propagate in the code? Is it mandatory for a catch block to be followed after a try block?
Will the finally block get executed when the return statement is written at the end of try block and catch block as shown below? Can you call a constructor of a class inside the another constructor? Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Java Advanced Interview Questions Although inheritance is a popular OOPs concept, it is less advantageous than composition. How is the creation of a String using new different from that of a literal?
Is exceeding the memory limit possible in a program despite having a garbage collector? Why is synchronization necessary? Explain with the help of a relevant example. In the given code below, what is the significance of Can you explain the Java thread lifecycle? What could be the tradeoff between the usage of an unordered array versus the usage of an ordered array?
Is it possible to import the same class or package twice in Java and what happens to it during runtime? In case a package has sub packages, will it suffice to import only the main package? Does importing of com. Will the finally block be executed if the code System.
What do you understand by marker interfaces in Java? Why is it said that the length method of String class doesn't return accurate results? What is the output of the below code and why? What are the possible ways of making object eligible for garbage collection GC in Java? Java Interview Programs Check if a given string is palindrome using recursion.
Write a Java program to check if the two strings are anagrams. Write a Java Program to find the factorial of a given number. Given an array of non-duplicating numbers from 1 to n where one number is missing, write an efficient java program to find that missing number. Write a Java Program to check if any number is a magic number or not. A number is said to be a magic number if after doing sum of digits in each step and inturn doing sum of digits of that sum, the ultimate result when there is only one digit left is 1.
Conclusion The only condition to run that byte code is for the machine to have a runtime environment JRE installed in it. Click here to download. A String is made immutable due to the following reasons: String Pool: Designers of Java were aware of the fact that String data type is going to be majorly used by the programmers and developers. Thus, they wanted optimization from the beginning. They came up with the notion of using the String pool a storage area in Java heap to store the String literals.
They intended to decrease the temporary String object with the help of sharing. An immutable class is needed to facilitate sharing. The sharing of the mutable structures between two unknown parties is not possible. Thus, immutable Java String helps in executing the concept of String Pool.
Multithreading: The safety of threads regarding the String objects is an important aspect in Java. No external synchronization is required if the String objects are immutable. Thus, a cleaner code can be written for sharing the String objects across different threads.
The complex process of concurrency is facilitated by this method. If the String objects are not immutable, then it can get modified during the period when it resides in the HashMaps. Consequently, the retrieval of the desired data is not possible. Such changing states pose a lot of risks.
Therefore, it is quite safe to make the string immutable. Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage area. Efficiency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. For example appending a character is fastest in StringBuilder and very slow in String because a new memory is required for the new String with appended character.
Thread-safe: In the case of a threaded environment, StringBuilder and StringBuffer are used whereas a String is not used. However, StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads. Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes.
Variable types : Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables. Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances.
Mayur says:. August 17, at am. August 16, at am. July 31, at pm. Yeek says:. April 26, at pm. November 29, at am. March 3, at am. February 26, at pm. February 14, at am. February 5, at pm. January 3, at pm. Amol shinde says:. December 23, at pm. Manishanand Joshi says:. November 26, at pm. November 25, at pm. AbuGe says:. November 20, at pm. Viswanathan S says:. November 18, at am. November 16, at pm. November 14, at am. November 6, at pm. October 31, at am.
Manoj says:. October 11, at am. August 31, at am. August 30, at am. Rita says:. July 22, at pm. July 11, at am. Aruna says:. June 1, at am. May 28, at pm. May 29, at am. A says:. May 26, at am. Jansi says:. April 21, at am. April 21, at pm. April 24, at pm. Raj says:. December 5, at am. Shaheed says:. April 18, at am. Sudhakar says:.
April 14, at am. April 1, at pm. Anil says:. March 31, at am. Devendra Patel says:. March 19, at am. March 13, at am. Ruchi says:. March 12, at pm. March 9, at pm. March 8, at pm. March 17, at am. Yogesh S says:. March 6, at pm. Jerin J says:. March 5, at am. Ans: Multi threaded applications can be developed in Java by using any of the following two methodologies:. By using Java. Runnable Interface. Classes implement this interface to enable multi threading.
There is a Run method in this interface which is implemented. When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer? If we use String in such a case, for every data change a new String object will be created which will be an extra overhead. Ans: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically.
For automatic garbage collection java calls either System. Ans: If we want to execute any statements before even creation of objects at load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method. Can a class be a super class and a sub-class at the same time? Ans: If there is a hierarchy of inheritance used, a class can be a super class for another class and a sub-class for another one at the same time.
How objects of a class are created if no constructor is defined in the class? Ans: Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters. Ans: In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of synchronization.
Using synchronized keyword, we can ensure that only one thread can use shared resource at a time and others can get control of the resource only once it has become free from the other one using it. Ans: Constructor is called automatically when we create an object using new keyword. There are two classes named classA and classB. Both classes are in the same package. Can a private member of classA can be accessed by an object of classB?
Which method is to get invoked will depend upon the parameters passed. For example in the class below we have two print methods with same name but different parameters. Depending upon the parameters, appropriate one will be called:. Ans: We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object. Clone is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies.
Ans: Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class. Polymorphism Extensibility is another great benefit which allow new functionality to be introduced without effecting existing derived classes.
Ans: Default access specifier for variables and method is package protected i. How can we restrict inheritance for a class so that no class can be inherited from it? Ans: If we want a class not to be extended further by any class, we can use the keyword Final with the class name. Ans: When a method or a variable is declared with Protected access specifier, it becomes accessible in the same class,any other class of the same package as well as a sub-class.
Ans: Stack and Queue both are used as placeholder for a collection of data. Ans: If we want certain variables of a class not to be serialized, we can use the keyword transient while declaring them. Ans: Primitive data types like int can be handled as objects by the use of their respective wrapper classes.
For example, Integer is a wrapper class for primitive data type int. We can apply different methods to a wrapper class, just like any other object. Ans: Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code. Can we use a default constructor of a class even if an explicit constructor is defined? Ans: Java provides a default no argument constructor if no explicit constructor is defined in a Java class.
Can we override a method by using same method name and arguments but different return types? Ans: The basic condition of method overriding is that method name, arguments as well as return type must be exactly same as is that of the method being overridden.
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
Ans: Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block like from a static main method , an object of the class needs to be created first. Then using object reference, these methods can be invoked. What are the two environment variables that must be set in order to run any Java programs? Ans: Java programs can be executed in a machine only once following two environment variables have been properly set:.
0コメント