{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

30846_sample%5B2%5D - CSE 1030 3.0 Introduction to Computer...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 1030 3.0 Introduction to Computer Science II Sample test 1 (6 marks) A constructor of a subclass usually starts with a special statement that communicates with its parent class. (a) What is the general syntax of this statement? super(<zero or more arguments>); (b) What is its purpose; that is, why do we use it? It allows the subclass to select the constructor to be used for instantiating the superclass and to pass parameters to it. In other words, it allows the subclass to initialize the state of its parent. (c) Can this statement appear anywhere in the subclass or only in special locations? In the latter case, specify these locations. No, it must be the very first statement of the body of a constructor. (d) What happens if we do not use this statement at all in the subclass? If absent, the compiler automatically inserts super(), that is, one that selects the default constructor of the superclass. 2 (6 marks) Consider the class A shown below. public class A { private int x; private double y; public A(int x, double y) { this.setX(x); this.setY(y); } public void setX(int x) 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{ this.x = x; } public void setY(double y) { this.y = y; } } We want to create the following subclass. public class B extends A { private String s; public B(String s, int x, double y) { } } Write the body of the constructor. super(x, y); this.s = s; 3 (8 marks) Consider the following UML class diagram in which the Animal class is abstract. Animal Dog 6 Cat 6 (a) Why is the class Animal abstract? Because one should not be able to construct Animal objects (one cannot create instances of an abstract class). 2
Image of page 2
(b) The class Animal contains a constructor. Explain why is there a constructor even though the class is abstract.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern