Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Practice questions of Java Programming_UPDATED

Uploaded by

helloworld85585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Practice questions of Java Programming_UPDATED

Uploaded by

helloworld85585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

School of Computer Science & Engineering

Subject Name: Java Programming

Practice question set 1

1. Explain the key features of Object-Oriented Programming (OOP) and how they are
implemented in Java. Provide examples for at least two features.
2. Differentiate between JDK, JVM, and JRE. Illustrate their roles in Java program
execution.
3. Describe the concept of type casting in Java. Differentiate between implicit and
explicit type casting with examples.
4. Write a Java program to demonstrate the use of access specifiers by creating a class
with fields and methods having different access levels. Explain the behavior of each
access specifier.
5. Develop a Java program to showcase method overloading by creating methods with the
same name but different parameter lists. Include at least three overloaded methods.
6. Create a Java class with both static and non-static members. Write a program to call
these members from the main method and explain the difference between their
behavior.
7. Design a Java program that accepts a string as a command-line argument and checks
whether it is a palindrome. Use StringBuffer or StringBuilder in your solution.
8. Develop a Java program that creates a user-defined exception to handle invalid input
for a specific scenario (e.g., age below 18 for voting). Include a try-catch block to
handle the exception.
9. Explain the difference between `abstract` classes and `interfaces` in Java. Write a Java
program to implement an `Interface` and demonstrate how a class can implement
multiple interfaces. Include methods from each interface and call them in the main
method.
10. Create a Java program to demonstrate the use of `ArrayList` and `HashMap` from the
Collections Framework. Perform basic operations such as adding, removing, and
iterating over elements.
11. . Write a Java program to read data from a file using `BufferedReader` and write the
processed output to another file using `BufferedWriter`. Include exception handling
mechanisms to manage possible I/O errors.
12. . Implement a Java program that demonstrates the difference between byte streams and
character streams by reading and writing a text file using both approaches.
13. Explain how object serialization works in Java.
14. . Design a Java program to demonstrate linked structures by implementing a simple
singly linked list with operations like add, delete, and display.
15. Write a Java program to implement exception handling strategies by demonstrating the
use of `try-catch-finally` and `throws` keywords with a scenario involving division by
zero.
16. Develop a Java program to simulate a queue using the `LinkedList` class from the
Collections Framework. Include operations like enqueue, dequeue, and display.
17. . Write a Java program to create multiple threads and assign different priorities to
them. Observe and explain the impact of thread priority on their execution order.
18. Write a Java program to demonstrate the use of generic collections by implementing a
`HashMap` to store and retrieve student details (e.g., roll number and name). Include
operations to add, remove, and search for students.
19. What is method overriding in Java? What is basic Difference between Overriding and
Overloading?
20. Describe the steps involved in creating and using a user-defined exception in Java.
Write a Java program to handle an invalid bank transaction using a custom exception
class.
21. Differentiate between `Set` and `List` in the Java Collections Framework. Write a
program to demonstrate their usage by storing and iterating over elements.
22. . Explain the concept of object serialization in Java. Write a program to serialize and
deserialize an object of a custom class that stores employee details.
23. Create a menu based calculator to perform various operations
24. Write a Java program to design the patterns

25. Write a java program to take temperature at command line in Celsius and convert in
Fahrenheit.
26. Write a java program to find the number of and sum of all integers greater than 50 and
less than 100 and are prime numbers.
27. Write a Java program to insert an element (specific position) into an array.
28. Write a Java program to remove duplicate elements from an array.
29. Write a Java program to find the second largest element in a 2D array.
30. Write a Java recursive method to check if a given array is sorted in ascending order.
31. Write a Java program to create a class called Circle with a private instance variable
radius. Provide public getter and setter methods to access and modify the radius
variable. However, provide two methods called calculateArea() and
calculatePerimeter() that return the calculated area and perimeter based on the current
radius value.
32. Write a Java program to compare two strings lexicographically.
Two strings are lexicographically equal if they are the same length and contain the
same characters in the same positions.
33. Write a java program to take a string from user in which words are separated using
spaces. Tokenize the string using space delimiter and find whether India word is there
in the token list or not.

34. Write a Java program that creates a bank account with concurrent deposits and
withdrawals using threads.
35. Design a class named Circle. Construct three circle objects with radius 3.0,
3.2 4.1 and display the radius and area of each. A no-arg constructor set the default
value of radius to 1. A getArea() function is used to return the area of circle. Now
implementthe class.
36. Write a Java program to create a class with methods to search for flights and hotels,
and to book and cancel reservations.
37. Write a Java programming to create a banking system with three classes - Bank,
Account, SavingsAccount, and CurrentAccount. The bank should have a list of
accounts and methods for adding them. Accounts should be an interface with methods
to deposit, withdraw, calculate interest, and view balances. SavingsAccount and
CurrentAccount should implement the Account interface and have their own unique
methods.
38. Write a Java program to create an abstract class Employee with abstract methods
calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that
extend the Employee class and implement the respective methods to calculate salary
and display information for each role.
39. Write a Java program to create a method that takes an integer as a parameter and
throws an exception if the number is odd.
40. Write a JAVA Program to demonstrate Constructor overloading and Method
overloading. Also access parent class constructor in child class
41. Write a JAVA program which has
A Class called Account that creates account with 500Rs minimum balance, a deposit()
method to deposit amount, a withdraw() method to withdraw amount and also throws
LessBalanceException if an account holder tries to withdraw money which makes the
balance become less than 500Rs.
A Class called LessBalanceException which returns the statement that says withdraw
amount ( Rs) is not valid.
A Class which creates 2 accounts, both account deposit money and one account tries
to withdraw more money which generates a LessBalanceException take appropriate
action for the same.
42. Write a Java method that checks whether all the characters in a given string are
vowels (a, e,i,o,u) or not. Return true if each character in the string is a vowel,
otherwise return false.
43. Write a Java program in which inherit one abstract class and implement methods of 3
interfaces. One interface is containing default method and static method.
44. Create a class Student (name, roll_no, marks) with one method show() and initialize
instance variables using all the ways: reference, method and constructor.
45. Discuss the various access specifiers in Java. Create 2 packages P1 & P2 and create
classes Student and BTech in P1 and P2 respectively. Check the accessibility of 3
methods of the package p1 into package p2. Access specifier of one method is
private, one is protected and third one is default.
46. Write a Java program to get a substring of a given string at two specified positions.
47. What will be the output of the following code

Inner Class

class X{

static int x = 3131;

static class Y

static int y = x++;

static class Z

static int z = y++;

public class MainClass

public static void main(String[] args)

System.out.println(X.x);

System.out.println(X.Y.y);

System.out.println(X.Y.Z.z);

48. Write a Java program to create an abstract class Employee with abstract methods
calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that
extend the Employee class and implement the respective methods to calculate salary
and display information for each role.
49. Write a Java program to create an interface Shape with the getArea() method. Create
three classes Rectangle, Circle, and Triangle that implement the Shape interface.
Implement the getArea() method for each of the three classes.
50. Write a Java programming to create a banking system with three classes - Bank,
Account, SavingsAccount, and CurrentAccount. The bank should have a list of
accounts and methods for adding them. Accounts should be an interface with methods
to deposit, withdraw, calculate interest, and view balances. SavingsAccount and
CurrentAccount should implement the Account interface and have their own unique
methods.
51. Write a Java program to create a method that takes an integer as a parameter and
throws an exception if the number is odd.
52. Create a user defined exception “UnderageforVoting” exception and throw it when
voter’s age is below 18.
53. Write a Java program that creates a bank account with concurrent deposits and
withdrawals using threads.
54. Write a Java program using Anonymous class and override method in anonymous
class.
55. What will be the output of the following code?
class MOClassD {
private int x= 1;
class MIClass { // inner class definition
public void seeOuter () {
System.out.println("Outer Value of x is :" + x);
}
} // close inner class definition

public static void main(String args[]){


MOClassD.MIClass inner = new MOClassD().new MIClass();
inner. seeOuter(); }

}
56. You are developing a banking application that requires a BankAccount class. This class should
include a static variable interestRate, which is the same for all accounts, and a final variable
accountNumber, which is unique for each account and cannot be changed after the account is
created.
Tasks:
 Implement the BankAccount class with the interestRate as a static variable and
accountNumber as a final variable.
 Write a method calculateInterest() that calculates the interest based on the balance and
interestRate. This method should be accessible to all instances but must refer to the
shared static interestRate.
 Analyze the significance of using final and static in this context. Explain how these
modifiers impact the behavior of the BankAccount class, focusing on immutability,
shared resources, and performance.
57. Consider a scenario where you need to implement a utility class MathUtils that provides
common mathematical operations such as finding the maximum of two numbers, the power of
a number, and calculating factorials. This class should be designed in such a way that it cannot
be inherited, and all the methods should be static since they belong to the class rather than any
specific instance.
Tasks:
a. Design the MathUtils class, making sure it cannot be subclassed and all its methods
(e.g., max, power, factorial) are static.
b. Explain why making the methods static is appropriate for this utility class. Discuss the
advantages and any potential limitations.
c. Analyze the implications of marking the class as final. How does this decision affect the
design and future extensibility of the class?
58. You are tasked with developing a software for a university's course registration system. The
system includes a Student class and a Course class. The Student class has attributes like name,
studentID, and registeredCourses. The Course class has attributes like courseName, courseID,
and maxCapacity.
 Public Method:registerForCourse(Course course) in the Student class that allows a student
to register for a course if they meet the criteria.
 Protected Method:checkEligibility(Course course) in the Student class that checks whether
the student is eligible to register for a course (e.g., they haven’t exceeded their credit limit).
 Private Method:addCourseToStudentRecord(Course course) in the Student class that adds
the course to the student's record and is only accessible internally.
59. Develop a Java program to create and manage a thread that performs file I/O operations (e.g.,
reading data from one file and writing it to another). Handle exceptions appropriately and
ensure the program supports multithreading.
60. Develop a Java program to demonstrate thread synchronization using the `synchronized`
keyword. Create a scenario where multiple threads access a shared resource, and synchronize
the access to prevent data inconsistency

You might also like