Top 10 Java Interview Questions On main() Method

1.Can we define a class without main method?
  • No, you can’t run java class without main method. 
  • Before Java 7, you can run java class by using static initializers. But, from Java 7 it is not possible.
2.Can main() method take an argument other than string array?
  • No, argument of main() method must be string array. 
  • But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays.
  1. package com.instanceofjava;
  2. public class MainMethod
  3. {
  4. public static void main(String args[])
  5. {
  6. }
  7. }

3.Can we change return type of main() method?
  • No, the return type of main() method must be void only. Any other type is not acceptable.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public static int main(String[] args)
  5. {
  6.  return 1;    //run time error : No main method found
  7. }
  8. }

java main method interview questions



4.Why main() method must be static?
  • main() method must be static.
  • If main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class. 
  • While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument. 
  • For example, In the below program what argument JVM has to pass while instantiating class “A”?.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public A(int i)
  5. {
  6. //Constructor taking one argument
  7. }
  8.  public void main(String[] args)
  9. {
  10. //main method as non-static
  11. }


5.Can We Declare main() Method As Non-Static?
  • No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. 
  • If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public void main(String[] args)
  5. {
  6. System.out.println("indhu");         //Run time error
  7. }
  8. }



6.Can We Overload main() method?
  • Yes, We can overload main() method. A Java class can have any number of main() methods. But to run the java class, class should have main() 
  • method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful. 
  • But, you can’t run the java program. You will get run time error as main method not found.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. public static void main(String[] args)
  5. {
  6. System.out.println("Indhu");
  7.  }
  8. void main(int args)
  9. {
  10. System.out.println("Sindhu");
  11. }
  12. long main(int i, long d)
  13. {
  14. System.out.println("Saidesh");
  15. return d;
  16. }
  17. }

7.Can we declare main() method as private or protected or with no access modifier?
  • No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. 
  • This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.
  1. package com.instanceofjava;
  2. public class A
  3. {
  4. private static void main(String[] args)
  5. {
  6. //Run time error
  7. }
  8. }

8.Can we override main in Java ?
  • No you can not override main method in Java, Why because main is static method and in Java static method is bonded during compile time and you can not 
  • override static method in Java. 

9.Can we make main final in Java?
  • you can make main method final in Java. JVM has no issue with that. Unlike any final method you can not override main in Java.

10.Can we make main synchronized in Java?
  • Yes, main can be synchronized in Java,  synchronized modifier is allowed in main signature and you can make your main method synchronized in Java.

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


    Top 25 Java Basic Interview Questions for Freshers

    Imp Note: Openings for 2024 & 2025 pass outs (MNC) in india. Send your profile to Instanceofjava@gmail.com subject should be: yourName_fresher_resume

    Java Freshers Interview Questions



    1.What is the most Important feature of java?

    • Java is platform independent language.

    2.What do you mean by Platform Independence?

    • Platform Independence  you can run and compile program in one platform and can execute in any other platform.


    3.What is JVM(Java Virtual Machine)?
    • JVM is Java Virtual Machine which is a run time Environment for the compiled java class.

    4.What is JIT Compiler?

    • Just-In-Time(JIT) compiler is  used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time,

    5.Are JVM's Platform Independent?

    •  JVM'S are not platform Independent. JVM'S are platform Specific.

    6.What if main method declare as private?

    • No,It will compile fine but in run time it will error like main method should be in public.cannot find main method.

    7.What is platform?

    • A platform is basically the hardware or software environment in which a program runs.
    • There are two types of platforms software and hardware.
    • Java provides software-based platform.

    8.What all memory areas are allocated by JVM?

    • Heap, Stack, Program Counter Register and Native Method Stack

    9.What is the base class of all classes?

    • java.lang.Object

    10.What is javac ?

    • It produces the java byte code from *.java file.
    • It is the intermediate representation of your source code that contains instructions.

    11.Can we mark constructors final?

    • No, Constructor cannot be declared final.

    12.What are two different ways to call garbage collector?

    • System.gc() OR Runtime.getRuntime().gc().

    13.Can we override a static method?

    • No, we cannot override a static method. Static means class level.

    14.Use of finalize() method in java?

    •  finalize() method is used to free the allocated resource.

    15.Is it possible to overload main() method of a class?

    • Yes, we can overload main() method as well. 
    • But every time public static main(String[] args) will be called automatically by JVM.
    • Other methods need to call explicitly.

    16. Does Java support operator overloading?

    • Operator overloading is not supported in Java.

    17.Can I declare a data type inside loop in java?

    • Any Data type declaration should not be inside the loop. Its possible but not recommended.


    18.List two java ID Es?

    • 1.Eclipse, 2.Net beans and 3.IntelliJ

    19.Can we inherit the constructors?

    • No, we cannot inherit constructors. 
    • We can call super class constructors from subclass constructor by using super() call.

    20.Can this keyword be assigned null value?
    • No, this keyword cannot have null values assigned to it.
    21.Basic java interview programs for freshers 
    22.Java Quiz for freshers

    23. Top 25 java interview Questions for freshers  

    24.Top 15 oops concepts interview questions and answers

    25. Top 40 Java interview Programs asked in interview for freshers

    You Might Like:



    Java programming interview questions
    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. simple java interview questions on Super keyword
    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

    public static void main(String[] args) in Java

    • main method is a standard method used by JVM to execute any java program(Java SE).
    • Lets see an example class without main method.

    1. package instanceofjava;
    2. public Demo{
    3.  
    4. }


    • This class will compile fine but while executing JVM fails to find the main method in that class
    • So it will throw an exception: NoSuchMethodError:main

    1. package instanceofjava;
    2. public Demo{
    3. public static void main(String [] args){
    4.  
    5. }
    • This program will compile and executed. Because at run time JVM calls the main method on that class.

    Why main method is public?

    •  To call by JVM from anywhere main method should be public.
    • If JVM wants to call our method outside of our package then our class method should be public.

    1. package instanceofjava;
    2. public MainDemo{
    3. public static void main(String [] args){
    4.  
    5. }
    6. }

    public static void main(string args) interview Questions

    Why main method is static?

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

    Why main method return type is void?

    • main method wont return anything to JVM so its return type is void.

     Why the name main?

    • This is the name which is configured in the JVM.

    Why the arguments String[] args?

    •  Command line arguments.
    • String array variable name args : by the naming conventions they named like that we can write any valid variable name.

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

    What are the possible public static void main(String [] args)method declaration with String[] args?

    • public static void main(String[] args){ }
    • public static void main(String []args){ }
    • public static void main(String args[] ){ }
    • public static void main(String... args){ } 
    • Instead of args can place valid java identifier.

    Can we declared main method with final modifier?

    • Yes we can define main method with final modifier.

    1. package instanceofjava;
    2. public MainDemo{
    3. public static final void main(String [] args){
    4.  
    5. }

    Main method with all possible modifiers:

     

    1. package instanceofjava;
    2. public MainDemo{
    3. static final synchronized public void main(String [] args){
    4.  
    5.  System.out.println("Main method");
    6.  
    7. }

    Output:

    1. Main method

    Overloading main method:

    • We can declare multiple main methods with same name as per overloading concept.  
    • So we can overload main method but every time JVM looks for main method with String[] args method.
    • Lets see an example program on main method overloading.
    • main(String[] args) method will be called automatically by JVM.
    • If we want to call other methods we need to call explicitly.

     

    1. package instanceofjava;
    2. public MainDemo{
    3. public static void main(String [] args){
    4.  
    5.  System.out.println("Main method String [] args");
    6.  
    7. public static void main(int[] args){
    8.  
    9.  System.out.println("Main method int[] args");
    10.  
    11. }
    12.  





    Output:

    1. Main method String [] args


    Main method Inheritance:

    • Inheritance concept is applicable for main method.
    • If a sub class not having main method and extending a super class which is having main method.
    • Then if we execute super class main method will be executed fine.
    • If we execute sub class which is not having main method also executed fine and super class main method will be called.

    1. package instanceofjava;
    2. public A{
    3. public static void main(String [] args){
    4.  
    5.  System.out.println("Main method of class A");
    6.  
    7.  



    Output:

    1. Main method of class A

     

    1. package instanceofjava;
    2. public B extends A{
    3.  
    Output:

    1. Main method of class A

    What happen if both super and sub class having main method?

    • Actually its not overriding concept here. it is a method hiding concept in this scenario regarding with main method.

    1. package instanceofjava;
    2. public A{
    3. public static void main(String [] args){
    4.  
    5.  System.out.println("Main method of class A");
    6.  
    7.  

    Output:

    1. Main method of class A


    1. package instanceofjava;
    2. public B extends A{
    3. public static void main(String [] args){
    4.  
    5.  System.out.println("Main method of class B");
    6.  
    7.  


    Output:

    1. Main method of class B

    Java Jsp Interview Questions

    1.Explain JSP life Cycle methods?

    • jsp engine controls the life cycle of jsp
    • Jsp life cycle is described by three life cycle methods and six life cycle phases


    Jsp life cycle methods :

    • jspInit()
    • _jspService()
    • jspDestroy()


    • Jspinit() method is invoked when JSP page is initialized.
    • The _jspService() method corresponds to the body of the jsp page.
    • This method is defined automatically by the JSp container and should never be defined y the JSP page author.

    Jsp Life cycle Phases: 

    • Translation phase
    • Compilation phase
    • Instantiation phase
    • initialization phase
    • Servicing phase
    • Destruction phase

    Read More at : JSP Life Cycle

    2. What are the Jsp Implicit Objects?


    • Implicit objects in jsp are the objects that are created by the container automatically and the container makes all these 9 implicit objects to the developer.
    • We do not need to create them explicitly, since these objects are created and automatically by container and are accessed using standard variables(objects) are called implicit objects.
    • The following are of implicit variables available in the JSP.  


    1. out
    2. request
    3. response
    4. config
    5. application
    6. session
    7. pagecontext
    8. page
    9. exception
    Read More at: 9 Jsp Impicit Objects in java

    3. Explain Jsp Scripting Elements?

    • Jsp scripting elements are used to embed jva code in to the jsp.
    • Jsp scripting elements are three types
      1. Declaration
      2. Expression
      3. Scriplet
    Read More at: Jsp Scripting Elements



    4. What are the Directives in Jsp?

    • A jsp directive is a translation time instruction to the jsp engine.
    • we have three kinds of directives.
    1. Page directive
    2. Include directive
    3. Taglib directive

     Read More at: Jsp Directive Elements


     5. What are the differences between include directive and include action?


    Java Jsp interview Questions

     

    6. How to disable session in JSP?

    • By using page directive
    • <%@ page session="false" %> 

    7. How can we handle exceptions in a JSP page?

    • We can handle exception in jsp by using error page element of page directive 
    • The errorPage attribute tells the JSP engine which page to display if there is an error while the current page runs.
      The value of the errorPage attribute is a relative URL.
      The following directive displays MyErrorPage.jsp when all uncaught exceptions are thrown

      Syntax:

      errorPage="url"

      Example:

      <%@ page errorPage="error.jsp"%>

    8.How can we override jspInit() and jspDestroy() methods in a jsp?

    • Both methods are executed once in a life cycle of a jsp.
    • We can override these two method as shown below.
    • <%!
          public void jspInit() {
              . . .
          }
      %>
    • <%!    
          public void jspDestroy() {
              . . .   
          }
      %>

    9.Can a JSP extend a java class?

    • Yes we can extend a java class in jsp
    • <%@ include page extends="classname" %>
    • We can do this because jsp will convert to a servlet so a servlet can extend a class its fine

    10.How can we pass information from one jsp to included jsp page?

    • <jsp:include page="employeedetail.jsp" flush="true">
    • <jsp:param name="name" value="abc"/>
    • <jsp:param name="id" value="220"/>

    Java Servlet Interview Questions

     

    1.Explain Servlet life cycle in java.

    • Servlet engine controls the life cycle of servlet
    • Servlet life cycle is described by three life cycle methods under 5 life cycle phases
    • life cycle methods are
    1. init(ServletConfig obj)
    2. service(servletRequest, servletResponse)
    3. destroy() 

    • When ever somethings happens in the life of servlet. servlet engine calls these methods on the servlet instance and hence the name.

    Read more here: Servlet Life Cycle


    2.What is ServletConfig ?

    • Request parameters are used by the servlet to receive data from client to process the request.
    • Some specific data must be supplied to the servlet at the time of initialization of the servlet and this data is specific to the client.But data is not supplied by the client via request parameters
    • Use initialization parameters to supply the data to the servlet at the time of initialization of the servlet. initialization parameters are set in deployment descriptor   (Web.xml).
    • Servlet can access these parameters during the run time
    • Request parameters change form Request to request 
    • Initialization parameters change from servlet to servlet.

    Read more at : ServletConfig

    3. What is ServletContext?

    • We can deploy multiple web applications in a servlet container.
    • Each web application contains its own resources in a separate  environment. This environment called ad Web Application context or ServletContext.
    • The resources belongs to the one web application context are not available to the other web application context.
    • A Servlet context contains zero or more number of servlets. For every servlet object the container creates separate ServletConfig object.
    Read More at : ServlectContext


    4. Can we define a Constructor in servlet?

    • Yes we can define a constructor in servlet but we can not call the constructor explicitly because servlet container will create the object of servlet so it will be called by the servlet container.

    5. Can we call destroy() method inside init() method of a servlet. If yes what will happen?

    • Yes we can call destroy() method inside a init() method.
    • Actually container will call destroy() method if we call that method explicitly nothing will happen.
    • If we override destroy() method and called from init(). method will be called and code will be executed.

    6.What are the difference between GenericServlet and HttpServlet?

    GenericServlet HttpServlet
    Abstract Class Abstract Class
    Protocol Independent Http Protocol Dependent
    Subclass of Servlet Subclass of GenericServlet
    public void service(ServletRequest req,ServletResponse res ). Supports public void service() and protected void service(), doGet(),doPost(),doPut(),doDelete(),doHead(),doTrace(),doOptions()etc.


    7.What are the differences between doGet() and doPost()?



    doGet() doPost()
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException Protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    handles client's GET request handles client's POST request
    Request parameters appended to the URL and sent along with header information Request parameters are submitted via form.
    Request Parameters are not encrypted Request Parameters are encrypted
    Maximum size of data that can be sent using doget() method is 240 bytes There is no maximum size for data.




    8. Can we call a servlet from another servlet?

    • Yes. We can call a servlet from another servlet this is known as inter servlet communication.
    • By using RequestDispatcher object we can do this.
    • RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");
    • rd.forward(req, res);
    Read more at : RequestDipatcher in java

    9. What are the differences between forward() and sendRedirect() methods?

     


    forward() sendRedirect()
    request.getRequestDispathcer("example.jsp").
    forward(request, response);
    response.sendRedirect
    ("http://www.instanceofjava.com");
    Used to forward the Request to resources available in within the server Used to redirect the Request to other resources or domains

    10.How can you get the information about one servlet context in another servlet?

    • By using setAttribut() method we can set the data in one servlet and get in another
    • Context.setAttribute (“name”,” value”)
    • Context.getAttribute (“name”)

    11.Explain Servlet architecture?

    • Frequently asking java interview question in servlets you may get questions like
    • explain the architecture of java servlet?
    • explain the architecture of java servlet with diagram?
    • define the servlet architecture?
    • servlet architecture overview?
    • Servlets read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
    • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
    The Servlet architecture in Java is a Java-based framework for building web applications. It is a specification defined by the Java Community Process (JCP) and is implemented by various web containers such as Apache Tomcat, Jetty, and GlassFish.

    The Servlet architecture consists of several components, including:

    Servlets: These are the core components of the Servlet architecture. They are Java classes that handle HTTP requests and responses. Servlets are executed by a web container, which is responsible for managing their lifecycle, threading, and security.

    Web Containers: Also known as servlet engines, web containers are responsible for managing the lifecycle of servlets, providing services such as request handling, thread management, and security.

    JSP (JavaServer Pages): These are Java-based pages that are used to create dynamic web content. JSPs are compiled into servlets by the web container and executed by the servlet engine.

    JSP Tag Libraries: These are collections of custom tags that can be used in JSP pages to simplify the creation of dynamic web content.

    JavaBeans: These are Java classes that encapsulate business logic and data. They can be used in JSP pages to create dynamic web content.

    Filters: These are Java classes that can intercept and modify the requests and responses sent to and from servlets. They can be used to implement security, logging, and other common functionality.

    Listeners: These are Java classes that are notified of certain events that occur within the web container, such as a servlet being initialized or a session being created.

    In summary, the Servlet architecture provides a powerful and flexible framework for building web applications in Java, by providing a set of components and services to handle HTTP requests and responses, providing a powerful and flexible way to handle web content, and providing a set of components to handle security, logging, and other common functionality.
    Read More : servlet architecture with diagram
    Select Menu