Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Test your skills through the online practice test: Core Java Quiz Online Practice Test

Ques 16. How can I swap two variables without using a third variable?

Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable.

Example:
int a=5,b=10;a=a+b; b=a-b; a=a-b;

Is it helpful? Add Comment View Comments
 

Ques 17. What is the purpose of Void class?

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.

Is it helpful? Add Comment View Comments
 

Ques 18. What is Cloneable Interface in Core Java?

A  clone of an object is an object with distinct identity and equal contents. To define clone, a class must implement cloneable interface and must override Object’s clone method with a public modifier. At this point, it is worth nothing that cloneable interface does not contain the clone method, and Object’s clone method is protected.

When the Object class finds that the object to be cloned isn’t an instance of a class that implements cloneable interface, it throws CloneNotSupportedException.
If a class wants to allow clients to clone it’s instances, it must override Object’s clone method with a public modifier.

Is it helpful? Add Comment View Comments
 

Ques 19. Explain working of Java Virtual Machine (JVM)?

JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.

Is it helpful? Add Comment View Comments
 

Ques 20. What is abstraction?

something which is not concrete , something which is imcomplete.

java has concept of abstract classes , abstract method but a variable can not be abstract.

an abstract class is something which is incomplete and you can not create instance of it for using it.if you want to use it you need to make it complete by extending it.

an abstract method in java doesn't have body , its just a declaration.

so when do you use abstraction ? ( most important in my view )
when I know something needs to be there but I am not sure how exactly it should look like.

e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons .

the same concept apply to interface also , which we will discuss in some other post.

so implementation of those start() and stop() methods should be left to there concrete implementation e.g. Scooter , MotorBike , Car etc.

In Java Interface is an another way of providing abstraction, Interfaces are by default abstract and only contains public static final constant or abstract methods. Its very common interview question is that where should we use abstract class and where should we use Java Interfaces in my view this is important to understand to design better java application, you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop() method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class.

in Summary

1) Use abstraction if you know something needs to be in class but implementation of that varies.
2) In Java you can not create instance of abstract class , its compiler error.
3) abstract is a keyword in java.
4) a class automatically becomes abstract class when any of its method declared as abstract.
5) abstract method doesn't have method body.
6) variable can not be made abstract , its only behavior or methods which would be abstract.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook