Interview Questions : PART 1
Question 1: Give few reason of using java? Or it may be why java?
Answer: Instead of going into technical aspect I will tell some overview
a) Java provides Client technology, Server technology as well as integration technology for building small scale to large scale business application.
b) If you want to build application for multiple platform then java is best option as it dictate Write once run anywhere notion.
c) The emergence of many open source technologies like Apache, Spring and many others, integrated with java also makes it powerful.
d) As java has been in technical world since long so it has been tested and deployed in many industries so with the time it has matured more in terms of implementation as well as APIs
e) With its maturity java also provides Standardized specification publicly available from various resources and with the time it gets frequent updates and release, reducing flaws and bugs.
Question 2: What is the difference between a “compiler” and an “interpreter”?
Answer: Compilers and interpreters have similar functions: They take a program written in some programming language and translate it into machine language.
A compiler does the translation all at once. It produces a complete machine language program that can then be executed. An interpreter, on the other hand, just translates one instruction at a time, and then executes that instruction immediately.
Java uses a compiler to translate java programs into Java Bytecode, which is a machine language for the imaginary Java Virtual Machine. Java Bytecode programs are then executed by an interpreter.
Question 3: If you have the source code for a Java program, and you want to run that program, will you need both a compiler and an interpreter? What does the Java compiler do and what does the Java interpreter do?
Answer: The Java compiler translates Java programs into a language called Java bytecode. Although bytecode is similar to machine language, it is not the machine language of any actual computer.
A Java interpreter is used to run the compiled Java bytecode program. Each type of computer needs its own Java bytecode interpreter, but all these interpreters interpret the same bytecode language.
Important Points: Since packages were created to avoid naming conflict, what happens when two packages contain two classes with the same name?
for example ,we have a package called in.technoblog.post containing a Abc class (along with other classes).
Then we have another package in.technoscienc1 also containing a Abc class (obviously with different logic and fuctionality).
Now let’s suppose we need every class in in.technoblog.post and the Abc class from in.technoblog.post1
when we write following line:Which one gets called in this case?
Abc abc = new Abc();
Class from Which package gets called in above case?
Or does this give a compile error? if compilation error,then what should be your solution?
Question 4: Why java uses Classpath parameter or Environment variables?
Answer: The CLASSPATH variable is a Java way to tell the possible locations of user classes or jar files for a Java application. Since each user/computer may choose to have classes in different location its best to have custom locations configured in Classpath variable.
In case you have not set the classpath environment variable, the default value is used as the “.” (current directory). That means, the current directory is searched.
In a Java class import statements are used to access other classes. You also do a wild card import like in.technoblog.post.* on your java file.
In such cases, It will become very impractical/slow for the Java Virtual Machine to search for classes in every file/folder on a machine, therefore you can provide the Java Virtual Machine with a list of places to look. This is done by putting folder and jar files in your classpath.
Question 5: When does java read values of classpath variable?
Answer: Java uses the CLASSPATH environment variable to read the classes and libraries from file system. This variable is used by all JDK Tools and Extension including Java Compiler (javac) and JRE(java).These use this variable to locate the dependent user classes and jar files to perform specific tasks.
Java Compiler uses it to locate the dependent user classes and jar files to compile Java source files.
Java Run-time Environment (JRE) uses the classpath variable to identify the location of files to be loaded for the run-time dependencies (e.g. classes and jar files) of java program.
Question 6: Out of following, which happens at compile time and which happens at runtime or may be at compile time and runtime both?
1) object creation. // run-time
2) method overloading //compile-time
3) method overriding //run-time
4) generics //compile-time
5) Annotation // compile time : @override, runtime : @test used by junit to write unit test
6)inheritance // compile-time
7)composition // run-time
8)Exception // compile-time as well as run-time
Question 7: Can I import same package/class twice? Will the JVM load the package twice at runtime?
Answer: One can import the same package or same class multiple times. Neither compiler nor JVM complains will complain about it. And the JVM will internally load the class only once no matter how many times you import the same class.