0% found this document useful (0 votes)
5 views10 pages

Java

The document covers key concepts in Java, including interfaces, the Scanner class for file reading, differences between built-in and user-defined packages, exceptions versus errors, and the Java API. It provides syntax examples for defining interfaces and packages, as well as methods for handling exceptions. Additionally, it discusses Java I/O operations and common String methods.
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)
5 views10 pages

Java

The document covers key concepts in Java, including interfaces, the Scanner class for file reading, differences between built-in and user-defined packages, exceptions versus errors, and the Java API. It provides syntax examples for defining interfaces and packages, as well as methods for handling exceptions. Additionally, it discusses Java I/O operations and common String methods.
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

UNIT – III

1. Describe the interface in Java along with its


syntax. How is an interface defined? with a
simple example.
An interface in Java is a blueprint of a class that contains abstract methods
(methods without body) and constants. It is used to achieve abstraction and
multiple inheritance.
Syntax:
public interface InterfaceName
{
// Abstract method
void methodName();
// Constant
int CONSTANT_VALUE = 100;
}
How to Define and Use an Interface
• Use the keyword interface
• Methods are public and abstract by default
• Variables are public, static, and final by default
• A class implements an interface using implements

• Example
• interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
[Link]("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}

2. Explain how the Scanner class is used to read


data from a file in Java. Provide a suitable
example.
The Scanner class (from [Link]) is used to read input from different sources,
including files.
Steps
1. Import required classes
2. Create a File object
3. Pass the file to Scanner
4. Use methods like nextLine(), nextInt()

5. Example
6. import [Link];
import [Link];
import [Link];

public class ReadFileExample {


public static void main(String[] args) {
try {
File file = new File("[Link]");
Scanner sc = new Scanner(file);

while ([Link]()) {
String data = [Link]();
[Link](data);
}

[Link]();
} catch (FileNotFoundException e) {
[Link]("File not found");
}
}
}
3. Describe the difference between built-in
packages and user-defined packages in Java.
Feature Built-in Packages User-defined Packages

Predefined packages provided Created by the


Definition
by Java programmer

mypackage,
Example [Link], [Link], [Link]
[Link]

Available by default or via Must be created


Availability
import manually

Provides standard classes


Usage Organizes user classes
(Scanner, ArrayList)

Created using package


Creation No need to create
keyword

4. Explain the difference between exceptions and


errors in Java with examples.
Basis Exceptions Errors

Problems that occur during Serious problems related


Meaning
program execution to system/JVM

Can be handled using try- Cannot be handled


Handling
catch normally

Programming mistakes or System failure or lack of


Cause
user input resources

Program can continue after Program usually


Recovery
handling terminates

ArithmeticException, OutOfMemoryError,
Examples
NullPointerException StackOverflowError
5. Describe the concept of package in Java and
write the syntax used to define a package
A package in Java is a collection of related classes and interfaces grouped
together. It helps in:
• Organizing code properly
• Avoiding name conflicts
• Providing access control
• Making code reusable and easier to maintain
For example, Java provides built-in packages like [Link] and [Link].
Syntax to Define a Package
package package_name;

Example
package mypackage;

public class Hello {


public void show() {
[Link]("Hello from package");
}
}

6. Explain the purpose of try, catch, and finally


blocks in Java exception handling
1. try Block
• Contains the code that may cause an exception
• Must be followed by catch or finally
try {
int a = 10 / 0; // may cause exception
}

2. catch Block
• Handles the exception thrown in the try block
• You can have multiple catch blocks
catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero");
}

3. finally Block
• Always executes whether an exception occurs or not
• Used for cleanup (like closing files, releasing resources)
finally {
[Link]("This always executes");
}
UNIT-IV

1. Describe the Java API and explain how it is used


in Java programs.
The Java API is a collection of pre-written classes, interfaces, and methods
provided by Java. It helps programmers perform common tasks without writing
code from scratch.
It is organized into packages like:
• [Link] (basic classes like String, Math)
• [Link] (utility classes like Scanner, ArrayList)
• [Link] (input/output operations)
How it is Used in Java Programs
1. Import the package
import [Link];
2. Use the class
Scanner sc = new Scanner([Link]);
3. Call methods
int num = [Link]();
Example Program
import [Link];

public class TestAPI {


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

[Link]("Enter number:");
int n = [Link]();

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


}
}

Q1. Describe the Java API


The Java API (Application Programming Interface) is a collection of predefined classes,
interfaces, and methods provided by Java.

It helps programmers:

• Perform common tasks easily

• Avoid writing code from scratch

• Improve efficiency and speed

Q2. List the Java API (Common Packages)


Some commonly used Java API packages are:

• [Link] → Basic classes (String, Math)

• [Link] → Utility classes (Scanner, ArrayList)

• [Link] → Input/Output operations

• [Link] → Networking

• [Link] → GUI components


Q3. How Java API is Used in Code
Steps

1. Import the package

2. Create object of class

3. Use methods

Example

import [Link];

public class Test {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link]();
[Link](num);
}
}

Q5. Five Methods of Java String


1. length() → returns string length

String s = "Hello";
[Link]([Link]()); // 5

2. toUpperCase() → converts to uppercase

[Link]([Link]()); // HELLO

3. toLowerCase() → converts to lowercase

[Link]([Link]()); // hello

4. charAt() → returns character at index

[Link]([Link](1)); // e
5. equals() → compares two strings

[Link]([Link]("Hello")); // true

Q12. Java I/O (Input/Output) with Example


Java I/O is used to read input and write output using classes from [Link] package.

Example (Reading from File)

import [Link];
import [Link];

public class IOExample {


public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("[Link]");
int i;

while ((i = [Link]()) != -1) {


[Link]((char) i);
}

[Link]();
}
}

Explanation

• FileReader → reads file data

• read() → reads one character

• Loop continues till end of file

• close() → closes file

You might also like