By: Bijan Patel - In:: Java For Testers Interview Questions and Answers - Part 7
By: Bijan Patel - In:: Java For Testers Interview Questions and Answers - Part 7
By: Bijan Patel|In: Automation
11. Write a Java program to calculate the power of a number using a while loop?
public class Power {
public static void main(String[] args) {
int base = 3, exponent = 4;
long result = 1;
while (exponent != 0)
{
result *= base;
--exponent;
}
System.out.println("Answer = " + result);
}
}
12. Can we have duplicate key value in HashMap?
HashMap doesn’t allow duplicate keys but allows duplicate values.
14. Write a Java program to verify whether a number is perfect number or not?
class PerfectNumber
{
static boolean isPerfect(int n)
{
int sum = 1;
for (int i = 2; i * i <= n; i++)
{
if (n % i==0)
{
if(i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return true;
return false;
}
public static void main (String[] args)
{
System.out.println("Below are all perfect" +
"numbers till 10000");
for (int n = 2; n < 10000; n++)
if (isPerfect(n))
System.out.println( n +
" is a perfect number");
}
}
15. Write a Java program for printing the Fibonacci series from 1 to 10?
class FibonacciNumber
{
// Method to print first n Fibonacci Numbers
static void printFibonacciNumbers(int n)
{
int f1 = 0, f2 = 1, i;
if (n < 1)
return;
for (i = 1; i <= n; i++)
{
System.out.print(f2+" ");
int next = f1 + f2;
f1 = f2;
f2 = next;
}
}
public static void main(String[] args)
{
printFibonacciNumbers(10);
}
}
18. In the given String, remove the white spaces, reverse it and print only the odd position characters?
public Class PrintOddChars{
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string: ");
String name = br.readLine();
String odd = "";
String reversedString = reverseString(name.replaceAll("\\s",""));
for(int i=0;i<reversedString.length();i++){
if(i%2!=0){
odd = odd + reversedString.charAt(i);
}
}
System.out.println(odd);
}
public static String reverseString(String s){
String rev="";
char[] arr = s.toCharArray();
for(int i=arr.length-1;i>=0;i--)
rev = rev + arr[i];
return rev;
}
}
22. Find the duplicate strings in a given statement and remove them?
public class RemoveDuplicateWords
{
public static void main(String[] args)
{
String input="Welcome to QAScript Java Interview Question in QAScript";
String[] words=input.split(" ");
for(int i=0;i<words.length;i++)
{
if(words[i]!=null)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)
{
if(words[k]!=null)
{
System.out.println(words[k]);
}
}
}
}
24. Find and remove the duplicate characters from a given string and print ?
public static String removeDuplicates(String word){
Set<Character> chars = new HashSet<>();
StringBuilder output = new StringBuilder(word.length());
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (chars.add(ch)) {
output.append(ch);
}
}
return output.toString();
}