Paper 2 Java
Paper 2 Java
for 2
marks
Answer :In Java, a variable is a container that holds data that can be changed during
program execution. Each variable has a name (identifier), a type, and a value.
1. Valid Characters: Variable names can include letters (a-z, A-Z), digits (0-9), underscores (_),
and dollar signs ($). However, they cannot start with a digit.
2. Case Sensitivity: Variable names are case-sensitive (e.g., myVar and myvar are different).
3. No Reserved Keywords: Variable names cannot be the same as Java reserved keywords (e.g.,
class, public, void).
4. Length: There is no specific limit to the length of a variable name, but it should be descriptive
enough to convey its purpose.
Example:
int age;
String firstName;
double $price;
These rules help ensure that variable names are meaningful and do not conflict with
Java's syntax.
Key Points:
Example:
If you want to calculate the factorial of a number (e.g., 5), you can use recursion:
factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1 (base case)
Answer :In Java, an error refers to a problem in the code that prevents it from
running properly. Errors can occur for various reasons, and they can be classified into
several types:
Syntax Errors:
1.
o These occur when the code violates the grammar rules of Java. For example, missing
semicolons, incorrect parentheses, or misspelled keywords.
o Example: int number = 5 // Missing semicolon
Runtime Errors:
2.
o These errors occur while the program is running. They can happen due to invalid
operations, like dividing by zero or accessing an index that doesn't exist in an array.
o Example: int result = 10 / 0; // Division by zero
Logical Errors:
3.
o These occur when the program runs without crashing, but the output is incorrect
due to mistakes in the logic of the code. The program behaves as intended but gives
wrong results.
o Example: Using the wrong formula to calculate a value.
Question :List any two restriction for applet?for 2 marks use easy
language
No File System Access: Applets cannot access the local file system of the
user's computer for security reasons. They cannot read or write files on the
user's hard drive.
2.
Limited Network Access: Applets can only communicate with the server
from which they were downloaded. They cannot connect to other servers or
domains, which helps prevent malicious activity.
Answer :In Java, classes and objects are fundamental concepts in object-oriented
programming.
Class:
A class is a blueprint or template that defines the properties (attributes) and behaviors
(methods) of an object. It describes what an object will look like and what it can do.
Example: A Car class might have attributes like color, model, and year, and methods
like drive() and brake().
Object:
An object is an instance of a class. It is created from a class and represents a specific entity
with actual values for the class’s attributes. Each object can have its own state and behavior.
Example: If Car is a class, then a specific car, like a red 2020 Toyota Camry, is an object of
that class.
Answer :In Java, a container is a special type of component that holds and organizes
other components (like buttons, text fields, and labels) in a graphical user interface
(GUI). Containers manage the layout and display of these components, making it
easier to create user-friendly interfaces.
Types of Containers:
1. Frame: A window that can hold other components. It usually has a title and can be resized.
2. Panel: A smaller container that groups components together. It can be used within other
containers.
Summary:
Containers are essential for organizing and displaying GUI components in Java
applications, helping to create structured and visually appealing interfaces.
Answer :
Packages in Java are collections of classes, interfaces, and sub-packages grouped
together to organize code and avoid name conflicts. They are like folders for your
Java programs.
Answer :An exception in Java is an event that disrupts the normal flow of a program's
execution. It indicates that an error or an unexpected condition has occurred, which
can be handled to prevent the program from crashing.
try: This block is used to enclose the code that might throw an exception. It
allows you to test a block of code for errors.
catch: This block follows a try block and is used to handle the exception. It
catches the exception thrown by the try block and allows the program to continue
running.
finally: This block is optional and follows the catch block. It contains code
that will execute regardless of whether an exception was thrown or caught. It's
typically used for cleanup actions, like closing files or releasing resources.
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
} finally {
Collections Framework:
o The java.util package provides interfaces and classes for working with
collections (groups of objects), including:
o The Random class allows you to generate random numbers in various ways .
Answer :In Java applets, handling events involves responding to user actions like
mouse clicks, keyboard input, or other interactions. This is typically done using event
listeners, which are interfaces that listen for specific events and define methods to
handle those events.
Summary:
Answer :A layout manager in Java is a class that controls the size and position of
components (like buttons, labels, and text fields) within a container (like a frame or
panel). Layout managers help in organizing the user interface in a consistent and
flexible manner, allowing for better management of component alignment and
spacing, especially when the window is resized.
1. FlowLayout
2. BorderLayout
3. GridLayout
4. GridBagLayout
5. BoxLayout
BorderLayout is one of the most commonly used layout managers in Java. It divides
the container into five regions: North, South, East, West, and Center. Components can
be added to these regions, and the layout manager automatically arranges them.
Five Areas:
Component Stretching: The components added to the North, South, East, and
West regions will be stretched to fill the available space, while the Center
component expands to occupy the remaining area.
Answer :
An anonymous class in Java is a class without a name, created on the fly. It is used
when you need a one-time implementation of a class, typically for interfaces or
abstract classes.
Key Points:
interface Greeting {
void sayHello();
}
Answer :In Java, predefined streams are the default streams provided by the Java
runtime for input and output operations. These streams are associated with the console
and are part of the System class.
System.in:
System.out:
System.err: