These are basic coding questions asked in java Interviews for checking your Programming Logic.
import java.util.Scanner;
public class CheckPalindrome
{
public static void palinNum(String st)
{
StringBuffer b=new StringBuffer( st);
b.reverse();
String s=b.toString();
if(st.equals(s))
{
System.out.println("Palindrome");
}
else
{
System.out.println("not palindrome");
}
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter string: ");
String sn=s.next();
CheckPalindrome.palinNum(sn);
}
}
Q.2 Reverse the String ?
Ans:
import java.util.Scanner;
public class ReverseString {
public static String reverse(String str)
{
String s1=" ";
for(int i=str.length()-1;i>=0;i--)
{
s1=s1+str.charAt(i);
}
return s1;
}
public static void main(String[] args) {
System.out.println("enter the string");
Scanner s=new Scanner(System.in);
String st=s.nextLine();
System.out.println(ReverseString.reverse(st));
}
}
Q.3 Find the Power of the number
Ans:
import java.util.Scanner;
public class Base_Power
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter base");
int base=s.nextInt();
System.out.println("enter the exponent");
int expo=s.nextInt();
int power=1;
for(int i=1;i<=expo;i++)
{
power=power*base;
}
System.out.println(power);
}
}
Q.4 Find the Fibonaci Series of N Terms
Ans:
import java.util.Scanner;
public class FibonaciSeries
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt();
System.out.println("First " + n + " terms of fibonnaci series: ");
for (int i = 1; i <= n; ++i)
{
System.out.println(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Q.5 Find Longest Word from String
Ans:
import java.util.Arrays;
import java.util.Scanner;
public class LargeWorld
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("enter String");
String str=s.nextLine();
str+=" ";
String Word="",lWord="";
int len=str.length();
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch != ' ')
{
Word+=ch;
}
else {
if(Word.length()>lWord.length())
{
lWord=Word;
}
Word="";
}
}
System.out.println("longestm word is="+lWord);
}
}
Q.6 Write a program for Given Star pattern
Ans:
public class Pattern_5Star {
public static void main(String[] args)
{
for (int i=4; i>=0 ; i--)
{
for (int j=0; j<=i; j++)
{
System.out.print("*" + " ");
}
System.out.println();
}
}
}
Q.7 Remove Space from String
Ans:
public class RemoveSpacesFromString {
public static void main(String[] args) {
String str = "My Name Is xyz";
String str1 = str.replace(" ", "");
System.out.println(str1);
}
}
Q.8 Replace Zeros as 1 from array
Ans:
import java.util.Scanner;
public class ReplaceZeroAs1
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter size");
int size=s.nextInt();
int a[]=new int[size];
System.out.println("enter array elements");
for(int i=0;i<a.length;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<a.length;i++)
{
if(a[i]==0)
{
a[i]=1;
}
System.out.println(a[i]);
}
}
}
Q.9 Reverse The Number
Ans:
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("enter the number");
int num=s.nextInt();
int reverse=0;
while(num>0)
{
int r=num%10;
reverse=reverse*10+r;
num=num/10;
}
System.out.print(reverse);
}
}