0% found this document useful (0 votes)
21 views

Java Program Ans 5 Sem

Uploaded by

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

Java Program Ans 5 Sem

Uploaded by

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

1. What is a java program structure?

2. Define this Keyword.


3. Explain in detail the data types in java?
4. What is an Interface?
5. What is the use of Reader and Writer class?
6. Which method is used to specify containers layout with syntax.
7. g) What is the default layout for Frame and Panel?
8. Explain Modifiers and Access Controls used in java.
9. List and explain any 2 in-built exceptions.
10. Explain the purpose of getContentPane().Define variable in Java? What are
the naming rules of variable?
11. What is recursion?
12. Define Inheritance?
13. What is difference between Anay and Array List?
14. What is error? List types of error?
15. List any two restrictions for applet.
16. What is an event?
17. What is Object and Class?
18. Write the definition of abstract class?
19. What is Container? .
20. What is Java? Why Java is a platform neutral language?
21. What is access specifiers? List them.
22. Define Keyword-Static.
23. Why we set environment variable in Java?
24. Write advantages of Inheritance.
25. Define class and object with one example.
26. What is Swing?
27. When buffered reader is used?
28. What is main difference between exception and error?
29. What is Panel?
30. What is JDK? How to build and run java program?
31. What is use of classpath?
32. What is collection? Explain collection frame work in details.
33. What is the use of layout manager?
34. What is difference between paint ( ) and repaint ( ).
35. Define keyword throw.
36. Define polymorphism.
a) What is a Java Program Structure?

A Java program structure typically consists of the following components:

1. Package Declaration: (Optional) Specifies the package in which the


class resides.
2. Import Statements: (Optional) Imports external classes and packages.
3. Class Declaration: Contains the main program logic.
4. Main Method: public static void main(String[] args) — entry point of the
Java program

b) Define this Keyword.

In Java, the this keyword refers to the current instance of a class. It is used to
refer to the current object within an instance method or constructor.

 Used to distinguish instance variables from local variables with the


same name.
 Used to call other constructors in the same class (constructor
chaining).
 Used to pass the current object as a parameter to another method.

c) Explain in detail the data types in Java.

Primitive Data Types:

1. byte: 1 byte, ranges from -128 to 127.


2. short: 2 bytes, ranges from -32,768 to 32,767.

Non-Primitive (Reference) Data Types:

 Classes
 Interfaces
 Arrays

d) What is an Interface?

An interface in Java is a reference type, similar to a class, that can contain


only constants, method signatures, default methods, static methods, and
nested types. Interfaces cannot contain instance fields or constructors.

 Used to specify a contract that other classes can implement.


 A class can implement multiple interfaces, enabling multiple
inheritance-like behavior.

e) What is the use of Reader and Writer class?

 Reader class is used for reading character-based data, making it


suitable for reading text files (e.g., FileReader, BufferedReader).
 Writer class is used for writing character-based data, useful for writing
text files (e.g., FileWriter, BufferedWriter).

f) Which method is used to specify container layout with syntax?

The method setLayout() is used to specify a container's layout. Syntax:

container.setLayout(new LayoutManager());

g) What is the default layout for Frame and Panel?

 Frame: The default layout is BorderLayout.


 Panel: The default layout is FlowLayout.

h) Explain Modifiers and Access Controls used in Java.

Modifiers: Keywords that define properties of classes, methods, variables,


etc

o public, private, protected: Access control modifiers.


o static, final, abstract: Other modifiers.
o

Access Controls:

 public: Accessible from any other class.


 private: Accessible only within the class.
 protected: Accessible within the same package or subclasses.
 Default (no modifier): Accessible within the same package.

i) List and explain any 2 in-built exceptions.

1. NullPointerException: Occurs when trying to access or modify an


object reference that is null.
2. ArrayIndexOutOfBoundsException: Thrown when an invalid index is
accessed in an array.

j) Explain the purpose of getContentPane(). Define variable in Java?


What are the naming rules of variable?

 getContentPane(): In AWT or Swing, it returns the content pane of a


Frame or JFrame, where UI components are added.

· Variable in Java: A variable is a container for storing data values.

· Naming Rules:

 Must start with a letter, $, or _.


 Subsequent characters can be letters, digits, $, or _.
 Cannot be a Java keyword.
K)What is recursion?

Recursion is a process in which a method calls itself to solve a problem. It


typically involves a base case to stop the recursion.

L) Define Inheritance.

Inheritance is a mechanism in object-oriented programming where one class


(child class) inherits fields and methods from another class (parent class). It
promotes code reusability.

v) What is the difference between Array and ArrayList?

 Array:
o Fixed size.
o Can store elements of the same type.
 ArrayList:

o Dynamic size.
o Can store elements of any type (if using generics).
o Provides more functionality with methods like add(), remove(),
etc.

v) What is error? List types of error.

An error is a problem that arises during program execution that typically


cannot be recovered from.

Types:

1. Compilation Errors: Occur when there are issues in the source code
(e.g., syntax errors).
2. Runtime Errors: Occur during program execution (e.g., exceptions like
NullPointerException).
3. Logical Errors: The program runs but produces incorrect results due
to flaws in logic.

vi) List any two restrictions for applet.

1. Applets cannot perform certain network operations, like reading from or


writing to files without permission.
2. Applets cannot access system resources like the file system or interact
with other applications on the user's machine.

vii) What is an event?


An event is an occurrence that can be detected by a program, such as a user
action (clicking a button, typing in a text box), or a system-generated action
(like a timer tick). Event handling is the mechanism for responding to events.

viii) What is Object and Class?

 Class: A blueprint or template for creating objects, defining attributes


(fields) and behaviors (methods).
 Object: An instance of a class that contains real values for attributes
and can invoke the behaviors defined in the class.

Write the definition of abstract class?

An abstract class is a class that cannot be instantiated on its own and may
contain abstract methods (methods without implementation). Subclasses are
required to implement abstract methods.

x) What is Container?

A Container in Java is an object that can hold other components (such as


buttons, text fields) in a GUI application. Examples include Frame, Panel, and
JFrame.

a) What is Java? Why is Java a platform-neutral language?

Java is a high-level, object-oriented programming language developed by Sun


Microsystems (now owned by Oracle). Java is designed to be platform-neutral
because it follows the principle of "Write Once, Run Anywhere" (WORA). Java
programs are compiled into bytecode, which is executed by the Java Virtual
Machine (JVM). This bytecode can run on any platform with a JVM, making
Java platform-independent.

b) What are access specifiers? List them.

Access specifiers define the visibility or accessibility of classes, methods,


and variables in Java. The four main access specifiers are:

1. public: Accessible from any other class.


2. private: Accessible only within the same class.
3. protected: Accessible within the same package or subclasses.
4. default (no specifier): Accessible only within the same package.

c) Define Keyword-Static.

The static keyword in Java is used to indicate that a particular field, method,
or inner class belongs to the class itself rather than instances of the class. A
static member is shared by all instances of the class. Example:
d) Why do we set the environment variable in Java?

Environment variables in Java (such as JAVA_HOME and PATH) are set to


tell the system where the Java Development Kit (JDK) or Java Runtime
Environment (JRE) is located. These variables are important for running Java
programs from the command line or in an IDE.

e) Write advantages of Inheritance.

Inheritance allows:

1. Code Reusability: Reuse code from a parent class in a child class.


2. Extensibility: You can extend existing functionality in subclasses.
3. Method Overriding: Allows modifying or enhancing the behavior of
parent class methods in the child class.
4. Polymorphism: Supports method overriding and dynamic method
dispatch.

g) What is Swing?

Swing is a GUI (Graphical User Interface) toolkit in Java for building desktop
applications. It is part of the Java Foundation Classes (JFC) and provides a
rich set of GUI components, such as buttons, text fields, and tables, for
building interactive applications.

h) When is BufferedReader used?

BufferedReader is used to read text from an input stream (like a file or


console) efficiently. It buffers characters to allow for fast reading of characters,
arrays, and lines of text.

i) What is the main difference between exception and error?

 Exception: An issue that can be caught and handled in the program


using try-catch blocks. Examples include IOException, SQLException,
etc.
 Error: A severe problem that typically cannot be handled by the
program. Examples include OutOfMemoryError, StackOverflowError,
etc.

k) What is JDK? How to build and run a Java program?

JDK (Java Development Kit) is a software development kit used to develop


Java applications. It includes the JRE (Java Runtime Environment) and
various development tools like the compiler (javac), debugger, etc.

Steps to build and run a Java program:


1. Write the code in a .java file.
2. Compile the code using javac MyProgram.java.
3. Run the compiled code using java MyProgram.

l) What is the use of classpath?

Classpath is an environment variable that tells the Java runtime where to


look for class files (compiled Java code) and libraries when running a Java
program.

m) What is a collection? Explain the collection framework in detail.

A collection in Java is a framework that provides architecture to store and


manipulate a group of objects. It includes classes and interfaces like List, Set,
Queue, and Map.

The Collection Framework includes:

 Interfaces: Collection, List, Set, Queue, Map.


 Classes: ArrayList, HashSet, LinkedList, HashMap.
 Algorithms: Sorting, searching, etc.

n) What is the use of layout manager?

A Layout Manager in Java is used to arrange the components (buttons, text


fields, etc.) within a container in a particular layout. It helps to manage
component resizing and positioning automatically. Common layout managers
include BorderLayout, FlowLayout, and GridLayout.

o) What is the difference between paint() and repaint()?

 paint(): This method is called when a component needs to be redrawn.


It is invoked automatically by the AWT when needed.
 repaint(): This method requests a call to paint() by the AWT. It doesn't
directly perform the drawing, but schedules the drawing event.

p) Define keyword throw.

The throw keyword in Java is used to explicitly throw an exception from a


method or block of code.

q) Define polymorphism.

Polymorphism is the ability of an object to take many forms. In Java, this


allows methods to behave differently depending on the object that invokes
them. There are two types:
1. Compile-time polymorphism (Method Overloading).
2. Runtime polymorphism (Method Overriding).

You might also like