Java Programming Examples


Java Programming Examples

Below is the list of Java Programming Examples and the most frequent questions the interviewer is asking in the java interview round.


1. Is it possible to override a private or static method in java?

No, we cannot override a private or static method.
A private method can not be inherited so we cannot redefine or override it.
A static method belongs to a class and we cannot override it.
Suppose there is a static method in the parent class and if you want to create a method in the child class to override the parent class static method then this child class method should also be static and this concept is known as hiding.


2. Explain multiple inheritance and does java support it?

For example,

If a child class inherits the property from multiple class it is known as multiple inheritance. And java does not support multiple inheritance. Because it creates a diamond problem.
The problem with multiple inheritance is that if multiple parent class have the same method name, then at the run time it will be very difficult for the compiler to decide which method to execute from the child class.


3. How can we find out the greatest among the 3 given numbers?

To find out the greatest among the 3 given numbers, We will simply do a comparison.

package javaprograns;
public class Greatest_in_3 {
public static void main(String[] args) {
int a= 20;
int b=5;
int c=40;
if(a>b && a>c)
System.out.println("Greated number is:"+a);
System.out.println("Greated Number is: "+b);
System.out.println("Greatest Number is: "+c);


4. How can we find out the count of digits in a number.

In order to get the count of digits in a number, we will simply divide the number by 10 and will maintain a counter.

package javaprograns;
public class count_digit {
public static void main(String[] args) {
int a= 123450;
int count=0;
System.out.println("count is: "+count);


5. Explain ASCII code for a given character.

ASCII is a code used to represent the English characters as numbers. Every letter of the English alphabets is assigned a number ranging from 0 to 127. 

For example, the ASCII code for  ‘P’ is 80.

For example, the ASCII code for ‘a’ is 97.

package javaprograms;
public class Ascii_Value {
public static void main(String[] args) {
char ch='a';
int ascii= (int)ch;
System.out.println("The ascii value of the a is: "+ ascii);


6. Explain Constructor in java.

Five points about constructor you should always remember.

  1.  Constructor is a special type of method.
  2.  It is used to initialising the class variable.
  3.  Constructor name should be the same as the class name.
  4.  It doesn’t return any value.
  5. The constructor will be invoked at the time of object creating, we don’t need to call the constructor explicitly.


7. When the constructor of a class is invoked?

The constructor of a class is invoked every time an object is created with new keyword.

For example, in the following class, two objects are created using the new keyword and hence, the constructor is invoked two times.

public class constructor_example {
constructor_example() {
        system.out.println("Inside constructor");
    public static void main(String args[]) {
        constructor_example c1 = new const_example();
        constructor_example c2 = new const_example();


8. In java, can a class have multiple constructors?

Yes, we can have multiple constructors in java.

A class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

Check Java Interview Questions

Wikipedia Java

Leave a Comment

Your email address will not be published.