DISHA NIRDESHAN

Enlightens your career

Tuesday, June 28, 2011

Basic Questions on Core Java

Q:Explain working of Java Virtual Machine (JVM)?

A:  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.

 

Q: What is OOPS?
A: OOP is the common abbreviation for Object-Oriented Programming.  
 

Q: Describe the principles of OOPS.
A: There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.  
 

Q: Which class is the superclass of every class?

A: Object.


Q: Name primitive Java types.

A: The 8 primitive types are byte, char, short, int, long, float, double, and boolean.


Q: Is main a keyword in Java?

A: No, main is not a keyword in Java.


 Q: Is next a keyword in Java?

A: No, next is not a keyword.


Q: Is delete a keyword in Java?

A: No, delete is not a keyword in Java. Java does not make use of explicit destructors the way C++ does.


 Q: Is exit a keyword in Java?

A: No. To exit a program explicitly yo we use exit method in System object.

 

Q:  How can we declare a variable as constant in Java

A :

· Using the final keyword to declare a variable.

· The final keyword specifies that the value of a variable is final and cannot be changed.

· It is a convention in Java to write constants in uppercase letters 

 

Q:What are the different scopes for Java variables?


A: The scope of a Java variable is determined by the context in which the variable is declared. Thus a java variable can have one of the three scopes at any given point in time.
1. Instance: - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.


2. Local: - These are the variables that are defined within a method. They remain accessible only during the course of method excecution. When the method finishes execution, these variables fall out of scope.

3. Static: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.     

 

Q:What is the default value of the local variables?


A:  In Java Local variables will not be initialized to any default value , we have to explicitly initialize the variables or else at the time of compilation an error will be displayed

 


Q:What will be the initial value of an object reference which is defined as an instance variable?


A: The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerExceptions everywhere you try to use such default initialized references.

 

 

Q:Why java is not pure Object Oriented?


A:  Many languages are Object Oriented. There are seven qualities to be satisfied for a programming language to be pure Object Oriented. They are:

1.     Encapsulation/Data Hiding

2.     Inheritance

3.     Polymorphism

4.     Abstraction

5.     All predifined types are objects

6.     All operations are performed by sending messages to objects

7.     All user defined types are objects.


JAVA is not because it supports Primitive datatype such as int, byte, long... etc, to be used, which are not objects.
Contrast with a pure OOP language like Smalltalk, where there are no primitive types, and boolean, int and methods are all objects.


Q:What is data encapsulation?


A:

Encapsulation may be used by creating 'get' and 'set' methods in a class (JAVABEAN) which are used to access the fields of the object. Typically the fields are made private while the get and set methods are public. Encapsulation can be used to validate the data that is to be stored, to do calculations on data that is stored in a field or fields, or for use in introspection (often the case when using javabeans in Struts, for instance). Wrapping of data and function into a single unit is called as data encapsulation. Encapsulation is nothing but wrapping up the data and associated methods into a single unit in such a way that data can be accessed with the help of associated methods. Encapsulation provides data security. It is nothing but data hiding.

 


Q: Explain the Inheritance principle.


A    : Inheritance is the process by which one object acquires the properties of another object.  
 

Q: Explain the Polymorphism principle.


A    : The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".  

Interface


 

Q: Explain the different forms of Polymorphism.


A    : From a practical programming viewpoint, polymorphism exists in  distinct forms in Java:

  • Method overloading
  • Method overriding  (Dynamic dispatchment method) 
     

Q: What are Access Specifiers available in Java?


A    : Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing


privileges to parts of a program such as functions and variables. These are:
Public : accessible to all classes
Protected : accessible to the classes within the same package and any subclasses.
Private : accessible only to the class to which they belong
Default : accessible to the class to which they belong and to subclasses within the same package

 

Q: If I write System.exit (0); at the end of the try block, will the finally block still execute?


A: No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.


Q: What is the purpose of Void class?


A: The Void class is an uninstantiable class i.e. we can't create an object to that class , void class is used to hold a reference to the Class object representing the primitive Java type void.

0 comments :