Which of the following method calls are valid ways to fill in the blank line of
ID: 3903792 • Letter: W
Question
Which of the following method calls are valid ways to fill in the blank line of code?
class Car{
public void m1(){
System.out.println("Car 1");
}
public void m2(){
System.out.println("Car 2");
}
public String toString(){
return "vroom";
}
}
class Truck extends Car{
public void m1()
{
System.out.println("Truck 1");
}
public String toString()
{
return super.toString() + super.toString();
}
public void m3()
{
System.out.println("Only in truck");
}
}
public class Driver{
public static void main(String [] args){
Car mycar;
mycar = new Car();
System.out.println(mycar);
__________________________________
}
}
1)mycar.m2();
2)mycar.m3();
3)mycar.m1();
QUESTION 2
Which of the following method calls are valid ways to fill in the blank line of code?
class Car{
public void m1(){
System.out.println("Car 1");
}
public void m2(){
System.out.println("Car 2");
}
public String toString(){
return "vroom";
}
}
class Truck extends Car{
public void m1()
{
System.out.println("Truck 1");
}
public String toString()
{
return super.toString() + super.toString();
}
public void m3()
{
System.out.println("Only in truck");
}
}
public class Driver{
public static void main(String [] args){
Truck mytruck;
mytruck = new Truck();
System.out.println(mytruck);
__________________________________
}
}
1)mytruck.m1();
2)mytruck.m3();
3)mytruck.m2();
QUESTION 3
Which of the following method calls are valid ways to fill in the blank line of code?
class Car{
public void m1(){
System.out.println("Car 1");
}
public void m2(){
System.out.println("Car 2");
}
public String toString(){
return "vroom";
}
}
class Truck extends Car{
public void m1()
{
System.out.println("Truck 1");
}
public String toString()
{
return super.toString() + super.toString();
}
public void m3()
{
System.out.println("Only in truck");
}
}
public class Driver{
public static void main(String [] args){
Car mycar;
mycar = new Truck();
System.out.println(mycar);
__________________________________
}
}
1)mycar.m2();
2)mycar.m3();
3)mycar.m1();
QUESTION 4
What is the output of the following program?
class Car{
public void m1(){
System.out.println("Car 1");
}
public void m2(){
System.out.println("Car 2");
}
public String toString(){
return "vroom";
}
}
class Truck extends Car{
public void m1()
{
System.out.println("Truck 1");
}
public String toString()
{
return super.toString() + super.toString();
}
public void m3()
{
System.out.println("Only in truck");
}
}
public class Driver3{
public static void main(String [] args){
Car mycar;
mycar = new Truck();
System.out.println(mycar);
mycar.m1();
mycar.m2();
}
}
1)vroom
Truck 1
Car 2
2)The code is not valid.
3)vroom
Car 1
Car 2
4)vroomvroom
Truck 1
Car 2
QUESTION 5
What is the output of the following program?
class Car{
public void m1(){
System.out.println("Car 1");
}
public void m2(){
System.out.println("Car 2");
}
public String toString(){
return "vroom";
}
}
class Truck extends Car{
public void m1()
{
System.out.println("Truck 1");
}
public String toString()
{
return super.toString() + super.toString();
}
public void m3()
{
System.out.println("Only in truck");
}
}
public class Driver3{
public static void main(String [] args){
Truck mytruck;
mytruck = new Truck();
System.out.println(mytruck);
mytruck.m1();
mytruck.m2();
mytruck.m3();
}
}
1)The code is not valid.
2)vroom
Truck 1
Car 2
3Only in truck
3)vroomvroom
Truck 1
Car 2
Only in truck
4)vroomvroom
Truck 1
Only in truck
QUESTION 6
Given the following class declarations, and the variable declared in the main method, which of the following statements are valid as a continuation to the main method?
class Bay extends Lake
{
public void method1()
{
System.out.println("Bay 1");
}
public void method2()
{
System.out.println("Bay 2");
}
}
class Pond
{
public void method1()
{
System.out.println("Pond 1");
}
public void method2()
{
System.out.println("Pond 2");
}
}
class Ocean extends Bay
{
public void method2()
{
System.out.println("Ocean 2");
}
}
class Lake extends Pond
{
public void method3()
{
System.out.println("Lake 3");
}
}
public class Water
{
public static void main(String [] args)
{
Pond var1 = new Bay();
}
}
1)var1.method2();
2)var1.method1();
3)((Lake) var1).method1();
4)((Bay) var1).method1();
5)((Pond) var1).method1();
6)((Ocean) var1).method1();
QUESTION 7
What is the output of the following program?
class Bay extends Lake
{
public void method1()
{
System.out.println("Bay 1");
}
public void method2()
{
System.out.println("Bay 2");
}
}
class Pond
{
public void method1()
{
System.out.println("Pond 1");
}
public void method2()
{
System.out.println("Pond 2");
}
}
class Ocean extends Bay
{
public void method2()
{
System.out.println("Ocean 2");
}
}
class Lake extends Pond
{
public void method3()
{
System.out.println("Lake 3");
}
}
public class Water
{
public static void main(String [] args)
{
Pond var1 = new Bay();
((Lake) var1).method1();
((Bay) var1).method1();
((Pond) var1).method1();
}
}
1)Bay 1
Pond 1
Pond 1
2)Bay 1
Bay 1
Bay 1
3)Pond 1
Bay 1
Pond 1
4)Lake 1
Bay 1
Pond 1
QUESTION 8
Given the following class declarations, and the variable declared in the main method, which of the following statements are valid as a continuation to the main method?
class Bay extends Lake
{
public void method1()
{
System.out.println("Bay 1");
}
public void method2()
{
System.out.println("Bay 2");
}
}
class Pond
{
public void method1()
{
System.out.println("Pond 1");
}
public void method2()
{
System.out.println("Pond 2");
}
}
class Ocean extends Bay
{
public void method2()
{
System.out.println("Ocean 2");
}
}
class Lake extends Pond
{
public void method3()
{
System.out.println("Lake 3");
}
}
public class Water
{
public static void main(String [] args)
{
Pond var2 = new Bay();
}
}
1)((Ocean) var2).method3();
2)var2.method1();
3)((Lake) var2).method2();
4)var2.method3();
5) var2.method2();
6)((Pond) var2).method3();
7)((Pond) var2).method2();
1)mycar.m2();
2)mycar.m3();
3)mycar.m1();
Explanation / Answer
1) Here Car class is parent class and Truck class is child class
"mycar" is of Car class type that is of parent class therefore all the methods which are in parent class can be called.
Hence mycar.m2() & mycar.m1() can be called producing the output as -
vroom //sytem.out.println(mycar) will run toString() of parent class
Car 2
Car 1
2) Here Car class is parent class and Truck class is child class
"mytruck" is of Truck class type that is of child class therefore all the methods which are in Truck class as well as in Car class can be called.
Hence mytruck.m1(), mytruck.m3() & mytruck.m2() can be called producing the output as -
vroomvroom //sytem.out.println(mytruck) will run toString() of child class
Truck 1
Only in truck
Car 2
Note that overriding of method m1 is done in child class and hence for object of child class type,this overriden method will run.
3) Here Car class is parent class and Truck class is child class
"mycar" is of Car class type that is of parent class and is referencing to a child class therefore all the methods which are in Car class can be called.
Hence mycar.m2() & mycar.m1() can be called producing the output as -
vroomvroom //sytem.out.println(mycar) will run toString() of child class due to late binding
Car 2
Truck 1
Note that when parent class refers to a child class then method of child class is called because of late binding.
4) Here Car class is parent class and Truck class is child class
"mycar" is of Car class type that is of parent class and is referencing to a child class therefore all the methods which are in Car class can be called.
Hence mycar.m1() & mycar.m2() will produce the output as -
vroomvroom //sytem.out.println(mycar) will run toString() of parent class
Truck 1
Car 2
Note that when parent class refers to a child class then method of child class is called.
5) Here Car class is parent class and Truck class is child class
"mytruck" is of Truck class type that is of child class therefore all the methods which are in Truck class as well as in Car class can be called.
Hence mytruck.m1(), mytruck.m2() & mytruck.m3() can be called producing the output as -
vroomvroom //sytem.out.println(mytruck) will run toString() of child class
Truck 1
Car 2
Only in truck
Note that overriding of method m1 is done in child class and hence for object of child class type,this overriden method will run.
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.