JavaInterview Flashcards
What do you understand by a variable?
Variable is a named memory location that can be easily referred in the program. A variable is just a container for holding data and is accesible through a reference.
What is the Java API?
The Java API(Application Programming Interface) is the set of classes included with the Java Development Environment. These classes are written using the Java language and run on the JVM.
The Java API includes everything from collection classes to GUI classes
What is JVM,JRE, JDK?
The Java Virtual Machine(JVM) is the software that can be ported onto various hardware-based platforms.The JVM executes instructions that a Java compiler generates.
The JRE is the Java Runtime Environment consisting of the JVM, the Java libraries, and all other components necessary to run Java applications and applets, but does not contain any development tools such as a compiler or a debugger JDK contains software development tools which are used to compile and run the Java program.
Both JDK and JRE contains the JVM.
What if the main method is declared as private?
The program compiles properly but at runtime it will give “Main method not public” error message.
What if the static modifier is removed from the signature of the main method?
Program compiles.But at runtime throws an error “NoSuchMethodError”
What if I do not provide the String array as the argument to the method?
The program works fine.
What is meant by pass by reference and pass by value in Java?
Pass by reference means, passing the address itself(like pointers) rather than passing the value.
Pass by value means passing a copy of the value.
Java uses pass by value.
If you’re overriding the method equals() of an object, which other method you might also consider?
hashCode()
What is Byte Code?
All Java programs are compiled into class files that contain bytecodes.
Java bytecode is produced by the Java compiler and executed by the JVM.
Bytecode is the intermediate representation of Java programs just as assembly code is the intermediate representation of C or C++ programs
Java bytecode is one of the things that make it possible for Java code to run on many different platforms.
Expain the reason for each keyword of public static void main(String args[])?
public main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.
static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.
void: main does not return anything so the return type must be void
String args[] The argument String indicates the argument type which is given at the command line and args is an array for string given during command line.
What are the differences between == and .equals()?
The == operator compares two objects to determine if they are present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory.
== compares references while .equals compares contents.
The method public boolean equals(Object obj) is provided by the Object class and can be overridden.
The default implementation uses the equality operator == to compare the object.
String, BitSet, Date, and File override the equals() method.
For two String objects, value equality means that they contain the same character sequence.
Example:
public static void main(String[] args) { String s1 = "abc"; String s2 = s1; String s5 = "abc"; String s3 = new String("abc"); String s4 = new String("abc"); System.out.println("== comparison : " + (s1 == s5)); System.out.println("== comparison : "" + (s1 == s2)); System.out.println("Using equals method : " + s1.equals(s2)); System.out.println("== comparison : "" + s3 == s4); System.out.println("Using equals method : " + s3.equals(s4)); }
Output
== comparison : true == comparison : true Using equals method : true == comparison : false Using equals method : true
What is final, finalize() and finally? What does it mean that a class or member is final?
final - Variables defined in an interface are implicitly final.
A final class can’t be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve.
A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant)
finally - a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method.
finalize - helps in garbage collection. finalize() method is used just before an object is destroyed and garbage collected
Why there are no global variables in Java?
Global variables are globally accessible. Java encapsulates data and makes it available through methods. Every variable in Java must be declared within a class.
How to convert String to Number in java program?
The valueOf() function of Integer class is is used to convert string to Number. Here is the code
Eg:
String numString = "1000"; int id = Integer.valueOf(numString).intValue();
What is the difference between a while statement and a do statement?
A while statement (pre test) checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement (post test) checks at the end of a loop.
The do statement will always execute the loop body at least once.
Describe the principles of OOPS.
There are three main principles:
- Polymorphism
- Inheritance
- Encapsulation
Explain the Inheritance principle.
Inheritance is the process by which one object acquires the properties and behaviour of another object. Inheritance allows well-tested methods to be reused and enables changes to be made once and have effect in all relevant places.
What do you understand by casting in java language? What are the types of casting?
The process of converting one data type to another is called Casting. There are two types of casting in Java; these are implicit casting and explicit casting.
What is implicit and explicit casting?
Implicit casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not work for all scenarios.
Explicit casting in the process in which the complier are specifically informed about transforming the object.
Implicit casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not work for all scenarios.
Explicit casting in the process in which the complier are specifically informed about transforming the object.
Eg:
int i = 1000, k =100.20; long j = i; //Implicit int j = (int) i; //Explicit
What do you understand by downcasting?
The process of Downcasting refers to the casting from a general to a more specific type, i.e. casting down the hierarchy.