core java interview questions Lavel-1

 

Q. What is Java technology and where it is used?

 

Ans: Java is a platform independent programming language  and a computing platform.It was first released by Sun Microsystems as java 1.0 in 1995.It is used in Mobile,Embedded System, Smart Card,Robotics,Games etc. 


Q. Why java is called Platform Independent OR compile once run anywhere programming language?

 

Ans:  When a java program is compilled a .class file is generated.This .class file contains the byte code or the intermediate code which is understood by JRE installed on the operating sytems e.g Macintosh, windows or Linux etc. Hence java is called Platform Independent OR compile once run anywhere programming language.


Q. What is the difference between JDK, JRE and JVM ?

 

Ans: JDK is an acronym for Java Development Kit.It physically exists.It contains JRE + development tools e.g javac,java etc.

JRE is an acronym for Java Runtime Environment.The JRE provides the libraries, Java virtual machine, and other components necessary to run applets and applications written in the Java programming language.

JVM is the acronym for Java virtual machine.It is an abstract computing machine that has an instruction set and manipulates memory at run time.JVM is platform dependent.


Q. Is JVM platform independent or dependent?

 

Ans: JVM is platform dependent.Every  OS  e.g Macintosh, windows or Linux etc. has different JVMs.Java compiller converts the java source file into .class file which is nothing but Byte code or intermediate code which is understood by JVM.Now, JVM  converts the byte code into machine code which is platform specific. Hence,JVM is platform dependent.


Q. Is java pure or fully object oriented programing language?

 

Ans: No, java is not fully object oriented programing language because it supports Primitive data types such as int, byte, long... etc,which are not objects.Smalltalk is a pure OOP language where there are no primitive data types, and boolean, int ... etc are all objects.



Q. Can you execute java application without using main() method ?


Ans:  • Yes, (using static block) before java 7

         • No, from java 7.


Q. What is keywords in java?How many keywords are there in java?

 

Ans: Keywords  are the reserved words in java language that have a predefined meaning.Keywords cannot be used as names for variables, methods, classes, or as any other identifier.There are total 50 keywords in java.  Example: abstract, assert,break,case,catch,class,const,continue,default,do,double,else,enum,extends, final,finally,float,for etc.


Q. what is identifier in java?

 

Ans:Any name in java is called an identfier in java.It may be a method name,class name,variable name etc.

 The allowed characters in identifier are :

1. Alphabets a to z both upper and lower case.

2.Digits 0-9

3.$ and _

e.g- Customer_name, _$Car,_Car etc..

If we are using other then these we will get a compillation error.

  • Identifier cannot start with digits.

          e.g- 123total          // compillation error

  • java identifiers are case sensitive.

         e.g- student and Student are different.

  • Reserved words cannot be used as identifiers.

  • All predefined java class name and interface name we can use as identifier but not recommendable. 

e.g-  class TestIdentifier

      {

            int String=25;       // OK

            System.out.println(String);

       }

 

Q.  What is class, object and instance ?

 

Ans: Class: class is a blueprint or template which is used to create objects.For example- Suppose you have to make a car and so you made a protype or blueprint on paper that the car will have four wheels,one starring,two headlights etc...etc..Basically, a class will consists of fields,methods and constructors.Field is used to hold the state of the class. Method is used to represent the behavior of the class e.g Car have state (current speed, current gear) and behavior (applying brake, changing gear). Constructor is used to create a new Instance of the Class.

 

Objects:Now,will that car created on paper run ...?.No, because it is on paper. Now, using that protype or blueprint you can make N number of actual cars which is nothing but objects.An object is an entity that has properties for identifying its state, methods for behavior or functionality and events for depicting the change of state.An object stores its state in fields and exposes its behavior through methods e.g Car have state (current speed, current gear) and behavior (applying brake, changing gear).


Instance:An object in memory is called an instance.When a new instance of a class is created, the JVM will allocate memory for that class instance.The instance of the object is created using new operator.

      

         e.g   Car c=new Car();


Q. What is instance and static variables in java ?


Ans: Instance variable: A variable in the class without static modifier is called as an instance variable.The value of the instance variable is varied from object to object because every object will have a seperate copy of instance variable. At the time of object creation the instance variable will be initialized with the default values.The scope of the instance variable is same as the scope of the object.Instance variables cannot be accessed from a static context directly.


static variable: A variable in the class with static modifier is called as a static variable or class level variable.In the case of static variable single copy of the variable will be created at  class level and the same copy will be shared by all the objects of the class.This means if an object changes the value of the static variable then the same changed value will be accessed by all the other objects. static variables can be accessed directly with the class name from  static as well as a non-static context directly.



Q. What will happen if you write System.out.println(null);

 

Ans: When you write System.out.print(null);The compiller will generate an error saying "The method print(char[]) is ambiguous for the type PrintStream".This is because for print(null) the compiller finds two best matches print(char [] s) and print(String s).Both the methods can take null as argument, so the compiller is confused in which method to call.

 

 Q. What is the difference between Encapsulation and Abstraction ?

 

Abstraction: Abstraction means representing the essential features without showing the background details.It lets you focus on what the object does instead of how it does.For example: Mobile phone has features calling,messaging etc.

A user can call or message without knowing technical details that how a call is connected,how voice is transfered over the network etc.Thus, hiding internal implementation datails and just highlighting the set of services offered is called as Abstraction.

         Abstraction in java is achieved through interface and abstract class and methods.


abstract class MobilePhone

{

          public abstract call(int number);

          public abstract sendMsg(int number,String msg);

}


Encapsulation:Binding data and its functionality together into a single unit so that it cannot be accessed directly from outside is called as Encapsulation.For e.g Mobile phone comprises of many  parts such as memory, cpu etc.All these parts are binded together inside the body of the mobile phone so that there is no direct access to its parts.

           Encapsulation in java is achieved through class and access modifier (mainly private ).Hiding data behind the methods is the central concept of encapsulation.If a class has all its member variables private then it is called as tightly encapsulated class.

                            

class EncapsulationTest

{

       private int a;

       private int b;


       public int getA()  {    }

       public int getB()  {    }

       public int setA()  {    }

       public int setB()  {    }



}


Advantages:

                       1) Security

                       2) Enhancement will become easy.

                       3) Improves modularity of the application.


 

No comments:

Post a Comment