Java Interview Questions
Top 100 Java Interview Questions
Basic Java
What is API ?
An API is a collection of packages, classes, interfaces and sub-packages. And sub-package is also a collection of classes interfaces and sub sub packages etc.
Why Java take 2 byte of memory for store character ?
Java support more than 18 international languages so java take 2 byte for characters, because for 18 international language 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages. Java supports Unicode but c support ascii code. In ascii code only English language are present, so for storing all English latter and symbols 1 byte is sufficient.
What is java and javac ?
Java and javac are tools or application programs or exe files developed by sun micro system and supply as a part of jdk 1.5/1.6/1.7/1.8 in bin folder. Java tool are used for run the java program and javac tool are used for compile the java program.
Why Using naming Conversion ?
Different Java programmers can have different styles and approaches to write program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Readability of Java code is important because it means less time is spent trying to figure out what the code does, and leaving more time to fix or modify it.
Which syntax follow by java for naming conversion ?
Java follows camelcase syntax for naming the class, interface, method and variable.
What is JVM ?
JVM (Java Virtual Machine) is a software. It is a specification that provides runtime environment in which java bytecode can be executed.
What is operation of JVM ?
JVM mainly performs following operations.
- Allocating sufficient memory space for the class properties.
- Provides runtime environment in which java bytecode can be executed
- Converting byte code instruction into machine level instruction.
What is classloader ?
Class loader subsystem will load the .class file into java stack and later sufficient memory will be allocated for all the properties of the java program into following five memory locations.
- Heap area
- Method area
- Java stack
- PC register
- Native stack
What is Main aim of JIT Compiler ?
The main aim of JIT compiler is to speed up the execution of java program.
Why Boolean data types take zero byte memory ?
Boolean data type takes zero bytes of main memory space because Boolean data type of java implemented by Sun Micro System with a concept of flip - flop. A flip - flop is a general purpose register which stores one bit of information (one true and zero false).
Why Java is Simple and easy
Java is simple because of the following factors:
- Java is free from pointer due to this execution time of application is improve. [whenever we write a Java program we write without pointers and internally it is converted into the equivalent pointer program].
- Java have Rich set of API (application protocol interface).
- Java have garbage collector which is always used to collect un-Referenced (unused) Memory location for improving performance of a Java program.
- Java contains user friendly syntax for developing Java applications.
How Java have high performance ?
Java have high performance because;
- Java is use Bytecode which is more faster than ordinary pointer code so Performance of java is high.
- Garbage collector, collect the unused memory space and improve the performance of java application.
- Java have no pointers so that using java program we can develop an application very easily.
- It support multithreading, because of this time consuming process can be reduced to execute the program.
How Java is Platform Independent
A programming language or technology is said to be platform independent if and only if which can run on all available operating systems with respect to its development and compilation. (Platform represents Operating System).
Java is a platform independent programming language, Because when you install jdk software on your system then automatically jvm are install on your system. For every operating system separate jvm is available which is capable to read .class file or byte code. When we compile your java code then .class file is generated by javac compiler these code are readable by jvm and every operating system have its own jvm so jvm is platform dependent but due to jvm java language is become platform independent.
Difference between conditional and looping statement
Conditional statement executes only once in the program where as looping statements executes repeatedly several number of time.
Why a static block executes before the main method ?
A class has to be loaded in main memory before we start using it. Static block is executed during class loading. This is the reason why a static block executes before the main method.
Can we override static method ?
No, static method cannot be overridden.
Why we cannot override static method ?
because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.
Which Java operator is right associative ?
The = operator is right associative.
What is dot operator ?
The dot operator(.) is used to access the instance variables and methods of class objects.It is also used to access classes and sub-packages from a package.
Why use this keyword
The main purpose of using this keyword is to differentiate the formal parameter and data members of class, whenever the formal parameter and data members of the class are similar then jvm get ambiguity (no clarity between formal parameter and member of the class)
To differentiate between formal parameter and data member of the class, the data member of the class must be preceded by "this".
When Need of super keyword ?
Whenever the derived class is inherits the base class features, there is a possibility that base class features are similar to derived class features and JVM gets an ambiguity. In order to differentiate between base class features and derived class features must be preceded by super keyword.
What is the difference between this. (this dot) and this() (this off).
this. can be used to differentiate variable of class and formal parameters of method or constructor.
this() can be used to call one constructor within the another constructor without creation of objects multiple time for the same class.
Difference between static and final keyword
static keyword always fixed the memory that means that will be located only once in the program where as final keyword always fixed the value that means it makes variable values constant.
why main method is static ?
Because object is not required to call static method if main() is non-static method, then jvm create object first then call main() method due to that face the problem of extra memory allocation.
We can overload main() method ?
Yes, We can overload main() method. A Java class can have any number of main() methods. But run the java program, which class should have main() method with signature as "public static void main(String[] args). If you do any modification to this signature, compilation will be successful. But, not run the java program. we will get the run time error as main method not found.
Can we override java main method ?
No, because main is a static method.
Difference between Path and ClassPath
Path variable is set for use all the tools like java, javac, javap, javah, jar, appletviewer etc.
Classpath variable is used for set the path for all classes which is used in our program so we set classpath upto rj.jar. in rt.jar file all the .class files are present. When we decompressed rt.jar file we get all .class files.
What do you mean by portable ?
If any language supports platform independent and architectural neutral feature known as portable. The languages like C, CPP, Pascal are treated as non-portable language. JAVA is a portable language.
Why use Import keyword in java ?
Import is a keyword in java language used to import the predefined properties of java API into current working java program. Import Statement in Java
What is Wrapper Classes ?
For each and every fundamental data type there exist a pre-defined class, Such predefined class is known as wrapper class.
Why use Wrapper Classes ?
The main purpose of wrapper class is to convert numeric string data into numerical or fundamental data. We know that in java whenever we get input form user, it is in the form of string value so here we need to convert these string values in different different datatype (numerical or fundamental data), for this conversion we use wrapper classes. Wrapper Classes in Java
Access Specifier
Which access specifier are called universal access specifier ?
Public
Which access specifier is not a keyword ?
Default
Which access specifier is package level access specifier ?
Default
Scope of protected specifier ?
protected members of class is accessible within the same class and other class of same package and also accessible in inherited class of other package.
Scope of private access specifier ?
private members of class in not accessible any where in program these are only accessible within the class. private are also called class level access specifier.
Write a program in java which prints the numbers from 1 to 100. But, multiples of 3 should be replaced with "Fizz", multiples of 5 should be replaced with "Buzz" and multiples of both 3 and 5 should be replaced with "FizzBuzz"?.
Example Fizz Buzz Problem
public class FizzBuzzProblem { public static void main(String args[]) { for(int i = 1; i <= 100; i++) { if((i % (3*5)) == 0) { System.out.println("FizzBuzz"); } else if ((i % 5) == 0) { System.out.println("Buzz"); } else if ((i % 3) == 0) { System.out.println("Fizz"); } else { System.out.println(i); } } } }