Exam 2: Elementary Programming

arrow
  • Select Tags
search iconSearch Question
  • Select Tags

Write a method to find the max in an array of comparable objects (Assume that the objects in the argument are instances of Comparable). The method signature is as follows: public static Comparable max(Comparable[] a)

Free
(Essay)
4.7/5
(41)
Correct Answer:
Verified

public static Comparable max(Comparable[] a) {
Comparable result = a[0];
for (int i = 1; i < a.length; i++)
if (result.compareTo(a[i]) < 0)
result = a[i];
return result;
}

What exception type does the following program throw? public class Test { Public static void main(String[] args) { Object o = new Object(); String d = (String)o; } }

Free
(Multiple Choice)
4.9/5
(36)
Correct Answer:
Verified

E

Analyze the following code. class Test { Public static void main(String[] args) { Object x = new Integer(2); System.out.println(x.toString()); } }

Free
(Multiple Choice)
4.7/5
(40)
Correct Answer:
Verified

C

Write a class named Hexagon that extends GeometricObject and implements the Comparable interface. Assume all six sides of the hexagon are of equal size. The Hexagon class is defined as follows: public class Hexagon extends GeometricObject implements Comparable { private double side; /** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it } /** Implement the abstract method getArea in GeometricObject */ public double getArea() { // Implement it ( Write a class named Hexagon that extends GeometricObject and implements the Comparable interface. Assume all six sides of the hexagon are of equal size. The Hexagon class is defined as follows: public class Hexagon extends GeometricObject implements Comparable { private double side; /** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it } /** Implement the abstract method getArea in GeometricObject */ public double getArea() { // Implement it (   ) } /** Implement the abstract method getPerimeter in GeometricObject */ public double getPerimeter() { // Implement it } /** Implement the compareTo method in the Comparable interface to */ public int compareTo(Object obj) { // Implement it (compare two Hexagons based on their areas) } } ) } /** Implement the abstract method getPerimeter in GeometricObject */ public double getPerimeter() { // Implement it } /** Implement the compareTo method in the Comparable interface to */ public int compareTo(Object obj) { // Implement it (compare two Hexagons based on their areas) } }

(Essay)
4.9/5
(49)

Which of the following possible modifications will fix the errors in this code? public class Test { Private double code; Public double getCode() { Return code; } Protected abstract void setCode(double code); }

(Multiple Choice)
4.8/5
(32)

What is the output of running the class C. public class C { Public static void main(String[] args) { Object[] o = {new A(), new B()}; System.out.print(o[0]); System.out.print(o[1]); } } Class A extends B { Public String toString() { Return "A"; } } Class B { Public String toString() { Return "B"; } }

(Multiple Choice)
4.8/5
(29)

What is wrong in the following code? Test { main(String[] args) { Number x = Integer(3); System.out.println(x.intValue()); System.out.println((Integer)x.compareTo(new Integer(4))); } }

(Short Answer)
4.8/5
(34)

When you implement a method that is defined in a superclass, you __________ the original method.

(Multiple Choice)
4.9/5
(36)

A subclass inherits _____________ from its superclass. a. private method b. protected method c. public method d. a and c e. b and c

(Short Answer)
4.9/5
(42)

The method _____ overrides the following method: protected double xMethod(int x) {…};

(Multiple Choice)
4.8/5
(46)

Analyze the following code: public class Test1 { Public Object max(Object o1, Object o2) { If ((Comparable)o1.compareTo(o2) >= 0) { Return o1; } Else { Return o2; } } }

(Multiple Choice)
4.8/5
(41)

What exception type does the following program throw? public class Test { Public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }

(Multiple Choice)
4.8/5
(31)

The program has a syntax error because the member access operator The java.util.Date class implements java.lang.Cloneable and overrides the equals method to return true if two objects have the same date and time. Show the output of the following code. import java.util.*; public class Test extends Object { public static void main(String[] args) { Date d1 = new Date(); Date d2 = new Date(349324); Date d3 = d1; System.out.println("(1) " + (d1 == d2)); System.out.println("(2) " + (d1 == d3)); System.out.println("(3) " + d1.equals(d2)); System.out.println("(4) " + d1.equals(d3)); } }

(Essay)
4.7/5
(42)

Show the output of running the class Test in the following code: interface A { Void print(); } Class C {} Class B extends C implements A { Public void print() { } } Public class Test { Public static void main(String[] args) { B b = new B(); If (b instanceof A) System.out.println("b is an instance of A"); If (b instanceof C) System.out.println("b is an instance of C"); } }

(Multiple Choice)
4.8/5
(31)

What is the output of running class C? class A { Public A() { System.out.println( "The default constructor of A is invoked"); } } Class B extends A { Public B(String s) { System.out.println(s); } } Public class C { Public static void main(String[] args) { B b = new B("The constructor of B is invoked"); } }

(Multiple Choice)
4.7/5
(37)

What is wrong in the following code? Test { main(String[] args) { Number x = Integer(3); System.out.println(x.intValue()); System.out.println)); } }

(Short Answer)
4.8/5
(39)
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)