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

OOP Java

The document provides an overview of Object-Oriented Programming (OOP) using Java, covering key concepts such as encapsulation, inheritance, and polymorphism, as well as the differences between function-oriented design and OOP. It includes details on Java's primitive data types, exception handling, and the use of packages and interfaces. Additionally, it discusses UML diagrams and features of Java, emphasizing the importance of encapsulation and abstraction in software design.

Uploaded by

sruthyravi1123
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 views16 pages

OOP Java

The document provides an overview of Object-Oriented Programming (OOP) using Java, covering key concepts such as encapsulation, inheritance, and polymorphism, as well as the differences between function-oriented design and OOP. It includes details on Java's primitive data types, exception handling, and the use of packages and interfaces. Additionally, it discusses UML diagrams and features of Java, emphasizing the importance of encapsulation and abstraction in software design.

Uploaded by

sruthyravi1123
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

Object Oriented Programming Using Java

Module 1:

1) What is object-oriented programming (OOP)?


Ans) Object-oriented programming is a programming paradigm that uses objects and their
interactions to design and implement software systems. It focuses on the concepts of
encapsulation, inheritance, and polymorphism to create reusable and modular code.
2) Define function oriented design?
Ans) Function-oriented design (FOD) is a software design approach that focuses on creating
software systems by identifying the functions or operations that the system needs to perform,
and then designing the software around these functions.

3) List the basic object oriented concepts.


Ans) Object,Class,Polymorphism,Encapsulation,Abstraction,Inheritance.

4) What is a class?
Ans) A class is a blueprint for creating objects. It defines the properties and methods that an
object will have. In Java, a class is defined using the "class" keyword followed by the name
of the class.

5) What is encapsulation in OOP, and why is it important?


Ans) Encapsulation is the process of hiding the internal details of an object and only exposing
its interface to the outside world. It is important because it helps to ensure data integrity,
maintainability, and security.

6) What is abstraction in OOP, and how is it implemented?


Ans) Abstraction is the process of identifying the essential characteristics of an object and
ignoring the non-essential details. It is implemented through the use of abstract classes and
interfaces.
7) What is polymorphism in OOP, and how does it work?
Ans) Polymorphism is the ability of an object to take on many forms. It works by creating a
superclass with a method that is overridden in its subclass.
8) What is polymorphism in OOP, and how does it work?
Ans) Inheritance is a mechanism by which a subclass can inherit the properties and behavior
of its superclass. It works by creating a subclass that inherits all the public and protected
members of its superclass.
9) What are the different types of diagrams available in UML?
Ans) class diagrams, use case diagrams, sequence diagrams, activity diagrams, state machine
diagrams, component diagrams, and deployment diagrams.
10) What is a class diagram in UML?
Ans) A class diagram in UML is a type of diagram that shows the structure of a system by
depicting its classes, their attributes, and their relationships with other classes.
11) What is a use case diagram in UML?
Ans) A use case diagram in UML is a type of diagram that depicts the interactions between a
system and its external actors. It is commonly used to model the various use cases of a system
and the actors who interact with the system.
12) What is a sequence diagram in UML?
Ans) A sequence diagram in UML is a type of diagram that depicts the interactions between
objects in a system over time. It is commonly used to model the dynamic behavior of a
system and to visualize the sequence of messages exchanged between objects.
13) What is an activity diagram in UML?
Ans) An activity diagram in UML is a type of diagram that depicts the workflow or flow of
control in a system. It is commonly used to model business processes and to visualize the
steps involved in completing a task.
14) What is an association in UML?
Ans) An association in UML is a relationship between two classes that signifies a connection
or dependency between them. It is depicted in a class diagram as a line connecting the two
classes, with optional labels to indicate the nature of the association.
15) What is inheritance in UML?
Ans) Inheritance in UML is a relationship between two classes where one class (the subclass)
inherits the properties and methods of another class (the superclass). It is depicted in a class
diagram as an arrow pointing from the subclass to the superclass, with a triangle at the end of
the arrow.
16) What is aggregation in UML?
Ans) Aggregation in UML is a relationship between two classes where one class (the
container) contains one or more instances of another class (the contained). It is depicted in a
class diagram as a diamond shape on the container end of the relationship.
17) List the features of Java.
Ans) Object Oriented , simple , secured, Platform independent, robust, portable, dynamic,
interpreted, high performance, multithreaded.
18) What is garbage collection in Java?
Ans) Garbage collection is the automatic process of freeing up memory occupied by objects
that are no longer being used by a Java program.
19) Which component is used to compile, debug and execute the java programs?
Ans) JDK
20) Which feature of OOPS described the reusability of code?
Ans) Inheritance
21) How can the concept of encapsulation be achieved in the program?
Ans) By the use of access specifiers.
22) The principle of abstraction___________.
Ans) is used to avoid duplication.
23) Which of the following UML diagrams has a static view?
Ans) Use case diagram
24) What is the primary function of JVM?
Ans) to execute the byte code produced by the compiler.
25) Use of finalize() keyword in java?
Ans) The finalize method in Java is a special method that is called by the garbage collector
before an object is destroyed. The finalize method allows the programmer to perform some
clean-up actions before the object is destroyed

Module 2:

1. What are the primitive data types in Java?


Answer: Java has eight primitive data types: byte, short, int, long, float, double, boolean, and
char.
2. What is the range of the byte data type?
Answer: The byte data type is an 8-bit signed two's complement integer, which means it can
store values from -128 to 127.
3. What is the difference between float and double data types?
Answer: Both float and double data types are used to store floating-point numbers, but float
is a 32-bit single-precision data type, while double is a 64-bit double-precision data type. This
means that double can store larger and more precise floating-point numbers than float.
4. What is the default value of the boolean data type in Java?
Answer: The default value of the boolean data type in Java is false.
5. What is the difference between char and String data types?
Answer: Char is a primitive data type that represents a single character, while String is a
reference data type that represents a sequence of characters. Char variables are enclosed in
single quotes (' '), while String variables are enclosed in double quotes (" ").
6. What are the primitive data types in Java?
Answer: Java has eight primitive data types: byte, short, int, long, float, double, boolean, and
char.
7. What is the range of the byte data type?
Answer: The byte data type is an 8-bit signed two's complement integer, which means it can
store values from -128 to 127.
8. What is the difference between float and double data types?
Answer: Both float and double data types are used to store floating-point numbers, but float
is a 32-bit single-precision data type, while double is a 64-bit double-precision data type. This
means that double can store larger and more precise floating-point numbers than float.
9. What is the default value of the boolean data type in Java?
Answer: The default value of the boolean data type in Java is false.
10. What is the difference between char and String data types?
Answer: Char is a primitive data type that represents a single character, while String is a
reference data type that represents a sequence of characters. Char variables are enclosed in
single quotes (' '), while String variables are enclosed in double quotes (" ").
[Link] are the primitive data types in Java?
Answer: Java has eight primitive data types: byte, short, int, long, float, double, boolean, and
char.
[Link] is the range of the byte data type?
Answer: The byte data type is an 8-bit signed two's complement integer, which means it can
store values from -128 to 127.
[Link] is the difference between float and double data types?
Answer: Both float and double data types are used to store floating-point numbers, but float is
a 32-bit single-precision data type, while double is a 64-bit double-precision data type. This
means that double can store larger and more precise floating-point numbers than float.
[Link] is the default value of the boolean data type in Java?
Answer: The default value of the boolean data type in Java is false.
[Link] is the difference between char and String data types?
Answer: Char is a primitive data type that represents a single character, while String is a
reference data type that represents a sequence of characters. Char variables are enclosed in
single quotes (' '), while String variables are enclosed in double quotes (" ").

[Link] are the primitive data types in Java?


Answer: Java has eight primitive data types: byte, short, int, long, float, double, boolean, and
char.
[Link] is the range of the byte data type?
Answer: The byte data type is an 8-bit signed two's complement integer, which means it can
store values from -128 to 127.
[Link] is the difference between float and double data types?
Answer: Both float and double data types are used to store floating-point numbers, but float
is a 32-bit single-precision data type, while double is a 64-bit double-precision data type. This
means that double can store larger and more precise floating-point numbers than float.
[Link] is the default value of the boolean data type in Java?
Answer: The default value of the boolean data type in Java is false.
[Link] is the difference between char and String data types?
Answer: Char is a primitive data type that represents a single character, while String is a
reference data type that represents a sequence of characters. Char variables are enclosed in
single quotes (' '), while String variables are enclosed in double quotes (" ").

Module 3:
1) What is a package in Java?

Ans) A package in Java is a way of organizing related classes and interfaces together. It helps
in organizing the code, making it more modular, and improving code reusability.

2) How do you declare a package in Java?

Ans) To declare a package in Java, you need to include the package statement as the first
statement in the source file. For example: package [Link];

3) What is an interface in Java?

Ans) An interface in Java is a collection of abstract methods and constants that can be
implemented by a class. It provides a way for different classes to communicate with each
other.

4) What is the difference between a package and a class in Java?

Ans) A package is a collection of related classes and interfaces, while a class is a blueprint for
creating objects. A package is used for organizing code and improving code reusability, while
a class is used for creating objects and defining their behavior.

5) What is the difference between an abstract class and an interface in Java?

Ans) An abstract class can have abstract and non-abstract methods, while an interface can
only have abstract methods. An abstract class can also have instance variables, while an
interface can only have constants. A class can implement multiple interfaces, but can only
inherit from one abstract class.

6) How do you declare an interface in Java?

Ans) To declare an interface in Java, you need to use the interface keyword followed by the
interface name. For example: public interface MyInterface { }

7) How do you implement an interface in Java?

Ans) To implement an interface in Java, you need to use the implements keyword followed
by the interface name. For example: public class MyClass implements MyInterface { }

8) Can a class belong to multiple packages in Java?

Ans) No, a class can only belong to one package in Java.

9) What is the purpose of access modifiers in Java?


Ans) Access modifiers are used to control the access to classes, interfaces, methods, and
variables in Java. They help in encapsulating the code and hiding implementation details.

10) What are some commonly used access modifiers in Java?

Ans) The commonly used access modifiers in Java are public, private, protected, and default.

11) What is an exception in Java?

Ans) An exception in Java is an event that occurs during the execution of a program that
disrupts the normal flow of instructions.

12) What is the purpose of exception handling in Java?

Ans) The purpose of exception handling in Java is to handle errors or exceptional conditions
that occur during the execution of a program, preventing the program from crashing or
behaving unexpectedly.

13) Show exception handling in Java with a suitable program.

Ans) try {
// Code that may throw an exception
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
// Code to handle the exception
[Link]("An arithmetic exception occurred: " + [Link]());
} finally {
// Code that will always be executed, whether an exception is thrown or not
[Link]("The try-catch block has finished executing.");
}

14) What is the syntax for catching an exception in Java?

Ans) The syntax for catching an exception in Java is to enclose the code that may throw an
exception in a try block, and then handle the exception with a catch block.

15) What is the difference between checked and unchecked exceptions in Java?

Ans) Checked exceptions are those that are checked at compile time, and the programmer
must handle them explicitly with a try-catch block. Unchecked exceptions, on the other hand,
are not checked at compile time and can be handled at runtime.

16) What is the purpose of the finally block in Java exception handling?

Ans) The finally block in Java exception handling is used to define a block of code that will
be executed regardless of whether an exception was thrown or not.
17) Can we have multiple catch blocks in Java?

Ans) Yes, we can have multiple catch blocks in Java, each handling a different type of
exception.

18) Can we throw an exception from a catch block in Java?

Ans) Yes, we can throw an exception from a catch block in Java, but it must be declared in
the method signature or be an unchecked exception.

19) Explain throw, throws and finally constructs with the help of a Java program.

Ans) import [Link];

public class ExceptionExample {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

try {
[Link]("Enter a number between 1 and 10: ");
int num = [Link]();

if (num < 1 || num > 10) {


throw new IllegalArgumentException("Number must be between 1 and 10.");
}

[Link]("You entered: " + num);


} catch (IllegalArgumentException e) {
[Link]("Error: " + [Link]());
} finally {
[Link]();
[Link]("Scanner has been closed.");
}
}
}

20) What is the difference between throw and throws in Java?

Ans) The throw keyword is used to throw an exception explicitly, while the throws keyword
is used to declare that a method may throw a particular type of exception.

21) What is the hierarchy of exception classes in Java?


Ans) The hierarchy of exception classes in Java begins with the Throwable class, which has
two subclasses, Exception and Error. The Exception class has many subclasses, including
IOException, SQLException, and NullPointerException.

22) Can we create custom exceptions in Java?

Ans) Yes, we can create custom exceptions in Java by extending the Exception class or one of
its subclasses.

23) How do you read input from the console in Java?

Ans) You can read input from the console in Java using the Scanner class or the
BufferedReader class.

24) How do you write output to the console in Java?

Ans) You can write output to the console in Java using the [Link]() method or the
[Link]() method.

25) What is the PrintWriter class in Java?

Ans) The PrintWriter class in Java is used to write formatted text to a character-output
stream.

26) What are object streams in Java?

Ans) Object streams in Java are streams that allow you to read and write objects to and from
a file or a network.

27) What is serialization in Java?

Ans) Serialization in Java is the process of converting an object into a stream of bytes so that
it can be stored in a file or transmitted over a network.

28) How do you work with files in Java?

Ans) You can work with files in Java by using classes such as File, FileInputStream,
FileOutputStream, FileReader, FileWriter, BufferedInputStream, BufferedOutputStream,
BufferedReader, BufferedWriter, and others.

29) Explain the role of Serializable interface with a


suitable code sample.

Ans) import [Link].*;

public class Person implements Serializable {


private String name;
private int age;

public Person(String name, int age) {


[Link] = name;
[Link] = age;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}

public static void main(String[] args) {


Person person = new Person("John Doe", 30);

try {
FileOutputStream fileOut = new FileOutputStream("[Link]");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
[Link](person);
[Link]();
[Link]();
[Link]("Person object has been serialized.");
} catch (IOException e) {
[Link]();
}
}
}

Module 4:

[Link] in which thread can be created:


Ans. • Implement the Runnable interface.
• Extend the Thread class, itself.

[Link] implement Runnable, which method is to be used?


Ans. public void run ()

[Link] is the use of the start() method?


Ans. To start a newly created thread

[Link] are the most commonly handled events?


Ans. – Generated by mouse
– Keyboard
– Various controls like buttons,dropdown,radio buttons,checkbox

[Link] are the steps to generate and process events?


Ans. 1. A source generates an event and sends it to one or more listeners.
2. Here the listener simply waits until it receives an event.
3. Once received, the listener processes the event and then returns.

6. What is an event?
Ans. An event describes the change in state of source.
Events are generated as result of user interaction with the graphical user interface
components.

7. What is an event source?


Ans. A source is an object that generates an event.

8. What is meant by multicasting the event?


Ans. When an event occurs , all registered listeners are notified and receive a copy of the
event object. This is multicasting the event.

9. What is meant by unicasting the event?


Ans. Some sources may allow only one listener to register. When such an event occurs, the
registered listener is notified.
This is known as unicasting the event.

10. What is an event listener?


Ans. A listener is an object that is notified when an event occurs.

11. What are the major requirements of an event listener?


Ans. – It must have been registered with one or more sources to receive notifications about
specific events.
– It must implement methods to receive and process these notifications.
12. The method that receive and process events are defined in a set of interfaces
found in ___.
Ans. [Link]

13. Root of event class hierarchy is ___ which is in [Link]


Ans. EventObject

14. Use of getSource() and toString()?


Ans. Object getSource() :- returns the source of the event.
String toString() :- returns the string equivalent of the event.

15.___is the superclass of all AWT events that are handled by the delegation event model.
Ans. AWTEvent
16. ActionEvent is generated:
Ans. – when a button is pressed
– A list item is double clicked
– Or a menu item is selected

17.___ is the event class that handles all the component-level input events.
Ans. InputEvent

18. Which are the types of mouse events?


Ans. 1. MOUSE_DRAGGED the user dragged the mouse
2. MOUSE_ENTERED the mouse entered a component
3.MOUSE_EXITED the mouse exited from a component
4.MOUSE_MOVED the mouse moved
5.MOUSE_PRESSED the mouse was pressed
6.MOUSE_RELEASED the mouse was released
7.MOUSE_WHEEL the mouse wheel was moved

[Link] ___ class defines integer constants that can be used to identify them.
[Link]

20. An ___ is generated when a check box or a list item is clicked or when a checkable menu
item is selected or deselected.
Ans. ItemEvent

21. ItemEvent defines one integer constant, ___, that signifies a change of state.
Ans. ITEM_STATE_CHANGED

22. The ___ method can be used to obtain a reference to the item that generated an event.
Ans. getItem( )

23. The ___ method can be used to obtain a reference to the ItemSelectable object that
generated an event
Ans. getItemSelectable( )

24. The Delegation Event Model has the following key participants namely:
And. • Source -The source is an object on which event occurs.
• Listener -It is also known as event handler. Listener is responsible for generating responses
to an event.

25. Explain Delegation Event handling.


Ans. [Link] the appropriate interface in the listener so that it will receive the type of
event desired.
2. Implement code to register and unregister (if necessary) the listener as a recipient for
the event notifications.

26. What is the Collections Framework?


Ans. The Collections Framework is a sophisticated hierarchy of interfaces and
classes that provide state-of-the-art technology(best possible technology) for
managing groups of objects.

27. What are the advantages of Java Generics?


Ans. ■ Type-safety
■ Type casting is not required
■ Compile-Time Checking

28. Objects are added to a collection by calling ___.


Ans. add( )

29. What is synchronization in java?


Ans. Synchronization in java is the capability to control the access of multiple
threads to any shared resource.

30. Why use Synchronization?


Ans. [Link] prevent thread interference.
[Link] prevent consistency problems.

31. What is the key to synchronization?


Ans. Key to synchronization is the concept of the monitor(also called semaphore). A monitor
is an object that is used as a mutually exclusive lock (mutex).

32. Which are the two types of thread synchronization?


Ans. Mutual exclusive and inter-thread communication.
[Link] Exclusive
1. Synchronized method.
2. Synchronized block.
3. static synchronization.
[Link] (Inter-thread communication in java)

33. Which methods of object class is used for inter thread communication?
Ans. • wait()
• notify()
• notifyAll()

34. The ___ method of thread class puts the thread from running to waiting state.
Ans. suspend()

35. What is deadlock?


Ans. A special type of error that you need to avoid that relates specifically to multitasking is
deadlock, which occurs when two threads have a circular dependency on a pair of
synchronized objects.

36. Synchronization is built around an internal entity known as the ___.


Ans. lock or monitor

37. Use of strings in java?


Ans. Strings are used for storing text.

38. How to create a string?


Ans. [Link] literal
Eg: String s = “GeeksforGeeks”;
[Link] new keyword
Eg: String s = new String (“GeeksforGeeks”);

39. What is the use of getChars()?


Ans. Copies characters from this string into the destination character array.

40. The java string ___ method returns the byte array of the string.
Ans. getBytes()

41. •If you want to convert all the characters in a String object into a character array, the
easiest way is to call ___.
Ans. toCharArray()

42. Use of compareTo()


Ans. Compares two strings lexicographically

[Link] ___ method returns a copy of the invoking string from which any leading
and trailing whitespace has been removed.
Ans. trim( )

44. String is ___ in Java


Ans. immutable

45. What is multitasking?


Ans. Multitasking refers to having multiple processes, tasks, or threads running at
the same time.

46. What is a process?


Ans. A program in execution is often referred as process

47. What is thread?


Ans. A thread is a subset(part) of the process.

48. What are the advantages of multi threading?


Ans. [Link] sharing
2. Responsiveness
3. Used in server applications for improving high throughput and resource
utilization.
4. If several threads work on the same set of data, they can actually share their cache,
leading to better cache usage or synchronization on its values.
5. Utilization of Multiprocessor Architecture.
6. Simultaneous and parallelized occurrence of tasks
7. Decreased cost of maintenance
8. Better use of CPU resource
9. Enhanced performance by decreased development time.

49. Which are the possible states of a thread?


Ans. [Link]
[Link]
[Link]
4. Waiting
5. Terminated

Module 5:

[Link] is swing in java?


Ans. Swing is a set of classes that provides more powerful and flexible GUI components than
does the AWT. Swing provides the look and feel of the modern Java GUI. Swing Is Built on
the AWT.

[Link] are the key swing features?


Ans. [Link] Components Are Lightweight
[Link] Supports a Pluggable Look and Feel.

[Link] collection of appearances and common behavior of GUI components is called a ___.
Ans. look-and-feel(L&F)

[Link] Swing is the advanced and optimized version of ___


Ans. AWT

[Link] follows ___ software design pattern.


Ans. MVC( model view controller)

[Link] uses a modified version of MVC that combines the view and the controller into a
single logical entity called the ___.
Ans. UI delegate

7. Which are the key items of Swing GUI?


Ans. components and containers

[Link] programs are ___.


Ans. event-driven

[Link] controls will inherit the properties from which classes?


Ans. • Component
• Container
• JComponent

[Link] event handling mechanism used by Swing is the ___.


Ans. delegation event model.

[Link] obtain the text currently in the text field, call ___.
Ans. getText( )

12. ___ is a methodology or paradigm to


design a program using classes and objects.
Ans. Object-Oriented Programming

13. OOP concepts are:


Ans. Object, class, inheritance, polymorphism, abstraction, encapsulation

14. What is DBMS?


Ans. A DBMS is a software that allows creation, definition and manipulation of
database, allowing users to store, process and analyze data easily.

15. What is JDBC?


[Link] stands for Java Database Connectivity, which is a standard Java API for database-
independent connectivity between the Java programming language and a wide range of
databases.

16___ is a Java API that can access any kind of tabular data, especially data stored in a
Relational Database.
Ans. JDBC API

[Link] is JDBC Driver?


Ans. A JDBC driver is a software component enabling a Java application to
interact with a database.

18. Which are the JDBC driver types?


Ans. JDBC Drivers Types:
Type 1: JDBC-ODBC Bridge Driver
Type 2: JDBC-Native API
Type 3: JDBC-Net pure Java

19. Which are the two layers of JDBC Architecture?


Ans. [Link] API
[Link] Driver API

20. List the basic steps in using JDBC?


Ans. 1. Import JDBC packages.
2. Load and register the JDBC driver.
3. Open a connection to the database.
4. Create a statement object to perform a query.
5. Execute the statement object and return a query resultset.
6. Process the resultset.
7. Close the resultset and statement objects.
8. Close the connection.

21. Which are the different statement types in JDBC?


Ans. [Link]
2. Prepared statement
3. Callable statement

[Link] is ResultSet?
Ans. ResultSet is an object that contains the results (rows) of executing a SQL
statement on a database.

[Link] ___ class is used to create a pop-up list of items from which the user may choose.
Ans. Choice

[Link] add a selection to the list, call ___.


Ans. add( )

[Link] is the difference between choice and list object?


Ans. Unlike the Choice object, which shows only the single selected item
in the menu, a List object can be constructed to show any number of choices in the visible
window.

26. ___ are components that allow a user to interact with your application in various ways.
Ans. Controls

27. What is FlowLayout?


Ans. The FlowLayout is the default layout. It layout the components in a directional flow.

You might also like