Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Java Unit II Answers

Uploaded by

Mohd shami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Java Unit II Answers

Uploaded by

Mohd shami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Answers to UNIT-II Introducing Classes Questions

1 a) What is mean by OOP? Illustrate the Concepts OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects.

Objects contain data in the form of fields and methods. OOP principles include Encapsulation,

Inheritance, Polymorphism, and Abstraction. These principles enhance modularity, code reuse, and

maintainability.

1 b) Write about Garbage Collector in Java and test how it works.

Garbage Collection in Java is the process of reclaiming memory by removing unused objects. The

JVM automatically performs this. To test it, you can use `System.gc()` to request garbage collection,

although it's not guaranteed to run immediately.

2 a) Define a Class, Method, and Object? Write the syntax to define these in Java.

A class is a blueprint for creating objects. A method defines the behavior of a class. An object is an

instance of a class. Syntax:

class MyClass {

void myMethod() {

System.out.println("Hello");

MyClass obj = new MyClass();

2 b) What is a Constructor? Classify the types of Constructors in Java?

A constructor is a special method used to initialize objects. Types:

1. Default Constructor: No parameters.

2. Parameterized Constructor: Accepts parameters to initialize fields.

3. Copy Constructor: Creates a copy of an object (not directly supported in Java).


3 a) Discuss the static, final keywords with an example.

`static`: Used for class-level variables and methods.

`final`: Used to declare constants or prevent inheritance/overriding.

Example:

class Example {

static int count = 0;

final int CONSTANT = 10;

3 b) Write a Java program to illustrate Constructor Overloading.

class ConstructorDemo {

int x;

ConstructorDemo() {

x = 0;

ConstructorDemo(int value) {

x = value;

4 a) What are the varargs in Java? Write the syntax and develop any program.

Varargs allow a method to accept variable arguments. Syntax: `void methodName(int... args)`

Example:

void printNumbers(int... numbers) {

for (int num : numbers) {

System.out.println(num);

}
4 b) What is Inheritance? Explain types of inheritance.

Inheritance is a mechanism where one class acquires properties and behavior of another.

Types:

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Multiple Inheritance (via interfaces)

5 a) Write about the super keyword in Java with an example.

`super` is used to refer to the immediate parent class's members.

Example:

class Parent {

void display() {

System.out.println("Parent");

class Child extends Parent {

void display() {

super.display();

System.out.println("Child");

5 b) Distinguish Method Overriding and Method Overloading.

Overriding: Redefining a parent class method in a child class.

Overloading: Defining methods with the same name but different parameters within the same class.

6) Explain about the Dynamic Method Dispatch in Java with example program.
Dynamic Method Dispatch is the process by which a call to an overridden method is resolved at

runtime.

Example:

class Parent {

void display() {

System.out.println("Parent");

class Child extends Parent {

void display() {

System.out.println("Child");

Parent obj = new Child();

obj.display(); // Calls Child's display

7) What is an abstract class? Explain all the cases to implement abstract class.

An abstract class cannot be instantiated and can have abstract and non-abstract methods. It

provides a template for subclasses.

Cases:

1. When you want to define common behavior.

2. When some methods must be implemented by child classes.

8) Write a Java program to implement inheritance concept.

class Parent {

void display() {

System.out.println("Parent Method");

}
}

class Child extends Parent {

void displayChild() {

System.out.println("Child Method");

9 a) What is a package? How to create user-defined package in Java?

A package is a namespace for organizing classes and interfaces.

Creating a package:

1. Declare `package packagename;` at the top of the Java file.

2. Compile: `javac -d . ClassName.java`

3. Access using: `import packagename.ClassName;`

9 b) What is an interface? Rules to create an interface in Java with example.

An interface is a reference type in Java that contains abstract methods. All methods are public and

abstract by default.

Rules:

1. Use the `interface` keyword.

2. Implement using the `implements` keyword.

Example:

interface MyInterface {

void display();

class MyClass implements MyInterface {

void display() {

System.out.println("Implementation");

}
}

10) Write a Java program to find the factorial value of the given number using user-defined

package concept.

package mypackage;

public class Factorial {

public int calculate(int n) {

if (n == 0) return 1;

return n * calculate(n - 1);

// In another file:

import mypackage.Factorial;

class Test {

public static void main(String[] args) {

Factorial fact = new Factorial();

System.out.println(fact.calculate(5));

You might also like