Java OOP SHORT
ANSWER QUESTIONS CONSTRUCTORS QUESTIONS – PART 1 a)
The special method that is used to create an object is
called a _____________. b)
A constructor’s name is always the same as the
_____________ name. c)
What is a constructor’s return-type? _______________ d)
What is the purpose of a constructor? ________________ e)
For the following class, write the statement(s) needed to
create the point (2, 7). public class Point
{ public double x; public double y; } f)
For
the following class, write the statement(s) needed to create the point (6,
4). public class Point
{ public double x; public double y; public
Point(double tx, double ty) { x = tx; y = ty; } } g)
Assume
the following code creates Point objects with the Point class above. What will be outputted to screen? public class
PointTester { public static void
main(String[] args) { Point p1 = new
Point(2, 3); Point p2 = new
Point(4, 8); p1.x = p2.y * 2; System.out.println(p1.x); } } h)
Assume
the following code creates Point objects with the Point class above. What will be outputted to screen? public class
PointTester { public static void
main(String[] args) { Point a = new
Point(2, 3); Point
b = a; System.out.println(b.y); } } i)
Assume
the following code creates Point objects with the Point class above. What will be outputted to screen? public class
PointTester { public static void
main(String[] args) { Point a = new
Point(2, 3); Point b = a; b.x = 0; b.y = 4; System.out.println(a.x); } } j)
Assume
the following code creates Point objects with the Point class above. What will be outputted to screen? public class
PointTester { public static void
main(String[] args) { Point
coco = new
Point(5, 5); Point
dodo = new
Point(1, 1); dodo = coco; coco = dodo; System.out.println(coco.x); } } k)
Assume
the following code creates Point objects with the Point class above. What will be outputted to screen? public class
PointTester { public static void
main(String[] args) { Point
p = new
Point(1, 5); Point
q = new
Point(4, 7); q = p; q.x = 2; q = new
Point(9, 8); System.out.println(p.x); } } l)
Write
the missing statements to complete the class below: public class Name { public String
fullName; public
Name(String firstName, String lastName) { //missing
code } } SOLUTIONS Click here. QUESTIONS – PART 2
m)
One
can have multiple constructors in a class.
True or false? ______________ n)
Constructor
_______________ is when we have several constructors inside a class. Each one gives us a different way of
creating an object. Java requires that
each constructor have a different parameter list so that it can tell which one
is being used at a given time. o)
How
do we refer to the instance that we are inside? p)
Complete
the constructor by providing the missing code. No, you may not change any of the other
code. public class Point
{ public double x; public double y; public
Point(double x, double y) { //missing code } } q)
How
many instance variables and how many constructors does the following Person
class have? public class
Person { public
String name; public int age; public
Person(String tname, int tage) { name = tname; age = tage; } public
Person() { name = "Jimmy"; age = 4; } public
Person(int tage) { name = "Karen"; age = tage; } } r)
Assuming
we are using the Person class from above, what will the following code
output? public class
PersonTester { public static void
main(String[] args) { Person p = new
Person("Pat", 25); System.out.println(p.age); } } s)
Assuming
we are using the Person class from above, what will the following code
output? public class
PersonTester { public static void
main(String[] args) { Person p = new
Person(9); System.out.println(p.name); } } t)
Assuming
we are using the Person class from above, what will the following code
output? public class
PersonTester { public static void
main(String[] args) { Person p = new
Person(); System.out.println(p.name); } } u)
If
the class Coconut contains only instance variables and no constructor, then
why can we still do something like Coconut
c = new Coconut(); ? v)
Does
the default constructor ever have parameters? w)
What
values do the instance variables get when the default constructor is used? x)
Is
it good practice to create classes that make use of a default constructor? y)
When
you create an object of a class that has no constructor, Java uses the
____________ constructor. z)
When
a constructor calls a different constructor of the same class, this is called
_______________ ______________. aa) Constructor chaining has to occur on
the ______________ line of a constructor. bb) We can call another constructor by
using the _____________ keyword following by a parameter list that matches
another constructor’s needed parameter list. cc) In the Point class below, what statement(s)
is needed to make the 2nd constructor call the first constructor
and make it create a Point at (0,0)?
SOLUTIONS Click here. |
|
|