21CSE44_OOC using Java_QBANK
21CSE44_OOC using Java_QBANK
Self-Study:
Try to Solve the question available in open web portals such as follows:
1. https://www.geeksforgeeks.org/java-programming-examples/
2. https://www.hackerrank.com/domains/java?filters%5Bstatus%5D%5B%5D=unsolved&b
adge_type=java
So on…
----------------------------------------------------------------------------------------------------------
1. List and explain the features of Java language. Or List and explain the JAVA Buzzwords.
2. Explain how Java is robust and Architectural neutral.
3. Discuss three Object-oriented principles. Or Explain Data Abstraction, Encapsulation, and
Polymorphism can be achieved in Java with a suitable example.
4. Define Byte code. How does it help the Java program achieve portability? Or How “Compile
once and Run anywhere” is implemented in the Java language
5. List down various operators available in Java and Explain any three with suitable examples.
6. Compare and explain the below two code snippets.
a. int num, den;
if(den!=0 && num/den>2 )
{}
b. int num, den;
if(den!=0 & num/den>2 )
{}
Or Explain Short-circuit logical operators and Bitwise operators.
7. With an example, explain the working of
the sum of even and odd positions separately. The total seating capacity of an exam hall
is 100. Develop a software application to perform the above operations.
c. A professor asks the students in a class to do the arithmetic operations based on a
choice with the menu-driven operation. Here, the students have been restricted to
performing one arithmetic operation only at a time. Help the students to perform the
above operations.
Requirements:
i. Display the Menu of Arithmetic operations
ii. Capture the user choice on the operations
iii. Capture the user inputs for the operation
iv. Perform the operation based on the user choice
v. Display the result to the console
2. Write a Java program for the below problem using command line arguments:
a. Ms. Sharada went to participate in a coding contest and she has been assigned to
generate different patterns using the asterisk symbol (*) one among them is to generate
Diamond Shape Pattern for a given number as shown below:
Sample input: if n = 4
Sample output:
*
***
*****
*******
*****
***
*
Requirement:
i. The number of rows the pattern is to be printed has to be captured
ii. Using control statements to generate the pattern
b. Accept a number and check as below and display the message accordingly:
i. Whether the number is divisible by 2 and 5.
ii. Whether the number is divisible by 2 but not by 5.
iii. Whether the number is divisible by 5 but not by 2.
3. Use any method to read the input and try to solve the following:
a. Write and demonstrate a Java program to initialize and display different types of
integers and floating-point variables.
b. Write a Java program to demonstrate the jump statement.
c. Write a Java program to sum only the first 5 elements of the array using the “foreach”
loop version.
d. Write a note on foreach type with an example program to calculate the average among
the elements {4, 5, 7, 8} using foreach in Java. How foreach is different from for loop?
e. Write a Java Program to implement Stack using arrays (Define Stack class with push(),
pop(), and display() member functions).
f. Write a Java program to perform the addition of two complex numbers by using the
method add() by passing an object as a parameter and displaying the result using
method display(). Initialize the real and imaginary values of the complex number using
a parameterized constructor.
g. Suppose the government of India has approached you to remove all the duplicate Adhar
no from the Adhar database. Help the government by giving a Java solution.
h. There are many companies that advertise their brand by sponsoring sports events like
T-shirts for players, the equipment needed to play the game, and so on. Likewise, the
two softball teams submitted their equipment lists to their sponsors. The equipment
lists, its costs for Team A and Team B are as follows.
As a programmer help the sponsors to calculate the total cost of the equipment for each
team they are spending for their branding.
i. In an organization, the gross salary for an employee is computed as per the below
formula
Gross salary = Basic salary + HRA + DA
After computing the gross salary, the net salary is computed using the below formula
Net salary = Gross salary – Professional tax – income tax
Requirements:
i. The Basic salary, HRA, and DA has to be captured and the gross salary has to be
computed using the above formula
ii. Professional tax has to be captured (Rs.200)
iii. Income tax is 10% of the gross salary if the gross salary is more than 50000
otherwise it will be kept minimum charges as 2% of the gross salary
iv. Net salary has to be computed using the above formula
v. Display the Net salary drawn by the employee
j. Scenario. Mr. Janardhan has joined a Company as Database Administrator and his
major role is to maintain the Employee database which includes Employee Id, Name,
Age, and Salary.
Q1: He has been given the freedom to read/store the data in the database as shown
below:
i. Read all the data by taking values from the user.
ii. Read only Employee Id from the user and store some default values as he likes to
name, age, and Salary.
iii. Read Employee Id and Name from the user and store some default values as he
likes to age and Salary.
Q2: He has also been constrained to generate some default values to the Employee data
fields before adding employee records to the database in the following fashion:
i. With default values as Id=123, Name=“Gat”, Age=25 and Salary=20000.00.
ii. With default values taken from the user as parameters.
iii. By copying already created employee records.
Using Constructors.
Q3: Use this keyword to overcome Instance Variable Hiding.
Q4: Assume he has given the criteria that, he needs to store all the records in one single
table. How he is going to access all the records which he has stored? (Hint: use an Array
of objects.)
Help Mr. Janardhan to store the records and retrieve all the records which he has
stored.
1. A customer on e-commerce orders a product online by giving both his/her company and
home address. The e-commerce site has maintained both addresses entered by a customer.
When a product has to be delivered the delivery boy working for e-commerce wants to
know the address to deliver the product. Thus, the delivery boy wants to access both the
address from his portal but there is only one column. Help him to access both addresses
without overriding the values.
2. A Professor is having marks list of her students. One of the students asked the professor to
share the marks. She/he is worried that if she/he shared the marks with the students they
may edit the marks they secured. So, help the professor by providing secure access to
students to view their marks without editing/modifying them.
3. Define an Abstract class called "Area" containing an abstract method called area ();
Implement this method in rectangle & triangle classes to find their area respectively using
Inheritance. Demonstrate the above implementation by writing a main method in a new
class called "DemoAbstractArea".
4. Every geometrical diagram will generally be called a Shape like Circle, Rectangle, Square,
cube, and so on. Where each shape will have some of the features like the number of sides,
area, volume, circumference, etc. Demonstrate this feature by creating a base class Shape
with data members x (dimension) and having an operation called as CaluculateArea.
Create sub-classes called Square and Rectangle having their own features along with
inheriting the features of its superclass called Shape. The subclass also uses the same
CalculateArea operation by inheriting in its class to compute its square area, rectangle
area, and triangle area, given a choice to the user to choose which shapes area he/she is
interested to know. [Hint: use two uses of super keyword].
5. Write a Java program to read two integers a and b. Compute a/b and print, when b is not
zero. Raise an exception when b is equal to zero
6. Assume that an interviewer asked you to submit and execute a program to perform the
division of integers in Java, you wrote a program and during the execution, the program
abruptly ended at a condition. Identify the condition where your program abruptly ended
and handle the condition using suitable measures.
7. Mr. Janardhan went to the supermarket with a basket holding 5 kg thinking that he can buy
5 types of fruits. In the market, He purchased 1 kg of apple, 1kg of orange, 1 kg of grapes,
and 1 kg of kiwi fruit. He puts all the fruits in his basket and in a hurry miscalculated the
types of fruits he purchased and came back home thinking that his basket is full. At home,
he starts to check what he has bought from the market but at last, he came to know that he
has not bought 5 types of fruits that he planned to buy. Demonstrate the same with your
application. [Hint. Use User-defined Exception].
8. Mr. X is participating in a coding contest, he will win the contest if he creates an application
for Bank with instance variable accno, name, balance, and methods deposit(any),
withdraw(amt), If the amount is less than 500 then throw exception (“insufficient
balance”), (Hint: use exception handling)
1. What is a Package? What are the steps involved in creating user-defined Packages?
2. What are Access Specifiers? Explain with suitable examples. Or
3. Define an Interface? List the differences between an Interface and an Abstract Class with
syntax.
4. What is the alternative approach to implementing multiple inheritances in Java? Explain
with an example.
5. What is multithreading? Explain any two advantages of multithreading.
6. What are threads? Explain the different ways of creating threads in Java.
7. Explain the different states (Life Cycle) of the thread with a diagram.
8. Why main thread is important? Write a Java program that creates multiple child threads
and also ensures that the main thread stops last.
9. With syntax, explain use of isalive[ ], join[ ], and yield() methods.
10. Describe the thread priority. How to assign and get the thread priority?
11. What do you mean by synchronization? How it is achieved for threads in Java? Explain with
syntax.
12. Describe Inter-thread Communication in Java? How to avoid polling by using an inter-
process communication mechanism.
13. Write a short note on the Bounded buffer problem.
14. With suitable examples explain the Producer-Consumer problem.
1. Create a package named "Global". Under this create two packages named "Employee" and
"Student". Under the employee package create a class called "EmployeeDetails" having
required member fields and methods. Under the Student package create a class called
"StudentDetails" having required member fields and methods. Demonstrate the above by
creating objects of StudentDetails and EmployeeDetails inside another class that resides in
another package. [Hint: You can assume the relevant fields and methods to be written
inside the EmployeeDetails and StudentDetails class].
2. Define an interface "Area" which should contain a method named area (); Implement this
interface in rectangle & triangle classes to find their area. Demonstrate the above
implementation by writing a main method in a new class called "DemoInterfaceArea".
3. Develop a Java application using Inheritance to Create an interface called CashTree which
demonstrates any ATM Machine. The CashTree interface has methods like load_amount
and checkBalance. These banks X_Bank, Y_Bank, and Z_Bank, implements the CashTree
interface by overriding the methods in the interface. The class has specific attributes like
atmLocation, atmCode, amount, and balance. Add functionalities like ViewBalance, and
loadAmount. The load amount should specify the number of Rs. 2000 notes, Rs.500 notes,
Rs. 200 notes, and Rs.100. Based on the number of notes, calculate the total amount loaded
in the ATM. Create a class userAccount, which maintains an account with any of the banks.
On withdrawal of the amount by the users, the bank wanted to know the amount available
in the ATM on a daily basis. Write a method to know the balance in the particular banks
with the details of atmLocation, atmCode, and the balance. If the balance is less than one
lakh, advise the authorities to load the amount again. Complete the given scenario with
Menu driven options.
4. Write a Java program, which creates two threads, one thread displays "GAT-Bengaluru"
every 100 seconds, and another thread displays "CSE" every 50 seconds, continuously.
5. With the help of the Multithreading concept, create two separate threads, one thread titled
"primeThread" by extending the Thread class, and the other titled "fiboThread" by
implementing the Runnable interface. "primeThread" will be responsible to print all the
prime numbers from 1 to 100 in a regular interval of 0.25 seconds. "fiboThread" will be
responsible to print Fibonacci series of 20 numbers, in a regular interval of 0.5 seconds. On
executing this application, get the following information also.
a) Get the id, name, and priority of the main thread. (t.getid(), t.getName(),
t.getPriority())
b) Change the name and priority of the main thread and print the same. (setx())
c) Print the thread group info of both the child threads
d) Use the isAlive method to check the status of the childThread. (t.isAlive())
6. Mr. X is participating in a general quiz, there he got a question to display “n” numbers in
reverse to finish the answer. Develop a Java program to help Mr. X with this problem using
static synchronization in threads.
7. Write a Java program to implement a producer-consumer problem, using threads.
8. Write a simulation program in Java for the fruit market. The farmer will be able to produce
different types of fruits (apple, orange, grape, and watermelon), and put them in the market
to sell. The market has limited capacity and farmers have to stand in a queue. The farmer
can sell 10 fruits at a time to the consumer. So, the farmer should have fruits in multiple of
10s. Consumers can come to the market any time and purchase their desired fruits; and if
the fruits they want to buy run out, they are willing to wait until the supply of that kind is
ready.
9. Assume that a company is organizing an event. When each participant arrives, get the name
of the participant and assign participant IDs to each participant starting from 101, and
should be followed with the sequence of IDs for the further participants. The company has
created a String array that has the following information like {“Hi”, “name of the
participant”, “ID of the participant”, and “Welcome Message”}. The registration team
registers the Participant details. You can create a class “Participant” with participant_Id,
participant_Name as members. Create a setter method to set the participant details. Once
the participants are registered, the welcome message is printed by separate Threads on the
Participant instance. Write a Java program to handle this scenario.
1. Write a Java program to illustrate the KeyListener interface by creating a button and when
it is pressed a Text message is displayed as “Button is Pressed”.
2. Write a Java program to illustrate the KeyListener interface by creating a text box and
performing the Counting of Words & Characters.
3. Write a Java program to illustrate the Window Listener interface.
4. Write a Java program to illustrate the difference between the Listener interface and
adapter classes.
1. What is the string in Java? Write a Java program that demonstrates string constructors of
the String class
2. Explain String constructors with an example.
3. Explain the syntax of compareTo(). Write a Java program to sort a set of strings using
compareTo().
4. Explain the syntax of replace(). Write a Java program to replace all instances of one string
with another string.
5. Differentiate between equals( ) and equalsIgnoreCase( ) with respect to string comparison.
6. Differentiate between equals() and == with respect to string comparison.
7. Explain the methods that search a string for a specified character or substring with an
example program.
8. Differentiate between String and StringBuffer class.
9. Discuss the number of ways in which characters can be extracted from a String object.
10. Explain the following functions with an example:
a) concat() b)trim() c)substring() d)length() e)toUpperCase()
11. Explain the following methods of the StringBuffer class with an example:
a) capacity() b) ensureCapacity() c) append()
d) insert() e) reverse() f) replace()
12. Explain how to modify a string by using the following methods:
a) substring() b) concat() c) replace() d) trim()
13. Explain the following methods of the String class:
a) regionMatches( ) b) startsWith( ) c) endsWith( )
1. Create two Strings str1 and str2, using String functions find the
a. Character at index 5 for str1
b. Check if the str2 starts with “k”
c. Find the length of str1 and str2
d. Find if str1 and str2 are equal
e. Find the substring of str1 for index 1:4
f. Create a character array called dst, store dst[0]=’a’ and dst[1]=’b’ respectively and
display it.
g. Use getchars function to add characters to a character array dst and display it.
h. Concatenate str2 with Global Academy of Technology
2. Create a String “ScienceTechnology” using StringBuffer, Perform the following operations
a. insert string “for” at location 7
b. insert 0 at location 2
c. insert “true” at location 3
d. insert a character array
3. Develop a Java program to replace all instances of one substring with another within a
string.
4. Develop a Java program to illustrate various index methods to search inside strings.
5.
6. In this modern era, naming the child passionately becoming common, like keeping the
child’s name as a combination of the child’s mother’s and father’s names. Think you as an
application developer have an interest in building such applications which take the names
of the father and mother and suggest the Name of the child by combining the names of the
parents.