[Go to site: main page, start]

0% found this document useful (0 votes)
2 views4 pages

Java Coding Practice Questions

The document provides Java coding practice problems focused on arrays, strings, and object-oriented programming (OOP). It includes solutions for finding the second largest element in an array, checking if two strings are anagrams, and demonstrating inheritance and method overriding with an Animal and Dog class. Each section includes sample input/output and corresponding Java code implementations.

Uploaded by

taskmaster1506
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)
2 views4 pages

Java Coding Practice Questions

The document provides Java coding practice problems focused on arrays, strings, and object-oriented programming (OOP). It includes solutions for finding the second largest element in an array, checking if two strings are anagrams, and demonstrating inheritance and method overriding with an Animal and Dog class. Each section includes sample input/output and corresponding Java code implementations.

Uploaded by

taskmaster1506
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

Java Coding Practice: Arrays, Strings, and OOP

1. Arrays: Find Second Largest Element

Problem:

Write a Java method to find the second largest number in an integer array.

Return -1 if there is no second largest (all elements are equal or only one element).

Sample I/O:

Sample Input: int[] arr = {4, 8, 1, 9, 4, 9};

Expected Output: 8

Solution:

public class ArrayUtils {

public static int secondLargest(int[] arr) {

if ([Link] < 2) return -1;

int largest = Integer.MIN_VALUE;

int second = Integer.MIN_VALUE;

for (int num : arr) {

if (num > largest) {

second = largest;

largest = num;

} else if (num > second && num < largest) {

second = num;

return (second == Integer.MIN_VALUE) ? -1 : second;

public static void main(String[] args) {

int[] nums = {4, 8, 1, 9, 4, 9};


[Link](secondLargest(nums)); // Output: 8

2. Strings: Check Anagram

Problem:

Write a method to check if two strings are anagrams (case-insensitive).

An anagram means the same characters in different order.

Sample I/O:

Sample Input: str1 = "Listen", str2 = "Silent"

Expected Output: true

Solution:

import [Link];

public class StringUtils {

public static boolean isAnagram(String s1, String s2) {

char[] a = [Link]().toCharArray();

char[] b = [Link]().toCharArray();

[Link](a);

[Link](b);

return [Link](a, b);

public static void main(String[] args) {

[Link](isAnagram("Listen", "Silent")); // Output: true

3. OOP: Inheritance and Method Overriding


Problem:

Create a class Animal with method makeSound() returning "Some sound".

Create subclass Dog that overrides makeSound() to return "Woof".

Sample I/O:

Expected Output:

Some sound

Woof

Solution:

class Animal {

public String makeSound() {

return "Some sound";

class Dog extends Animal {

@Override

public String makeSound() {

return "Woof";

public class TestOOP {

public static void main(String[] args) {

Animal a = new Animal();

Dog d = new Dog();

[Link]([Link]()); // Output: Some sound

[Link]([Link]()); // Output: Woof

}
}

You might also like