20 most frequently asked Java interview questions

Java Program interview Question Basics

Java Program interview Question Basics

 

  1. Write a java program which show hello world
  2. Write a java program to display a simple string.
  3. Write a simple java program to display a string and perform a simple multiplication.
  4. Write a program which demonstrate the if statement.
  5. write a program to demonstrate the for loop.

1. Write a java program which show hello world

public class HelloWorldApp {                 //this line declare a class name and class definition starts from here
 
public static void main(String args[]) {     // line where program main function starts
 
System.out.print(" Hello World! ");         //this line prints HelloWorldApp!
 }                                          //at this lime program main function starts 
}                                          // program where class end

Output:

Hello World!

 


2. Write a java program to display a simple string.

 

public class example {                               // This line declare a  class name & the class definition is start from here .
	public static void main(String a[])       // This line begins the main() method.
    {
	  System.out.println(" How are You?");   //this print this string followed by newline.
    }		                                              // end of main program 
}	                                                   	 //end of class

Output:

How are you?

3. Write a simple java program to display a string and perform a simple multiplication.

public class Example_2{  
	public static void main(String a[]){             // This line begins the main() method.
		int num;                                     // this declares a variable called num.
		num=100 ;                                    // this assign num the value 100.
		System.out.println("this is num: " + num); 	 //This line outputs the string and number.
 
		num=num * 2;		                         // here * is a unary operator and it will increase value of num by two times.
		System.out.print("The value of num * 2 is ");     // No new line..Normal print()
 
		System.out.println(num);  	                 //This line will print new value of num.
 
	}		// end of main program
}	// end of class

Output:

this is num: 100
The value of num * 2 is 200

4. Write a program which demonstrate the if statement.

public class IfSample {  
	public static void main(String a[])                  // This line begins the main() method.
	{ 
		int x, y;                                        // this declares a variable called num.
		x=10;                                            // this assign x the value 10.
		y=20 ;                                           // this assign y the value 20.
		if(x<y) // in this line we give if Statement that ifx is less than y than give System.out.println("x is less than y"); // This line outputs the string. x= x*2; // multiply x by 2. if(x==y) System.out.println("now x is equal to y"); // this is a output line which show a string x= x*2; //multiply again by 2 if(x>y) System.out.println("now x is greater than y");		// this is a output line which show a string
	}	                                                            //end of main program
}

Output:

x is less than y
now x is equal to y
now x is greater than y

5. Write a program to demonstrate the for loop.

public class ForTest{  
	public static void main(String a[]){       // This line begins the main() method.
		int x;                                 //declare an integer x.
 
	for(x=0; x<10; x=x+1)                      //for loop start #for(initialization, Condition, Increment)
 
	System.out.println("this is x: " + x);	   // this line show value of x
	}	
}

Output:

Leave a Reply