Question

In: Computer Science

What output is produced by the following code? Explain how it works (at least 5 lines)....

What output is produced by the following code? Explain how it works (at least 5 lines).

class Super {
int num = 1;
int id = 2;
}

class Sub extends Super {
int num = 3;

public void display() {
System.out.println(num);
System.out.println(super.num);
System.out.println(id);
}
}

public class Inheritance1 {
public static void main (String[] args) {
Sub obj = new Sub();
obj.display();
}
}

===============================
Example of "Explain how it works"

Sample Question:
What output is produced by the following code?   Explain how it works.

class student {

    String name;

    int age;

}

public class q6_ArryList1 {

public static void main(String args[]) {

      ArrayList<student>myArray = new ArrayList<>();

       for(int i=0; i< 4; i++){

         student s = new student();      

         s.name="Emily"+i;

         myArray.add(s);  

      }

    for (int i = 0; i<myArray.size(); i++){

        System.out.format("%s %n", myArray.get(i).name, myArray.get(i).age);

     }

    System.out.println();

    for (int i = 0; i<myArray.size(); i++){

       if (i % 2 ==0) {

        myArray.remove(1); }

    }

   for (int i = 0; i<myArray.size(); i++){

     System.out.format("%s %n", myArray.get(i).name, myArray.get(i).age);

    }

}

}

Answer:

Emily 0
Emily 1
Emily 2
Emily 3
Emily 0
Emily 3

How it works:
In this problem, an arrayList is created, then Emily + i is added to the arrayList where i is the for loop variable. The for loop goes from 0 inclusive to 4 exclusive. Therefore the arrayList contains [Emily0,Emily1, Emily2, Emily3]. After this, another for loop that goes through the arrayList is executed. An if statement inside the for loop gives a condition that if i %2 = 0, then remove the value at position 1. Thus, at 0, index 1 which is Emily1 is removed. At 1, nothing is removed. At 2, Emily2 is removed. Lastly, the arrayList, which contains[Emily0 and Emily3] is printed.

Solutions

Expert Solution

Question :

Answer :

3

1

2

How it works:

  • Given code snippet contains base class with name Super with two public integer variables num and id
  • These variables are assigned with value num=1 and id=2
  • Then there is one sub class called Sub which extends base class Super.
  • These Sub class contains integer variable num=3
  • Then sub class contains display() method which is public
  • This display() method print value of num using
  • System.out.println(num); which refers to the value of variable num declared in the Sub class hence num=3
  • Then statement System.out.println(super.num); will print the value of the num from base class Super.
  • Here super is a keyword which is used to refer to the variable from the Super class.
  • Note here super is keyword and Super is class
  • Then a driver class is created with main() method
  • In the main() method object of Sub class is created with name obj and then display method is called using obj.
  • Hence output is 3 1 2
  • Below screen shows the output


Related Solutions

What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
In Java What is the output produced by the following code? char letter = 'B'; switch...
In Java What is the output produced by the following code? char letter = 'B'; switch (letter) { case'A': case'a': System.out.println("Some kind of A."); case'B': case'b': System.out.println("Some kind of B."); break; default: System.out.println("Something else."); break; }
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
5. Explain how a currency swap works, and what it achieves.
5. Explain how a currency swap works, and what it achieves.
What is the output of the following code? Explain your answer in a paragraph or two....
What is the output of the following code? Explain your answer in a paragraph or two. class ExceptionThrown { static int divideByZero(int a, int b){ int i = a/b; return i; }    static int computeDivision(int a, int b) { int res =0; try{    res = divideByZero(a,b); } catch(NumberFormatException ex){ System.out.println("NumberFormatException is occured"); }    return res; }    public static void main(String args[]){ int a = 1;    int b = 0; try{ int i = computeDivision(a,b); }...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1,...
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1, i = 6; while (i < 9) {       product = product * i;       i++; } cout << “i is : ” << i << endl; cout << “product is : ” << product << endl; 6.    What is the output of the following code: int product = 1, i = 6;      do {       product = product * i;       i++;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT