Interview Questions and Answers - Order By Rating Q791. What will be the output of following code ?
Base Interface
public interface BaseInterface {
int i = 4;
}
Derived Interfaces
public interface DerivedInterface1 extends BaseInterface{
int i = 5;
}
public interface DerivedInterface2 extends BaseInterface{
int i=6;
}
Implementing Class
public class Class implements DerivedInterface1,DerivedInterface2 {
int i=10;
public static void main(String[] args){
System.out.println(new Class().i);
}
}
What will be the output upon executing main and Why ? Core Java
Ans. 10 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interfaces  coding  code intermediate Q792. What will be the output of following code ?
Base Interface
public interface BaseInterface {
int i = 4;
}
Derived Interfaces
public interface DerivedInterface1 extends BaseInterface{
int i = 5;
}
public interface DerivedInterface2 extends BaseInterface{
int i=6;
}
Implementing Class
public class Class implements DerivedInterface1,DerivedInterface2 {
public static void main(String[] args){
System.out.println(BaseInterface.i);
}
}
What will be the output upon executing main and Why ? Core Java
Ans. It will print 4 because member elements of an interface are implicitly static and hence the concept of overriding doesn't work.
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interfaces  coding  code  extending interfaces  diamond interfaces Asked in 1 Companies intermediate Q793. How and Why Strings are interned in Java ? Core Java
Ans. String class has a public method intern() that returns a canonical representation for the string object. String class privately maintains a pool of strings, where String literals are automatically interned.
these are automatically interned so as to have efficient String comparison using == operator instead of equals which is usually slower. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  string pool  string intern Asked in 1 Companies Ans. Component methods called by the container to notify the component of the events Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Ans. Applet is a J2EE component or a tiny application that gets executed in a widget engine ( like browser ) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  applet Q796. What does the Web module contains ? Java EE
Ans. JSP files, media files and deployment descriptor ( web.xml ) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee  j2ee web module Q797. What are the different J2EE modules ? Java EE
Ans. 1. Application Client Module
2. Web Module
3. EJB
4. Resource Adapter Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee  j2ee modules Q798. What is the difference between
File f = new File("homexyz.txt");
and
File f = File.createTempFile("xyz",".txt","home"); ? Core Java
Ans. First will not create physical file in storage whereas the second will. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  file handling  File.createTempFile  file handling intermediate   rare Q799. What are the benefits of creating immutable objects ? Core Java
Ans. 1. Security and Safety - They can be shared across multiple threads as they are thread safe. Moreover, it protects then from bad state due to interception by the other code segment. One such problem due to mutability and access by alternate code segment could be the change of hash code and then the impact on its search with hash collections.
2. Reuse - In some cases they can be reused as only one copy would exist and hence it can be relied upon. For example - String Pool Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutable  immutability objects Asked in 1 Companies Intermediate   frequent Usually asked to entry level software developers. Q800. Write a program to swap two variables without using third Core Java
Ans. public static void main(String[] args) {
int num1 = 1;
int num2 = 2;
num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;
System.out.print("num1 = " + num1 +", num2 = "+num2);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding Asked in 37 Companies basic   frequent Q801. What is memory leak ? How Java helps countering memory leaks compared to C++ ? Core Java
Ans. Memory Leak is a situation wherein we have a reserved memory location but the program has lost its reference and hence has no way to access it.
Java doesn't have concept of Pointers, Moreover Java has concept of Garbage collection that frees up space in heap that has lost its references. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  memory leak  garbage collection intermediate   frequent Q802. Can we override wait() or notify() methods? Core Java
Ans. wait and notify are declared final in object class and hence cannot be overridden. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  object class methods  wait  notify Q803. Can we have only try block in java ? Core Java
Ans. No, It should be followed by either catch or finally. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  try  exception handling Q804. Does Java supports Default arguments ? Core Java
Ans. No Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Ans. Its used to access the object properties using the object reference or class properties using the Class Name. Moreover its used to access the classes and Interfaces of a package. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  operators Q806. What is a class and object ?
or
How would you explain a fresher the concept of class and object in simple terms ? Core Java
Ans. Class is a template using which objects are created in memory. It's kind of a mold using which objects with body / state are made.
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  class  object  class vs object Asked in 4 Companies basic   frequent Q807. Have you heard of SiteMesh ? Java EE
Ans. Sitemesh is a web page layout and decoration framework by OpenSymphony. SiteMeshFilter can intercept the requests and then build UI components (based on configuration) then renders the final page. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  sitemesh Q808. What is the difference between filter and filter-mappings xml tag within deployment descriptor ? Java EE
Ans. filter tag speficies the filter name and respective class for handling filter action whereas filter mapping maps the filter to the url patterns ( specifying the url that needs to be intercepted ) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  deployment descriptor  web.xml  filters  filter mapping Q809. What is a Servlet Filter ? Java EE
Ans. It's an object that can intercept http request and response and hence we can take appropriate action on those requests.
There are different types of filters based on Specifications like
Authentication
Logging
Encryption
Tokenizing
etc Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  servlets  servlet filters Asked in 3 Companies basic   frequent Q810. Do you feel that its useless to define hashCode method for a class ? Core Java
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hashcode  hash collections  search Q811. How can be restrict creation of more than n objects in Java ? Core Java
Ans. We can follow the same design which we follow in singleton Classes. We can have a static counter that will keep the count of number of objects already created, We can keep the constructor private and being called through the static method. We can keep incrementing the counter before calling the constructor and put a check to call only if it's lesser than n. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q812. What are the cursors available in Java ? Core Java
Ans. Enumeration
Iterator
List iterator Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  cursors  iterator Q813. Which memory segment holds String Pool in Java ? Core Java
Ans. String Pool resides in runtime constant Pool which is a part of Heap memory segment. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string pool  memory management Expert   rare Q814. how to access the methods of an inner class? Core Java
Ans. It depends on the type of inner class
To access non static inner class method
new OuterClass().new InnerClass().getMethod();
To access static method of static inner class
new OuterClass.InnerClass().getMethod(); Sample Code for inner class Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested aclasses Asked in 1 Companies Intermediate   frequent Ans. Shutdown hook is a thread that is invoked before the JVM is shut down. we can use it perform resource cleaning task. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  shutdown hook  jvm Try 1 Question(s) Test Q816. Difference between shallow copy and object cloning ? Core Java
Ans. Shallow copy is one of the way for object cloning, other being deep copy. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  cloning  shallow copy  object cloning basic Q817. Can we convert a numeric IP address to a web host name ? Java EE
Ans. Yes, using InetAddress.getByName("<IP Address>").getHostName(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  ip address to hostname  INETAddress intermediate Ans. It's an object that reads from one stream and writes to another. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java io  io filter intermediate Q819. What is the difference between the Reader/Writer java.io hierarchy and the Stream class hierarchy? Core Java
Ans. The Reader/Writer hierarchy is character oriented, whereas the Stream class hierarchy is byte oriented. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java io  streams intermediate Q820. What kind of thread is garbage collection thread ? Core Java
Ans. Daemon thread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  garbage collection intermediate Try 2 Question(s) Test