Free Essay

Sd1420 Unit6 Assignment 1

In: Computers and Technology

Submitted By jonny1
Words 973
Pages 4
Unit 6 Assignment 1: Object Questions
Course Objectives and Learning Outcomes
Describe the fundamentals of the Java programming language.
Create a subclass from a superclass through inheritance.
Differentiate between overriding and overloading methods.
Explore the Object class and its toString() method.
Explain polymorphism, dynamic binding, and generic programming.
Explain how to cast Objects and implement the instanceOf operator.
Restrict access to data and methods using the protected visibility modifier.
Declare constants, unmodifiable methods, and nonextendable classes using the final modifier.
Use the this keyword to refer to the calling object.
Assignment Requirements
The following homework is designed to cover the course objectives for Unit 6. The following questions require you to do research and examine the questions to find the correct answer to the 20 questions listed below.
1. The following two programs display the same result.

// Program 1 public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(((A)a1).equals((A)a2)); }
}

class A { int x;

public boolean equals(A a) { return this.x == a.x; }
}

// Program 2 public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); }
}

class A { int x;

public boolean equals(A a) { return this.x == a.x; }
}

a. True
2. Encapsulation means ______________.
a. that data fields should be declared private

3. What is the output of the following code?

import java.util.Date;

public class Test { public static void main(String[] args) { Date date1 = new Date(); Date date2 = new Date(); System.out.print((date1 == date2) + " " + (date1.getClass() == date2.getClass())); }
}

a. false true

4. What is the output of the following program?

public class Test { public static void main(String[] args) { new A(); } { System.out.print("Z"); }
}

class A extends B { A() { System.out.print("A"); } { System.out.print("X"); }
}

class B { B() { System.out.print("B"); } { System.out.print("Y"); }
}

a. YBXA

5. Analyze the following code:

public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); }
}

class A { int i;

public void m(int i) { this.i = i; }
}

class B extends A { public void m(String s) { }
}

What happens when the code is executed?
a. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

6. The visibility of the following modifiers increases in this order:
a. private; none, if no modifier is used; protected; and public

7. Which of the following statements is correct?
a. The clone() method can be invoked on an array object to create another array.

8. Analyze the following code:

public class Test { int x;

{ x++; }
}

How will you debug this code?
a. The program cannot be compiled because the statement x++ must be placed inside a method or a constructor.

9. A class design requires that a particular member variable be accessible by any subclasses of the same class but not by the classes that are not members of the same package. What should be done to achieve this?

a. The variable should be marked protected.

10. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?
a. x.add(1, "Chicago")

11. Which of the following modifiers should you use on the members of a class so that they are not accessible to another class in a different package but are accessible to any subclasses in any package?
a. protected
12. Analyze the following code:

public class Test { int x;

static { x++; }
}

Which of the following statements is true?
a. The program cannot be compiled because no main method is declared.

a. You can use super.super.p to invoke a method in superclassā€™s parent class.

13. Polymorphism means ______________.
a. that data fields should be declared private

14. Debug the following program segment:

public class Test { public static void main(String[] args) { String s = new String("Welcome to Java"); String o = s; String d = (String)o; }
}

a. Strings s, o, and d reference the same String object.
15. Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.
a. inheritance

16. 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() { System.out.println( "The default constructor of B is invoked"); }
}

public class C { public static void main(String[] args) { B b = new B(); }
}
a. "The default constructor of A is invoked""The default constructor of B is invoked"

17. What is the output of running A?

public class A extends B { int x = 1; public static void main(String[] args) { System.out.print(new A().x); System.out.print(new B().x); }
}

class B { int x = 5;
}
a. 15

18. What is the output of the following program?

public class Test extends Object { public static void main(String[] args) { Test test = new Test(); }

public Test() { System.out.print("A"); }

{ System.out.print("B"); }

static { System.out.print("C"); }
}

a. CBA

19. Suppose you create a class Cylinder to be a subclass of Circle. Debug the following code:

class Cylinder extends Circle { double length; Cylinder(double radius) { Circle(radius); }
}

a. The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder.

Tasks
Present your findings in the form of a numbered answer list.
Cite your sources using APA format.
Submit your report in a Word document to your instructor at the beginning of
Unit 7.

Submission Requirements
Format: Microsoft Word, Double-Spaced
Font: 12-point Times New-Roman
Citation Style: APA
Length: 1ā€“2 pages
Due By: Unit 7

Similar Documents