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; }
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.
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++;...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT