Practice 70 Java Static Members Programming Questions
Q1. Write a Java program to demonstrate the use of a static variable that counts the number of instances of a class.
Expected Output:
Number of instances created: 1
Number of instances created: 2
Q2. Write a Java program to create a static method that prints a message without the need for an object of the class.
Expected Output:
This is a static method.
Q3. Write a Java program to demonstrate the use of a static block that initializes a static variable.
Expected Output:
Static variable initialized to 10
Q4. Write a Java program to create a static variable that keeps track of the total number of employees in a company.
Input:
Employee 1: John
Employee 2: Alice
Expected Output:
Total number of employees: 2
Q5. Write a Java program to demonstrate the use of a static method to calculate the factorial of a number.
Input:
Enter a number: 5
Expected Output:
Factorial of 5 is: 120
Q6. Write a Java program to create a class with a static variable and a static method that returns the value of that variable.
Expected Output:
Value of static variable is: 10
Q7. Write a Java program to demonstrate how a static variable is shared among all instances of a class.
Expected Output:
Instance 1: 100
Instance 2: 100
Q8. Write a Java program to use a static method to calculate the sum of two numbers.
Input:
Enter first number: 4
Enter second number: 5
Expected Output:
Sum of 4 and 5 is: 9
Q9. Write a Java program to create a class with a static variable that is incremented every time a new object is created.
Input:
Create first object
Create second object
Expected Output:
Number of objects created: 1
Number of objects created: 2
Q10. Write a Java program to demonstrate the use of a static method that does not require an object to be called.
Expected Output:
Static method called without an object.
Q11. Write a Java program to create a static method that returns the square of a given number.
Input:
Enter number: 6
Expected Output:
Square of 6 is: 36
Q12. Write a Java program to create a class with a static counter that tracks how many times a specific method is called.
Expected Output:
Method called: 1 time
Method called: 2 times
Q13. Write a Java program to demonstrate how to access static members using the class name without creating an object.
Expected Output:
Static value accessed: 50
Q14. Write a Java program to create a class with a static constant and print its value.
Expected Output:
Value of constant is: 3.14
Q15. Write a Java program to create a static method that checks whether a given number is prime or not.
Input:
Enter number: 7
Expected Output:
7 is a prime number.
Q16. Write a Java program to use a static block to initialize a static array and display its contents.
Expected Output:
Array: [10, 20, 30, 40, 50]
Q17. Write a Java program to demonstrate the use of a static method for string concatenation.
Input:
Enter first string: Hello
Enter second string: World
Expected Output:
Concatenated string: HelloWorld
Q18. Write a Java program to create a static method that checks whether a given string is a palindrome or not.
Input:
Enter string: madam
Expected Output:
madam is a palindrome.
Q19. Write a Java program to create a static method that reverses a given string.
Input:
Enter string: hello
Expected Output:
Reversed string: olleh
Q20. Write a Java program to demonstrate the use of static method to find the largest of three numbers.
Input:
Enter first number: 10
Enter second number: 20
Enter third number: 15
Expected Output:
Largest number is: 20
Q21. Write a Java program to use a static method to calculate the average of an array of numbers.
Input:
Array: [5, 10, 15, 20]
Expected Output:
Average is: 12.5
Q22. Write a Java program to create a static method that checks if a number is even or odd.
Input:
Enter number: 7
Expected Output:
7 is odd.
Q23. Write a Java program to create a static method that converts temperature from Celsius to Fahrenheit.
Input:
Enter Celsius: 30
Expected Output:
Fahrenheit: 86.0
Q24. Write a Java program to create a static method that converts kilometers to miles.
Input:
Enter kilometers: 5
Expected Output:
Miles: 3.1
Q25. Write a Java program to demonstrate a static method that performs addition of all elements in an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Sum of elements: 15
Q26. Write a Java program to demonstrate how to increment a static variable inside a static method.
Expected Output:
Incremented value: 1
Incremented value: 2
Q27. Write a Java program to create a static method that returns the reverse of a given number.
Input:
Enter number: 12345
Expected Output:
Reversed number: 54321
Q28. Write a Java program to demonstrate a static variable that is shared among multiple objects.
Expected Output:
Object 1 count: 1
Object 2 count: 2
Q29. Write a Java program to use a static variable to store the count of objects created in a class.
Expected Output:
Number of objects created: 3
Q30. Write a Java program to demonstrate a static block for static variable initialization.
Expected Output:
Static initialization block executed.
Q31. Write a Java program to create a static method that multiplies two numbers.
Input:
Enter first number: 5
Enter second number: 6
Expected Output:
Product is: 30
Q32. Write a Java program to use a static method to find the minimum of three numbers.
Input:
Enter first number: 2
Enter second number: 7
Enter third number: 5
Expected Output:
Minimum number is: 2
Q33. Write a Java program to demonstrate how a static variable is accessed from another class.
Expected Output:
Static value from another class: 100
Q34. Write a Java program to create a static method that checks if a number is prime.
Input:
Enter number: 11
Expected Output:
11 is a prime number.
Q35. Write a Java program to create a static method to calculate the cube of a number.
Input:
Enter number: 3
Expected Output:
Cube of 3 is: 27
Q36. Write a Java program to create a static method that finds the power of a number.
Input:
Enter base: 2
Enter exponent: 3
Expected
Output:
Power is: 8
Q37. Write a Java program to create a static method that returns the smallest number in an array.
Input:
Array: [3, 1, 5, 9]
Expected Output:
Smallest number is: 1
Q38. Write a Java program to create a static method that calculates the square root of a number.
Input:
Enter number: 16
Expected Output:
Square root of 16 is: 4.0
Q39. Write a Java program to demonstrate the use of static method to reverse an array of integers.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Reversed array: [4, 3, 2, 1]
Q40. Write a Java program to use a static method to return the Fibonacci series up to a given number.
Input:
Enter limit: 5
Expected Output:
Fibonacci series: 0, 1, 1, 2, 3
Q41. Write a Java program to use a static method to print all the even numbers between 1 and a given number.
Input:
Enter limit: 10
Expected Output:
Even numbers: 2, 4, 6, 8, 10
Q42. Write a Java program to create a static method that counts the number of vowels in a string.
Input:
Enter string: Hello World
Expected Output:
Number of vowels: 3
Q43. Write a Java program to create a static method that converts binary to decimal.
Input:
Enter binary number: 1010
Expected Output:
Decimal equivalent: 10
Q44. Write a Java program to demonstrate the use of static method to check if a number is divisible by another number.
Input:
Enter first number: 10
Enter second number: 2
Expected Output:
10 is divisible by 2.
Q45. Write a Java program to create a static method that finds the greatest common divisor (GCD) of two numbers.
Input:
Enter first number: 12
Enter second number: 15
Expected Output:
GCD is: 3
Q46. Write a Java program to create a static method that returns the least common multiple (LCM) of two numbers.
Input:
Enter first number: 4
Enter second number: 5
Expected Output:
LCM is: 20
Q47. Write a Java program to demonstrate how a static variable can be accessed and modified in a different class.
Expected Output:
Static value: 30
Static value modified: 50
Q48. Write a Java program to use a static method to convert an integer to hexadecimal.
Input:
Enter integer: 255
Expected Output:
Hexadecimal: FF
Q49. Write a Java program to use a static method to convert an integer to octal.
Input:
Enter integer: 8
Expected Output:
Octal: 10
Q50. Write a Java program to create a static method to find the length of a string.
Input:
Enter string: Java
Expected Output:
Length of the string: 4
Q51. Write a Java program to create a static method that checks if a string is empty.
Input:
Enter string: Hello
Expected Output:
String is not empty.
Q52. Write a Java program to create a static method that calculates the area of a circle.
Input:
Enter radius: 5
Expected Output:
Area of circle: 78.54
Q53. Write a Java program to create a static method that calculates the perimeter of a rectangle.
Input:
Enter length: 4
Enter breadth: 6
Expected Output:
Perimeter of rectangle: 20
Q54. Write a Java program to use a static method to check whether a year is a leap year.
Input:
Enter year: 2024
Expected Output:
2024 is a leap year.
Q55. Write a Java program to create a static method that checks whether a string is a valid email.
Input:
Enter email: user@example.com
Expected Output:
Valid email address.
Q56. Write a Java program to create a static method that checks if a number is Armstrong.
Input:
Enter number: 153
Expected Output:
153 is an Armstrong number.
Q57. Write a Java program to demonstrate how a static method can modify a static variable.
Expected Output:
Static variable before modification: 10
Static variable after modification: 20
Q58. Write a Java program to create a static method that checks if a number is perfect.
Input:
Enter number: 6
Expected Output:
6 is a perfect number.
Q59. Write a Java program to use a static method to calculate the perimeter of a square.
Input:
Enter side length: 5
Expected Output:
Perimeter of square: 20
Q60. Write a Java program to create a static method that returns the maximum element in an array.
Input:
Array: [2, 3, 7, 1]
Expected Output:
Maximum element: 7
Q61. Write a Java program to create a static method that finds the sum of digits in a number.
Input:
Enter number: 123
Expected Output:
Sum of digits: 6
Q62. Write a Java program to use a static method to check if a number is a perfect square.
Input:
Enter number: 16
Expected Output:
16 is a perfect square.
Q63. Write a Java program to create a static method that returns the absolute value of a number.
Input:
Enter number: -5
Expected Output:
Absolute value: 5
Q64. Write a Java program to create a static method to remove duplicate elements from an array.
Input:
Array: [1, 2, 2, 3, 4, 4, 5]
Expected Output:
Array without duplicates: [1, 2, 3, 4, 5]
Q65. Write a Java program to create a static method that swaps two numbers.
Input:
Enter first number: 3
Enter second number: 5
Expected Output:
After swapping: 5 3
Q66. Write a Java program to use a static method to check if a number is divisible by 5.
Input:
Enter number: 10
Expected Output:
10 is divisible by 5.
Q67. Write a Java program to create a static method that counts the number of consonants in a string.
Input:
Enter string: Java Programming
Expected Output:
Number of consonants: 10
Q68. Write a Java program to create a static method that calculates the area of a triangle.
Input:
Enter base: 5
Enter height: 8
Expected Output:
Area of triangle: 20
Q69. Write a Java program to use a static method to check if a number is even.
Input:
Enter number: 4
Expected Output:
4 is even.
Q70. Write a Java program to create a static method that converts a string to uppercase.
Input:
Enter string: java
Expected Output:
Uppercase string: JAVA
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!