Java Coding Questions
Q. Write a function to find out duplicate words in a
given string?
Approach
1. Define a string.
2. Convert the string into lowercase to make the comparison insensitive.
3. Split the string into words.
4. Two loops will be used to find duplicate words. Outer loop will select a word and
Initialize variable count to 1. Inner loop will compare the word selected by outer
loop with rest of the words.
5. If a match found, then increment the count by 1 and set the duplicates of word to
'0' to avoid counting it again.
6. After the inner loop, if count of a word is greater than 1 which signifies that the
word has duplicates in the string.
public class DuplicateWord {
public static void main(String[] args) {
String string = "Big black bug bit a big black dog on his big black nose";
int count;
//Converts the string into lowercase
string = string.toLowerCase();
//Split the string into words using built-in function
String words[] = string.split(" ");
System.out.println("Duplicate words in a given string : ");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
//Set words[j] to 0 to avoid printing visited word
words[j] = "0";
}
}
//Displays the duplicate word if count is greater than 1
if(count > 1 && words[i] != "0")
System.out.println(words[i]);
Java Coding Questions 1
, }
}
}
Output
Duplicate words in a given string :
big
black
Q. Find the missing number in an array?
Approach
1. Calculate A = n (n+1)/2 where n is largest number in series 1…N.
2. Calculate B = Sum of all numbers in given series
3. Missing number = A – B
// Java program to find missing Number
public class Test {
public static void main(String[] args) {
int total;
int[] numbers = new int[]{1, 2, 3, 4, 6, 7};
total = 7;
int expected_sum = total * ((total + 1) / 2);
int num_sum = 0;
for (int i: numbers) {
num_sum += i;
}
System.out.print( expected_sum - num_sum );
}
}
Output
5
Java Coding Questions 2
, Q. Write a program to generate random numbers
between the given range?
Approach
1. Get the Min and Max which are the specified range.
2. Call the nextInt() method of ThreadLocalRandom class
(java.util.concurrent.ThreadLocalRandom) and specify the Min and Max value as
the parameter as
ThreadLocalRandom.current().nextInt(min, max + 1);
3. Return the received random value
// Java program to generate a random integer
// within this specific range
import java.util.concurrent.ThreadLocalRandom;
class GFG {
public static int getRandomValue(int Min, int Max)
{
// Get and return the random integer
// within Min and Max
return ThreadLocalRandom
.current()
.nextInt(Min, Max + 1);
}
// Driver code
public static void main(String[] args)
{
int Min = 1, Max = 100;
System.out.println("Random value between "
+ Min + " and " + Max + ": "
+ getRandomValue(Min, Max));
}
}
Input
Input: Min = 1, Max = 10
Output
Java Coding Questions 3
, Random value between 1 and 100: 35
Q. Write a java program to swap two string variables
without using temp variable?
Approach
1. Append second string to first string and store in first string:
a=a+b
2. call the method substring(int beginindex, int endindex)
by passing beginindex as 0 and endindex as,
a.length() - b.length():
b = substring(0,a.length()-b.length());
3. call the method substring(int beginindex) by passing
b.length() as argument to store the value of initial
b string in a
a = substring(b.length());
/**
* Java program to swap two strings without using a temporary
* variable.
**/
import java.util.*;
class Swap
{
public static void main(String args[]) {
// Declare two strings
String a = "Hello";
String b = "World";
// Print String before swapping
System.out.println("Strings before swap: a = " + a + " and b = "+b);
// append 2nd string to 1st
a = a + b;
// store intial string a in string b
b = a.substring(0, a.length() - b.length());
// store initial string b in string a
a = a.substring(b.length());
// print String after swapping
System.out.println("Strings after swap: a = " + a + " and b = " + b);
Java Coding Questions 4
Q. Write a function to find out duplicate words in a
given string?
Approach
1. Define a string.
2. Convert the string into lowercase to make the comparison insensitive.
3. Split the string into words.
4. Two loops will be used to find duplicate words. Outer loop will select a word and
Initialize variable count to 1. Inner loop will compare the word selected by outer
loop with rest of the words.
5. If a match found, then increment the count by 1 and set the duplicates of word to
'0' to avoid counting it again.
6. After the inner loop, if count of a word is greater than 1 which signifies that the
word has duplicates in the string.
public class DuplicateWord {
public static void main(String[] args) {
String string = "Big black bug bit a big black dog on his big black nose";
int count;
//Converts the string into lowercase
string = string.toLowerCase();
//Split the string into words using built-in function
String words[] = string.split(" ");
System.out.println("Duplicate words in a given string : ");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
//Set words[j] to 0 to avoid printing visited word
words[j] = "0";
}
}
//Displays the duplicate word if count is greater than 1
if(count > 1 && words[i] != "0")
System.out.println(words[i]);
Java Coding Questions 1
, }
}
}
Output
Duplicate words in a given string :
big
black
Q. Find the missing number in an array?
Approach
1. Calculate A = n (n+1)/2 where n is largest number in series 1…N.
2. Calculate B = Sum of all numbers in given series
3. Missing number = A – B
// Java program to find missing Number
public class Test {
public static void main(String[] args) {
int total;
int[] numbers = new int[]{1, 2, 3, 4, 6, 7};
total = 7;
int expected_sum = total * ((total + 1) / 2);
int num_sum = 0;
for (int i: numbers) {
num_sum += i;
}
System.out.print( expected_sum - num_sum );
}
}
Output
5
Java Coding Questions 2
, Q. Write a program to generate random numbers
between the given range?
Approach
1. Get the Min and Max which are the specified range.
2. Call the nextInt() method of ThreadLocalRandom class
(java.util.concurrent.ThreadLocalRandom) and specify the Min and Max value as
the parameter as
ThreadLocalRandom.current().nextInt(min, max + 1);
3. Return the received random value
// Java program to generate a random integer
// within this specific range
import java.util.concurrent.ThreadLocalRandom;
class GFG {
public static int getRandomValue(int Min, int Max)
{
// Get and return the random integer
// within Min and Max
return ThreadLocalRandom
.current()
.nextInt(Min, Max + 1);
}
// Driver code
public static void main(String[] args)
{
int Min = 1, Max = 100;
System.out.println("Random value between "
+ Min + " and " + Max + ": "
+ getRandomValue(Min, Max));
}
}
Input
Input: Min = 1, Max = 10
Output
Java Coding Questions 3
, Random value between 1 and 100: 35
Q. Write a java program to swap two string variables
without using temp variable?
Approach
1. Append second string to first string and store in first string:
a=a+b
2. call the method substring(int beginindex, int endindex)
by passing beginindex as 0 and endindex as,
a.length() - b.length():
b = substring(0,a.length()-b.length());
3. call the method substring(int beginindex) by passing
b.length() as argument to store the value of initial
b string in a
a = substring(b.length());
/**
* Java program to swap two strings without using a temporary
* variable.
**/
import java.util.*;
class Swap
{
public static void main(String args[]) {
// Declare two strings
String a = "Hello";
String b = "World";
// Print String before swapping
System.out.println("Strings before swap: a = " + a + " and b = "+b);
// append 2nd string to 1st
a = a + b;
// store intial string a in string b
b = a.substring(0, a.length() - b.length());
// store initial string b in string a
a = a.substring(b.length());
// print String after swapping
System.out.println("Strings after swap: a = " + a + " and b = " + b);
Java Coding Questions 4