Question

In: Computer Science

What is the output produced by each statement below? If the statement causes an error, comment...

What is the output produced by each statement below? If the statement causes an error, comment it out and write either "compiler error" or "runtime error" as appropriate. For each line make your guess and then move the open comment down a line to test your theory.

public class Main {
    public static void main(String[] args) {
        First var1 = new Second();
        First var2 = new Third();
        First var3 = new Fourth();
        Second var4 = new Third();
        Object var5 = new Fourth();
        Object var6 = new Second();

/*
        var1.method2();
        var2.method2();
        var3.method2();
        var4.method2();
        //var5.method2();
        //var6.method2();
        var1.method3();
        ((Third)var4).method1();
        var2.method3();
        var3.method3();
        var4.method3();
        var5.method3();
        var6.method3();
        ((Third)var4).method1();
        ((Third)var4).method1();
        ((Second)var5).method2();
        ((First)var5).method3();
        ((Third)var5).method1();
        ((First)var6).method3();
        ((Second)var6).method1();
        ((Second)var6).method3();
        ((Third)var6).method2();
*/
    }
}

class First extends Object{
    public void method2() {
        System.out.println("First2");
    }
    public void method3() {
        method2();
    }
}

class Second extends First {
    public void method2() {
        System.out.println("Second2");
    }
}

class Third extends Second {
    public void method1() {
        System.out.println("Third1");
        super.method2();
    }
    public void method2() {
        System.out.println("Third2");
    }
}

class Fourth extends First {
    public void method1() {
        System.out.println("Fourth1");
    }
    public void method2() {
        System.out.println("Fourth2");
    }
}

Solutions

Expert Solution

Required Solution-

Code version-

The corresponding outputs or errors are written as comment in front of each statement.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
  public static void main(String[] args) {
        First var1 = new Second();
        First var2 = new Third();
        First var3 = new Fourth();
        Second var4 = new Third();
        Object var5 = new Fourth();
        Object var6 = new Second();

        var1.method2();// output- Second2
        var2.method2();//output- Third2
        var3.method2();//output - Fourth2
        var4.method2();//output - Third2
        //var5.method2();//compilation error, error: cannot find symbol
        //var6.method2();//compilation error, error: cannot find symbol
        var1.method3();//output - Second2
        ((Third)var4).method1();//output- Third1 Second2
        var2.method3();// output- Third2
        var3.method3();
        var4.method3();//output - Fourth2
        //var5.method3();//compilation error, error: cannot find symbol
        //var6.method3();//compilation error, error: cannot find symbol
        ((Third)var4).method1();//output - Third2 Third1 Second2
        ((Third)var4).method1();//Output - Third1 Second2
        //((Second)var5).method2();// Runtime error, java.lang.ClassCastException: Fourth cannot be cast to Second
        ((First)var5).method3();// output - Fourth2
        //((Third)var5).method1();// Runtime error, java.lang.ClassCastException:: Fourth cannot be cast to Second
       ((First)var6).method3();// output - Second2
        //((Second)var6).method1();//compilation error, error: cannot find symbol
       ((Second)var6).method3();//output - Second2
        //((Third)var6).method2();//Runtime error, java.lang.ClassCastException: Second cannot be cast to Third
        
    }
}
class First extends Object{
    public void method2() {
        System.out.println("First2");
    }
    public void method3() {
        method2();
    }
}

class Second extends First {
    public void method2() {
        System.out.println("Second2");
    }
}

class Third extends Second {
    public void method1() {
        System.out.println("Third1");
        super.method2();
    }
    public void method2() {
        System.out.println("Third2");
    }
}

class Fourth extends First {
    public void method1() {
        System.out.println("Fourth1");
    }
    public void method2() {
        System.out.println("Fourth2");
    }
}

Screenshot of solution


Related Solutions

Each of the following statements contains an error. Describe each error and explain why the statement...
Each of the following statements contains an error. Describe each error and explain why the statement is wrong. (a) If the residuals are all negative, this implies that there is a negative relationship between the response variable and the explanatory variable. (b) A strong negative relationship does not imply that there is an association between the explanatory variable and the response variable. (c) A lurking variable is always something that can be measured.
The output produced by Samuel and Roberto in 20 hours is given below for wine and...
The output produced by Samuel and Roberto in 20 hours is given below for wine and cheese. wine Cheese Samuel 6 4 roberto 2 3 Who has comparative advantage in what commodity? How much would they exchange and at what price? Show all calculations. Please help me with what terms of trade mean
The table identifies the total output produced for each given number of workers. Workers Output 1...
The table identifies the total output produced for each given number of workers. Workers Output 1 100 2 190 3 270 4 340 5 400 6 450 7 490 At a nominal wage of $75 per unit labor and a price of output equal to $1 per unit, how many workers are hired?     Enter a whole number. Enter increase, decrease, or same for the rest of the questions below. If the wage and price remain constant, an increase in labor...
Read, analyze, and comment on the reading and statement in green below: The '77 Cents on...
Read, analyze, and comment on the reading and statement in green below: The '77 Cents on the Dollar' Myth About Women's Pay Once education, marital status, and occupations are considered, the "gender wage gap" all but disappears. April 8 is "Equal Pay Day," an annual event to raise awareness regarding the so-called gender wage gap. As President Obama said in the State of the Union address, women "still make 77 cents for every dollar a man earns," a claim echoed...
trace through the program and what the output would be. If there is an error explain...
trace through the program and what the output would be. If there is an error explain what to change. #include <iostream> using namespace std; int fun(int c, int b); int main(){ int a = 0, b= 5, c = 10; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; b=fun(a, c); cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; while(b==21){ int a = 3; b = a; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; } cout<<"a is:...
Below is a partial computer output based on a sample of 40 observations. Coefficient Standard Error...
Below is a partial computer output based on a sample of 40 observations. Coefficient Standard Error Constant 51.696 17.7 X1 -14.728 10.52 X2 180.864 50.24 Analysis of Variance Source of Variation Degrees of Freedom Sum of Squares Mean Square F Regression 19.412 9.706 Error 1,941.2 Required: 1. Use the output shown above and write the estimated regression equation. 2. Interpret the meaning of the coefficient of X1 and X2. 3. Compute the test statistic required to know whether the parameter...
For each call to the following method, indicate what console output is produced: public void mystery2(int...
For each call to the following method, indicate what console output is produced: public void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } } mystery2(1); mystery2(4); mystery2(16); mystery2(30); mystery2(100);
what will be a thesis statement for symptoms,causes,and treatment?
what will be a thesis statement for symptoms,causes,and treatment?
What is the output of the code below? After each line that has a cout, write...
What is the output of the code below? After each line that has a cout, write what the output of the code is. See example in code below. int i = 2, j = 1, k = i + j; cout << k << endl; ➔ 3 double d = 2.99, e = 1, f = d + e; cout << f << endl; double q = 1.50; cout << q/3 << endl; cout << 6.0/4 << endl; char c...
The table below provides information about price and output produced in country X for the years...
The table below provides information about price and output produced in country X for the years 2010 and 2015. The base year is 2010.                                                             p2010              y2010              p2015             y2015 Clothing                                              20                    100                  20                    150 Food                                                   10                    50                    12                    60 Government pension payments        75                    500                  100                  750 Military equipment                            250                  750                  250                  800 Using the GDP deflator, determine whether inflation or deflation occurred during the period. Enter the value of the GDP deflator and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT