Java Coding Questions

SQL INTERVIEW QUESTION PART-2

Java Coding Questions

Below is the list of the most frequently asked java coding questions in interviews.

1. Write a java program to reverse a given number.

Reversing a given number means reversing the order of the number. For example, the given number is 1234 then its reverse will be 4321. We will see how to achieve this in different ways.
Logic 1
Firstly, Using an algorithm: We will create a variable rev which will store the reverse number
and will apply the below logic in the program. Assuming given number num=1234.
int rev=0;
while(num!=0)
{
rev=rev*10 + num%10; // 0*10+4
num=num/10; // 1234/10= 123
}
Program:
public class swap_numb {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Provide the number");
int num=sc.nextInt();
int rev=0;
while(num!=0)
{
rev=rev*10 + num%10;
num=num/10;
}
System.out.println("reversed number is "+ rev);
}
}

Logic 2

Secondly, Using StringBuffer Class. In the StringBuffer class, there is a method called reverse method using which we can easily reverse the given number without using any algorithm.

We will create a reverse variable of StringBuffer type.

Program:

 
public class swap_numb {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Provide the number");
int num=sc.nextInt();
StringBuffer sbf= new StringBuffer(String.valueOf(num));
StringBuffer rev=  sbf.reverse();
System.out.println("reversed number is "+ rev);
}
}

2. Java program to reverse the given string.

Reversing a given string means reversing the order of the string. Suppose the given number is abcd then it’s reverse will be dcba. We will see how to achieve this in different ways.

Logic 1

We will write a logic, to reverse the string at first we will find out the length of the given string. After that, we will write a for loop and will iterate from the end extracting the character from the string.

And we will add the extracted character in some other variable say reverse variable. We will keep on concatenating the character in the reverse variable. To extract the character from the string we will use the charAt method

 Program:

publicclass reverseString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Provide the String");
String str=sc.next();
String rev=" ";
int length= str.length();
for(int i=length-1; i>=0; i--)
{
rev=rev+str.charAt(i);
}
System.out.println("Reverse of String is:  "+  rev);
}
}

Logic 2

Using StringBuffer class. We will create one object of StringBuffer class and will pass the string value. Now we will directly use the reverse method to reverse the string.

public class reverseString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Entr the String");
String str=sc.next();
StringBuffer sbf= new StringBuffer(str);
StringBuffer rev=sbf.reverse();
System.out.println("Reverste of String is  "   +  rev);
}
}

3. Write a java program for Swapping 2 String without using the 3rd temp variable.

Swapping in other words, just to interchange the value in the variable.
Suppose variable a= hello and variable b= world
After swapping variable a= world and variable b= hello
 
package javaprograns;public class Swap_Two_string {
public static void main(String[] agrs) {
String a="Hello"; String b="World";
System.out.println("Values of String before Swapping"); System.out.println("Value of a:  "+a); System.out.println("Value of b:  "+b);
a=a+b; //HelloWorld
b=a.substring(0, a.length()-b.length()); a=a.substring(b.length());
System.out.println("Values of String after Swapping"); System.out.println("Value of a:  "+a); System.out.println("Value of b:  "+b);
}

}

4. Various operations on a given String

  • Find out the length
  • Find out a character at a given position
  • Find out the index of a character
  • How to compare 2 Strings
  • Replace method
  • Split Method
  • Converting the String in upper case to lower case and vice versa

Above all are various operations on a string.

package javaprograns;
public class String_Operations {
public static void main(String[] args) {
String str="Hello in the Java world";
String str2="Good Morning and ";
// length of the string
System.out.println("Length of str: "str.length());
//Printing a character at a position
System.out.println("Character at 5th position is : "+str.charAt(6));
// find the index of any character
System.out.println("Index of a is : "+str.indexOf("a"));
// find the index of any character, starting the count from somewhere and not from starting
System.out.println("index of a after 14:  " + str.indexOf("a",str.indexOf("a")+1));
// String comparison
System.out.println(str.equals(str2) );
// sub string
System.out.println(str.substring(3, 6));
// replace
System.out.println(str.replace(" """));
//Upper case
System.out.println(str.toLowerCase());
// split
String splitstring[]= str.split(" ");
for(int i=0;i<str.length(); i++)
{
System.out.println(splitstring[i]);
}
}
}

5. Remove special characters and junk characters from a String.

Removing special characters and junk characters from a string using regular expression and replace all method.

package javaprograns;
public class Remove_JunkChars_from_String {
public static void main(String[] args) {
String s1="@##@#@@[email protected]$%%^[email protected][email protected]%%!";
s1=s1.replaceAll(("[^a-zA-Z0-9]"), ""); // s1 is a immutable
System.out.println(s1);
}
}

Check Java Interview Questions

Wikipedia Java

3 thoughts on “Java Coding Questions”

  1. Logic 3 for String reversal with time complexityo(n)
    public class StringReversals {

    public static void main(String[] args) {
    System.out.println(reverse(“Shabana”));

    }
    public static String reverse(String s)
    {
    int low=0;int high=s.length()-1;
    char[] ch=s.toCharArray();
    while(low<high)
    {
    char c=ch[low];
    ch[low]=ch[high];
    ch[high]=c;
    low++;
    high–;
    }
    return String.valueOf(ch);
    }

    }

  2. Јust ɗesire to say yoսr article is as amazing.
    The clarity in youг post is just excellent and i ⅽoulԁ assume you аre an expеrt on this subject.
    Fine with your permission allow me to grab your RSS feed to kеep updated wіth forthcoming post.
    Thanks a milliօn and please continue the gratifying
    work.

Comments are closed.