Interview Questions on Java - File Handlings

1. How to read a File?

By using FileReader ex:-
FileReader in = new FileReader("file1.txt);
in.read(x);// reads all elemetnts of file1.txt into the Array x.

2. How to write Contents into a file.

By using FileWriter
ex:-
FileWriter out = new FileWriter("file1.txt");
out.write("abc");
try
{
out.flush();
out.close();
}
catch(FileNotFoundException ex)
{
//some code
}

3. Can we write the elements line by line by using FileWriter?

No.But you can use BufferedWriter.In BufferedReader you can insert a new line by using newLine();
ex:- out.newLine();

4. Can we read the elements line by line by using FileReader?

No.But you can use BufferedReader.In BufferedReader you can read entire line by using readLine();
ex:- in.readLine();

5. What is Serialization?

Serialization is the process of converting an object into a stream of bytes.

6. What is marker Interface?

Interface which has no methods.

7. What is serializable ?

It is a marker Interface which is used to achieve srialization. Without implementing serializable marker interface we can not do serialization.

8. What is de-serialization?

De-serialization is the process of constructing an object from a stream of bytes.

9. How many ways can we create the objects?

1. By using new() operator.
2. By calling newInstance() method
ex: Class c1 = class.forName(classname);
Object o1 = c1.newInstance();
3. By using Clone() method.
4. while de-serialization
FileInputStream fis = new FileInputStream(file1);
Object o1= in.readObject();

10. What is Externelization?

It is an Interface that extends serializable marker Interface.It is used for attribute wise operation.

11. What is transient variable ?

transient is a key word used for variables to avoid serialization.


File Handlings

Interview Questions on Java - Strings

1. What is String?

String is a immutable class which used to represent the multiple Characters.

2. What is the Difference between String and StringBuffer?

String is immutable class.
StringBuffer is not immutable and has extra methods reverse() and delete().

3. What is the output of System.out.println(2+3+"44"+6);

5446

4. What is output of below program String s1= new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

false
true
== checks the memory references
equals method checks the contents

5. What is output of below program
String s3 = "abc";
String s4 = "abc";
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));

true
true
here we did not use the new operator.
So both s3 and s4 points to same object.

6. What is the Difference between StringBuffer and StringBuilder?

StringBuffer is synchronized and String Builder is not Synchronized.


Strings

Interview Questions on Java - Threads

1. What is Thread?

In java, Thread is a class which is used for multi threading purpose. Thread class is used to create and run the threads.

2. What is the Difference between multi processing and multi Threading?

In multi processing processor switch from one process to another.It is heavy weight process. multi threading happens in one process. it switch from one block to another block. It is light weight process.

3. What are the type of Threads.

There are two types of Threads.
1. User Thread : If Thread is Independent of parent Thread then it is User Thread.
2. Daemon Thread :
If Thread is Depending on parent Thread then it is Daemon Thread.

4. How many Threads create when a program is running?

3 Threads:
1. thread scheduler: schedules available times to all threads
2. garbage collector: used to remove abandoned objects.
3. main : executes main thread

5. How many ways are there to create the Thread.

Two ways:
1. By extending Thread class :
class A extends Thread {
public void run() { }
}
A a1 =new A();
a1.start();// it calls run method.

2. By implementing Runnable:
class A implements Runnable {
public void run() { }
}
A a1 = new A();
Thread t1 = new Thread(a1); t1.start();// calls run method

6. tell me some methods exist in Thread class?

1. run()
2. start();
3. sleep();
4. stop();
5. join();

7. What will happen when we call run() method instead of start()?

Code will be executed, but It will not act as a separate thread.

8. What is Synchronization?

Synchronization is the process of controlling multiple access to particular resource. In java synchronization is achieved by using synchronized keyword. synchronized keyword is used for methods and blocks.

9.Are wait(),notify() and notifyAll() available in Thread class?

No. They are available in Object class.But they are used for Thread Operation only. Because Inter process communication happens object wise not class wise.

10. What is the Difference between sleep() and wait()?

sleep() method is used to to stop executing for a given amount of time
wait() method is used to stop executing untill notify() method is called.

11. What is the use of join()?

If we use join() method then after fully executing of child thread main thread will execute.

12. What is the use of yield()?

yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.


Threads

Interview Questions on Java - Packages

1. What is Polymorphism?

The ability to take more than one form is called polymorphism. There are two types polymorphism.
1. Compile time Polymorphism( or Static polymorphism) : It will be decided at the time of Compile time. ex: Method OverLoading.
2. Runtime Polymorphism( or Dynamic polymorphism): It will be decided at the time of Running time. ex: Method Overriding.

2. What is Method Overloading?

If multiple methods have same name but different signature then we will call it Method Overloading.

3. Can we use the same Method name twice by differing return type?

No. Because we won't use return type while calling the method.

4. What is Method Overriding?

The process of replacing the parent class method with child class method is called method overriding.

5. What is the main difference between Method Overloading and Method Overriding?

In Method Overloading Signature must be different. In Method Overriding Signature must be same.

6. What is co-variant return type?

While happening method overriding, the return type of child class can be subclass of parent class return type. this is nothing but c0-varient return type.

7. If the access specifier of parent class method is protected then what could be the access specifier of child class method?

protected or public. Because access specifier should be equal or wider than its.

8. If Parent class method throws some exception then what type of exception could be thrown at child class?

It should be same type or subclass to that exception.

9. Does static methods inherits to the child class?

No.

10. How to call Parent class methods in child class?

By using "super" key word.

11. How to achieve perfect run time polymorphism?

To achieve perfect polymorphism two things shpuld be happen
1. auto upcasting
2. method overriding



Packages

Interview Questions on Java - Polymorphism

1. What is Polymorphism?

The ability to take more than one form is called polymorphism. There are two types polymorphism.
1. Compile time Polymorphism( or Static polymorphism) : It will be decided at the time of Compile time. ex: Method OverLoading.
2. Runtime Polymorphism( or Dynamic polymorphism): It will be decided at the time of Running time. ex: Method Overriding.

2. What is Method Overloading?

If multiple methods have same name but different signature then we will call it Method Overloading.

3. Can we use the same Method name twice by differing return type?

No. Because we won't use return type while calling the method.

4. What is Method Overriding?

The process of replacing the parent class method with child class method is called method overriding.

5. What is the main difference between Method Overloading and Method Overriding?

In Method Overloading Signature must be different. In Method Overriding Signature must be same.

6. What is co-variant return type?

While happening method overriding, the return type of child class can be subclass of parent class return type. this is nothing but c0-varient return type.

7. If the access specifier of parent class method is protected then what could be the access specifier of child class method?

protected or public. Because access specifier should be equal or wider than its.

8. If Parent class method throws some exception then what type of exception could be thrown at child class?

It should be same type or subclass to that exception.

9. Does static methods inherits to the child class?

No.

10. How to call Parent class methods in child class?

By using "super" key word.

11. How to achieve perfect run time polymorphism?

To achieve perfect polymorphism two things shpuld be happen
1. auto upcasting
2. method overriding



Polymorphism

Interview Questions on Java - Static & Non static

1. Can we write java program without main?

Yes, by using static block. static blocks will load at the time of class loading.

2. What is the use of static block?

Static block is used to initialize the static variables.

3. How to access the static variables of another class?

By using class name.

4. What is the use of "this" Keyword?

"this" keyword is used to refer the current Object.

5. What is the use of "super" Keyword?

"super" keyword is used to refer the Parent Class Object.

6. Can we use "this" and "super" keywords inside static method?

No. For non-static methods only.

6. Can we use "this" for static variables?

No. You can use class name for static members.

7. public class A { int x= 3; void method1() { int x= 4; // some code } } here, how can you access the global variable inside method1?

By using "this" keyword i.e, "this.x".

8. class A { int x= 3; } class B implements A { int x=1; void method1() { // some code } } here, how can you access the super class variable inside method1?

By using "super" keyword i.e, "super.x".


Static & Non static

Interview Questions on Java - Objects

1. Which class is the super class of all classes?

java.lang.Object class

2. What are the methods exist in Object class?

1. toString();
2. equals();
3. hashCode();
4. finalize();
5. clone();
6. getClass();

3. What is the use of toString() method?

The purpose of toString() method is to return the all attribute values of an object.By default it returns the memory address of object associate with classname and special charecater '@'.

4. What is the use of equals method()?

equals() method is used to check the contents of two similar Objects.Its return type is boolean.

5. What is the use of hashCode()?

hashCode() is used to represent the memory address of an object in integer format.Its return type is int.

6. What is the use of finalize()?

It will execute before the Garbage Collector clean the Objects.

7. Can we call Garbage Collector explicitly? if Yes how?

Yes. we can call it in two ways:
1. System.gc();
2. Runtime.getRuntime().gc();

8. What is the use of Clone()?

Clone() is used to create seperate copy of an object. It is protected method. To clone Object we need to implement 'cloneable' marker interface.

9. What are the types of Clone operations?

There are two types Clone Operations.
1. Deep Copy : Modification of one object doesn't reflects in Cloned Object.
2. Shallow Copy: Modification of one Object reflects in another objects.

10. explain about getClass()?

In java.lang package there is a class which name has "class". getClass() is used to get the "class" object of particular class. ex : class A
{
}
class B
{
A a1 = new A();
method()
{
Class c1 = a1.getClass() // here "class" object of "a1" is assigned to c1;
}
here "class" object of "a1" is assigned to c1;

11. How many ways can we create the objects?

1. By using new() operator.
2. By calling newInstance() method
ex: Class c1 = class.forName(classname);
     Object o1 = c1.newInstance();
3. By using Clone() method.
4. while de-serialization
     FileInputStream fis = new FileInputStream(file1);
     Object o1= in.readObject();


Objects

Interview Questions on Java - Exception Handling

1. What is Exception?

Exception is an abnormal condition that can not handled by JVM. Whenever Exception occurs, at that time program will be terminated.

2. What is the order when we use try,catch and finally blocks

try
{
}
catch(Exception ex)
{
}
finally{
}

3. If Exception occurs out side try block will it handle by the catch block.

No. The Exceptions raised in try block only can be handled by catch blocks.

4. How many arguments can be taken by catch block.

only one.

5. How many number of catch blocks can we associate with try block?

We can associate any no.of catch blocks including zero(but in this case finally block should be there).

6. What is finally block?

finally block is executed after try and catch blocks.finally block can be used optional.

7. If try block has return type then will finally block execute?

Yes.

8. What is the situation that finally block doesn't execute?

If we use System.exit(); in try block then finally block won't be execute.

9. What are the types of Exceptions?

1. Checked Exception: The Exceptions which are known by Compiler.
2. Unchecked Exception: The Exceptions which will raised at the time of Running.

10. What is the difference between Exception and Error?

Exceptions are those which can be handled at the run time but errors cannot be handled.
Errors are unchecked exceptions and Exceptions can be both checked and unchecked exceptions.

11. Explain about throw,throws and Throwble?

1. throw : It is used to raise an exception explicitly.
2. throws: instead of using try and catch block we can avoid handling of checked exceptions with throws keyword. it is used at method definition.
3. Throwble: It is super class of Exception and Error classes.

12. Explain about final, finally and finalize?

1. final : final is a keyword which is used for classes, methods and variables.
final classes can not inherit to canother classes.
final methods can not override.
final variables can not change.
2. finally : finally block is used in exception handling and it is is executed after try and catch blocks.finally block can be used optional.
3. finalize(): It is a Object class method which will execute before the Garbage Collector clean the Objects.

13. Tell me some chechecked Exceptions?

ClassNotFoundException,FileNotFoundException and CloneNotSupportedExceptions.

14. Tell me some unchecked exceptions?

ArithmeticExcetion, ArrayIndexOutOfBoundException and NullPointerExceptions.

15. If Parent class method throws some exception then what type of exception could be thrown at child class?

It should be same type or subclass to that exception.


Exception Handling

Interview Questions on Java - Constructors & Methods

1. What is Constructor?

Constructor is a special kind of method which has same class name and invoked at the time of creation of Object. It is used to assign the values to the variables.

2. What is The first statement of Constructor?

this() or super().If we don't provide anything then compiler will keep super() by default.

3. Does recursion of Constructor allow in java?

No.

4. What are the modifiers not applicable for Constructor?

abstract, final, native, static, or synchronized.

5. Can Constructor return any type?

No, Constructor can not return any type even void, but it can return empty i.e, "return;";

6. Does Constructors inherits to the sub class.

No, but it will call by using super();

7. What are the Difference between Methods and Constructors.


Constructors Methods
1. It should have same Class Name 1. It can have any name even class name also
2. It should not return any value 2. It can return any type of Value.
3. It doesn't Inherit to the sub class 3. It Inherits to the sub class

8. What is Loacal Variable?

The Variable which is declared inside the method.

9. What is Global Variable?

The Varibale which is declared outside method and inside the class.

10. What are the modifiers that are not applicable for the Local Variable?

static, public, private and protected.

11. What are the modifiers applicable for the Local Variable?

final and no modifier.

12. What is Static variable?

Static variables are class level variables which are common to all Objects. If one Object modify the variable it reflects in to all Objects.

13. Can we use static variable inside non-static methods?

yes.

14. Can we use non-static variable inside static methods?

No, but we can access after creating the Object and using that reference.


Constructors & Methods

Interview Questions on Java - Abstrct & Interface

1. What is Abstract class?

A class which is not fully implemented is called abstract class.

2. What is Concrete class?

A class which is fully implemented is called concrete class.

3. Can we create an Object for Abstract class?

No.

4. Can we keep abstract keyword for concrete class?

Yes, to avoid creation of Object we can use this.

5. can we keep abstract methods in concrete class?

No.

6. What is final?

final is a keyword which is used for classes, methods and variables. final classes can not inherit to canother classes. final methods can not override. final variables can not change.

7. Can we declare class with both abstract and final.

No. Because abstract needs to inherits to the child class, but final is used to avoid the inheritance.

8. Can we keep Constructor as abstract.

No, always Constructor should be concrete.

9. What is Interface?

Interface is a blue print of classes and contains all abstract methods. We can achieve 100% abstraction by using interface because there will be no constructors.

10. What are the modifiers applicable for methods in Interface?

public and abstract.

11. What are the Default modifiers for methods in Interface?

public and abstract.

12. What are the modifiers applicable for variables in Interface?

Public,static and final.

13. What are the Default modifiers for variables in Interface?

Public,static and final.

14. What is the main difference between Abstract and Interface?

In Abstract we can use any modifiers, but in Interface we should use public modifiers only.

Abstraction & Interface

Interview Questions on Java - Basics

1. How java differs from other high level languages?

The most importance feature of java is platform independence. i.e, we can write and compile java program in one platform (say windows) and we can execute the generated ".class"(Byte code) file in another platform(say Linux).

2. What are the object-oriented programming concepts?

1. Class
2. Object
3. Inheritance
4. Encapsulation
5. Polymorphism

3. Define Class and Object?

Class: Class is a template or model for an Object.
Object: Object is an instance of Class and physically exist(Memory will be allocated for each Object of Class).

4. What is Inheritance?

Inheritance : The process of acquiring the properties of another class is called Inheritance.
In this Process two types of classes there
1. Base Class or Super Class : The class which gives the properties.
2. Child Class or Dirved Class: The Class which get the the properties.

5. What is Encapsulation?

Encapsulation: It is the packing of data and functions into a single component.We can achieve this by using classes.

6. What is Polymorphism?

The ability to take more than one form is called polymorphism. There are two types polymorphism.
1. Compile time Polymorhism( or Static polymorphism) : It decides at the time of Compile time. ex: Method OverLoading.
2. Runtime Polymorhism( or Dynamic polymorphism): It decides at the time of Running time. ex: Method Overriding.


Basics