Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Java_Coding_Questions

Rating
-
Sold
-
Pages
43
Uploaded on
14-07-2024
Written in
2023/2024

Java_Coding_Questions

Institution
Course

Content preview

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

Written for

Institution
Course

Document information

Uploaded on
July 14, 2024
Number of pages
43
Written in
2023/2024
Type
OTHER
Person
Unknown

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
rajeshrawat

Also available in package deal

Get to know the seller

Seller avatar
rajeshrawat Rajesh rawat
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
7
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions