Are You Looking For JAVA/J2EE Online Training ?

Fill in your details in below form, we will get back to you

Powered by Blogger.

Hadoop Online Training With Placement in USA

www.itlearnmore.com/special-offer-java-5-for-1-package

Limited Offer


Be an expert at low cost

Core Java and Adv JAVA, Struts, HTML and SQL Server Video courses for $20..... Click here


Thursday 30 April 2015

Q.1   What is the purpose of garbage collection in Java, and when is it used?

Ans:   The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Q.2    Describe synchronization in respect to multithreading.

Ans:   With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

Q.3   Explain different way of using thread?

Ans  The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, ’cause when you are going for multiple inheritance. the only interface can help.

Q.4   What are pass by reference and passby value?

Ans Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.

Q.5 What is HashMap and Map?

Ans:  Map is Interface and Hashmap is class that implements that.

Q.6    Difference  between HashMap and HashTable?

Ans The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.

Q.7   Difference between Vector and ArrayList?

Ans  Vector is synchronized whereas arraylist is not.

Q.8   Difference between Swing and Awt?

Ans:  AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT.

Q.9  What is the difference between a constructor and a method?

Ans:  A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

Q.10   What is an Iterator?

Ans:  Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.

Q.11   What is the difference between an Interface and an Abstract class?

Ans:  An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Q.12  What is the purpose of garbage collection in Java, and when is it used?

Ans  The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Q.13   Describe synchronization in respect to multithreading.

Ans:   With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

Q.14  Explain different way of using thread?

Ans  The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, ’cause when you are going for multiple inheritance..the only interface can help.

Q.15  What are pass by reference and passby value?

Ans  Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.

Q.16 How does Java handle integer overflows and underflows?

Ans:  It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

Q.17  What is the Vector class?

Ans:  The Vector class provides the capability to implement a growable array of objects

Q.18   What modifiers may be used with an inner class that is a member of an outer class?

Ans:   A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.


Q.19  What is the difference between yielding and sleeping?

Ans:   When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep()method, it returns to the waiting state.
Q.1 What is the Java API?

Ans:   The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

Q.2   Describe the principles of OOPS?

Ans:  There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.

Q.3   Explain the Inheritance principle?

Ans:  Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places  

Q.4   Explain the Polymorphism principle. Explain the different forms of Polymorphism?
Ans Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation.
Polymorphism exists in three distinct forms in Java:
* Method overloading
* Method overriding through inheritance
* Method overriding through the Java interface

Q.5   What type of parameter passing does Java support?

Ans:   In Java the arguments (primitives and objects) are always passed by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.

Q.6   Explain the Encapsulation principle?

Ans:  Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects allow procedures to be encapsulated with their data to reduce potential interference. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

Q.7   What do you understand by a variable?
Ans:   Variable is a named memory location that can be easily referred in the program. The variable is used to hold the data and it can be changed during the course of the execution of the program.

Q.8   What is data encapsulation?
Ans:   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.9 Name primitive Java types?

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

Q.10   State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

Ans:  public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
default: What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

Q.11   What if the main method is declared as private?

Ans:   The program compiles properly but at runtime it will give “Main method not public.” message.

Q.12   What if I write static public void instead of public static void?

Ans:  Program compiles and runs properly.

Q.13   What environment variables do I need to set on my machine in order to be able to run Java programs?
Ans:  CLASSPATH and PATH are the two variables.

Q.14   How can one prove that the array is not null but empty using one line of code? 

Ans:  Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

Question.15   If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

Ans:  It is empty. But not null.

Q.16   Can I have multiple main methods in the same class?

Answer:  No the program fails to compile. The compiler says that the main method is already defined in the class.

Q.17    Do I need to import java.lang package any time?

Ans:  No. It is by default loaded internally by the JVM.

Q.18   Can a top level class be private or protected?

Ans No. A top level class can not be private or protected. It can have either “public” or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the “modifier private is not allowed here”. This means that a top level class can not be private. Same is the case with protected.

Q.19   What is the difference between a while statement and a do statement?


Ans:  A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.
Q.1   What is J2EE?

Ans:  J2EE is an environment for developing and deploying enterprise applications. The J2EE platform consists of a set of services, application programming interfaces (APIs), and protocols that provide the functionality for developing multitier, web-based applications.

Q.2   What is the J2EE module?

Ans:  A J2EE module consists of one or more J2EE components for the same container type and one component deployment descriptor of that type.

Q.3   What are the components of J2EE application?

Ans   A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and communicates with other components. The J2EE specification defines the following J2EE components:
Application clients and applets are client components.
Java Servlet and JavaServer PagesTM (JSPTM) technology components are web components.
Enterprise JavaBeansTM (EJBTM) components (enterprise beans) are business components.
Resource adapter components provided by EIS and tool vendors.

Q.4   What are the four types of J2EE modules?

Ans:         1. Application client module
                 2. Web module
                 3. Enterprise JavaBeans module
                 4. Resource adapter module

Q.5   What does application client module contain?

Ans   The application client module contains:
class files, an application client deployment descriptor.
Application client modules are packaged as JAR files with a .jar extension.

Q..6   What does web module contain? 

Ans:  The web module contains:
JSP files,
class files for servlets,
GIF and HTML files, and  Web deployment descriptor.
Web modules are packaged as JAR files with a .war (Web ARchive) extension.

Q.  What are the differences between Ear, Jar and War files? Under what circumstances should we use each one?

Ans:   There are no structural differences between the files; they are all archived using zip-jar compression. However, they are intended for different purposes.
–Jar files (files with a .jar extension) are intended to hold generic libraries of Java classes, resources, auxiliary files, etc.
–War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.
–Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web applications.
Each type of file (.jar, .war, .ear) is processed uniquely by application servers, servlet containers, EJB containers, etc.

Q.8   What is the difference between Session bean and Entity bean ?

Ans:   The Session bean and Entity bean are two main parts of EJB container.
Session Bean
represents a workflow on behalf of a client
one-to-one logical mapping to a client.
created and destroyed by a client
not permanent objects
lives its EJB container(generally) does not survive system shut down
two types: stateless and stateful beans
Entity Bean
represents persistent data and behavior of this data
can be shared among multiple clients
persists across multiple invocations
findable permanent objects
outlives its EJB container, survives system shutdown
two types: container managed persistence(CMP) and bean managed persistence(BMP)

Q.  What is “applet”?

Ans:  A J2EE component that typically executes in a Web browser but can execute in a variety of other applications or devices that support the applet programming model.

Q.0   What is “applet container”?

Ans:  A container that includes support for the applet programming model.

Q.11   What is “application assembler”?

Ans:  A person who combines J2EE components and modules into deployable application units.

Q.12   What is “application client”?

Ans:   A first-tier J2EE client component that executes in its own Java virtual machine. Application clients have access to some J2EE platform APIs.

Q.13   What is “application client container”?

Ans:   A container that supports application client components.

Q.14   What is “application client module”?

Ans:  A software unit that consists of one or more classes and an application client deployment descriptor.

Q.15   What is “application component provider”?

Ans:  A vendor that provides the Java classes that implement components’ methods, JSP page definitions, and any required deployment descriptors.

Q.16   What is “application configuration resource file”?

Ans:   An XML file used to configure resources for a Java Server Faces application, to define navigation rules for the application, and to register converters, Validator, listeners, renders, and components with the application.

Q.17   What is “archiving”?

Ans:   The process of saving the state of an object and restoring it.

Q.18   What is “asant”?

Ans:   A Java-based build tool that can be extended using Java classes. The configuration files are XML-based, calling out a target tree where various tasks get executed..

Q.19   What is authentication? 


Ans:   The process that verifies the identity of a user, device, or other entity in a computer system, usually as a prerequisite to allowing access to resources in a system. The Java servlet specification requires three types of authentication-basic, form-based, and mutual-and supports digest authentication.
Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Note: 0 and 1 are not prime numbers. 2 is the only even prime number because all the numbers can be divided by 2.

Let's see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

class PrimeExample{  
 public static void main(String args[]){  
 int i,m=0,flag=0;    
  int n=17;//it is the number to be checked  
  m=n/2;    
  for(i=2;i<=m;i++){    
   if(n%i==0){    
   System.out.println("Number is not prime");    
   flag=1;    
   break;    
   }    
  }    
  if(flag==0)    
  System.out.println("Number is prime");    
}  
}  


Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.

Palindrome number algorithm

  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print "palindrome number"
  • Else print "not palindrome number"
Let's see the palindrome program in java. In this java program, we will get a number variable and check whether number is palindrome or not.

class PalindromeExample{  
 public static void main(String args[]){  
 int r,sum=0,temp;    
  int n=454;//It is the number variable to be checked for palindrome  

  temp=n;    
  while(n>0){    
   r=n%10;  //getting remainder  
   sum=(sum*10)+r;    
   n=n/10;    
  }    
  if(temp==sum)    
   System.out.println("palindrome number ");    
  else    
   System.out.println("not palindrome");    
}  

Output:
palindrome  number


In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
  • Fibonacci Series without using recursion
  • Fibonacci Series using recursion
Fibonacci Series in Java without using recursion

Let's see the fibonacci series program in java without using recursion.

class FibonacciExample1
{  
public static void main(String args[])  
{    
 int n1=0,n2=1,n3,i,count=10;    
 System.out.print(n1+" "+n2);   //printing 0 and 1    
    
 for(i=2;i<count;++i)
//loop starts from 2 because 0 and 1 are already printed   
                   {    
                             n3=n1+n2;    
                             System.out.print(" "+n3);    
 n1=n2;    
                             n2=n3;    
}    
  
}
}  
Output:
0 1 1 2 3 5 8 13 21 34


Fibonacci Series using recursion in java
Let's see the fibonacci series program in java using recursion.
class FibonacciExample2{  
 static int n1=0,n2=1,n3=0;    
static void printFibonacci(int count){    
    if(count>0){    
        n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         System.out.print(" "+n3);   
         printFibonacci(count-1);    
     }    
 }    
 public static void main(String args[]){    
  int count=10;    
  System.out.print(n1+" "+n2);//printing 0 and 1    
  printFibonacci(count-2);//n-2 because 2 numbers are already printed   
 }  
}  
Output:
0 1 1 2 3 5 8 13 21 34


Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

                               1.    4! = 4*3*2*1 = 24  
                               2.    5! = 5*4*3*2*1 = 120  
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java.
o    Factorial Program using loop
o    Factorial Program using recursion

Factorial Program using loop in java
Let's see the factorial Program using loop in java.
class FactorialExample{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;//It is the number to calculate factorial    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  
Output:
Factorial of 5 is: 120



Factorial Program using recursion in java

Let's see the factorial program in java using recursion.

class FactorialExample2{  
 static int factorial(int n){    
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int i,fact=1;  
  int number=4;//It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}