Automation Using Selenium Webdriver

Wednesday, 16 November 2016

Top 20 Basic Java Interview Programs on Increment Decrement operators

  • In most of the interviews questions on increment and decrement operators will come in face to face or in written test.
  • Every Java Interview written test will have compulsory one question on increment and decrements operators.
  • Lets see some of the frequently asking java interview  programming questions on increment and decrement operators.
  • I this pre increment and post increment , pre decrement and post decrement topics will cover in below programs.
  • Normal programs: use of increment and decrement in normal scenario is same.
  • They differ in while we use them in expressions.
  • To avoid confusions for basic java learners we provided individual programs.

1.Java Interview program on pre increment operator.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=10;
  8.      b=++a;
  9.     System.out.println(b);
  10.  
  11. }
  12. }
Output:
  1. 11

2.Java Interview program on post increment operator.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=10;
  8.      b=a++;
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 11

3.Java Interview program on pre increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= ++a + 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 22

4.Java Interview program on post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 21

5.Java Interview program on pre increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= ++a + ++a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 43

6.Java Interview program on post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + a++;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 41

7.Java Interview program on pre and post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + ++a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:

  • 1
  • 2
  • 4
  • 4
  • 5
  • 6
  • 5
  • 3
  • 2
  • 2



  • 8.Java Interview program on pre decrement


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a, b;
    7.      a=10;
    8.      b=--a;
    9.     System.out.println(b);
    10.  
    11. }
    12. }
    Output:
    1. 9

    9.Java Interview program on post decrement .


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a, b;
    7.      a=30;
    8.      b=a--;
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 29

    10.Java Interview program on pre decrement in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.     a= --a - 1;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 18

    11.Java Interview program on post decrement in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.     a= a-- - 1;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 19

    12.Java Interview program on pre decrement in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.     a= --a - --a;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 1

    13.Java Interview program on post decrement operator in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.     a= a-- - a--;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 1

    14.Java Interview program on pre and post decrement operator in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.    a= a--  - --a;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 2

    15.Java Interview program on increment and decrement operator in expression.


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=20;
    7.   a= a--  + ++a;
    8.  
    9.     System.out.println(a);
    10.  
    11. }
    12. }
    Output:
    1. 40

    16.increment and decrement operators Inside println() method:



    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.    int a=1;
    7.  
    8.     System.out.println(a++);
    9.     System.out.println(a++);
    10.     System.out.println(++a);
    11.  
    12.     System.out.println(a++);
    13.     System.out.println(a++);
    14.  
    15.     System.out.println(a--);
    16.     System.out.println(a--);
    17.  
    18.     System.out.println(--a);
    19.     System.out.println(--a);
    20.     System.out.println(a--);
    21.  
    22. }
    23. }
    Output:

    17.Java Interview program on increment and decrement operator in println


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.    int i=15;
    7.     System.out.println(i++);
    8.     System.out.println(i--);
    9.  
    10. }
    11. }
    Output:
    1. 15
    2. 16

    18.Java Interview program on increment and decrement operator in println


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int a=10,b=10;
    7.  
    8.      for(int i=0;i<5;i++){
    9.  
    10.          if(++a>2||++b>2){
    11.             a++;
    12.            }
    13.  
    14.       }
    15.      System.out.println("a= "+a+" b="+b);
    16.  
    17. }
    18. }
    Output: 
    1. 20
    2. 10

        

    19.Java Interview program on increment and decrement operator in loops


    1. package com.instanceofjava;
    2.  
    3. class IncrementDemo{
    4.  
    5. public static void main(String [] args){ 

    6.     int i;
    7.  
    8.     for( i=1;i<4;i++){    
    9.            
    10.          System.out.println(i);
    11.  
    12.       }
    13.       System.out.println("value of i after completion of loop: "+i);
    14.  
    15. }
    16. }
    Output:
    1. 1
    2. 2
    3. 3
    4. value of i after completion of loop: 4

    20.Java Interview program on increment and decrement




    1. package com.instanceofjava;
    2.  
    3. public class A{

    4.  
    5.  static int a = 1111;
    6.  static
    7.  {
    8.         a = a-- - --a;
    9.  }
    10.     
    11. {
    12.         a = a++ + ++a;
    13.  }
    14.  
    15.  public static void main(String[] args)  {
    16.  
    17.        System.out.println(a);
    18.  
    19.     }
    20.  
    21. }

    Output:
    1. 2