0% found this document useful (0 votes)
182 views2 pages

Core Java Interview Questions

This document contains sample code for reversing a string in Java using different approaches. The first code sample uses a for loop to iterate through the characters of the input string in reverse order and add them to an empty string to produce the reversed output. The second sample uses the reverse() method of the StringBuilder and StringBuffer classes to directly reverse two sample strings without needing a for loop. Strings in Java are immutable, which means their values cannot be changed once created.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
182 views2 pages

Core Java Interview Questions

This document contains sample code for reversing a string in Java using different approaches. The first code sample uses a for loop to iterate through the characters of the input string in reverse order and add them to an empty string to produce the reversed output. The second sample uses the reverse() method of the StringBuilder and StringBuffer classes to directly reverse two sample strings without needing a for loop. Strings in Java are immutable, which means their values cannot be changed once created.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

CORE JAVA INTERVIEW

QUESTIONS
1. Write a java program to reverse the string?

import java.util.*;
class ReverseString
{
  public static void main(String args[])
  {
    String original, reverse = "";
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a string to reverse");


    original = in.nextLine();

    int length = original.length();

    for (int i = length - 1 ; i >= 0 ; i--)
      reverse = reverse + original.charAt(i);

    System.out.println("Reverse of the string: " + reverse);


  }
}

public class StringReverse {

public static void main(String[] args) {


// TODO Auto-generated method stub
StringBuilder objBuilder = new StringBuilder("Sundari");
System.out.println(objBuilder.reverse());

StringBuffer objBuffer = new StringBuffer("Vadani");


System.out.println(objBuffer.reverse());

2. There is a variable declared inside the Interface. Can we change the value of the variable?

Inside any implementation class, you cannot change the variables declared in


interface because by default, they are public, static and final.
3. String is immutable or mutable.

1. Write code to filter duplicate elements from an array and print as a list?
2. Write code to sort the list of strings using Java collection?
3. Write a function to reverse a number in Java?
4. Write a method to check prime no. in Java?
5. Write a Java program to find out the first two max values from an array?
6. Write a Java program to find the longest substring from a given string which doesn’t
contain any duplicate characters?
7. Write Java code to get rid of multiple spaces from a string?
8. Write Java code to identify a number as Palindrome?
9. Write Java code to swap two numbers without using a temporary variable?
10. Write a Java program to demonstrate string reverse with and without StringBuffer class?

You might also like