Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Top 100 Java Interview Questions with Answers
Q75. Can a variable be local and static at the same time?
Ans: No a variable can't be static as well as local at the same time. Defining a local variable as static gives compilation error.
Q76. Can we have static methods in an Interface?
Ans: Static methods can't be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java.
Q77. In a class implementing an interface, can we change the value of any variable defined in the interface?
Ans: No, we can't change the value of any variable of an interface in the implementing class as all variables defined in the interface are by default public, static and Final and final variables are like constants which can't be changed later.
Q78. Is it correct to say that due to garbage collection feature in Java, a java program never goes out of memory?
Ans: Even though automatic garbage collection is provided by Java, it doesn't ensure that a Java program will not go out of memory as there is a possibility that creation of Java objects is being done at a faster pace compared to garbage collection resulting in filling of all the available memory resources.
So, garbage collection helps in reducing the chances of a program going out of memory but it doesn't ensure that.
Q79. Can we have any other return type than void for main method?
Ans: No, Java class main method can have only void return type for the program to get successfully executed.
Nonetheless , if you absolutely must return a value to at the completion of main method , you can use System.exit(int status)
Q80. I want to re-reach and use an object once it has been garbage collected. How it's possible?
Ans: Once an object has been destroyed by garbage collector, it no longer exists on the heap and it can't be accessed again. There is no way to reference it again.
Q81. In Java thread programming, which method is a must implementation for all threads?
Ans: Run() is a method of Runnable interface that must be implemented by all threads.
Q82. I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?
Ans: This can be implemented by use of the concept of synchronization. Database related code can be placed in a method which hs synchronized keyword so that only one thread can access it at a time.
Q83. How can an exception be thrown manually by a programmer?
Ans: In order to throw an exception in a block of code manually, throw keyword is used. Then this exception is caught and handled in the catch block.
public void topMethod() { try { excMethod(); } catch (ManualException e) {} } public void excMethod { String name = null; if (name == null) { throw (new ManualException("Exception thrown manually "); } }
Q84. I want my class to be developed in such a way that no other class (even derived class) can create its objects. How can I do so?
Ans: If we declare the constructor of a class as private, it will not be accessible by any other class and hence, no other class will be able to instantiate it and formation of its object will be limited to itself only.
Q85. How objects are stored in Java?
Ans: In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.
Q86. How can we find the actual size of an object on the heap?
Ans: In java, there is no way to find out the exact size of an object on the heap.
Q87. Which of the following classes will have more memory allocated?
Class A: Three methods, four variables, no object
Class B: Five methods, three variables, no object
Ans: Memory isn't allocated before creation of objects. Since for both classes, there are no objects created so no memory is allocated on heap for any class.
Q88. What happens if an exception is not handled in a program?
Ans: If an exception is not handled in a program using try catch blocks, program gets aborted and no statement executes after the statement which caused exception throwing.
Q89. I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor's body?
Ans: If a class has multiple constructors, it's possible to call one constructor from the body of another one using this().
Q90. What's meant by anonymous class?
Ans: An anonymous class is a class defined without any name in a single line of code using new keyword.
For example, in below code we have defined an anonymous class in one line of code:
public java.util.Enumeration testMethod() { return new java.util.Enumeration() { @Override public boolean hasMoreElements() { // TODO Auto-generated method stub return false; } @Override public Object nextElement() { // TODO Auto-generated method stub return null; } }
Q91. Is there a way to increase the size of an array after its declaration?
Ans: Arrays are static and once we have specified its size, we can't change it. If we want to use such collections where we may require a change of size ( no of items), we should prefer vector over array.
Q92. If an application has multiple classes in it, is it okay to have a main method in more than one class?
Ans: If there is main method in more than one classes in a java application, it won't cause any issue as entry point for any application will be a specific class and code will start from the main method of that particular class only.
Q93. I want to persist data of objects for later use. What's the best approach to do so?
Ans: The best way to persist data for future use is to use the concept of serialization.
Q94. What is a Local class in Java?
Ans: In Java, if we define a new class inside a particular block, it's called a local class. Such a class has local scope and isn't usable outside the block where its defined.
Q95. String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?
Ans: Although String and StringBuffer both represent String objects, we can't compare them with each other and if we try to compare them, we get an error.
Q96. Which API is provided by Java for operations on set of objects?
Ans: Java provides a Collection API which provides many useful methods which can be applied on a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet and TreeMap.
Q97. Can we cast any other type to Boolean Type with type casting?
Ans: No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data type to any other primitive data type.
Q98. Can we use different return types for methods when overridden?
Ans: The basic requirement of method overriding in Java is that the overridden method should have same name, and parameters.But a method can be overridden with a different return type as long as the new return type extends the original.
For example , method is returning a reference type.
Class B extends A { A method(int x) { //original method } B method(int x) { //overridden method } }
Q99. What's the base class of all exception classes?
Ans: In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class.
Q100. What's the order of call of constructors in inheritiance?
Ans: In case of inheritance, when a new object of a derived class is created, first the constructor of the super class is invoked and then the constructor of the derived class is invoked.