Question

In: Computer Science

Given the following code, what is output by the method call, mystery(6 * 8)? public static...

Given the following code, what is output by the method call, mystery(6 * 8)?

public static void mystery (int x[]) {
    System.out.println("A");
}
public static void mystery (int x) {
    System.out.println("B");
}
public static void mystery (String x) {
    System.out.println("C");
}

A

B

C

CA

CB

Which of the following is true about overloaded methods?

Java cannot use a method's return type to tell two overloaded methods apart.

Java cannot use a method's parameters to tell two overloaded methods apart.

You can only overload methods that have parameters.

All overloaded methods must have different names.

None of the above

Given the following code, what is output by the method call, mystery (5, 7.0015)?

public static void mystery (int a) {
    System.out.println("A");
}

public static void mystery (double a) {
    System.out.println("B");
}

public static void mystery (int a, double b) {
    System.out.println("C");
}

public static void mystery (double a, int b) {
    System.out.println("D");
}

A

B

C

D

Nothing is printed - there is an error.

Consider the following methods:

public static double doStuff(int a) {
    return a/2;
}
public static double doStuff(double val) {
    return val/10;
}

What is output by the following?

System.out.println(doStuff(5) + doStuff(5.0));

2.00.5

2.50.5

22.5

2.5

5

What is output to the screen by the following code?

System.out.println("Sum=" + 4 + 5);

Sum= 5 4

Sum=45

Sum=9

Sum= 4 5

Sum=54

What is output to the screen by the following code?

System.out.print(21/5);

3.5

4

4.2

5

5.5

Consider the following three classes: Clothing, Socks, and Sweater. Which would you choose be an abstract class?

Clothing

Socks

Sweater

Socks and Sweater

all of the above

Which of the following keywords allows a child class to access the overridden methods in a parent class?

extends

new

super

this

None of the above

Questions 9 and 10 refer to the following code:

public abstract class Phone {
    abstract void dial();    
}

public class MobilePhone extends Phone {
    
}

public class RotaryPhone extends Phone {
    public void dial () {
        //code not shown
    }
}

Which of the following statements is true?

RotaryPhone can be instantiated.

MobilePhone can be instantiated.

RotaryPhone cannot be instantiated.

Neither can be instantiated since you cannot extend an abstract class.

Neither can be instantiated since they do not include constructors.

Which of the following statements is true?

A Phone object can access methods in MobilePhone.

A RotaryPhone object can access methods in MobilePhone.

RotaryPhone inherits from Phone and MobilePhone.

RotaryPhone inherits from Phone.

None of the above.

Suppose a class implements the Comparable interface. Which of the following methods must the class include?

length

charAt

substring

indexOf

compareTo

Questions 12-14 refer to the following:

public class A {
    public A () {
        System.out.print(“one ”);
    }

    public A (int z) {
        System.out.print(“two ”);
    }

    public void doStuff() {
        System.out.print(“six ”);
    }
}

public class B extends A {
    public B () {
        super ();
        System.out.print(“three ”);
    }

    public B (int val) {
        super (val);
        System.out.print(“four ”);
    }
}

What is printed when the following line of code is executed?

B b = new B();

one three

two four

four two

three two

one four

What is printed when the following line of code is executed?

A a = new B(5);

four

two four

one

two

four one

Assume that variable b has been instantiated as a B object. What is printed when the following line of code is executed?

b.doStuff();

six

four

five

two

three

Which of the following is true about interfaces:

All methods in an interface must be abstract.

A class can only implement one interface.

An interface can have only non abstract methods.

Can not contain constants but can have variables.

None of the above.

What is the rule for a super reference in a constructor?

It must be in the parent class' constructor.

It must be the last line of the constructor in the child class.

It must be the first line of the constructor in the child class.

Only one child class can use it.

You cannot use super in a constructor.

Consider the following class definition.

public class WhatsIt {
    private int length;
    private int width;


    public int getArea () {
        // implementation not shown
    }

    private int getPerimeter () {
        // implementation not shown
    }
}

A child class Thingy that extends WhatsIt would have access to:

getArea()

getPerimeter()

width, length, getPerimeter()

width, length, getArea()

all of the above

Questions 18 - 20 pertain to the following class, Point:

public class Point {
    private double x;
    private double y;

    public Point() {
        this (0, 0);
    }

    public Point(double a, double b) {
        /* missing code */
    }
    // ... other methods not shown
}

Which of the following correctly implements the equals method?

public boolean equals(Point p) {
    return (x == Point.x && y == Point.y);
}

public void equals(Point p) {
    System.out.println(x == p.x && y == p.y);
}

public boolean equals(Point p) {
    System.out.println(x == p.x && y == p.y);
}

public boolean equals(Point p) {
    return (x == p.x && y == p.y );
}

public void equals(Point p) {
    return (x == p.x && y == p.y );
}

The default constructor sets x and y to (0, 0) by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended?

a = 0;
b = 0;

this(0, 0);

this (x, y);

a = x;
b = y;

x = a;
y = b;

Which of the following correctly implements a mutator method for Point?

public double getX() {
    return x;
}

public double getX() {
    return a;
}

public void setCoordinates (double a, double b) {
    x = a;
    y = b;
}

public void setCoordinates (double a, double b) {
    Point p = new Point(a,b);
}

None of the above

Solutions

Expert Solution

1. B

When the mystery(6*8) is called method mystery(int x) will be invoked.

2. All overloaded method must have different parameters.

The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.

3. C

Because the parameters passed to the method are 5.7.0 which is integer and the double so it calls the method with int and double as arguments.

4. 2.5

Because the + is the concatenation operator in java so the result from the two methods will be concatenated and gives us the output 2.5

5. Sum=45

The + is the concatenation operator so it gives the result of concatenation only.

6. 4

The compiler gives the output as 4 only because we doesn’t mention any datatype so it takes as int by default and also produces the same as output

7. Clothing

Clothing is the main method which we can declare this as abstract where the other socks and sweater are the part of clothing so these are the sub classes these will implement the main class clothing

8. Super

super is the keyword which is used to call the methods in the base class.

9. RotaryPhone can be instantiated.

Because this class extends the base class and implemented the method.

10. RotaryPhone inherits from Phone.

Rotary phone class was inherits the abstract methods from base class Phone.

11. compareTo

compareTo method must be implemented in a reasonable way.

12.  one three

Because there is no parameter is given in method call so one along with the three is printed.

13. two four

Because the parameter is passed so the two along with four is printed.

14. six

dostuff method is called here so that six is printed. class b extends class A so class b can access the methods in class A.

15. All methods in an interface must be abstract.

Interface contains only abstract methods and the body for this method are implemented in the following classes which implemented this interfaces.

16. It must be the first line of the constructor in the child class.

Super reference in the constructor is that it must be used the first line of the constructor in the child class.

17. getArea()

A child class that extends whatsIt only access to the getArea() method because it is publicly define and another method and variable doesn’t access due to the private declaration.

18.

public boolean equals (Point p)

{

   return(x == p.x && y == p.y);

}

Above method is equal to the given method.

19.

x = a;

y = b;

if we using above this method is replace by the default constructor by calling the second constructor.

20.

public void setCoordinates (double a, double b)

{

    x = a;

    y = b;

}

Mutator method is correctly implement by the given above method for point because Mutator method set the values to the class variables.


Related Solutions

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
For each call to the following method, indicate what console output is produced: public void mystery2(int...
For each call to the following method, indicate what console output is produced: public void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } } mystery2(1); mystery2(4); mystery2(16); mystery2(30); mystery2(100);
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
Given the following code below: public class Calculation { //method that returns cube of the given...
Given the following code below: public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){    int max=0;    for(int i=1;i<arr.length;i++){        if(max<arr[i]){           max=arr[i]; }     }     return max;   } //method that returns cube of the given number   public static int cube(int n){        return n*n*n;     }   } (5 points) Define the class CalculationTest a subclass of TestCase (10 points) Define a setUp() method (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class (5 points)Define...
What is the exact output of the following pseudocode segment? METHOD MAIN CALL myMethod (0,2) CALL...
What is the exact output of the following pseudocode segment? METHOD MAIN CALL myMethod (0,2) CALL myMethod (3,5) CALL myMethod (6,7)   END MAIN Answer: METHOD myMethod(A,B) BEGIN     WHILE (A < B)          PRINT(A + " ")          A ← A + 1     ENDWHILE     PRINTLINE(); END MyMethod
3. Given the test code below, what is the output to the console? public class TestMe{...
3. Given the test code below, what is the output to the console? public class TestMe{ public TestMe(){     System.out.print(“a”); } public void setUp(){     System.out.print(“b”); } public void tearDown(){     System.out.print(“c”); } @Test public void testX(){     System.out.print(“x”); } @Test public void testY(){     System.out.print(“y”); } } A. abxcbyc B. abxcabyc C. bxcbyc D. abxyc
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void...
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output? Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output?
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[]...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT