[Go to site: main page, start]

0% found this document useful (0 votes)
4 views14 pages

Java Practical Questions

The document outlines a comprehensive list of programming tasks in Java, categorized into various sections such as Basic Programs, Control Statements, Arrays, Strings, Object-Oriented Programming, Exception Handling, Collections, File Handling, Advanced Topics, and more. Each section contains specific programming challenges aimed at enhancing Java skills, including tasks related to algorithms, data structures, and GUI development. Additionally, it includes entry-level, second-level, and core Java practical questions to further assess and develop programming proficiency.

Uploaded by

Ajith Kumar A
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)
4 views14 pages

Java Practical Questions

The document outlines a comprehensive list of programming tasks in Java, categorized into various sections such as Basic Programs, Control Statements, Arrays, Strings, Object-Oriented Programming, Exception Handling, Collections, File Handling, Advanced Topics, and more. Each section contains specific programming challenges aimed at enhancing Java skills, including tasks related to algorithms, data structures, and GUI development. Additionally, it includes entry-level, second-level, and core Java practical questions to further assess and develop programming proficiency.

Uploaded by

Ajith Kumar A
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

Basic Program

1. Write a program to print "Hello, World!" in Java.


2. Write a program to add two numbers.
3. Write a program to find the largest of two numbers.
4. Write a program to check if a number is even or odd.
5. Write a program to swap two numbers without using a temporary variable.
6. Write a program to calculate the factorial of a number.
7. Write a program to print the Fibonacci series up to n terms.
8. Write a program to reverse a number.
9. Write a program to check if a number is a palindrome.
10. Write a program to check if a number is prime.

Control Statements

11. Write a program to find the largest among three numbers using if-else.
12. Write a program to demonstrate the use of a switch statement.
13. Write a program to display all prime numbers between 1 and 100.
14. Write a program to print a multiplication table.
15. Write a program to print numbers from 1 to 10 using a while loop.
16. Write a program to calculate the sum of the first n natural numbers.
17. Write a program to find the greatest common divisor (GCD) of two numbers.
18. Write a program to find the least common multiple (LCM) of two numbers.
19. Write a program to print the sum of even and odd numbers between 1 and 100.
20. Write a program to print the pattern:

Note: All star pattern (eg. start pattern basic. Must be include all the type of pattern in the
varient)

Arrays

21. Write a program to find the largest element in an array.


22. Write a program to find the smallest element in an array.
23. Write a program to calculate the sum of elements in an array.
24. Write a program to reverse an array.
25. Write a program to sort an array in ascending order.
26. Write a program to merge two arrays.
27. Write a program to find the second largest element in an array.
28. Write a program to remove duplicate elements from an array.
29. Write a program to count occurrences of a number in an array.
30. Write a program to perform binary search on a sorted array.
Strings

31. Write a program to reverse a string.


32. Write a program to check if a string is a palindrome.
33. Write a program to count vowels and consonants in a string.
34. Write a program to find the frequency of characters in a string.
35. Write a program to replace all occurrences of a character in a string.
36. Write a program to find the length of a string without using the length() method.
37. Write a program to compare two strings.
38. Write a program to convert a string to uppercase and lowercase.
39. Write a program to split a string into an array of substrings.
40. Write a program to remove spaces from a string.

Object-Oriented Programming

41. Write a program to create a class and an object.


42. Write a program to demonstrate the use of a constructor.
43. Write a program to demonstrate method overloading.
44. Write a program to demonstrate method overriding.
45. Write a program to implement inheritance.
46. Write a program to implement multiple inheritance using interfaces.
47. Write a program to demonstrate the use of the this keyword.
48. Write a program to demonstrate the use of the super keyword.
49. Write a program to create and use a static method.
50. Write a program to demonstrate encapsulation.

Exception Handling

51. Write a program to handle an ArithmeticException.


52. Write a program to handle an ArrayIndexOutOfBoundsException.
53. Write a program to handle a NullPointerException.
54. Write a program to demonstrate the use of try, catch, and finally.
55. Write a program to throw a custom exception.

Collections

56. Write a program to create and iterate over an ArrayList.


57. Write a program to demonstrate the use of a HashMap.
58. Write a program to implement a LinkedList.
59. Write a program to demonstrate the use of a HashSet.
60. Write a program to sort a list using Collections.
File Handling

61. Write a program to read a file.


62. Write a program to write data to a file.
63. Write a program to append data to a file.
64. Write a program to copy the contents of one file to another.
65. Write a program to count the number of lines in a file.

Advanced Topics

66. Write a program to implement a simple calculator using switch-case.


67. Write a program to demonstrate the use of threads.
68. Write a program to demonstrate synchronization.
69. Write a program to demonstrate inter-thread communication.
70. Write a program to implement a basic banking system.

Sorting and Searching Algorithms

71. Write a program to implement bubble sort.


72. Write a program to implement selection sort.
73. Write a program to implement insertion sort.
74. Write a program to implement merge sort.
75. Write a program to implement quicksort.

Miscellaneous

76. Write a program to check if a year is a leap year.


77. Write a program to calculate the power of a number.
78. Write a program to find the square root of a number without using the sqrt() function.
79. Write a program to convert a decimal number to binary.
80. Write a program to find the sum of digits of a number.

Intermediate Concepts

81. Write a program to demonstrate the Singleton design pattern.


82. Write a program to demonstrate polymorphism.
83. Write a program to implement a stack.
84. Write a program to implement a queue.
85. Write a program to create a lambda expression.
Java API and Utilities

86. Write a program to demonstrate the use of Java's Date and Time API.
87. Write a program to generate random numbers.
88. Write a program to demonstrate the use of the Math class.
89. Write a program to read input using Scanner.
90. Write a program to demonstrate the use of enums.

Networking

91. Write a program to create a simple client-server application.


92. Write a program to send and receive messages over a socket.

JDBC (Database)

93. Write a program to connect to a database.


94. Write a program to execute an SQL query.
95. Write a program to insert data into a database.
96. Write a program to update data in a database.
97. Write a program to delete data from a database.

GUI (Swing/JavaFX)

98. Write a program to create a simple GUI for a calculator.


99. Write a program to demonstrate event handling in Java.
100. Write a program to create a simple login form.
Entry level questions:

Basic Java Programs

1. Hello World: Write a program to print "Hello, World!" on the console.


2. Even or Odd: Write a program to check if a given number is even or odd.
3. Largest of Three Numbers: Write a program to find the largest of three numbers.
4. Leap Year Check: Write a program to check if a given year is a leap year.
5. Sum of Digits: Write a program to calculate the sum of the digits of a number.

Loops and Control Statements

6. Fibonacci Series: Write a program to print the Fibonacci series up to n terms.


7. Prime Number: Write a program to check if a number is prime.
8. Multiplication Table: Write a program to print the multiplication table of a given number.
9. Reverse a Number: Write a program to reverse a given integer.
10. Factorial: Write a program to calculate the factorial of a number.

Arrays

11. Array Initialization: Write a program to declare, initialize, and print an array.
12. Find Largest Element: Write a program to find the largest element in an array.
13. Sum of Array Elements: Write a program to calculate the sum of all elements in an
array.
14. Reverse an Array: Write a program to reverse an array.
15. Sort an Array: Write a program to sort an array using the bubble sort algorithm.

Strings

16. Reverse a String: Write a program to reverse a string.


17. Palindrome String: Write a program to check if a string is a palindrome.
18. Count Vowels and Consonants: Write a program to count the number of vowels and
consonants in a string.
19. String Length: Write a program to find the length of a string without using the
length() method.
20. Substring Check: Write a program to check if a substring exists in a given string.
Object-Oriented Programming (OOP)

21. Create a Class: Write a program to define a Person class with attributes name and
age. Create and print the details of a person.
22. Bank Account Simulation: Create a BankAccount class with methods to deposit and
withdraw money.
23. Inheritance: Create a Vehicle class and extend it into Car and Bike classes with
additional features.
24. Encapsulation: Implement a class Student with private fields for name and
rollNumber and public getter and setter methods.
25. Constructor Example: Write a program to create a constructor to initialize the attributes
of a class.

Basic Collections

26. ArrayList Example: Write a program to store and retrieve student names using an
ArrayList.
27. HashMap Example: Write a program to store and retrieve employee details using a
HashMap.
28. Iterate List: Write a program to iterate through a list using a for loop and an
iterator.
29. Set Example: Write a program to demonstrate the uniqueness property of a HashSet.
30. Sort a List: Write a program to sort a list of integers using [Link]().

File Handling

31. Write to a File: Write a program to create a file and write text into it.
32. Read from a File: Write a program to read the contents of a text file.
33. Copy File: Write a program to copy the content of one file to another.
34. Count Lines in a File: Write a program to count the number of lines in a file.
35. Check File Exists: Write a program to check if a file exists at a given path.
Exception Handling

36. Division by Zero: Write a program to handle ArithmeticException when dividing a


number by zero.
37. Array Index Out of Bounds: Write a program to handle an
ArrayIndexOutOfBoundsException.
38. File Not Found: Write a program to handle a FileNotFoundException.
39. Null Pointer Exception: Write a program to handle a NullPointerException.
40. Custom Exception: Create a custom exception and demonstrate its use in a program.

Basic Algorithms

41. Binary Search: Write a program to implement binary search on a sorted array.
42. Sum of N Numbers: Write a program to find the sum of the first n natural numbers.
43. Armstrong Number: Write a program to check if a number is an Armstrong number.
44. Number Pyramid: Write a program to print a pyramid pattern of numbers.
45. GCD and LCM: Write a program to calculate the GCD and LCM of two numbers.

Basic Java Features

46. Static Method: Write a program to demonstrate the use of static methods in a class.
47. Overloading Methods: Write a program to demonstrate method overloading.
48. Switch Case: Write a program to perform basic calculator operations using a switch
statement.
49. Command-Line Arguments: Write a program to read and print command-line
arguments.
50. Random Numbers: Write a program to generate and print 10 random numbers.
Second level Questions:

Core Java Practical Questions

1. Reverse an Integer: Write a program to reverse the digits of an integer without


using a string.
2. Armstrong Number: Write a program to check if a given number is an Armstrong
number.
3. Factorial Using Recursion: Implement a program to calculate the factorial of a
number using recursion.
4. Custom Sorting: Write a program to sort an array of integers without using built-in
sorting methods.
5. Number to Words: Write a program to convert a number into its equivalent words
(e.g., 123 -> "One Hundred Twenty-Three").

Object-Oriented Programming (OOP)

6. Bank Account Management: Create a class BankAccount with methods for


deposit, withdrawal, and checking the balance. Implement validation to ensure no
overdrafts.
7. Inheritance and Polymorphism: Create a base class Shape with a method
area(). Extend it into classes Circle, Rectangle, and Triangle, overriding
the area() method.
8. Encapsulation: Implement a class Employee with private attributes like name,
age, and salary. Provide public getter and setter methods.
9. Abstract Class Example: Create an abstract class Vehicle with methods like
start() and stop(). Implement subclasses Car and Bike.
10. Interface Example: Define an interface Playable with a method play().
Create classes Piano and Guitar that implement this interface.
Strings

11. Anagrams: Write a program to check if two strings are anagrams of each other.
12. Duplicate Characters: Write a program to find duplicate characters in a string.
13. Palindrome Check: Implement a function to check if a given string is a
palindrome.
14. Remove Vowels: Write a program to remove all vowels from a given string.
15. Word Frequency: Write a program to count the frequency of each word in a
string.

Collections

16. Custom Comparator: Write a program to sort a list of custom objects (e.g.,
Employee) by name and salary using Comparator.
17. Frequency Count: Use a HashMap to count the occurrences of each element in
an array.
18. Set Operations: Write a program to perform union, intersection, and difference
operations on two sets.
19. Find Duplicates: Write a program to find and remove duplicates from a List.
20. Top K Frequent Elements: Use a PriorityQueue to find the k most frequent
elements in an array.

Multithreading

21. Producer-Consumer Problem: Implement the producer-consumer problem using


wait() and notify().
22. Thread Synchronization: Write a program to demonstrate thread synchronization
using the synchronized keyword.
23. Thread Communication: Create two threads: one prints odd numbers, and the
other prints even numbers in sequence.
24. Deadlock Simulation: Write a program to simulate a deadlock scenario and
explain how to resolve it.
25. Callable and Future: Implement a program using Callable to calculate the sum
of numbers in a range and retrieve the result using Future.
File Handling

26. Read and Write Files: Write a program to read from one file and write its content
to another.
27. File Line Count: Write a program to count the number of lines, words, and
characters in a text file.
28. Serialization: Implement object serialization and deserialization for a class.
29. Directory Traversal: Write a program to list all files and directories in a given path.
30. Log File Analysis: Parse a log file to extract and display specific information, such
as errors.

Java 8 and Functional Programming

31. Stream API: Write a program to filter and sort a list of numbers using the Stream
API.
32. Lambda Expressions: Use a lambda expression to iterate through a list and print
each element.
33. Custom Collector: Implement a custom collector to group a list of strings by their
lengths.
34. Optional Example: Use Optional to avoid NullPointerException while
accessing an object’s properties.
35. Method References: Write a program to demonstrate the use of method
references in a functional interface.

Exception Handling

36. Custom Exception: Write a program to create a custom exception class and
throw it when a specific condition is met.
37. Exception Chaining: Demonstrate exception chaining with multiple levels of
exceptions.
38. Resource Management: Use a try-with-resources block to read from a file
and ensure proper resource closure.
39. Divide by Zero: Write a program to handle ArithmeticException when
dividing a number by zero.
40. Multiple Catch Blocks: Implement a program with multiple catch blocks to
handle different exceptions.
Database (JDBC)

41. CRUD Operations: Write a Java program to perform basic CRUD operations on a
database using JDBC.
42. Batch Processing: Demonstrate batch processing using JDBC to insert multiple
records efficiently.
43. Transaction Management: Implement a program that demonstrates JDBC
transactions with commit and rollback.
44. Stored Procedure: Write a program to call a stored procedure from Java.
45. Database Connection Pooling: Implement a simple connection pool using JDBC.

Sorting and Searching Algorithms

46. Binary Search: Write a program to implement binary search on a sorted array.
47. Merge Sort: Implement merge sort for an array of integers.
48. Quick Sort: Write a program to implement quicksort.
49. Bubble Sort: Implement bubble sort and count the number of swaps.
50. Custom Sorting: Write a program to sort an array of strings by their lengths.

Advanced Practical Problems

51. LRU Cache: Implement a Least Recently Used (LRU) cache using a
LinkedHashMap.
52. String Permutations: Generate all permutations of a given string.
53. Graph Traversal: Write a program to perform Depth-First Search (DFS) and
Breadth-First Search (BFS) on a graph.
54. Knapsack Problem: Solve the 0/1 Knapsack problem using dynamic
programming.
55. Sudoku Solver: Implement a program to solve a Sudoku puzzle.

Design Patterns

56. Singleton Pattern: Implement a thread-safe Singleton class.


57. Factory Pattern: Write a program to demonstrate the Factory design pattern.
58. Observer Pattern: Implement the Observer design pattern.
59. Builder Pattern: Create a class using the Builder design pattern.
60. Strategy Pattern: Demonstrate the use of the Strategy design pattern in a
real-world scenario.
Miscellaneous Challenges

61. Palindrome Number: Write a program to check if a number is a palindrome


without converting it to a string.
62. Matrix Multiplication: Write a program to multiply two matrices.
63. Custom HashMap: Implement a custom HashMap with basic operations like put,
get, and remove.
64. Circular Linked List: Implement a circular linked list with basic operations.
65. Snake Game: Develop a simple Snake game using Java Swing.
Repeated Interview questions:

Core Java and Data Structures

1. Reverse a String: Implement a function to reverse a given string without using


built-in reverse methods.
2. Check for Palindrome: Write a program to determine if a given string is a
palindrome.
3. Find Duplicate Numbers in an Array: Develop a method to identify duplicate
numbers in an array.
4. Implement Binary Search: Create a function to perform binary search on a sorted
array.
5. Merge Two Sorted Arrays: Write a program to merge two sorted arrays into a
single sorted array.

Object-Oriented Programming (OOP)

6. Design Patterns: Explain and implement common design patterns such as


Singleton, Factory, and Observer.
7. Inheritance and Polymorphism: Demonstrate the use of inheritance and
polymorphism in a Java program.
8. Abstract Classes vs. Interfaces: Discuss the differences between abstract
classes and interfaces, and provide examples of when to use each.
9. Encapsulation: Illustrate encapsulation by creating a class with private fields and
public getter and setter methods.
10. Overloading and Overriding: Show examples of method overloading and method
overriding in Java.

Concurrency and Multithreading

11. Producer-Consumer Problem: Implement the producer-consumer problem using


Java threads.
12. Deadlock Simulation: Write a program to simulate a deadlock scenario and
discuss how to prevent it.
13. Thread Synchronization: Demonstrate thread synchronization using the
synchronized keyword.
14. Callable and Future: Implement a task using Callable and retrieve the result
using Future.
15. Executor Framework: Show how to use the Executor framework to manage a
pool of threads.
Java 8 and Functional Programming

16. Lambda Expressions: Use lambda expressions to sort a list of strings by their
length.
17. Stream API: Filter a list of integers to find all even numbers using the Stream
API.
18. Optional Class: Demonstrate the use of the Optional class to handle potential
null values.
19. Method References: Show how to use method references as a shorthand for
lambda expressions.
20. Default Methods in Interfaces: Explain and provide an example of default
methods in interfaces.

Data Handling and File I/O

21. Read and Write Files: Write a program to read from one file and write its content
to another file.
22. Serialize and Deserialize Objects: Demonstrate how to serialize a Java object to
a file and then deserialize it back into an object.
23. Count Word Frequency in a File: Develop a program to count the frequency of
each word in a text file.
24. Directory Traversal: Write a program to list all files and directories within a given
directory.
25. File Encryption and Decryption: Implement basic file encryption and decryption
using Java's cryptography API.

You might also like