0% found this document useful (0 votes)
15 views8 pages

Answers To Java IIA Question Bank

Uploaded by

eafcmobile822
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views8 pages

Answers To Java IIA Question Bank

Uploaded by

eafcmobile822
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Answers to JAVA IIA Question Bank

1. Write a Java program to implement multilevel inheritance with 3 levels of hierarchy.

Multilevel inheritance involves creating a chain of classes where one class is derived from another.

Example code:

class A {

void displayA() { System.out.println("Class A"); }

class B extends A {

void displayB() { System.out.println("Class B"); }

class C extends B {

void displayC() { System.out.println("Class C"); }

public class Test {

public static void main(String[] args) {

C obj = new C();

obj.displayA();

obj.displayB();

obj.displayC();

2. Explain how an interface is used to achieve multiple Inheritances in Java.

In Java, interfaces are used to achieve multiple inheritance since a class can implement multiple

interfaces. Example:
interface A {

void methodA();

interface B {

void methodB();

class C implements A, B {

public void methodA() { System.out.println("Method A"); }

public void methodB() { System.out.println("Method B"); }

public class Test {

public static void main(String[] args) {

C obj = new C();

obj.methodA();

obj.methodB();

3. Compare and contrast method overloading and method overriding with suitable example.

Method overloading occurs within the same class, methods have the same name but different

parameters. Method overriding occurs in a subclass, where a method from the parent class is

redefined. Examples:

// Overloading

class OverloadingExample {

int add(int a, int b) { return a + b; }


double add(double a, double b) { return a + b; }

// Overriding

class Parent {

void display() { System.out.println("Parent class"); }

class Child extends Parent {

void display() { System.out.println("Child class"); }

4. Define inheritance and list the different types of inheritance in Java.

Inheritance is the mechanism where one class acquires the properties and behaviors of another

class. Types:

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Multiple Inheritance (via interfaces)

5. Hybrid Inheritance (combination).

5. What is abstract class and abstract method? Explain with an example.

Abstract classes in Java have at least one abstract method and cannot be instantiated. Abstract

methods have no body. Example:

abstract class Shape {

abstract void draw();

class Circle extends Shape {


void draw() { System.out.println("Drawing Circle"); }

public class Test {

public static void main(String[] args) {

Shape s = new Circle();

s.draw();

6. Illustrate the usage of super keyword in Java with a suitable example. Also explain the

Dynamic dispatch method.

The `super` keyword is used to refer to the immediate parent class. Dynamic dispatch determines

the method to invoke at runtime. Example:

class Parent {

void display() { System.out.println("Parent class"); }

class Child extends Parent {

@Override

void display() { System.out.println("Child class"); }

public class Test {

public static void main(String[] args) {

Parent obj = new Child(); // Dynamic dispatch

obj.display();

}
7. Develop a Java program to demonstrate polymorphism using a Shape class.

class Shape {

void draw() { System.out.println("Drawing Shape"); }

class Circle extends Shape {

void draw() { System.out.println("Drawing Circle"); }

class Square extends Shape {

void draw() { System.out.println("Drawing Square"); }

public class Test {

public static void main(String[] args) {

Shape s;

s = new Circle();

s.draw();

s = new Square();

s.draw();

8. Define package. Explain the steps to create a user-defined package with an example.

A package is a namespace for organizing classes and interfaces. Steps:

1. Create a directory for the package.

2. Declare the package name using `package` keyword.

3. Compile and run using `javac` and `java`.

Example:
package mypack;

public class MyClass {

public void display() { System.out.println("Hello"); }

9. Explain the concept of importing packages in Java.

Importing allows the use of classes from other packages using the `import` keyword. Example:

import java.util.*;

public class Test {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number: ");

int n = sc.nextInt();

System.out.println("You entered: " + n);

10. Demonstrate the working of a nested try block with an example.

Nested try blocks handle exceptions in a specific segment of code. Example:

public class Test {

public static void main(String[] args) {

try {

try {

int a = 10 / 0;

} catch (ArithmeticException e) {
System.out.println("Inner catch: " + e);

} catch (Exception e) {

System.out.println("Outer catch: " + e);

11. Define Exception. Explain exception handling mechanism in Java.

Exceptions are errors disrupting normal program flow. Java handles them using `try`, `catch`,

`finally`, and `throw`. Example:

try {

int a = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Caught: " + e);

12. Build a Java program to create a package 'balance' containing Account class.

Package example:

package balance;

public class Account {

public void displayBalance() {

System.out.println("Balance: $1000");

}
// Import package

import balance.Account;

public class Test {

public static void main(String[] args) {

Account acc = new Account();

acc.displayBalance();

13. Examine the various levels of access protections available for packages.

Access levels:

1. Public: Accessible everywhere.

2. Protected: Accessible within package and subclasses.

3. Default: Accessible within package.

4. Private: Not accessible outside the class.

You might also like