In: Computer Science
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
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.