Add

Top 20 Java Programming Interview Questions And Answers

Level 1 in Java Coding Problems for Interviews

Q1. Reverse a String in Java

You are given a string s. You need to reverse the string.

Example 1:

Input:
s = LearnKro
Output: orKnraeL
class LearnKro
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());

while(t-- >0)
{
String str = read.readLine();
System.out.println(new Reverse().reverseWord(str));
}
}
}// } Driver Code Ends
//User function Template for Java

class Reverse
{
// Complete the function
// str: input string
public static String reverseWord(String str)
{
int k= str.length();
String s = "";
for(int i=str.length()-1;i>=0;i--){
s = s+str.charAt(i);
}
return s;
}
}


Q2. Reverse String words in a given string using Java

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

Input:
S = i.like.this.program.very.much
Output: much.very.program.this.like.i

import java.util.*;

class LearnKro {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t > 0) {

            String s = sc.next();

            Solution obj = new Solution();

            System.out.println(obj.reverseWords(s));

            t--;

        }

    }

}

// } Driver Code Ends

class Solution 

{

    //Function to reverse words in a given string

    public static String reverseWords(String S)

    {

        String[] str = S.split("[.]", 0);

        String abc = "";

        

        for(int i = str.length - 1; i >= 0; i--) {

            if(i == 0) {

                abc = abc + str[i];

            } else {

                abc = abc + str[i] + ".";

            }

        }

        return abc;

    }

}


Q3. Longest Common Prefix in an Array 

Given a array of N strings, find the longest common prefix among all strings present in the array.

Input:
N = 4
arr[] = {LearnKro, kro, Learn,Java}
Output: kro
Explanation: "kro" is the longest common
prefix in all the given strings.















Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.