Java Questions with Answers #3

1. Explain various Operator in Java with example.
Type Operator Meaning Example
Arithmetic + Addition or unary plus c=a+b
Subtraction or unary minus c= a-b
* Multiplication c=a*b
Division c=a/b
% Modulo (Mod) c=a%b
Relational < Less than a<4
> Greater than b>10
<= Less than equal to b<=10
>= Greater than equal to c>=20
== Equal to x==100
!= Not equal to m!=8
Logical && And operator 0&&1
|| Or operator 0||1
Assignment = is assigned to a=5
Increment ++ Increment by one ++i or i++
Decrement -- Decrement by one --k or k--

2. Explain conditional operator.

The conditional operator is “?”. The syntax of conditional operator is 
     condition?expression1:expression2

Where expression1 denotes the true condition and expression2 denotes false condiotion.

For example:
     a>b?true:false

This means that if a is greater than the b then the expression will return the value true otherwise it will return false.
 

3. What is Entry-Controlled and Exit-Controlled statement?   Explain with example.

In Entry controlled loop the test condition is checked first and if that condition is true than the block of statement in the loop body will be executed while in exit controlled loop the body of loop will be executed first and at the end the test condition is checked, if condition is satisfied than body of loop will be executed again.

The example of Entry-Controlled loop is for loop, while loop while the example of Exit-Controlled loop is do…while loop.
 

4. Why we use break and return statement?  Explain.

Sometimes when we apply loops we need to come out of the loop on occurrence of particular condition. This can be achieved by break statement. 

Following program illustrate the use of break statement in the for loop.

class breakDemo
{
       public static void main(String args[])
       {
                  for(int i=1;i<=20;i++)
                  {
                         if(i%10==0)
                        { 
                               System.out.println(“Number “+i+” is divisible by 10”);
                               Break;
                         }
                         else
                               System.out.println(i+” is not divisible by 10”);
         }
}

Similarly we can use the return statement which is useful for returning the control from the current block.
 

5. What is Garbage collection?

Since objects are dynamically allocated by using the new operator, you might be wondering how such objects are destroyed and their memory released for later reallocation. In some languages, such as C++, dynamically allocated objects must be manually released by use of a delete operator. Java takes a different approach; it handles deallocation for you automatically. The technique that accomplishes this is called garbage collection. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no explicit need to destroy objects as in C++. Garbage collection only occurs sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are no longer used. Furthermore, different Java run-time implementations will take varying approaches to garbage collection, but for the most part, you should not have to think about it while writing your programs.

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.