1. Which of the following class definitions defines a legal abstract class? A. c
ID: 3547710 • Letter: 1
Question
1. Which of the following class definitions defines a legal abstract class?
A. class A { abstract void unfinished() { } }
B. class A { abstract void unfinished(); }
C. abstract class A { abstract void unfinished(); }
D. public class abstract A { abstract void unfinished(); }
2. Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();
3. What is the output of running class Test? Explain Why?
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** Default constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
A. ABCD
B. BACD
C. CBAE
D. AEDC
E. BEDC
4. Which of the following is a correct interface? Explain why?
A. interface A { void print() { }; }
B. abstract interface A { print(); }
C. abstract interface A { abstract void print() { };}
D. interface A { void print();}
5. Show the output of running the class Test in the following code lines. Explain why?
interface A {
}
class C {
}
class B extends D implements A {
}
public class Test extends Thread {
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");
}
}
class D extends C {
}
A. Nothing.
B. b is an instance of A.
C. b is an instance of C.
D. b is an instance of A followed by b is an instance of C.
6. (1 point) Assume the existence of an interface, CommDevice, with the following methods:
transmit: accepts two string parameters and returns nothing
receive: accepts two string parameters and returns a boolean
Define a class, Firewall that implements the above interface, and has the following members:
a string instance variable, permittedReceiver
a string instance variable, buffer
a constructor that accepts a string parameter that is used to initialize the permittedReceiver variable
an implementation of the transmit method that assigns the first parameter to the destination instance variable and the second to the buffer variable. It also send to System.out the message "Data scheduled for transmission to dest" where dest is replaced by the actual value of the destination string.
an implementation of the receiver method that checks if the first parameter is equal to the permittedReceiver and if so it sets the buffer instance variable to the second parameter and returns true; otherwise it sets the buffer to the empty string, prints the message "Attempted breach of firewall by " where is replaced by the method's first parameter, and returns false.
7. (1 points) All windows on the desktop have width and height (as well as numerous other attributes).
However the actual contents of the window change both in structure and appearance depending upon what sort of window is displayed (e.g. a word processing window will display text, a color chooser window display a pallette of colors, a file chooser window displays a directory tree, etc). Thus, the details of the method to actual fill the contents of the window must be deferred to the subclasses of the Window class
Write the definition of an abstract class, Window, containing the following: two integer instance variables, width and height, two accessor methods, getWidth and getHeight, a constructor that accepts two integers and uses them to initialize the two instance variables (the parameters should be width followed by height), and an abstract void-returning method named paint that accepts no parameters.
Explanation / Answer
1.
The correct option is C.
Explanation:
Hence, per the rules of the abstract class, only the third option is correct.
2.
The correct options is B and D.
Explanation:
Therefore, for this reason, the option B is correct.
3.
The correct option is E.
Explanation:
4.
The correct option is D.
Explanation:
5.
The correct option is D.
The output of the given code is as follows:
b is an instance of A
b is an instance of C
Explanation:
6.
The implementation per the given conditions are as follows:
//The interface CommDevice is assumed to be here.
//Define a class that implements an interface
//CommDevice.
public class Firewall implements CommDevice
{
//Declare the variables.
private String permittedReceiver;
private String buffer;
//Define a constructor.
public Firewall(String string)
{
//Initialize the variable.
permittedReceiver = string;
}//End of the constructor.
//Define a function transmit of type void.
public void transmit(String string1, String string2)
{
//Assign the variables.
permittedReceiver = string1;
buffer = string2;
//Display the message.
System.out.println("Data scheduled for "
+ "transmission to "
+ permittedReceiver );
}//End of function transmit.
//Define a function receiver of type boolean.
public boolean receiver(String string1,
String string2)
{
//Check the condition.
if(string1 == permittedReceiver)
{
//Assign the value to the variable
//buffer.
buffer = string2;
//Return true to the function receiver.
return true;
}
//Else part.
else
{
//Assign an empty string to the buffer.
buffer = "";
//Display the message.
System.out.println("Attempted breach of "
+ "firewall by " + string1);
//Return false to the function receiver.
return false;
}//End of else part.
}//End of function receiver.
}//End of class Fire wall.
7.
The implementation per the conditions are as follow:
//Create an abstract class.
public abstract class Window
{
//Declare the variables.
private int width;
private int height;
//Create an accessor method for width.
public int getWidth()
{
//Return the width.
return width;
}
//Create a getter method for width.
public int getHeight()
{
//Return the height.
return height;
}
//Define a parameterized constructor.
public Window(int w, int h)
{
//Initialize the variables.
width = w;
height = h;
}
//Create an abstract method.
public abstract void paint();
}//End of abstract class Window.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.