Commonly Asked Java Programming Interview Questions: Selfless (Freshersjob)
Commonly Asked Java Programming Interview Questions: Selfless (Freshersjob)
1. Fibonacci series
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
2. Prime number
if (count == 0) {
System.out.println(i);
}
}
}
}
3. String Palindrome
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
4. Integer Palindrome
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454; //It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
5. Armstrong number
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
6. Factorial
7. Reverse a String
//Lets take two numbers 55 and 121 and find their GCD
int num1 = 55, num2 = 121, gcd = 1;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if(leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
import java.util.Arrays;
import java.util.Scanner;
class JavaExample
{
public static void main(String[ ] arg)
{
boolean isVowel=false;;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a character : ");
char ch=scanner.next().charAt(0);
scanner.close();
switch(ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : isVowel = true;
}
if(isVowel == true) {
System.out.println(ch+" is a Vowel");
}
else {
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println(ch+" is a Consonant");
else
System.out.println("Input is not an alphabet");
}
}
}
7
Enter 7 integers
4
5
66
77
8
99
0
Enter the search value:
77
77 found at location 4.
import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is:
"+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}
Output:
import java.util.Scanner;
class FloydTriangle
int n, num = 1, c, d;
n = in.nextInt();
System.out.println("Floyd's triangle:");
System.out.print(num+" ");
num++;
}
Output :
Enter the number of rows of Floyd'striangle to display
6
Floyd's triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
import java.util.Scanner;
System.out.print(strNew);
}
}
Output:
Enter a String : Deepak Patel
Removing Vowels from The String [Deepak Patel]
All Vowels Removed Successfully..!!
Now the String is :
Dpk Ptl
Regards,
Dharmesh
Jamali
Dhiraj
Krishna