■ Java Assignment Notes
AS-1
Q1. Define the features of OOPS (ODPA).
Answer: Main features of OOPS are:
- Encapsulation – Binding data and methods together into a single unit (class).
- Abstraction – Showing only essential details and hiding the implementation.
- Inheritance – One class can acquire the properties of another class.
- Polymorphism – One task can be performed in different ways.
- Modularity – Dividing program into small modules.
- Reusability – Using the code multiple times.
Q2. What is String? With example.
Answer: A String is a sequence of characters enclosed in double quotes (""). In Java, String is a
class present in [Link] package.
Example:
String name = "Hello Java";
[Link](name);
Output: Hello Java
Q3. What is inheritance? Define it with example.
Answer: Inheritance is a feature of OOPS that allows one class (child) to acquire properties and
methods of another class (parent).
Example:
class Animal { void sound() { [Link]("Animal makes sound"); } }
class Dog extends Animal { void bark() { [Link]("Dog barks"); } }
public class Test { public static void main(String[] args) { Dog d = new Dog(); [Link](); [Link](); } }
Output:
Animal makes sound
Dog barks
Q4. What is constructor?
Answer: A constructor is a special method in Java that is automatically called when an object is
created. It has the same name as the class and does not have a return type.
Example:
class Student { Student() { [Link]("Constructor called"); } public static void main(String[]
args) { Student s = new Student(); } }
Q5. Define interface.
Answer: An interface in Java is a collection of abstract methods. It is used to achieve abstraction
and multiple inheritance.
Example:
interface Animal { void eat(); }
class Dog implements Animal { public void eat() { [Link]("Dog eats"); } }
AS-2
Q1. Define the operator in Java.
Answer: Operators are special symbols that perform operations on variables and values.
Types: Arithmetic, Relational, Logical, Assignment, Increment/Decrement, etc.
Q2. Define the control statement in Java.
Answer: Control statements decide the flow of program execution.
Types:
- Decision making: if, if-else, switch
- Looping: for, while, do-while
- Jump: break, continue, return
Q3. Define the Java Development Kit (JDK).
Answer: JDK is a software development kit used to develop Java applications. It contains JRE,
Compiler (javac), and development tools.
Q4. How many keywords in Java? Write the names.
Answer: Java has 51 keywords (Java 17 and above).
Examples: class, public, static, void, if, else, switch, for, while, do, break, continue, try, catch, return,
extends, implements, interface, import, package, new, this, super, final, abstract, synchronized,
throws, throw, etc.
Q5. What is JVM?
Answer: JVM (Java Virtual Machine) is an engine that runs Java bytecode and makes Java
platform-independent. It provides memory management, security, and garbage collection.
AS-3
Q1. Define data type in Java.
Answer: Data types specify the type of data a variable can store.
Types:
- Primitive: int, float, char, boolean, byte, short, long, double.
- Non-primitive: String, Array, Class, Object.
Q2. What is type casting?
Answer: Type casting is converting one data type into another.
- Widening (automatic): int → double
- Narrowing (manual): double → int
Example:
int a = 10; double b = a; // widening
double x = 9.7; int y = (int) x; // narrowing
Q3. Define the Thread.
Answer: A Thread is a lightweight process that runs independently inside a program. It is used for
multitasking in Java.
Example:
class MyThread extends Thread { public void run() { [Link]("Thread is running"); }
public static void main(String[] args) { MyThread t = new MyThread(); [Link](); } }
Q4. Ways to sum of array element.
Answer:
public class SumArray { public static void main(String[] args) { int arr[] = {10, 20, 30, 40}; int sum =
0; for(int i=0; i