0% found this document useful (0 votes)
22 views12 pages

Java 5 Marks Answers Cleaned

Java is a high-level, object-oriented programming language known for its platform independence, simplicity, and robustness. Key concepts include JDK, JRE, and JVM, which are essential for Java development and execution, along with data types, type casting, and inheritance. The document also covers method overloading, constructors, abstract classes, interfaces, exception handling, packages, and access modifiers.

Uploaded by

yallappakolur31
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)
22 views12 pages

Java 5 Marks Answers Cleaned

Java is a high-level, object-oriented programming language known for its platform independence, simplicity, and robustness. Key concepts include JDK, JRE, and JVM, which are essential for Java development and execution, along with data types, type casting, and inheritance. The document also covers method overloading, constructors, abstract classes, interfaces, exception handling, packages, and access modifiers.

Uploaded by

yallappakolur31
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

1. What is Java? Explain its features.

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


Microsystems in 1995.
It is designed to have as few implementation dependencies as possible and allows developers to
write code once and run it anywhere.
Features of Java:
1. Platform Independent - Java programs can run on any system with JVM.
2. Object-Oriented - Everything in Java is treated as an object.
3. Simple and Secure - Easy to learn and provides strong memory management.
4. Robust - Handles exceptions and has garbage collection.
5. Multithreaded - Supports multiple threads of execution.
6. High Performance - Uses Just-In-Time (JIT) compiler.

Page 1
2. Explain the concept of JDK, JRE, and JVM.

JDK (Java Development Kit):


- A software development environment used to develop Java applications.
- Contains JRE, compiler (javac), debugger, and development tools.

JRE (Java Runtime Environment):


- Provides libraries, JVM, and other components to run Java applications.
- It does not have development tools like compiler.

JVM (Java Virtual Machine):


- It is the engine that executes Java bytecode.
- Converts bytecode to machine-specific code at runtime.

Page 2
3. What are data types in Java?

Data types specify the type of data a variable can store in Java.
They are divided into two types:

1. Primitive Data Types:


- byte, short, int, long (integer types)
- float, double (floating-point types)
- char (character)
- boolean (true/false)

2. Non-Primitive Data Types:


- String, Arrays, Classes, Interfaces
- These are created by the programmer.

Page 3
4. Explain the concept of type casting in Java.

Type casting is the process of converting one data type into another.
There are two types of casting in Java:

1. Widening Casting (Implicit):


- Converting a smaller type to a larger type size.
- Example: int to float

2. Narrowing Casting (Explicit):


- Converting a larger type to a smaller type.
- Example: double to int
- Requires manual casting syntax: int x = (int) 5.5;

Page 4
5. What is the difference between == and equals() in Java?

The '==' operator:


- Compares object references (memory location).
- Used for primitive data types.
- Example: str1 == str2 returns true if both refer to the same object.

The equals() method:


- Compares the actual content of objects.
- Used in classes like String.
- Example: [Link](str2) returns true if the strings are equal in content.

Page 5
6. Explain the concept of inheritance in Java.

Inheritance allows one class (subclass) to acquire properties and behavior of another class
(superclass).
It promotes code reuse and establishes a parent-child relationship.
Syntax: class B extends A {}

Types of Inheritance in Java:


1. Single
2. Multilevel
3. Hierarchical

Note: Java does not support multiple inheritance with classes to avoid ambiguity (diamond problem).

Page 6
7. What is method overloading and method overriding?

Method Overloading:
- Multiple methods with same name but different parameters.
- Occurs within the same class.
- Compile-time polymorphism.

Method Overriding:
- Redefining a superclass method in subclass.
- Same method signature.
- Run-time polymorphism.
- Uses @Override annotation.

Page 7
8. What is constructor? Explain its types.

Constructor is a special method used to initialize objects.


- It has the same name as the class.
- It does not have a return type.

Types of Constructors:
1. Default Constructor - No parameters, provided by Java if none is written.
2. Parameterized Constructor - Takes arguments to initialize fields.

Example:
class A {
A(int x) {
// parameterized constructor
}
}

Page 8
9. Explain the concept of abstract class and interface.

Abstract Class:
- A class that cannot be instantiated.
- May contain abstract (without body) and non-abstract methods.
- Used for base class functionality.

Interface:
- Blueprint of a class.
- All methods are abstract by default (till Java 7).
- Supports multiple inheritance.
- Implemented using 'implements' keyword.

Page 9
10. What is exception handling in Java?

Exception Handling deals with runtime errors to maintain normal flow of application.
Handled using:
1. try - code that may cause exception
2. catch - handles the exception
3. finally - executes after try/catch
4. throw - throws an exception
5. throws - declares exceptions

Example:
try {
int a = 5/0;
} catch(ArithmeticException e) {
[Link](e);
}

Page 10
11. What are packages in Java?

Packages are used to group related classes and interfaces.


Helps in organizing code, avoiding name conflicts, and access control.
Types:
1. Built-in (e.g., [Link], [Link])
2. User-defined

To define a package:
package mypackage;

To import:
import [Link];

Page 11
12. Explain access modifiers in Java.

Access modifiers define the scope of class members.


Types:
1. private - accessible within the same class
2. default - accessible within the same package
3. protected - accessible within same package and subclasses
4. public - accessible from anywhere

Example:
public class A {
private int x;
}

Page 12

You might also like