Practice 70 Java Nested Classes Programming Questions
Q1. Write a Java program to create a simple static nested class.
Expected Output:
Static nested class example
Q2. Write a Java program to demonstrate the use of an inner class within a method.
Expected Output:
Inner class example inside method
Q3. Write a Java program to create an instance of a non-static nested class from the outer class.
Expected Output:
Non-static nested class instance created
Q4. Write a Java program to demonstrate the use of a nested class accessing outer class members.
Expected Output:
Outer class member accessed by nested class
Q5. Write a Java program that shows how a nested class can be used to implement a simple calculator.
Input:
Add 2 and 3
Expected Output:
Result: 5
Q6. Write a Java program to create a nested class that calculates the square of a number.
Input:
Number: 4
Expected Output:
Square: 16
Q7. Write a Java program to demonstrate an anonymous inner class that implements an interface.
Expected Output:
Anonymous inner class example
Q8. Write a Java program to use a nested class to reverse a string.
Input:
String: Java
Expected Output:
Reversed string: avaJ
Q9. Write a Java program that uses a nested class to calculate the factorial of a number.
Input:
Number: 5
Expected Output:
Factorial: 120
Q10. Write a Java program to demonstrate how an outer class can invoke the method of its nested class.
Expected Output:
Nested class method called from outer class
Q11. Write a Java program to demonstrate a nested class to print Fibonacci series.
Input:
Limit: 6
Expected Output:
Fibonacci series: 0 1 1 2 3 5
Q12. Write a Java program to create a nested class that calculates the power of a number.
Input:
Base: 2, Exponent: 3
Expected Output:
Power: 8
Q13. Write a Java program to demonstrate a nested class that adds two matrices.
Input:
Matrix 1: [[1, 2], [3, 4]]
Matrix 2: [[5, 6], [7, 8]]
Expected Output:
Resultant Matrix: [[6, 8], [10, 12]]
Q14. Write a Java program to use a nested class for sorting an array.
Input:
Array: [4, 2, 5, 1, 3]
Expected Output:
Sorted Array: [1, 2, 3, 4, 5]
Q15. Write a Java program to demonstrate a nested class for calculating the area of a circle.
Input:
Radius: 5
Expected Output:
Area of circle: 78.54
Q16. Write a Java program to demonstrate how an inner class can inherit from the outer class.
Expected Output:
Inner class inherited successfully
Q17. Write a Java program to use a nested class to check whether a number is prime.
Input:
Number: 7
Expected Output:
7 is prime
Q18. Write a Java program to create a nested class to find the maximum element in an array.
Input:
Array: [3, 7, 2, 9, 4]
Expected Output:
Maximum element: 9
Q19. Write a Java program to use a nested class for string manipulation.
Input:
String: hello
Expected Output:
String in uppercase: HELLO
Q20. Write a Java program to create a nested class that checks if a string is a palindrome.
Input:
String: madam
Expected Output:
madam is a palindrome
Q21. Write a Java program to demonstrate the use of static nested class to store student information.
Input:
Name: John, Age: 20
Expected Output:
Student: John, Age: 20
Q22. Write a Java program to use a nested class to calculate the sum of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q23. Write a Java program to create a nested class to reverse the digits of an integer.
Input:
Number: 12345
Expected Output:
Reversed number: 54321
Q24. Write a Java program to use an inner class for handling exceptions.
Expected Output:
Exception handled in inner class
Q25. Write a Java program to create a nested class to print the multiplication table of a number.
Input:
Number: 5
Expected Output:
Multiplication table of 5: 5 10 15 20 25
Q26. Write a Java program to create a nested class for finding the GCD of two numbers.
Input:
Number 1: 12, Number 2: 15
Expected Output:
GCD: 3
Q27. Write a Java program to use a nested class for checking whether a number is Armstrong.
Input:
Number: 153
Expected Output:
153 is an Armstrong number
Q28. Write a Java program to create a nested class that performs binary to decimal conversion.
Input:
Binary: 1011
Expected Output:
Decimal: 11
Q29. Write a Java program to demonstrate a nested class to calculate the LCM of two numbers.
Input:
Number 1: 4, Number 2: 6
Expected Output:
LCM: 12
Q30. Write a Java program to create a nested class for swapping two numbers.
Input:
Number 1: 10, Number 2: 20
Expected Output:
Swapped numbers: 20 10
Q31. Write a Java program to use a nested class to find the even numbers in an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Even numbers: 2, 4
Q32. Write a Java program to demonstrate a nested class to calculate the perimeter of a rectangle.
Input:
Length: 5, Width: 3
Expected Output:
Perimeter of rectangle: 16
Q33. Write a Java program to create a nested class that checks whether a year is a leap year.
Input:
Year: 2024
Expected Output:
2024 is a leap year
Q34. Write a Java program to use a nested class to find the sum of squares of an array of integers.
Input:
Array: [1, 2, 3]
Expected Output:
Sum of squares: 14
Q35. Write a Java program to demonstrate the use of nested class to find the sum of digits of a number.
Input:
Number: 123
Expected Output:
Sum of digits: 6
Q36. Write a Java program to create a nested class that calculates the area of a rectangle.
Input:
Length: 5, Width: 3
Expected Output:
Area of rectangle: 15
Q37. Write a Java program to use a nested class to print the powers of 2 up to a given limit.
Input:
Limit: 10
Expected Output:
Powers of 2: 1 2 4 8
Q38. Write a Java program to create a nested class to check if a number is a perfect number.
Input:
Number: 6
Expected Output:
6 is a perfect number
Q39. Write a Java program to use a nested class to sort an array of strings alphabetically.
Input:
Array: ["apple", "banana", "orange"]
Expected Output:
Sorted Array: [apple, banana, orange]
Q40. Write a Java program to create a nested class that computes the average of an array of integers.
Input:
Array: [2, 4, 6, 8]
Expected Output:
Average: 5.0
Q41. Write a Java program to use a nested class to calculate the length of the hypotenuse in a right triangle.
Input:
Side A: 3, Side B: 4
Expected Output:
Hypotenuse: 5
Q42. Write a Java program to create a nested class to count the occurrences of each character in a string.
Input:
String: hello
Expected Output:
Occurrences: h: 1, e: 1, l: 2, o: 1
Q43. Write a Java program to use a nested class to determine the number of vowels in a string.
Input:
String: java programming
Expected Output:
Vowels count: 5
Q44. Write a Java program to create a nested class that checks if a number is an automorphic number.
Input:
Number: 25
Expected Output:
25 is an automorphic number
Q45. Write a Java program to use a nested class to check if a string is an anagram of another string.
Input:
String 1: listen, String 2: silent
Expected Output:
The strings are anagrams
Q46. Write a Java program to create a nested class to find the sum of the digits of a number.
Input:
Number: 145
Expected Output:
Sum of digits: 10
Q47. Write a Java program to use a nested class to print the multiplication table of a number.
Input:
Number: 3
Expected Output:
3 6 9 12 15
Q48. Write a Java program to create a nested class that checks if a string is a substring of another string.
Input:
String 1: Java programming, String 2: prog
Expected Output:
prog is a substring
Q49. Write a Java program to use a nested class to reverse an array.
Input:
Array: [1, 2, 3]
Expected Output:
Reversed Array: [3, 2, 1]
Q50. Write a Java program to create a nested class to find the largest number in a list.
Input:
List: [2, 5, 8, 3, 1]
Expected Output:
Largest number: 8
Q51. Write a Java program to demonstrate the use of an inner class for calculating the square root of a number.
Input:
Number: 16
Expected Output:
Square root: 4.0
Q52. Write a Java program to create a nested class for calculating the volume of a cylinder.
Input:
Radius: 3, Height: 5
Expected Output:
Volume of cylinder: 141.37
Q53. Write a Java program to use a nested class to determine if a number is a palindrome.
Input:
Number: 121
Expected Output:
121 is a palindrome
Q54. Write a Java program to create a nested class for calculating the area of a triangle.
Input:
Base: 4, Height: 6
Expected Output:
Area of triangle: 12
Q55. Write a Java program to use a nested class to print the even numbers in a list.
Input:
List: [1, 2, 3, 4, 5]
Expected Output:
Even numbers: 2, 4
Q56. Write a Java program to demonstrate how nested classes can help manage a simple employee list.
Input:
Employee 1: John, Age: 30
Employee 2: Jane, Age: 25
Expected Output:
Employee list:
John, 30
Jane, 25
Q57. Write a Java program to create a nested class to check if a number is divisible by 3 and 5.
Input:
Number: 15
Expected Output:
15 is divisible by both 3 and 5
Q58. Write a Java program to use a nested class to calculate the sum of an arithmetic series.
Input:
First term: 1, Common difference: 2, Number of terms: 5
Expected Output:
Sum of series: 25
Q59. Write a Java program to create a nested class for calculating the sum of odd numbers in a list.
Input:
List: [1, 2, 3, 4, 5]
Expected Output:
Sum of odd numbers: 9
Q60. Write a Java program to use a nested class for converting a string to lowercase.
Input:
String: HELLO
Expected Output:
Lowercase string: hello
Q61. Write a Java program to demonstrate the use of a nested class for checking if a number is divisible by a given divisor.
Input:
Number: 20, Divisor: 4
Expected Output:
20 is divisible by 4
Q62. Write a Java program to create a nested class for checking if a string contains only alphabets.
Input:
String: Hello
Expected Output:
String contains only alphabets
Q63. Write a Java program to use a nested class to find the median of an array.
Input:
Array: [1, 3, 2, 5, 4]
Expected Output:
Median: 3
Q64. Write a Java program to create a nested class to convert a decimal number to binary.
Input:
Number: 10
Expected Output:
Binary: 1010
Q65. Write a Java program to use a nested class to calculate the area of a square.
Input:
Side length: 4
Expected Output:
Area of square: 16
Q66. Write a Java program to create a nested class for finding the sum of cubes of numbers in a range.
Input:
Range: 1 to 3
Expected Output:
Sum of cubes: 36
Q67. Write a Java program to use a nested class to check if a string contains a digit.
Input:
String: java123
Expected Output:
String contains a digit
Q68. Write a Java program to create a nested class for sorting a list of integers.
Input:
List: [10, 5, 3, 8]
Expected Output:
Sorted list: [3, 5, 8, 10]
Q69. Write a Java program to use a nested class to calculate the greatest common divisor (GCD) of two numbers.
Input:
Number 1: 14, Number 2: 21
Expected Output:
GCD: 7
Q70. Write a Java program to create a nested class to find the minimum number in a list.
Input:
List: [10, 4, 6, 2]
Expected Output:
Minimum number: 2
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!