What is the output for the following program codes?
a) [ 0.5 Mark ]
class A {
int i;
}
class B extends A {
int j;
void display() {
super.i = j + 1;
System.out.println(j + " " + i); }}
class inheritance {
public static void main(String args[]) {
B obj = new B();
obj.i=1;
obj.j=2;
obj.display(); }}
OUTPUT:
b) [ 0.5 Mark ]
class Parent {
public void getBike(){
System.out.println("Suzuki Bike");
}}
class Child extends Parent {
public void getCar(){
System.out.println("Swift car"); }}
class inheritance {
public static void main(String args[]) {
Parent p = new Parent();
p.getBike();
Child c = new Child();
c.getBike();
c.getCar(); }}
OUTPUT:
a) 2 3
Explanation: After declaration of obj, we assigned values i = 1 and j = 2. But when we call display, we are updating i value as super.i = j+1 = 2 + 1 = 3. As class B inherits i from A, it prints i values as 3. So, output is 2 3
b)
Suzuki Bike
Suzuki Bike
Swift car
Explanation: Parent p calls getbike(), which displays "Suzuki Bike". Child c also calls getBike(), which inherited getBike() method from Parent, it also prints "Suzuki Bike". Then Child c call getCar() method which prints "Swift car"
What is the output for the following program codes? a) [ 0.5 Mark ] class A...
a. Mention the appropriate relationship between following classes: [0.5 Marks] 1. HOD–StaffMember 2. Car–Ferrari 3. Student-Address 4. BankAccount–FixedAccount 5. House-Building 6. Department-Teacher 7. Traffic–TrafficSign b. Provide the UML diagram for the following program. [ 0.5 Marks] class Parent { public void getBike() { System.out.println("Suzuki Bike"); } } class Child extends Parent { public void getCar() { System.out.println("Swift car"); } } class inheritance { public static void main(String args[]) { Parent p = new Parent(); p.getBike(); Child c = new Child();...
Computer Programming II CS141(Java) Mention the appropriate relationship between following classes: HOD–StaffMember Car–Ferrari Student-Address BankAccount–FixedAccount House-Building Department-Teacher Traffic–TrafficSign Provide the UML diagram for the following program. class Parent { public void getBike() { System.out.println("Suzuki Bike"); } } class Child extends Parent { public void getCar() { System.out.println("Swift car"); } } class inheritance { public static void main(String args[]) { Parent p = new Parent(); p.getBike(); Child c = new Child(); c.getBike(); c.getCar(); } }
What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!
What is the output of the following code? class parent { public void someFunction() { System.out.println("Parent Function"); } } public class child extends parent { public void someFunction() { System.out.println("Child Function"); } public static void main(String args) { parent p1 = new parent(); parent p2 = new child(); child c1 = new child(); p1.someFunction(); p2.someFunction(); c1.someFunction(); } }
what is the output of running class Launcher? class parent { public Parent{ System.out.println( "The default constructor of Parent is invoked"); } } class Child extends Parent { public Child(); System.out.println( "The default constructor of Child is invoked"); } } public class Launcher { public static void main(String[] args) { Child c = new Child(); } a. no output since the main method does not have System.out.println() b."The default constructor of Child is invoked" c. "The default constructor of Parent...
What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); System.out.print(obj.PI); } } 3.1 3.14 None of the above
What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); obj.m1(); } } M1 of C1 M1 of C2 None of the above
What is the output of the following Java program? class Food { void flavor() { System.out.println("bland"); } } class Pepper extends Food { void flavor() { System.out.println("spicy"); } } public class Lunch { public static void main(String[] args) { Pepper lunch = new Food(); lunch.flavor(); } } Select one: a. bland b. bland spicy c. spicy d. the program does not compile e. no output
What is the output of the following codes? Line1: public class ArrayCompare ! Line2: public static void main (String[] args) { Line 3: int arr1[] = {1, 2, 3); Line4: int arr2[] = {1, 2, 3); Line5: if (arri == arr2) Line 6: System.out.println("Same"); Line 7: else Line 8: System.out.println("Not same"); Line9 : Line10: ) } Not same Same
What is the output of the following codes? Line1: public class ArrayCompare { Line2: public static void main (String[] args) { Line 3: int arrill {1, 2, 3}; Line 4: int arr2[1 {1, 2, 3}; Line5: if (arri == arr2) Line 6: System.out.println("Same"); Line: else Line 8: System.out.println("Not game"); Line9 : Line10:) حسین Same Not same