Java Questions
Java Questions
1. Differentiate between Java and C++. Highlight at least five major differences with the
help of code snippets.
2. What are the main characteristics of Java Language? Explain with the help of suitable
example.
9. Also explain the role of Java Virtual Machine to achieve architecture neutrality.
13. What is Java byte code? How do we get the byte codes from the source codes? Discuss.
15.
Fundamentals of Java
2. Briefly discuss the general structure of a Java program and the steps to execute it.
4. List various primitive data types. Also mention their size and range.
5. What are the primitive data types in Java? List these with their maximum and minimum
permissible value.
7. What numeric data types are provided in Java? Discuss all of them.
9. What is an arithmetic expression? How is it evaluated in Java? Discuss the rules with
small examples.
10. Explain various unary, binary and ternary operators available in Java.
12. Write a program in Java, which demonstrates all the control structures.
13. Explain the various control structures used in core Java giving examples.
14. Discuss various looping structures available in Java with their syntax and examples. Also,
explain the usage of break and continue statements.
15. What is the difference between if and switch conditional control structures? Write their
syntax and examples.
17. Define a class in Java and create its objects. Also discuss how to implement constructors
in Java.
19. What is a class and object? How can you access a class data member and function?
21. Discuss the importance of final keyword, when used with variable, method and class
using appropriate examples.
22. How is final keyword used in Java? Discuss different cases with suitable examples.
23. What is method overloading? In which situations should it be used? Explain with
examples.
34. What is the difference between public and private access specifiers?
35. What are the visibility restrictions imposed by various access protection modifiers?
38. What is a constructor? What are its special properties? Explain the different types of
constructors.
39. What do you mean by a constructor? How do you define multiple constructor methods
within a class? Describe with example.
40. What is a constructor? How do you overload it? Discuss with an example.
49. Write a java program to find whether a given number is prime or not.
50. Write a program in Java to compute the sum of the digits of a given integer number.
51. Write a Java program to take a few strings for person names using command line
arguments and display them in alphabetical sorted order.
52. Write a Java program which accepts three command line arguments. First two arguments
for two numbers and third for an operator (i.e. +,-,/,*). Use the switch statement and
display the result. Take care of all the errors/exceptions occurring during runtime.
53. Write a program to find the areas of a square room and a rectangle room using function
overloading.
54.
Inheritance
1. What is inheritance? How does it help us to create new classes quickly? Give suitable
examples.
2. What are the different forms of inheritance? Explain with suitable examples.
5. What is overriding?
9. Explain the concept of base class and derived class in Java. How super is used to call
super class constructors? Give code snippet to demonstrate inheritance and use of Final
method.
10. How can you initialize base class data members via derived class constructors?
11. How can one restrict the visibility of variables and methods during Inheritance?
15.
I/O Basics:
2. What are input and output streams? Explain them with illustrations.
3. How do you read console input and write console output in Java?
7. Distinguish between the following: InputStream and Reader Classes, OutputStream and
Writer Classes.
8. Differentiate between character and byte streams. How do you open a file for reading in a
Java program?
10. Discuss commonly used classes of Java for reading and writing files.
12. What is a stream in Java? Discuss two main stream classes available and their usage.
13. What is a stream? Brief out various stream classes used in Java application.
14. What are the various pre-defined streams in Java? Discuss each one of them briefly.
15. How do you open a file for reading in a Java program? Describe.
18. What built-in methods are available in Java to read from a character stream? Brief out.
19. Write a program in Java, which will read a text and count all occurrences of a particular
word.
20. Write a Java program to read a text file and output on edited version to another text file.
The edited version should convert every alphabet into its upper-case.
21. Write a Java program to read from and write a string into a file.
22.
2. How do you define and use multi-dimensional arrays in Java? Illustrate with relevant
examples.
6. How strings are created in Java? Differentiate between String and StringBuffer Class.
7. Write a Java program which takes a string from command line and creates all the strings
possible by jumbling different characters of the string.
8. Create a matrix class in Java that can perform the following operations on matrices:
Addition/Subtraction, Transpose and Multiplication.
9. Write a program in Java to calculate Pascal’s triangle to a depth of 12, storing each row
of the triangle in an array. Write a method that prints each row.
10. Write a Java program to get the values of 3x3 matrix and find its transpose.
11.
Packages
3. What is the use of packages in Java? How can you create user-defined packages and
where they should be stored? What are the types of packages?
4. Explain the process of adding classes to existing packages. Give one example program
also.
5. What is a package? How are they defined and imported in Java programs? Give syntax
with their proper usage.
6. How do we add a class or interface to a package? List down few standard Java packages.
7. Which package is by default imported in all the Java programs? Discuss commonly used
classes of that package.
9. Explain the process of defining your own package, with the help of an example.
11.
Interface
3. Give an example where interface can be used to support multiple inheritance. Develop a
standalone java program for the example.
7. Discuss the differences and similarities between abstract classes and interfaces.
9. What is an abstract class? What are the rules associated with it?
12.
Exception Handling
1. How are try and catch blocks defined in Java? What is a finally block? When and how is
it used? Give a suitable example.
2. Explain try-catch structure available in Java. Also explain the role of finally block. Is it
essential to catch all types of exceptions?
3. Explain how exception handling mechanism can be used for debugging a program.
6. What do you mean by throwing an exception? When does an exception throw? Discuss at
length.
7. How are exceptions handled in Java? List some of the most common types of exceptions.
Is it essential to catch all types of exceptions?
10. How do you define an exception of your own? Discuss with a small program.
18. What is the difference between exception and error? How exceptions are handled in
Java?
19. How do you use multiple catch clauses in exception handling? Demonstrate with a
relevant example.
23. Write a Java program to show the use of nested try statements.
24.
Multi Threading
1. What is multithreading? Discuss the various methods used for creating, running, stopping
a thread. How do we set priorities for threads?
2. How is thread class used to attain multithreading? Illustrate with suitable examples.
3. Define multithreading and explain how it is implemented in Java. What are the reasons
for non-execution of a thread?
5. How do we start a thread in Java? What are the two methods by which we may stop
threads? Also differentiate between suspending and stopping of a thread.
8. What is thread in Java? What are the various approaches of creating a thread? Discuss.
13. Distinguish between the init() and start() methods related to applets.
19. What are the various ways of creating a thread in Java? Illustrate with examples.
23. State and explain the methods used for Thread Synchronization.
25. Java is known for its thread model. Explain the complete Java Threading using example
code. Lay complete emphasis on deadlock avoidance and synchronization concepts.
26. Write a java program in which two thread classes ThreadA and ThreadB are created.
Both classes extend the Thread class. ThreadA class displays string every second for 20
iterations and ThreadB class displays a string every 2 seconds for 10 iterations.
27. Write a class Point, which has two data members X and Y. Write a constructor for
initializing these and a method for displaying the points. Using this class, create point
objects. Also demonstrate the use of 4 threads with these objects by displaying the points
after every 2 seconds, one after another.
28.
Applet
3. What are various stages in the lifecycle of an applet? Explain with the help of diagram.
4. Explain the life cycle of an Applet. Also describe various methods used in different
stages of its life cycle.
5. Discuss the steps involved in developing and running an applet. How many arguments
can be passed to an applet using <param> tag?
6. How an applet is embedded in an HTML document? How are parameters passed to it?
9. Why Java applets have more security issues than Java application?
11. Discuss various methods, which are called when an applet begins.
12. What restrictions have been imposed on applets to ensure secure communication?
Discuss.
13. Give some applications of Applet.
14. What are the advantages of using applets for web programming?
15. Design an applet that takes two strings from the user using text fields and provides a list
of functions that can possibly be applied on these entered strings. As soon as relevant
inputs are given applet performs the required operation and return the result
appropriately.
16. Design a Java applet which uses graphics primitives to create some good pictures.
17. Design an applet which provides user interface to accept name, DOB, email-id and
qualification (MCA, MA, BA etc. in a list). Then it asks whether user is computer
proficient or not using radio buttons. If user is computer proficient, then only he should
be asked about programming languages known. Display the entered data in a dialog box
appropriately.
18. Write an applet that takes principal amount through a text field, rate of interest and
number of years through lists, compound interest and simple interest through buttons.
Perform the necessary calculations and display the results appropriately. Use exception
handling wherever required.
19. Write a simple applet program that contains labels, text fields and buttons to add,
subtract, multiply and divide two numbers and display the result.
20. Create an applet which draws a shape using various methods of graphics class and set
different colors to these objects drawn.
21. Write a Java applet to reserve a hotel where GUI provides choice of two/three… Five star
hotels and then names in each category and their respective charges. A user can reserve
by making appropriate selection and providing relevant information. Handle relevant
events.
22. Write a Java applet to draw various shapes like lines, rectangles, circles and ellipses.
Write the steps to execute this applet.
24. Develop an applet that receives two numeric values as input from the user and then
displays their sum.
25. Write a code to display Hello world using both Application and Applet concepts.
26.
AWT
1. What is AWT?
2. Explain with examples, the various graphics methods supported by AWT. How color of
an object can be changed?
7. What is the role of layout managers in Java? How does the Border Layout manager
organize components?
15. Explain the event delegation model with the help of an example.
16. How are events handled in Java? Explain with suitable examples.
21. What can mouse related events be handled in a Java program? Discuss.
22. How do you create a choice list in Java program? Discuss with syntax.
23. Write a Java program which takes student information like name, father’s name, class,
address, state, etc. using graphical user interface. The data entered should be stored in a
text file appropriately.
24. Write an applet which contains three button labelled RED, GREEN and BLUE. Upon
clicking a button the applet window background area should be painted with relevant
color. (Use AWT controls and event handling).
25. Write a Java program which displays a GUI for a restaurant where you can order for
certain dishes. The GUI should contain appropriate controls for selecting a dish and then
its variations, number etc. Upon submission it should generate an order receipt containing
details and the total amount.
26. Design a GUI for an inventory system which takes information like item code, item
name, category (use selection list), unit of measurement (radio buttons), quantity etc. All
the entered data should be stored in a text file.
27. Design an application that has a choice and list object. The choice object has 3 items as
TV, Music system, Car. For each item selected in choice there is a corresponding list of
brands available for that product. Set the text field with the items selected in the choice
and list.
28. Write a program to draw lines of random length and random colors.
29.
JDBC
2. Explain in detail the Java database connectivity model with suitable illustrations. Also
discuss JDBC exception classes.
4. What are various JDBC exception classes? How are they used? Explain with small
examples.
9. Write a Java program to create a connection with the database. Explain the various
classes used in this program.
10. Write a Java code to create a JDBC console application, which inserts records of 4
employees in an Employee table with fields (Empno, Ename, Age, Designation,
Department, Salary).
11. Using JDBC object, explain how can you do the following by taking suitable examples:
a. creates a Student table (in the result database) with fields (Rollno, Name, Class,
Percent_marks)
c. Display the Rollno and Name of the students who have got above 60% marks.
13.