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. 42




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:
  1. 1
  2. 2
  3. 4
  4. 4
  5. 5
  6. 6
  7. 5
  8. 3
  9. 2
  10. 2

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

Top 13 Java Interview Questions on Static keyword

1.what is static in java?

  • Static is a keyword in java.
  • One of the Important keyword in java.
  • Clear understanding of static keyword is required to build projects.
  • We have static variables, static methods , static blocks. 
  • Static means class level.

2.Why we use static keyword in java?

  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.

3.What is static variable in java?

  • Variables declared with static keyword is known as static variables.
  • Static variables gets memory on class loading.
  • Static variables are class level.
  • If we change any static variable value using a particular object then its value changed for all objects means it is common to every object of that class.
  • static int a,b;


  1. package com.instanceofjavastatic;
  2. class StaticDemo{
  3.  
  4. static int a=40;
  5. static int b=60;
  6.  
  7. }



  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression
  10. }
  11. }

 Read more : Explain about static variables in java?

4. what is static method in java with example

  • Method which is having static in its method definition is known as static method.
  1. static void show(){
  2.  
  3. }
  • JVM will not call these static methods automatically. Develioper needs to call these static methods from main method or static block or variable initialization.
  • Only Main method will b called by JVM automatically.
  • We can call these static methods by using class name itself no need to create object.

 5.what is static block in java?

  •  Static blocks are the blocks which will have braces and with static keyword.
  • These static blocks will be called when JVM loads the class.
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }

 Read more @ Static blocks in java with example programs

 6.What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading.

7.Why main method is static in java?

  • To execute main method without creating object then the main method should be static so that JVM will call main method by using class name itself.

8.Can we overload static methods in java?

  • Yes we can overload static methods in java.

9.Can we override static methods in java?

  • NO we can not override static methods in java.

10.Can we write static public void main(String [] args)?


  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }
11.Can we call super class static methods from sub class?





Java programming interview questions and answers

  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Top 20 collection framework interview questions and answers in java

Java collections interview questions:
collections interview questions

1.Why Map interface doesn’t extend Collection interface?

  • Set is unordered collection and does not allows duplicate elements.
  • List is ordered collection allows duplicate elements.
  • Where as Map is key-value pair.
  • It is viewed as set of keys and collection of values.
  • Map is a collection of key value pairs so by design they separated from collection interface.


2.What is difference between HashMap and Hashtable?

  • Synchronization or Thread Safe 
  • Null keys and null values 
  • Iterating the values 
  •  Default Capacity 
hashmap vs hashtable

    Click here for the
    Differences between HashMap and Hash-table

    3.Differences between comparable and comparator?

    • Comparable Interface is actually from java.lang package.
    • It will have a method compareTo(Object obj)to sort objects
    • Comparator Interface is actually from java.util package.
    • It will have a method compare(Object obj1, Object obj2)to sort objects
    Read more :  comparable vs comparator

    4.How can we sort a list of Objects?

    •  To sort the array of objects we will use  Arrays.sort() method.
    • If we need to sort collection of object we will use Collections.sort().

    5.What is difference between fail-fast and fail-safe?

    • Fail fast is nothing but immediately report any failure. whenever a problem occurs fail fast system fails. 
    • in java Fail fast iterator while iterating through collection of objects sometimes concurrent modification exception will come there are two reasons for this.
    • If one thread is iterating a collection and another thread trying to modify the collection.
    • And after remove() method call if we try to modify collection object



    6. What is difference between Iterator ,ListIterator and Enumeration?

    • Enumeration interface implemented in java 1.2 version.So Enumeration is legacy interface.
    • Enumeration uses elements() method.
    • Iterator is implemented on all Java collection classes.
    • Iterator uses iterator() method.
    • Iterator can traverse in forward direction only.
    • ListIterator is implemented only for List type classes
    • ListIterator uses listIterator() method.
    Read more :  

    What is difference between Iterator ,ListIterator and Enumeration?

    7.What is difference between Set and List in Java?

    • A set is a collection that allows unique elements.
    • Set does not allow duplicate elements
    • Set allows only one null value.
    • Set having classes like :
    • HashSet
    • LinkedHashSet
    • TreeSet
    • List having index. and ordered  collection
    • List allows n number of null values.
    • List will display Insertion order with index.
    • List having classes like :
    • Vector
    • ArrayList
    • LinkedList

    8.Differences between arraylist and vector?

    • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
    • ArrayList was introduced in java version1.2, as part of java collections framework.
    • Vector is  synchronized. 
    • ArrayList is not synchronized.

    9.What are the classes implementing List interface?

    • ArrayList     
    • LinkedList     
    • Vector

    10. Which all classes implement Set interface ?

    • HashSet
    • LinkedHashSet
    • TreeSet

    11.How to make a collection thread safe?

    • Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment.
    • By using Collections.synchronizedList(list)) we can make list classes thread safe.
    • By using 
      java.util.Collections.synchronizedSet()  we can make set classes thread safe.

    12.Can a null element added to a TreeSet or HashSet?

    • One null element can be added to hashset.
    • TreeSet does not allow null values

    13. Explain Collection’s interface hierarchy?


    Collections interview Questions and answers java

    14.Which design pattern Iterator follows?

    • Iterator design pattern

    15.Which data structure HashSet implements

    • Hashset implements hashmap internally.

    16.Why doesn't Collection extend Cloneable and Serializable?

    • List and Set and queue extends Collection interface.
    • SortedMap extends Map interface.

    17.What is the importance of hashCode() and equals() methods? How they are used in Java?

    • equals() and hashcode() methods defined in "object" class. 
    • If equals() method return true on comparing two objects then hashcode() of those two objects must be same.

    18.What is difference between array & arraylist?

    • Array is collection of similar type of objects and fixed in size.
    • Arraylist is collection of homogeneous and heterogeneous elements.

    19.What is the Properties class?

    • Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.

    20.How to convert a string array to arraylist?

    • ArrayList al=new ArrayList( Arrays.asList( new String[]{"java", "collection"} ) ); 
    •  arrayList.toArray(); from list to array


    22.Java Collection framework programming interview questions 



    1. Print prime numbers? 
    2. Java Program Find Second highest number in an integer array 
    3. Java Interview Program to find smallest and second smallest number in an array 
    4. Java Coding Interview programming Questions : Java Test on HashMap  
    5. Constructor chaining in java with example programs 
    6. Swap two numbers without using third variable in java 
    7. Find sum of digits in java 
    8. How to create immutable class in java 
    9. AtomicInteger in java 
    10. Check Even or Odd without using modulus and division  
    11. String Reverse Without using String API 
    12. Find Biggest substring in between specified character
    13. Check string is palindrome or not?
    14. Reverse a number in java? 
    15. Collections interview questions and programs
     For more interview programs: Top 40 java interview programs


    Final static string vs Static string in java

    Static final variable java example:

    • If we declare any variable as static means it is class level variable.
    • If we declare any variable as static final its class level variable and its value can not be changed 
    • final static variables are constants in java.
    • We can access these final static variables directly without using object or class name.
    • We can not change the value of final static variables in any object.

    java example program on final static variable 

    1. Class A{
    2.  
    3. public static final String str="Learn Java Online";
    4.  
    5.  public static void main(String[]  args){ 

    6. System.out.println(str); 
    7.  //str="j2ee Interview Questions"; gives error

    8. }
    9. }

    Output:
    1. Learn Java Online

    Static String:

    • static Strings are class level strings.
    • we can access these static Strings using class name.


    1. Class A{
    2.  
    3. public static String str="Learn Java Online";
    4.  
    5.  public static void main(String[]  args){ 

    6. System.out.println(A.str); 
    7. System.out.println(str);
    8.  

    9. }
    10. }



    Output:
    1. Learn Java Online
    2. Learn Java Online

    Essential Classes of Java

    • Lets discuss about what are the common functionalities everyone needs when developing a application and what are the classes we require mostly to rectify common problems.

    Exceptions:

    •  Exception classes are very important in every java project,
    • Finding the places where the chances of occurring exception and placing in try and in catch block assigning to corresponding exception class or super class of all exceptions Exception.
    1. Checked Exceptions
    2. Unchecked Exceptions
    3. Errors

    Basic IO:

    •  Basic input and output operations covered by java platform classes focuses on IO streams and serialization.
    I/O Streams:
    1.     Byte Streams are provided to handle I/O of raw binary data.
    2.     Character Streams handles I/O of character data
    3.     Buffered Streams optimize input and output by reducing the number of calls to the native API.
    4.     Scanning and Formatting allows a program to read and write the formatted text.
    5.     I/O from the Command Line describes the Standard Streams and the Console object.
    6.     Data Streams will handle the binary I/O Strings and primitive data types
    7.     Object Streams provided to handle binary I/O of all objects.

     Concurrency:

    • Will Explains how to build applications which perform multiple tasks simultaneously.
    • Java Platform having full support to concurrent programming 
    • In JDK 5 Java introduces high level concurrent programming APIs.
    • Like java.util.concurrent packages. 

      The Platform Environment 

    • To examine and configure java environment java provides classes and libraries.
    • JVM and Java class libraries
    • Configuration Utilities describes APIs used to access configuration data supplied when the application is deployed, or by the application's user.
    • System Utilities describes miscellaneous APIs defined in the System and Runtime classes.
    • PATH and CLASSPATH describes environment variables used to configure JDK development tools and other applications.
    • Here the List of  commonly used java classes

    1. java.lang.Exception
    2. java.lang.System
    3. java.lang.String
    4. java.util.ArrayList
    5. java.util.HashMap
    6. java.lang.Object
    7. java.lang.Thread
    8. java.lang.Class
    9. java.util.Date
    10. java.util.Iterator



    Can we override private method in java

    • We can not override private methods in java.
    • The basic inheritance principle is when we are extending a class we can access all non private members of a class so private members are private to that class only we can not access anywhere outside the class if we declare anything as private.
    • Know more information about access specifiers here

    1. Class A{
    2.  
    3.   int a;
    4.  private int b;
    5.  
    6. public void print(){
    7.  
    8. System.out.println("print method of super class A"); 

    9. }
    10.  
    11. private void add(){
    12.  
    13. System.out.println("add method of super class A"); 

    14. }
    15. }
    16. Class B extends A{
    17.  
    18. public void show(){
    19.  
    20.  System.out.println("show method of sub class B"); 

    21. }
    22. public static void main(String[] args){
    23.   
    24. B obj= new B();
    25. obj.a=30;
    26.  
    27. obj.print();
    28. obj.show();

    29.  System.out.println("a="obj.a); 
    30. //obj.b=20;  compile time error. The field Super.a is not visible
    31. //obj.add(); compile time error : The method show() from the type Super is not visible

    32. }
    33. }



    Output:
    1. print method of super class  A
    2. show method of sub class B
    3. 30


    • From the above program we can say super class private members are not accessible to sub class.
    • lets see what will happen if we try to override a method of super class?
    • Sorry if we are unable to access super class private methods then there should not be a questions of overriding that method right?
    • Yes private methods of super class can not be overridden in sub class.
    • Even if we try to override same method of super class that will became a sub class own method not overridden method.


    1. Class A{
    2.  
    3.   int a;
    4.  private int b;
    5.  
    6. private void add(){
    7.  
    8. System.out.println("add method of super class A"); 

    9. }
    10. }
    11. Class B extends A{
    12.  
    13. private void add(){
    14.  
    15.  System.out.println("add method of sub class B"); 

    16. }
    17. public static void main(String[] args){
    18.   
    19. B obj= new B();
    20. obj.a=30;
    21.  
    22. obj.add();


    23.  System.out.println("a="obj.a); 

    24. }
    25. }

    Output:
    1. add method of sub class B
    2. 30

    • We can prove this by providing @override annotation in sub class method 

    1. Class A{
    2.  
    3.   int a;
    4.  private int b;
    5.  
    6. private void add(){
    7.  
    8. System.out.println("add method of super class A"); 

    9. }
    10. }
    11. Class B extends A{
    12.  //@override
    13. private void add(){// if we place @override annotation compile time error will come here
    14.  
    15.  System.out.println("add method of sub class B"); 

    16. }
    17. public static void main(String[] args){
    18.   
    19. B obj= new B();
    20. obj.a=30;
    21.  
    22. obj.add();


    23.  System.out.println("a="obj.a); 

    24. }
    25. }

    Output:
    1. add method of sub class B
    2. 30
    Select Menu