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

Java Code Output and Explanation Guide

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)
15 views4 pages

Java Code Output and Explanation Guide

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

Find Output for below codes alongwith explanation for each.

Q1-

class A {
A() {
[Link]("Constructor A");
}
}

class B extends A {
B() {
[Link]("Constructor B");
}
}

class C extends B {
C() {
[Link]("Constructor C");
}

public static void main(String[] args) {


C obj = new C();
}
}

Q2-

class Animal {
void sound() {
[Link]("Animal makes sound");
}
}

class Dog extends Animal {


void sound() {
[Link]("Dog barks");
}
}

class Cat extends Animal {


void sound() {
[Link]("Cat meows");
}

public static void main(String[] args) {


Animal a1 = new Dog();
Animal a2 = new Cat();
[Link]();
[Link]();
}
}

Q3-

interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

class Duck implements Flyable, Swimmable {


public void fly() {
[Link]("Duck flies a short distance");
}
public void swim() {
[Link]("Duck swims in the pond");
}

public static void main(String[] args) {


Flyable f = new Duck();
Swimmable s = new Duck();
[Link]();
[Link]();
}
}

Q4-

import [Link].*;
public class FileExample {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
int number = [Link]([Link]());
[Link]("Number: " + number);
[Link]();
} catch (IOException e) {
[Link]("File error: " + [Link]());
} catch (NumberFormatException e) {
[Link]("Invalid number format");
} finally {
[Link]("Execution completed");
}
}
}

Q5-

class Parent {
void show() throws Exception {
[Link]("Parent show()");
}
}

class Child extends Parent {


void show() {
[Link]("Child show()");
}

public static void main(String[] args) {


Parent p = new Child();
try {
[Link]();
} catch (Exception e) {
[Link]("Exception caught");
}
}
}

You might also like