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

Short Summary:

  • Provided the output and explanation for the requirement

Answer:

3

1

2

Explanation:

  • Super is the base or parent class which has instance variables num and id as initialized to 1 and 2 respectively. Sub is the child class which is inherited from Super class. Sub class initialized num to 3. Object is created for Sub class inside main function. Using the sub class object created calling the display method. When display is called, it prints out local num value as 3 and super.num value will be 1 that from parent class and id value of 2 which can be directly access from parent class

  

**************************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

**************************************************************************************


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...
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; }
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...
5. Explain how a currency swap works, and what it achieves.
5. Explain how a currency swap works, and what it achieves.
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++;...
Question 5 (10 marks) Python Language What is the output of the following code ? (2...
Question 5 Python Language What is the output of the following code ? (2 points) a, b = 0, 1 while b < 10: print b a, b = b, a+b B. Explain List Comprehension (2 points) Given v = [1 3 5] w = [ [2*x, x**2] for x in v] What is the content of w? c. What is tuple ?   What is the difference between tuple and list ? (2 points) D. What is a module ?  ...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); } END of the questions :::::::::::::::::::::::::: :::::::::::::::::::::::::: Submission...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line in the file lists a user ID, the ID of the movie the user watched, the rating the user gave for the movie, and the timestamp. For example line 1 indicates that the user’s ID is 196, the movie ID is 242, the user gave this movie a rating of 3, and the timestamp is 881250949. Given the file, find out the top similar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT