0% found this document useful (0 votes)
26 views3 pages

Java Round Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Java Round Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1:

public class ReverseString {


public static void main(String[] args) {
String str = "Hello";
String reversed = reverseString(str);
System.out.println(reversed);
}

public static String reverseString(String str) {


StringBuilder sb = new StringBuilder(str);
return sb.reverse.toString();
}

Expected Output:

Expected Output:
olleH

Question 2:
public class MaxFinder {
public static void main(String[] args) {
int[] arr = {1, 5, 3, 9, 2};
int max = findMax(arr);
System.out.println("Max: " + max);
}

public static int findMax(int arr) {


int max = arr[0];
for (int i = 1; i <= arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}

Expected Output:

Expected Output:
Max: 9

Question 3:
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean result = isPrime(num);
System.out.println(num + " is prime: " + result);
}
public static boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= num / 2; i++) {
if (num % i = 0) {
return false;
}
}
return true
}
}

Expected Output:

Expected Output:
29 is prime: true

Question 4:
public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
swap(a, b);
System.out.println("a: " + a + ", b: " + b);
}

public static void swap(int a, int b) {


a = a + b;
b = a - b;
a = a - b;
}
}

Expected Output:

Expected Output:
a: 10, b: 5

Question 5:
public class VowelCounter {
public static void main(String[] args) {
String str = "Hello World";
int count = countVowels(str);
System.out.println("Vowel count: " + count);
}

public static int countVowels(String str) {


int count = 0;
for (char c : str.toCharArray()) {
if ("aeiouAEIOU".contains(c)) {
count++
}
}
return count;
}
}

Expected Output:

Expected Output:
Vowel count: 3

You might also like