Question

In each of the following questions, first use NetBeans IDE to run the code (if there...

In each of the following questions, first use NetBeans IDE to run the code (if there is an error fix it) then take a screenshot of the output and paste it under (A).

In (B), justify the output you obtained in (A).

-

9. Given the following:

public class WorkingSheet {

    public static void main(String[] args) {

       B myB = new B();

       A myA = new B();

       System.out.print(myB instanceof A);

       System.out.print(myB instanceof C);

       System.out.print(myA instanceof B);    }

}

class B extends A implements C, D {

    int getID() {return 6;}}

class A {public int name;}

interface C {public final int a = 1;}

interface D {public final int b = 2;}

(A) What is the output of running the above program ?

(B) Justify your answer in (A)

10. Given the following,

public class Figures {

    public static void main(String[] args) {

        printFigure(8);

    }

    static void printFigure(int num) {

        if (num <= 2) {

            System.out.println(" A ");

        } else {

            System.out.println(" B ");

            printAnotherFigure(num - 2);

        }

    }

    static void printAnotherFigure(int n) {

        if (n <= 2) {

            System.out.println(" C ");

        } else {

            System.out.println(" D ");

            printFigure(n - 1);

        }

    }

}

(A) What is the output of running the above program?

(B) Justify your answer in (A)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer to this question is as follows:

The answer to the first question is as follows:

We are creating the object myB as the object for class B then class B is inheriting the class A and inheriting the class c and class D this is called as the multiple inheritances

when the statement myB is an instance of A is true because class B extends class A so the output is true

for the myB is an instance of c is true because class B extends the class c so the output is true

for the myA is an instance of B is also true because we are implementing with the polymorphism of class B so the output is true

so the output is true true true

The answer to the second question is :

B
D
B
D
A

First, we are calling printFigure(8) then 8<=2 false then else will be executed then the value B is printed

then we are calling the printAnotherFigure(num-2) i.e printAnotherFigure(6) then the value 6<=2 false then the value D is printed then

we are calling the printFigure(num-1) i.e printFigure(5) i.e 5<=2 false then B is printed

then we are calling printAnotherFigure(3) i.e 3<=2 false then D is printed then calling printFigure(2) is called then value 2<=2 is true it prints the value A

Add a comment
Know the answer?
Add Answer to:
In each of the following questions, first use NetBeans IDE to run the code (if there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show the output of running the class Test in the following code lines: interface A {...

    Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B extends C implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b...

  • Show the output of running the class Test in the following code lines: a) Nothing. b)...

    Show the output of running the class Test in the following code lines: a) Nothing. b) b is an instance of A followed by b is an instance of c c) b is an instance of C d) b is an instance of A interface A { void print (); } class C {} class B extends c implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new...

  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT