short ans oop
short ans oop
Example:
3. What is type casting
java
Definition: Converting one data type into Copy code
class Demo {
another. static int z = 30; // Static variable
Types: }
Example:
10. Why java became platform independent
language? Explain. java
Copy code
Bytecode: Java programs compile into int[] arr = {1, 2, 3, 4, 5}; // Declaration and initialization
System.out.println(arr[0]); // Output: 1
intermediate bytecode.
JVM: Each platform has its own JVM that
interprets the bytecode into machine code. 17. What is the difference between a single-
Example: Write on Windows, run on Linux without dimensional array and a multi-dimensional
changes. array?
13. What is the difference between == 18. Enlist types of constructor. Explain any
operator and .equals () method in Java? two with example.
14. Define a class and object. Write syntax to Default Constructor: No parameters.
create class and object with an Example. Example:
java
Copy code
• Class: Blueprint for objects; defines class Demo {
properties and behavior. Demo() {
• Object: Instance of a class. System.out.println("Default Constructor");
}
class ClassName { }
// Fields Parameterized Constructor: Accepts
// Methods parameters.
} Example:
java
Copy code
ClassName obj = new ClassName(); // Object creation
class Demo {
class Car {
Demo(String msg) {
String model;
System.out.println("Message: " + msg);
int year;
}
}
void display() {
System.out.println("Model: " + model + ", Year: " + 20. State three uses of final keyword.
year);
} Final Variable: Prevents modification of a
} variable.
java
public class Main { Copy code
public static void main(String[] args) { final int x = 10;
Car car = new Car(); // Create object // x = 20; // Error
car.model = "Tesla"; Final Method: Prevents method overriding.
car.year = 2022; java
car.display(); Copy code
} final void display() { }
} Final Class: Prevents inheritance.
java
Copy code
15. What is the difference between a default final class A { }
constructor and a parameterized constructor?
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("From Interface A");
}
public void methodB() {
System.out.println("From Interface B");
}
23. Define a class and object. Write syntax to }
create class and object with an public class Main {
example.Which are public static void main(String[] args) {
C obj = new C();
the restrictions present for static declared obj.methodA();
methods? obj.methodB();
}
}
• Class: Blueprint for objects, containing
fields and methods.
• Object: Instance of a class. 25. What is abstraction in Java? How do you
implement it?
Syntax:
• Definition: Hiding implementation details
java
and showing only essential features.
Copy code
class ClassName { • Implementation: Achieved using abstract
int x; // Field classes or interfaces.
void method() { } // Method
} Example (Abstract Class):
ClassName obj = new ClassName(); // Object
java
Restrictions of static Methods: Copy code
abstract class Shape {
1. Cannot use non-static fields or methods abstract void draw();
}
directly.
2. Cannot use this or super. class Circle extends Shape {
void draw() {
Example: System.out.println("Drawing Circle");
}
java }
Copy code
class Demo {
static void display() {
System.out.println("Static Method");
// Cannot access non-static methods or variables 26. What is the significance of the ‘super’
} keyword in Java?
}
• Refers to the parent class’s members. Example:
29. Which are the ways to access package public class Main {
from another package? Explain with example. public static void main(String[] args) {
MyClass obj = new MyClass();
obj.show();
1. mport the Package: }
}
java
Copy code
import packageName.ClassName;
java
Copy code 31. What is single level inheritance? Explain
packageName.ClassName obj = new
packageName.ClassName(); with suitable example.
• Definition: A child class inherits from a
single parent class.
• Purpose: Promotes code reuse. 34. Explain inheritance and polymorphism
features of Java.
Example:
Inheritance: A child class inherits properties
java and methods of a parent class. Example:
Copy code
class Parent { java
void display() { Copy code
System.out.println("Parent Class"); class Parent {
} void show() {
} System.out.println("Parent");
}
class Child extends Parent { }
void show() {
System.out.println("Child Class"); class Child extends Parent { }
}
}
Polymorphism: The ability to perform a single
public class Main { action in multiple ways. Example:
public static void main(String[] args) {
Child obj = new Child(); java
obj.display(); // Parent Class Copy code
obj.show(); // Child Class class Demo {
} void show() {
} System.out.println("Base Class");
}
}
32. What is package? State how to create and
class Derived extends Demo {
access user defined package in Java.
void show() {
System.out.println("Derived Class");
• Package: A namespace for organizing }
classes and interfaces. }
• Creation: Use the package keyword.
• Access: Use import or fully qualified name.
35. What is the multiple inheritance? Write a
java program to implement multiple
inheritance.
33. What is meant by interface? State its need
and write syntax and features of interface. • Definition: A class inherits from multiple
parent classes/interfaces.
• Definition: A contract that defines a set of • Java: Achieves multiple inheritance using
methods that implementing classes must interfaces.
provide.
• Need: Example:
o Supports multiple inheritance.
o Achieves abstraction. java
Copy code
interface A {
Syntax: void methodA();
}
java
Copy code interface B {
interface InterfaceName { void methodB();
void method(); }
}
class C implements A, B {
Features: public void methodA() {
System.out.println("From Interface A");
}
1. Methods are public and abstract by default. public void methodB() {
2. Can include static and default methods. System.out.println("From Interface B");
3. Variables are public, static, and final. }
}
obj.show(); // Overridden Method
}
36. What is package? How do we create it? }
Give the example to create and to access 38. What is importance of super and this
package. keyword in inheritance ? Illustrate with
suitable
Definition: A collection of related classes and Example
interfaces.
• super: Refers to the parent class. Used to
Example:
call parent methods, variables, or
java
constructors.
Copy code • this: Refers to the current object. Used to
// File: mypackage/Hello.java call current class methods, variables, or
package mypackage; constructors.
public class Hello {
public void greet() {
Example:
System.out.println("Hello from Package");
} java
} Copy code
class Parent {
// Access: void display() { System.out.println("Parent Method"); }
import mypackage.Hello; }
class Child extends Parent {
public class Main { void display() { System.out.println("Child Method"); }
public static void main(String[] args) { void show() {
Hello obj = new Hello(); this.display(); // Current class
obj.greet(); super.display(); // Parent class
} }
} }
37. Explain method overriding with suitable 39. What is thread ? Draw thread life cycle
example diagram in Java.
Thread: A small part of a process that runs
• Definition: A subclass provides a specific independently.
implementation of a method already Thread Life Cycle:
defined in the parent class.
• Rules: 1. New → start()
o Method must have the same name,
2. Runnable → Waiting for CPU.
return type, and parameters. 3. Running → Executing.
o Subclass method cannot have
4. Blocked/Waiting → Temporarily paused.
stricter access. 5. Terminated → Finished execution.
Example: 40. What is thread priority ? Write default
java
priority values and methods to change
Copy code them.What is
class Parent {
void show() { exception ?Thread Priority: Sets execution
System.out.println("Parent Method"); importance.
}
}
o Default Priority: 5
class Child extends Parent { o Range: 1 (Low) to 10
@Override (High).Methods:
void show() {
System.out.println("Overridden Method"); • setPriority(int)
} • getPriority()
}
43. With syntax and example explain try • Determines the importance of a thread.
& catch statement. Higher priority threads are executed before
lower priority ones.
Syntax: • Priority range: 1 (lowest) to 10 (highest).
Default priority is 5.
java
Copy code (ii) Types of Exception:
try {
// Risky code
} catch (ExceptionType e) { • Checked Exception: Exceptions that are
// Handle exception checked at compile-time (e.g., IOException,
} SQLException).
• Unchecked Exception: Exceptions that
Example: are checked at runtime (e.g.,
NullPointerException, ArithmeticException).
java
Copy code
try {
int[] arr = new int[5];
arr[10] = 50;
} catch (Exception e) {
System.out.println("Error: " + e);
}
46. Write any four methods of file class with
their use.
exists(): Reader:
// Display values
System.out.println("Integer: " + num);
System.out.println("Double: " + price);
System.out.println("Character: " + grade);
System.out.println("Boolean: " + isActive);
}